首页 > 解决方案 > 如何在谷歌翻译 cookie 上设置安全属性

问题描述

在我们网站的每次 PCI 扫描之后,我必须进入扫描结果并为 Google Translate cookie 的“Cookie 不使用安全属性”问题添加争议,这是 PCI 失败条件。有没有办法为谷歌翻译 cookie 设置这个属性?我在网上搜索过,但到目前为止什么也没找到,除了 Chrome 78 的一个旧 GitHub 问题,其中一张海报提到谷歌翻译,但只是说它遇到了与其他海报相同的问题。

服务器是 Windows Server 2016 上的 IIS 10。

这是我尝试过的。我将以下内容放入 web.config,但传出的 cookie 仍然没有 Secure 属性。

<outboundRules rewriteBeforeCache="true">
    <rule name="Add Secure" preCondition="No Secure" patternSyntax="Wildcard">
        <match serverVariable="RESPONSE_Set_Cookie" pattern="*" negate="false" />
        <action type="Rewrite" value="{R:0}; secure" />
        <conditions />
    </rule>
    <rule name="Add HttpOnly" preCondition="No HttpOnly" patternSyntax="Wildcard">
        <match serverVariable="RESPONSE_Set_Cookie" pattern="*" negate="false" />
        <action type="Rewrite" value="{R:0}; HttpOnly" />
        <conditions />
    </rule>
    <rule name="Add SameSite" preCondition="No SameSite" patternSyntax="Wildcard">
        <match serverVariable="RESPONSE_Set_Cookie" pattern="*" negate="false" />
        <action type="Rewrite" value="{R:0}; SameSite=None" />
        <conditions />
    </rule>
    <preConditions>
        <preCondition name="No Secure">
            <add input="{RESPONSE_Set_Cookie}" pattern="." />
            <add input="{RESPONSE_Set_Cookie}" pattern="; secure" negate="true" />
        </preCondition>
        <preCondition name="No HttpOnly">
            <add input="{RESPONSE_Set_Cookie}" pattern="." />
            <add input="{RESPONSE_Set_Cookie}" pattern="; HttpOnly" negate="true" />
        </preCondition>
        <preCondition name="No SameSite">
            <add input="{RESPONSE_Set_Cookie}" pattern="." />
            <add input="{RESPONSE_Set_Cookie}" pattern="; SameSite" negate="true" />
        </preCondition>
    </preConditions>
</outboundRules>

标签: cookiesgoogle-translate

解决方案


经过两天徒劳的搜索和反复试验,我终于解决了这个问题。所以我想我会在这里发布解决方案来避免其他人的头痛。通过使用 Failed Request Tracing 来查看 URL 重写正在做什么(或不做什么,事实证明)发现的问题是,在评估先决条件时,IIS将所有cookie 评估为一个字符串。因此,即使其中一个cookie 已经具有安全属性,前提条件也会失败并且没有其他 cookie 将获得安全属性集。这似乎是 IIS 中的一个错误,因为重写规则本身分别作用于每个 cookie。无论如何,解决方案是完全放弃先决条件。我不喜欢这个“解决方案”的部分,它确实使它成为一种解决方法,如果一个 cookie 已经具有该Secure属性,它会获得第二个。但浏览器似乎忽略了这一点,现在将所有 cookie 称为 Secure。


推荐阅读