0
Challenge · 59Routing & Data

Cookies

Coreused in ~60% of apps

Where do you keep a logged-in user's token? Pick wrong and a single injected script steals every account. Cookies — done right — are the one place JavaScript can't reach.

Core idea. A cookie is a small key/value string the browser stores per-site and attaches automatically to every matching request; its attributes (HttpOnly, Secure, SameSite) decide who can read it and when it's sent — which is why an HttpOnly cookie is the safest home for an auth token.
Playground, this isn't a screenshot
Set-Cookie response header
Set-Cookie: session=a1b2c3; HttpOnly; Secure; SameSite=Lax
Request over:
evil JS on page
document.cookie
blocked
""

HttpOnly hides it from JS.

evil.com forges
cross-site request
blocked

SameSite=Lax withholds the cookie on cross-site requests.

your app
same-site request
sent

Cookie rides along automatically — you stay logged in.

A cookie is sent with every matching request automatically. HttpOnly blocks the XSS read; SameSite blocks the cross-site (CSRF) send; Secure keeps it on HTTPS — which is exactly why an auth token belongs in an HttpOnly cookie, not in localStorage.

↑ play a little first, then dig in