Track 01 · Lesson 7 of 9How the web works
Cookies
Lesson 7 of 9· 6 min
HTTP has no memory: every request arrives as if from a stranger. Cookies fix that. The server hands your browser a small value, and the browser sends it back with every request to that site, automatically.
How it works#
- The server responds with a
Set-Cookieheader. - The browser stores the cookie under that site, with an expiry date.
- On every later request to that site, the browser adds a
Cookieheader by itself. - The server reads it to recognise you, usually by looking up a session ID.
- It expires, or the server clears it when you log out.
Why "automatically" matters#
That convenience is also the catch. The browser attaches a site's cookies to every request to that site, including requests that another website quietly starts on your behalf. And whoever gets hold of the cookie's value is you, as far as the server can tell.
That's what the flags are for. HttpOnly keeps it away from JavaScript, Secure keeps it off plain http, and SameSite stops other sites from triggering it. You'll see exactly which attacks each one blocks in Attacks and defenses, and how cookies compare to tokens in Identity and login.
Check yourself