After login, the shop sends a random session id in a cookie named sid. The browser sends it back with every request, so whoever holds it is logged in. A cookie with no flags can be read by any script on the page. It also travels over plain http and goes along with requests that other sites start.
That gives the attacker three ways in. A script injected through any XSS hole reads sid from document.cookie. Someone on the same café Wi-Fi reads it off a plain http:// request. And a hidden form on another site posts to the shop with the visitor's cookie and changes their delivery address.
Each flag closes one door. HttpOnly: document.cookie leaves the cookie out. Secure: the browser sends it only over https. SameSite=Lax: a POST that another site starts arrives without it, while following a link to the shop still keeps the visitor logged in. In Flask, each flag is a keyword argument of set_cookie, the way max_age is here:
response.set_cookie("lang", "ro", max_age=60 * 60 * 24 * 365)
# Set-Cookie: lang=ro; Expires=…; Max-Age=31536000; Path=/
The check stands in for a browser. It prints the cookie's value as <sid>, answers /check/document-cookie with what a page script could read, sends the evil form from https://evil.example, and holds the cookie back wherever a real browser would:
POST /login {"email": "ana@example.com", "password": "guess-123"} → 401 {"error": "Wrong email or password"}
POST /login {"email": "ana@example.com", "password": "ana-pass-123"} → 200 {"email": "ana@example.com"}
Set-Cookie: sid=<sid>; Secure; HttpOnly; Path=/; SameSite=Lax
GET /me → 200 {"address": "Str. Florilor 3, Cluj", "email": "ana@example.com"}
GET /check/document-cookie → 200 {"document.cookie": ""}
POST /account/address (form address=Evil Street 1, Faraway, from https://evil.example) → 401 {"error": "Login required"}
GET http://localhost/me → 401 {"error": "Login required"}
POST /account/address (form address=Str. Lunga 5, Cluj) → 200 {"address": "Str. Lunga 5, Cluj", "email": "ana@example.com"}
GET /me → 200 {"address": "Str. Lunga 5, Cluj", "email": "ana@example.com"}
Your task: in the set_cookie call in login, below sid,, add the three flags, one per line: httponly and secure set to True, and samesite set to "Lax".
Assignment
Follow the Coding Tutorial to set the HttpOnly, Secure and SameSite flags on the session cookie.
Hint
Each flag is a keyword argument on its own line, ending with a comma. samesite takes a string, "Lax", with a capital L.
Free plan: 30 tutor messages. PRO is unlimited.
