Příručka:SameSite cookies
SameSite je nedávný doplněk k syntaxi HTTP cookie.
Pokud je soubor cookie označen jako SameSite=Lax nebo SameSite=Strict, prohlížeč jej neodešle s požadavky napříč doménami.
(Rozdíl mezi těmito dvěma pojmy spočívá v interpretaci pojmu "cross-domain": u Laxu se vztahuje pouze na "skryté" požadavky, jako je AJAX nebo iframe, zatímco u Strict je zahrnuta i navigace uživatele na nejvyšší úrovni, jako je kliknutí na odkaz vedoucí na jinou doménu.)
Prohlížeče plánují nastavit jako výchozí nastavení cookies bez specifikace SameSite pro Lax. Od podzimu 2020 je Chrome jediný, který to skutečně dělá.[1]
Weby běžící na více doménách, které nejsou na tuto změnu připraveny, mohou narazit na různé problémy, například selhání ověřování.
(Webové stránky běžící na jedné doméně nebudou ovlivněny.)
As of 1.34, MediaWiki supports setting the SameSite flag on cookies.
The default for authentication-related cookies is determined by the $wgCookieSameSite setting.
Setting this to None (and enabling $wgForceHTTPS, as use of secure HTTPS cookies is required by browsers for SameSite=None) can fix issues such as MediaWiki seeing the user as not logged in when using cross-domain features.
Setting the flag on non-authentication cookies is the responsibility of the code handling the cookie; this can only be decided individually, and in most cases it is not needed.
Upozornění prohlížeče
Browsers can show various warnings on cookies which do not have the SameSite flag, e.g. Firefox tends to show errors like this in the developer console: Cookie “<name>” will be soon rejected because it has the “sameSite” attribute set to “none” or an invalid value, without the “secure” attribute. This is a confusing and poorly worded message; what it is supposed to mean is that the cookie will soon be treated as SameSite=Lax and thus not sent with cross-domain AJAX requests.
For most non-authentication cookies this is not a problem and the warning can be ignored.
It might be worth to explicitly set SameSite=Lax to get rid of the warning, though.
Používání souborů cookie SameSite v MediaWiki
In PHP, to set the SameSite flag on a cookie, use WebResponse::setCookie() with $options['sameSite'] = 'lax' or similar.
To take $wgUseSameSiteLegacyCookies compatibility cookie into account when reading cookies, use WebRequest::getCrossSiteCookie() instead of WebRequest::getCookie().
In Javascript, use the sameSite property of the options object passed to $.cookie to set the SameSite flag on a cookie.
Ladění a hlášení chyb
- https://samesite-sandbox.glitch.me/ can be used to check a browser's standard compliance. With the new default-to-Lax SameSite behavior, it should be all green.
- Set specific SameSite handling behavior for testing:
- Chrome:
same-site-by-default-cookiesandcookies-without-same-site-must-be-secureflags (see also their debugging tutorial) - Firefox:
network.cookie.sameSite.laxByDefaultandnetwork.cookie.sameSite.noneRequiresSecureconfiguration keys
- Chrome:
Související odkazy
- Documentation on MDN
- caniuse browser stats and docs
- The draft standards for SameSite and for defaulting to Lax.
- Příručka:HTTPS
- Main task for SameSite issues: T255366
Poznámky pod čarou
- ↑ Chrome používá některé starší výjimky. Viz "Zmírnění Lax + POST" v jejich často kladených otázkách.