Stop 13 · Authentication & Web SecurityTopics · 3/10

Cookies

Part of the Web Development Roadmap

In short

Cookies are how a website remembers you between visits, like keeping you logged in. They're tiny and automatic, which is also why attackers love them.

Cookies are small pieces of data the server asks the browser to store and send back with every request, often used to keep a user logged in.

The jaadu at a cloakroom counter hands a token to a student.

Think of it like this

A cloakroom token

You hand over your bag and get a numbered token. Next time you show the token, they know which bag is yours. A cookie is that token: the server gives it once, and your browser shows it on every visit.

Watch it happen

How you stay logged in

Browser
Server

POST /login

1/5You submit your email and password.

The big ideas

What you need to know

Small key-value data

A cookie is a small piece of text (about 4 KB at most) that the browser stores for a site.

Sent automatically

The browser attaches matching cookies to every request to that site, so the server knows who you are.

Flags keep them safe

HttpOnly hides a cookie from JavaScript, Secure sends it only over HTTPS, and SameSite limits cross-site requests.

They can expire

Session cookies vanish when the browser closes. Cookies with Expires or Max-Age stay until that time.

See it in code

A tiny example

http
1# The server sends this after login2Set-Cookie: session_id=abc123; HttpOnly; Secure; SameSite=Lax; Max-Age=864003 4# The browser then adds this to every request to the site5Cookie: session_id=abc123

Max-Age=86400 keeps the cookie for one day. HttpOnly means document.cookie can't read it, which protects it from injected scripts.

Save yourself the pain

Common mistakes

Oops

Storing a login token in a cookie without HttpOnly.

Do this instead

Add HttpOnly so scripts, including injected ones, can't steal it.

Oops

Stuffing lots of data into cookies.

Do this instead

Keep cookies tiny. They travel with every request, so big cookies slow the whole site down.

Your mission

Find the cookies

~10 min

Quick check

Which flag stops JavaScript on the page from reading a cookie?

Go deeper

  1. 1Open the Application tab in DevTools and look at a site's cookies.
  2. 2Learn what the HttpOnly, Secure and SameSite flags protect against.
  3. 3Set a cookie from Express and read it on the next request.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge