hysryt / wiki

https://hysryt.github.io/wiki/
0 stars 0 forks source link

Cookie #153

Open hysryt opened 3 years ago

hysryt commented 3 years ago

SameSite

Set-Cookie ヘッダーの属性の一つ。 Set-Cookie で渡したCookieの送信範囲をブラウザに指示する。

サイトAからサイトBへの遷移時にCookieを送るか?

大前提として、SameSiteにかかわらず、Cookieは送られてきたサイト以外には送らない。 サイトAから受け取ったCookieをサイトBに送ることはない。

以下のように使用する。

Set-Cookie: choco=banana; SameSite=Strict

SameSiteがとりうる値は、StrictLaxNone の内のいずれか。

このSameSiteはCSRF対策となる。 トラッキング対策とはならない。(Noneを指定すればいい) ブラウザ側がNoneを禁止すればトラッキング対策となりうる。

Strict

Cookie はファーストパーティのコンテキストでのみ送信され、サードパーティの Web サイトによって開始されたリクエストと一緒に送信されることはありません。

→ ファーストパーティ内の回遊でのみcookieを送る。サイトAからサイトBに遷移する時はまだサイトBにCookieは送らない。

Lax

None

サイトAにiframeでサイトBを埋め込んでいるとする。 サイトAにアクセスした場合、サイトAはファーストパーティ、サイトBはサードパーティとなる。 サイトBにアクセスした場合、サイトBがファーストパーティとなる。

Strict

サイトAにアクセスし、サイトAとサイトBからCookieを受け取ったとする。 その後再びサイトAにアクセスした時、サイトAにはCookieを送るが、サイトBにはCookieを送らない。(サードパーティには送信しない) さらにその後サイトBにアクセスした時も、サイトBにはCookieを送らない。(受け取った時相手がサードパーティなため)

Lax

サイトAにアクセスし、サイトAとサイトBからCookieを受け取ったとする。 その後再びサイトAにアクセスした時、サイトAにはCookieを送るが、サイトBにはCookieを送らない。(サードパーティには送信しない) さらにその後サイトBにアクセスした時、サイトBにCookieを送る。(相手がファーストパーティの場合は送信する)

None

サイトAにアクセスし、サイトAとサイトBからCookieを受け取ったとする。 その後再びサイトAにアクセスした時、サイトAにもサイトBにもCookieを送る。(送信先がサードパーティでもファーストパーティでも送信する) さらにその後サイトBにアクセスした時、サイトBにCookieを送る。(送信先がサードパーティでもファーストパーティでも送信する)

https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-05

The "SameSite" attribute limits the scope of the cookie such that it will only be attached to requests if those requests are same-site, as defined by the algorithm in Section 5.2. For example, requests for "https://site.example/sekrit-image" will attach same-site cookies if and only if initiated from a context whose "site for cookies" is "site.example".

If the "SameSite" attribute's value is "Strict", the cookie will only be sent along with "same-site" requests. If the value is "Lax", the cookie will be sent with same-site requests, and with "cross-site" top-level navigations, as described in Section 5.3.7.1. If the value is "None", the cookie will be sent with same-site and cross-site requests. If the "SameSite" attribute's value is something other than these three known keywords, the attribute's value will be treated as "None".

The "SameSite" attribute affects cookie creation as well as delivery. Cookies which assert "SameSite=Lax" or "SameSite=Strict" cannot be set in responses to cross-site subresource requests, or cross-site nested navigations. They can be set along with any top-level navigation, cross-site or otherwise.

"same-site" か "cross-site" かの判定

A request is "same-site" if its target's URI's origin's registrable domain is an exact match for the request's client's "site for cookies", or if the request has no client. The request is otherwise "cross-site". For a given request ("request"), the following algorithm returns "same-site" or "cross-site":

1.If "request"'s client is "null", return "same-site". Note that this is the case for navigation triggered by the user directly (e.g. by typing directly into a user agent's address bar).

2.Let "site" be "request"'s client's "site for cookies" (as defined in the following sections).

3.Let "target" be the registrable domain of "request"'s current url.

4.If "site" is an exact match for "target", return "same-site".

5.Return "cross-site".

The request's client's "site for cookies" is calculated depending upon its client's type, as described in the following subsections:

https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#samesite-cookie-attribute