16 min read
HTTP cookies: HttpOnly, Secure and SameSite
In one sentence: A cookie is a small piece of data the server asks the browser to store and send back on its next requests to that site. That's how a protocol with no memory recognizes you. HttpOnly, Secure and SameSite decide who can read it and when it travels.
What you will learn
- Explain how Set-Cookie and Cookie give HTTP a memory, and what a session is.
- Know which requests the browser adds a cookie to, based on its domain, path, expiry and SameSite.
- Choose a cookie's attributes (HttpOnly, Secure and SameSite) and explain what each one protects against.
- See cookies with curl and in DevTools, and tell third-party cookies and localStorage apart.
Before you start, read: 04-headers.md
The problem
Section titled “The problem”Imagine you add a mug to your cart in Ana and Luis’s shop and press “Checkout”. The checkout page loads… and your cart is empty. To the server, that request comes from a stranger.
It’s not a bug in the shop: that’s how HTTP works. As you saw in lesson 2 of Phase 0, every request reaches the ServerA program that waits for requests and answers them. By extension, the computer it runs on is also called a server.Go to definition as if it were the first one. HTTP is a stateless protocol: it keeps nothing from one request to the next.
For the shop to remember you, the browser has to bring it something that identifies you on every request: a . And since, to the server, whoever has your cookie is you, half of this lesson is about how to protect it.
The analogy
Section titled “The analogy”The analogy
Think of a theater’s coat check. You leave your coat and they give you a ticket with a number, 42. On your way out, you show the 42 and they give you back your coat, without asking your name.
The ticket is the cookie, and the coat is what the server keeps about you: your cart or your account. The rules printed on the ticket (“valid only in this theater,” “expires at closing time”) are the cookie’s attributes.
The coat check doesn’t look at faces: whoever shows the 42 takes the coat. That’s why who can see your cookie matters so much.
Where the analogy breaks down
- You decide when you show the ticket. With a cookie, you don’t: the browser adds it to every request that meets its rules, even if another website triggers it.
- A ticket is hard to forge; anyone can change a cookie in their browser. Don’t store anything in it the user shouldn’t change, like a price.
How it really works
Section titled “How it really works”The server asks to store it and the browser sends it back
Section titled “The server asks to store it and the browser sends it back”This is how the browser talks to the shop, the server you have in your VM:
- Shop server → Browser200 OK
Set-Cookie: cart=c4f9e2; Path=/; Max-Age=3600; HttpOnly; SameSite=Lax - BrowserStores cart=c4f9e2 for one hour
- Shop server → Browser200 OK
(knows you're cart c4f9e2)
There are two HeaderInformation a protocol puts in front of its data to do its job. In HTTP they are text lines of the form “Name: value”, such as Host or Content-Type; in TCP or IP they are numbers in fixed positions, such as ports or addresses.Go to definition, one in each direction:
Set-Cookie, in the response: the server asks to store a cookie. First, the name and the value (cart=c4f9e2); after them, separated by;, the attributes.Cookie, in the following requests: the browser sends it back. Only the name and the value: the attributes are instructions for the browser and never go back to the server.
Each cookie arrives on its own Set-Cookie line. On the way back, the browser puts them together in a single header: Cookie: cart=c4f9e2; lang=en.
A session: the number in the cookie, the data on the server
Section titled “A session: the number in the cookie, the data on the server”c4f9e2 means nothing on its own: it’s the number on the ticket. A real server has a table with what belongs to each number: “c4f9e2: a mug and a T-shirt.”
That’s a Session (on a website)What a server remembers about a visitor from one request to the next, such as who they are or what’s in their cart. The server keeps that data under a long, random identifier, and the browser only keeps that identifier, almost always in a cookie.Go to definition. Logging in works the same way: you type your password once, the server checks it, creates a session and sends you its identifier. From then on, the cookie proves who you are. That’s why a real identifier isn’t short like c4f9e2, but long and random, impossible to guess.
Why not store the whole cart in the cookie?
- The user can change it: a
price=1in a cookie is an invitation. - Not much fits: about 4 KB per cookie.
- It travels on every request to that site, including the ones for images: the heavier it is, the slower the website.
Another way to remember who you are is a token in the Authorization header (lesson 4): sessions versus tokens, in Phase 6.
How long it lasts: Expires and Max-Age
Section titled “How long it lasts: Expires and Max-Age”Max-Age=3600: it lasts 3600 seconds, one hour, from when it arrives.Expires=Tue, 12 Oct 2027 18:36:42 GMT: it lasts until that date. If it has both,Max-Agewins.- Neither: it’s deleted when you close the browser, unless the browser reopens the tabs from last time.
Chrome doesn’t let a cookie last more than 400 days. To delete it, the server sends it again with the same name, the same path and Max-Age=0.
Who the browser sends it back to
Section titled “Who the browser sends it back to”The browser doesn’t send all its cookies on every request. Each cookie goes only on requests to the site that created it, and only if it meets all its rules:
| Attribute | Without it | With it |
|---|---|---|
Domain |
Only to the exact domain that created it, shop.example |
Domain=shop.example: also to its subdomains, like api.shop.example |
Path |
To the folder of the path that created it: if /account/details creates it, to everything under /account |
Path=/: to every path on the site |
Max-Age or Expires |
Until you close the browser | Until it expires |
Secure |
Over https:// and over http:// |
Only over https:// |
SameSite |
Depends on the browser (below) | Decides whether it travels when another website triggers the request |
shop.example can’t create cookies for bank.example. Path is for keeping things tidy, not for protection. And cookies don’t look at the port.
HttpOnly: JavaScript can’t read it
Section titled “HttpOnly: JavaScript can’t read it”Web pages carry programs in JavaScript, the language that makes buttons and menus work. That code reads its page’s cookies with document.cookie: document is the page, and cookie, its cookies.
The danger is someone else’s code. If somebody sneaks their JavaScript into one of the shop’s pages (an attack called XSS, in Phase 6), it can read the session cookie, send it to them, and they get into the victim’s account.
HttpOnly hides the cookie from JavaScript, not from the network. The browser keeps sending it on every request, so the session works the same, but the page’s code can’t read it or change it. A session cookie must always have HttpOnly; only what the page needs to read goes without it, like the language.
Secure: only over HTTPS
Section titled “Secure: only over HTTPS”A cookie without Secure also travels on http:// requests, unencrypted: anyone along the way can read it, as you saw in lesson 8 of Phase 0. With Secure, it only travels over HTTPSHTTPS stands for HyperText Transfer Protocol Secure.HTTP inside a TLS connection, usually on port 443. Nobody along the way can read or change the requests and responses, and the browser uses a certificate to check that the server really belongs to the domain.Go to definition.
A website served over http:// can’t create Secure cookies either: Chrome and Firefox discard them, except on http://localhost, so you can develop.
SameSite: whether it travels when the request comes from another website
Section titled “SameSite: whether it travels when the request comes from another website”Imagine you visit fake-deals.example. That page can have a link to the shop, load one of its images or hide a form that sends it a POST. Do those requests carry your cookie?
SameSite decides, by comparing the site of the page that triggers the request with the site it goes to. A site is the registered domain (shop.example) with its subdomains (api.shop.example); for Chrome, http:// and https:// are also different sites. In lesson 6 you’ll see the Origin (on the web)What identifies a website to the browser: the scheme (http or https), the server name and the port. https://shop.example and https://www.shop.example are different origins; the path doesn’t count.Go to definition, a stricter idea.
| From another website… | Strict |
Lax |
None |
|---|---|---|---|
| You click a link to the shop | Doesn’t travel | Travels | Travels |
A form sends a POST to the shop |
Doesn’t travel | Doesn’t travel | Travels |
| The page loads an image from the shop | Doesn’t travel | Doesn’t travel | Travels |
| The page’s JavaScript makes a request to the shop | Doesn’t travel | Doesn’t travel | Only if that code asks for it (lesson 6) |
From the shop itself, it travels with all three values. With None, as long as the browser doesn’t block third-party cookies (below).
Strict: it never travels from another site, not even when following a link: if you reach the shop from Google, the first page sees you without a session. It’s for the most sensitive things, like a bank.Lax: it travels when you go to the shop through a link (a navigation withGET), but not in what another website does on its own. It’s the usual choice for a session.None: it always travels, for things that work inside other websites, like an embedded payment. It requiresSecure: without it, Chrome and Firefox discard it.
And without SameSite? Chrome, and the browsers based on it like Edge, treat it as Lax (with one exception: in its first two minutes, it also travels in a POST form from another website). Firefox and Safari don’t apply that rule. Always write it.
Lax lets links through: if the shop emptied the cart with a GET /cart/empty, a link on another website would empty it. That’s why a GET must not change anything (lesson 2). Another website making your browser send requests with your cookies is the basis of an attack, CSRFCSRF stands for Cross-Site Request Forgery.An attack in which a page makes your browser send a request to a site where you’re logged in, with your cookies, to do something on your behalf, such as changing your email or buying something. CORS doesn’t prevent it, because many requests reach the server even if the page can’t read the response.Go to definition, which you’ll see in lesson 6 and in Phase 6.
First-party and third-party cookies
Section titled “First-party and third-party cookies”The cookies of the site you see in the address bar are first-party. But a page loads things from other domains, like ads or a “Like” button, and each one can bring its own cookies. Those are .
An advertiser present on a thousand websites receives its cookie from all thousand: it knows where you go. That’s why browsers limit them. As of October 2026:
- Safari blocks them by default since 2020.
- Firefox, since 2022, keeps each website’s cookies in a separate jar, so the advertiser sees a different cookie on each one.
- Chrome allows them, except in incognito or if you block them yourself. In April 2025 it announced it’s keeping it that way.
In the European Union, a website has to ask your permission for the cookies that aren’t essential, like advertising ones: hence the banners. The cart cookie and your session cookie don’t need it.
Cookies or localStorage
Section titled “Cookies or localStorage”A website can also store data in localStorage, which works the other way around:
| Cookie | localStorage | |
|---|---|---|
| Who reads it | The server, on every request, and the page’s JavaScript if it isn’t HttpOnly |
Only the page’s JavaScript |
| Travels to the server on its own | Yes | Never |
| How much fits | About 4 KB per cookie | About 5 MB per website |
| If someone sneaks in their JavaScript | HttpOnly protects it |
They can read all of it |
For a session identifier, the HttpOnly cookie wins. localStorage is for things that don’t matter if someone reads them, like the light or dark theme.
Try it
Section titled “Try it”The goal: play the browser with curl and check in the browser that JavaScript can’t read the cart cookie, even though it travels on every request.
First, on your laptop:
1. A real website's cookies
curl -sI https://github.com | grep -i '^set-cookie'What you will see:
set-cookie: _gh_sess=qco6SrnNp%2BGaJM73…; path=/; HttpOnly; secure; SameSite=Laxset-cookie: _octo=GH1.1.1411589659.1791830202; expires=Tue, 12 Oct 2027 18:36:42 GMT; domain=.github.com; path=/; secure; SameSite=Laxset-cookie: logged_in=no; expires=Tue, 12 Oct 2027 18:36:42 GMT; domain=.github.com; path=/; HttpOnly; secure; SameSite=Lax-I asks only for the headers, -s silences the progress bar and grep -i (lesson 3 of Phase 1) keeps the set-cookie lines, which come out in lowercase because GitHub uses HTTP/2.
_gh_sessis the session: long and random (we’ve cut it short), with all three attributes and noexpires: it’s deleted when you close the browser._octodoesn’t haveHttpOnly: the page’s JavaScript can read it.logged_in=no, withdomain=.github.com, also goes to subdomains likegist.github.com.
Your values and dates will be different.
Now, in your VM (multipass shell course), where the shop has been answering on port 8080 since you installed it in this phase’s introduction. If you don’t have a VM, read the outputs. Your version of curl may be different.
2. The server gives you the cookie
curl -i localhost:8080/cartWhat you will see:
HTTP/1.1 200 OKServer: nginx/1.24.0 (Ubuntu)Date: Tue, 13 Oct 2026 08:01:09 GMTContent-Type: text/plain; charset=utf-8Content-Length: 20Connection: keep-aliveSet-Cookie: cart=c4f9e2; Path=/; Max-Age=3600; HttpOnly; SameSite=Lax
Your cart is empty.Look at the Set-Cookie line: it doesn’t have Secure because the practice shop has no HTTPS. If you repeat the command, curl doesn’t send the cookie: it stores nothing unless you ask it to. If you get Couldn't connect to server, go over the introduction again.
3. Store it in a file
curl -c cookies.txt localhost:8080/cartcat cookies.txtWhat you will see:
Your cart is empty.# Netscape HTTP Cookie File# https://curl.se/docs/http-cookies.html# This file was generated by libcurl! Edit at your own risk.
#HttpOnly_localhost FALSE / FALSE 1791882069 cart c4f9e2-c (from cookie jar) stores the cookies it receives in a file. The last line is the cookie: the domain (with the #HttpOnly_ mark), whether it’s valid for subdomains, the path, whether it’s Secure, the expiry (in seconds since 1970; yours will be different), the name and the value.
4. Send it back
curl -v -b cookies.txt localhost:8080/productsWhat you will see:
…> GET /products HTTP/1.1> Host: localhost:8080> User-Agent: curl/8.5.0> Accept: */*> Cookie: cart=c4f9e2>< HTTP/1.1 200 OK< Server: nginx/1.24.0 (Ubuntu)…[{"id":1,"name":"T-shirt","price":20},{"id":2,"name":"Mug","price":8.5},{"id":3,"name":"Hoodie","price":35}]…-b adds the cookies from the file that apply. Look at > Cookie: cart=c4f9e2: no attributes, and on /products, because it has Path=/. We’ve removed some lines (…). nginx ignores it; in Phase 3, your server will look it up in its sessions table.
Now, a cookie the shop’s JavaScript can read: the language. Open the configuration with sudo nano /etc/nginx/sites-available/shop (the editor from lesson 2 of Phase 1) and, in location = /cart, below the cart line, add this one, the same but without HttpOnly:
add_header Set-Cookie "lang=en; Path=/; Max-Age=3600; SameSite=Lax" always;Save with Ctrl + O and Enter, and quit with Ctrl + X.
5. Check and reload
sudo nginx -t && sudo systemctl reload nginxWhat you will see:
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulIf nginx -t says failed, look at the line it points to: usually the final ; or a quotation mark is missing.
6. Two cookies, two lines
curl -i localhost:8080/cartWhat you will see:
HTTP/1.1 200 OKServer: nginx/1.24.0 (Ubuntu)Date: Tue, 13 Oct 2026 08:01:15 GMTContent-Type: text/plain; charset=utf-8Content-Length: 20Connection: keep-aliveSet-Cookie: cart=c4f9e2; Path=/; Max-Age=3600; HttpOnly; SameSite=LaxSet-Cookie: lang=en; Path=/; Max-Age=3600; SameSite=Lax
Your cart is empty.Each cookie on its own line. If only the cart one shows up, repeat it: nginx takes a moment to reload.
Now, the browser. Open http://192.168.64.2:8080/cart in Chrome, with your VM’s IP (it shows up in multipass list, as in lesson 9 of Phase 1). Open DevToolsThe developer tools that come with browsers such as Chrome or Firefox, opened with F12. Their Network tab shows every request the page makes, with its response and headers; the Console, the page’s errors; and Application (Storage, in Firefox), what the site stores in your browser, such as cookies.Go to definition, as in this phase’s introduction:
- Where they’re stored. In the Application tab, open Storage › Cookies and choose your VM’s address. Both are there, and the
HttpOnlycolumn is only checked forcart. In Firefox, it’s the Storage tab. - That both travel. In the Network tab, reload and click the
cartrequest. Under Headers, the request carriesCookie: cart=c4f9e2; lang=en. - What JavaScript sees. In the Console tab, type this and press Enter:
7. What the page's JavaScript sees
document.cookieWhat you will see:
'lang=en'Only lang shows up. The cart cookie travels on every request, but the page’s code doesn’t see it: that’s HttpOnly.
Type it by hand: Chrome holds back what you paste into the console, because that’s how sessions get stolen.
To undo it, delete the language line and repeat step 5. You can delete the browser’s cookies in Application, by right-clicking each row.
Challenge: the shop wants an “Empty cart” button that also forgets the cookie. What Set-Cookie would it have to send? Change it in the cart line and check it with curl -b cookies.txt -c cookies.txt.Show answer
The same cookie, with the same name and the same path, with no value and with Max-Age=0:
add_header Set-Cookie "cart=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax" always;After reloading nginx, curl -b cookies.txt -c cookies.txt localhost:8080/cart sends the old cookie, receives the order to delete it and rewrites the file: with cat cookies.txt you’ll see the cart line is gone. With another path, like Path=/cart, it wouldn’t delete it: to the browser it would be a different cookie. When you’re done, put the line back as it was.
You have already seen it
Section titled “You have already seen it”- “Keep me signed in”. It’s a cookie with a long expiry. And “Sign out” makes the server forget your session and usually deletes the cookie with
Max-Age=0. - The incognito window. It has its own cookie jar, which is emptied when you close it: that’s why you’re not signed in there.
- This website. It doesn’t create cookies: the light or dark theme you choose is stored in localStorage and never travels to the server.
And if you code:
fetchsends cookies to its own origin; to another one, only withcredentials: 'include'and, to read the response, if the server allows it (lesson 6).- Your frontend on
localhost:5173and your API onlocalhost:3000share cookies: they don’t look at the port.
Common mistakes
Section titled “Common mistakes”- Thinking the browser sends all your cookies everywhere. Each cookie goes only to its site, if it meets its domain, its path,
SecureandSameSite. A website never receives another website’s cookies. - Thinking
HttpOnlyhides the cookie from the server or the user. It only hides it from JavaScript: it travels on every request, and you can see it and change it in DevTools. - Storing in a cookie what the user must not change, like
price=20oradmin=true. Store an identifier and keep the data on the server. SameSite=NonewithoutSecure, orSecureon a website overhttp://. Chrome and Firefox discard the cookie without any error on the page. If you trySecureon the VM’s shop through its IP, the cookie won’t show up.
Summary
Section titled “Summary”- HTTP has no memory. The server sends
Set-Cookie, and the browser sends back the name and the value inCookie, only on requests to that site that meet its rules. - A session keeps the data on the server and a long, random identifier in the cookie. To the server, whoever has it is you.
HttpOnlyhides the cookie from JavaScript,Securelimits it to HTTPS andSameSitedecides whether it travels when another website triggers the request.- localStorage doesn’t travel to the server on its own, but any JavaScript on the page can read it.
- In Phase 3 you’ll write the shop’s server in code: it will create each cart’s identifier and send it with
Set-Cookie.
Did you get it?
Section titled “Did you get it?”To review:
Further reading
Section titled “Further reading”- Using HTTP cookies (MDN): the complete guide, with every attribute.
- Set-Cookie (MDN): the header, attribute by attribute.
- SameSite cookies explained (web.dev):
Strict,LaxandNonewith examples, from the Chrome team. - RFC 6265: the cookie standard.