具有 Domain 和 SameSite strict 的 cookie 之间的安全差异是什么?
What are the security differences between cookies with Domain vs SameSite strict?
当我们创建一个 cookie 时,我们可以通过设置域属性来指定它的使用位置。
Set-Cookie: Foo=bar; Path=/; Secure; Domain=baz.qux.com;
上面的 cookie 将仅与对域 baz.qux.com
.
的请求一起使用
Set-Cookie: Foo=bar; Path=/; Secure; SameSite=strict;
上面的 cookie 省略了 domain
属性,这意味着将使用 set cookie 所在的域(不包括子域,IE 除外)。它还有属性SameSite=strict
,意思是:
SameSite cookies let servers require that a cookie shouldn't be sent with cross-site (where Site is defined by the registrable domain) requests, which provides some protection against cross-site request forgery attacks (CSRF).
来自 MDN
如果这两个 cookie 都设置在域 baz.qux.com
上,它们之间的行为有何不同?
SameSite=strict
属性如何防止具有指定域的其他 cookie 无法防止的 CSRF?
Domain
属性限制将 cookie 发送到的主机。 SameSite
属性限制可以发送 cookie 的来源。
所以第一个 cookie:
Set-Cookie: Foo=bar; Path=/; Secure; Domain=baz.qux.com;
可以发送到 baz.qux.com
或其任何子域,无论请求来源如何(即是否从托管在 baz.qux.com
或 foo.example.com
的网页发送)
第二个饼干:
Set-Cookie: Foo=bar; Path=/; Secure; SameSite=strict;
只能发送到 baz.qux.com
(因为没有指定域,并且忽略 IE 异常),并且只有当请求来自 qux.com
站点时(即不会发送给跨站点请求。)
这有助于防止随机网站 (hacker.example.com
) 向包含会话 cookie 的第三方 (baz.qux.com
) 执行经过身份验证的请求,从而防止 CSRF。
当我们创建一个 cookie 时,我们可以通过设置域属性来指定它的使用位置。
Set-Cookie: Foo=bar; Path=/; Secure; Domain=baz.qux.com;
上面的 cookie 将仅与对域 baz.qux.com
.
Set-Cookie: Foo=bar; Path=/; Secure; SameSite=strict;
上面的 cookie 省略了 domain
属性,这意味着将使用 set cookie 所在的域(不包括子域,IE 除外)。它还有属性SameSite=strict
,意思是:
SameSite cookies let servers require that a cookie shouldn't be sent with cross-site (where Site is defined by the registrable domain) requests, which provides some protection against cross-site request forgery attacks (CSRF).
来自 MDN
如果这两个 cookie 都设置在域 baz.qux.com
上,它们之间的行为有何不同?
SameSite=strict
属性如何防止具有指定域的其他 cookie 无法防止的 CSRF?
Domain
属性限制将 cookie 发送到的主机。 SameSite
属性限制可以发送 cookie 的来源。
所以第一个 cookie:
Set-Cookie: Foo=bar; Path=/; Secure; Domain=baz.qux.com;
可以发送到 baz.qux.com
或其任何子域,无论请求来源如何(即是否从托管在 baz.qux.com
或 foo.example.com
的网页发送)
第二个饼干:
Set-Cookie: Foo=bar; Path=/; Secure; SameSite=strict;
只能发送到 baz.qux.com
(因为没有指定域,并且忽略 IE 异常),并且只有当请求来自 qux.com
站点时(即不会发送给跨站点请求。)
这有助于防止随机网站 (hacker.example.com
) 向包含会话 cookie 的第三方 (baz.qux.com
) 执行经过身份验证的请求,从而防止 CSRF。