iframe 中的 Chrome 第 3 方 cookie(SameSite=None;安全)

log*_*lic 1 cookies google-chrome samesite third-party-cookies

就像其他人一样,我正在努力支持 Chrome 中即将到来的第 3 方 cookie 更改。我的用户(其他域上的其他网站)将我的网页包含在 iframe 中。在我的页面中,我设置了一个 cookie(只有 iframe 需要在该父网站的上下文中看到该 cookie,因此实际上不是第 3 方 cookie)。

这在 Firefox 中工作正常(可能已经禁用了第 3 方 cookie),但是当在隐身模式下使用 Chrome 时,禁用第 3 方 cookie,我无法设置我的 cookie。

下面是一个例子。如果您重新加载父级,cookie 就会丢失(实际上它永远不会被设置,即使在重新加载之前也是如此)。Chrome 开发工具中没有警告,但您可以在“应用程序”选项卡中看到 cookie 从未被设置。

有什么想法为什么这在 Firefox 中有效,但在禁用 3rd party cookie 的 Chrome 隐身模式中无效?有办法让这个工作吗?

父页面(domain1.com):

<html>
    <head>
        <style>
            iframe.myframe {
                width: 800px;
                height: 500px;
            }
        </style>
    </head>
    <body>
        <iframe class="myframe" src="https://domain2.com/my_thing.html"/>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

子页面(domain2.com):

<html>
    <head>
        <style> body{ background: blue; } </style>
    </head>
    <body>
        <script>
            let num = getCookie('num');
            console.log('Pre-change: '+num);
            num = (parseInt(num)||0) + 1;

            document.cookie = "num="+num+"; SameSite=None; Secure";

            console.log('Post-change: '+num);


            function getCookie(cname) {
                let name = cname + "=";
                let ca = document.cookie.split(';');
                for(let i = 0; i < ca.length; i++) {
                    let c = ca[i];
                    while (c.charAt(0) == ' ') {
                        c = c.substring(1);
                    }
                    if (c.indexOf(name) == 0) {
                        return c.substring(name.length, c.length);
                    }
                }
                return "";
            }
        </script>
    </body>
</html> 
Run Code Online (Sandbox Code Playgroud)

log*_*lic 5

最后意识到答案是Partitioned在设置cookie时添加param,如下所示:

document.cookie = "num="+num+"; SameSite=None; Secure; Partitioned";
Run Code Online (Sandbox Code Playgroud)

Firefox 似乎隐式地执行了此操作,因此行为有所不同。