SameSite警告Chrome 77


168

自上次更新以来,我的Cookie出现错误,与SameSite属性相关。

Cookies来自第三方开发人员(Fontawesome,jQuery,Google Analytics,Google reCaptcha,Google Fonts等)。

Chrome控制台中的错误是这样的。

A cookie associated with a cross-site resource at <URL> was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at <URL> and <URL>.
(index):1 A cookie associated with a cross-site resource at http://jquery.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
(index):1 A cookie associated with a cross-site resource at http://fontawesome.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
(index):1 A cookie associated with a cross-site resource at http://google.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
(index):1 A cookie associated with a cross-site resource at https://google.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
(index):1 A cookie associated with a cross-site resource at https://www.google.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
(index):1 A cookie associated with a cross-site resource at http://www.google.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
(index):1 A cookie associated with a cross-site resource at http://gstatic.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.

我是否需要在本地计算机或服务器上做任何事情,或者只是它们应该在库的将来版本中实现的某些功能?


9
与您的代码无关。其Web服务器将必须支持的内容。
丹尼尔·A·怀特

我有同样的问题,这是否意味着我们不能在代码中使用第三方网站?
Love2Code

1
您可以毫无问题地使用这些库。只是警告控制台将抛出该警告,直到他们像@ DanielA.White之前所说的那样在服务器中实现该警告为止。
peiblox

9
如果第3方在该日期之前仍未修复其Cookie,那A future release of Chrome will only deliver ... 会发生什么?看来将来的日期是02/04/2020-不太远。
JK。

1
如@ DanielA.White所述,此问题必须固定在第三方代码上,有关更多上下文,请参见此 答案
Hooman Bahreini

Answers:


141

这个控制台警告不是错误或实际问题,Chrome只是在传播有关这一新标准的信息,以提高开发人员的采用率。

它与您的代码无关。这是他们的Web服务器必须支持的东西。

该修补程序的发布日期为2020年2月4日,每个日期:https : //www.chromium.org/updates/same-site

2020年二月:执行首次展示的Chrome 80稳定:SameSite按默认和SameSite =无-需要-安全的行为将开始推出Chrome浏览器80稳定的初始人口有限开始一周的2020 2月17日,不包括美国星期一的总统假期。从最初的有限阶段开始,通过逐步增加部署,我们将密切监视和评估生态系统的影响。

有关完整的Chrome发布时间表,请参见此处

我通过添加响应标题解决了相同的问题

response.setHeader("Set-Cookie", "HttpOnly;Secure;SameSite=Strict");

SameSite阻止浏览器发送cookie和跨站点请求。主要目标是减轻跨域信息泄漏的风险。它还提供了一些针对跨站点请求伪造攻击的保护。该标志的可能值为Lax或Strict。

SameSite Cookie 在这里说明

在应用任何选项之前,请先参考此内容

希望这对您有帮助。


39
您是否进行过任何研究,以了解当引入设置Cookie的第三方脚本时如何处理此问题(例如Google Analytics(分析)和Google跟踪代码管理器)?
布拉斯

26
我应该在哪里添加响应头?谢谢。
克里斯·温

2
如果您使用的是.NET,则在IIS中附加web.config或添加URL ReWrite规则将解决此问题。幸得溶液中 stackoverflow.com/questions/38954821/...
BDarley

3
是否有不涉及JavaScript的解决方案?
posfan12

11
我必须说,友好的警告确实会触发像我这样的许多开发人员的强迫症。当我看到YouTube,谷歌的财产,罪犯在Google Chrome中产生控制台错误时,我对肮脏的控制台的不满情绪大大增加。
Marc

22

如果在localhost上进行测试,并且无法控制响应头,则可以使用chrome标志禁用它。

访问该网址并禁用它:chrome:// flags /#same-site-by-default-cookies 默认情况下,SameSite Cookies屏幕快照

我需要禁用它,因为Chrome Canary刚从大约V 82.0.4078.2开始执行此规则,现在它没有设置这些Cookie。

注意:我只会在用于开发的Chrome Canary中将此标志打开。出于Google引入Chrome浏览器的相同原因,最好不要为Chrome浏览器的日常使用打开该标志。


1
如果禁用此标志,并重新推出金丝雀并没有为我工作,所以我刚才添加-SameSite到主“过滤器”对话框,我也可以用来删除这个恼人的问题sourcemap => superuser.com/questions/1523427/...
无动机

4

为了详细说明Rahul Mahadik的答案,此方法适用于MVC5 C#.NET:

AllowSameSiteAttribute.cs

public class AllowSameSiteAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var response = filterContext.RequestContext.HttpContext.Response;

        if(response != null)
        {
            response.AddHeader("Set-Cookie", "HttpOnly;Secure;SameSite=Strict");
            //Add more headers...
        }

        base.OnActionExecuting(filterContext);
    }
}

HomeController.cs

    [AllowSameSite] //For the whole controller
    public class UserController : Controller
    {
    }

要么

    public class UserController : Controller
    {
        [AllowSameSite] //For the method
        public ActionResult Index()
        {
            return View();
        }
    }

你能看看我的代码吗?它也与Chrome的CORS政策有关。stackoverflow.com/questions/60556740/...
阿沛·萨尔维

4

通过在脚本标记中添加跨域来修复。

来自:https : //code.jquery.com/

<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>

完整性和跨域属性用于子资源完整性(SRI)检查。这使浏览器可以确保第三方服务器上托管的资源未被篡改。每当从第三方源加载库时,建议使用SRI作为最佳实践。在srihash.org上阅读更多内容


srihash.org说很遗憾,我们不知道该域。
弗拉德

在哪里可以找到脚本标签?它在web.config中吗?
CarlD

1
错误消息中的@CarlD查找导致错误的脚本,然后查看网页的html源
John Magnolia

@JohnMagnolia我只是做了,什么也没发生。
CarlD
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.