setSameSiteDomain.py (1957B)
1 from cookies.resources.helpers import makeCookieHeader, setNoCacheAndCORSHeaders 2 3 from wptserve.utils import isomorphic_encode 4 5 def main(request, response): 6 """Respond to `/cookie/set/samesite?{value}` by setting four cookies: 7 1. `samesite_strict={value};SameSite=Strict;path=/;domain={host}` 8 2. `samesite_lax={value};SameSite=Lax;path=/;domain={host}` 9 3. `samesite_none={value};SameSite=None;path=/;Secure;domain={host}` 10 4. `samesite_unspecified={value};path=/;domain={host}` 11 Where {host} is the hostname from which this page is served. (Requesting this resource 12 without a Host header will result in a 500 server error.) 13 Then navigate to a page that will post a message back to the opener with the set cookies""" 14 headers = setNoCacheAndCORSHeaders(request, response) 15 value = isomorphic_encode(request.url_parts.query) 16 host_header = request.headers['host'] 17 hostname = host_header.split(b":")[0] 18 host = isomorphic_encode(hostname) 19 headers.append((b"Content-Type", b"text/html; charset=utf-8")) 20 headers.append(makeCookieHeader(b"samesite_strict", value, {b"SameSite":b"Strict", b"path":b"/", b"domain":host})) 21 headers.append(makeCookieHeader(b"samesite_lax", value, {b"SameSite":b"Lax", b"path":b"/", b"domain":host})) 22 # SameSite=None cookies must be Secure. 23 headers.append(makeCookieHeader(b"samesite_none", value, {b"SameSite":b"None", b"path":b"/", b"Secure": b"", b"domain":host})) 24 headers.append(makeCookieHeader(b"samesite_unspecified", value, {b"path":b"/", b"domain":host})) 25 26 document = b""" 27 <!DOCTYPE html> 28 <script> 29 // A same-site navigation, which should attach all cookies including SameSite ones. 30 // This is necessary because this page may have been reached via a cross-site navigation, so 31 // we might not have access to some SameSite cookies from here. 32 window.location = "../samesite/resources/echo-cookies.html"; 33 </script> 34 """ 35 36 return headers, document