Skip to content
Sign inGet all access
Chapter 11·12 minFree

Cookies, CORS and Security Headers

The browser rules, and why curl cannot reproduce them.

The browser enforces all of this. The server only makes statements.

Everything after the value is an instruction, not data:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Set-Cookie: s=1; HttpOnly; Secure; SameSite=Lax; '
  printf 'Path=/; Max-Age=3600\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i set-cookie
OutputSet-Cookie: s=1; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=3600
Attribute Does
HttpOnly JavaScript cannot read it
Secure only sent over HTTPS
SameSite when it travels cross-site
Path which paths it is sent to
Max-Age how long it lives, in seconds

HttpOnly is the one that matters: a session cookie readable from JavaScript is stealable by any script on the page.

SameSite

Three values, and the default recently changed:

Value Sent cross-site
Strict never
Lax only on top-level navigation
None always, and Secure is mandatory

Absent the attribute, browsers now assume Lax. That broke every cross-site POST relying on a cookie, which was the point.

A server can send None alone; the browser is what rejects it:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Set-Cookie: s=1; SameSite=None\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i set-cookie
OutputSet-Cookie: s=1; SameSite=None

SameSite=None without Secure is dropped by the browser, so a cross-site cookie requires HTTPS. There is no cleartext path.

What an origin is

Scheme, host and port. All three must match:

URL Same origin as https://app.com/a?
https://app.com/b yes — path is irrelevant
http://app.com/a no — different scheme
https://api.app.com/a no — different host
https://app.com:8443/a no — different port

A subdomain is a different origin — app.com and api.app.com are as foreign to each other as two unrelated sites.

Why CORS exists

The same-origin policy stops one site reading another’s responses. CORS opts out for chosen origins, and the browser says who is asking:

terminal
nc -l 9000 | tr -d '\r' | grep -iE '^(GET|Origin)' &
curl -s --max-time 1 -H 'Origin: https://app.example.com' localhost:9000/x
OutputGET /x HTTP/1.1Origin: https://app.example.com

Simple requests

A GET with ordinary headers goes straight out:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Access-Control-Allow-Origin: https://app.example.com\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI -H 'Origin: https://app.example.com' localhost:9000/ \
  | grep -i access-control
OutputAccess-Control-Allow-Origin: https://app.example.com

The browser then decides whether the page may read it. CORS restricts the browser, not the network.

Preflight

Anything else gets an OPTIONS first:

terminal
nc -l 9000 | tr -d '\r' | grep -iE '^(OPTIONS|Origin|Access-Control)' &
curl -s --max-time 1 -X OPTIONS localhost:9000/api \
  -H 'Origin: https://app.example.com' \
  -H 'Access-Control-Request-Method: PUT' \
  -H 'Access-Control-Request-Headers: content-type'
OutputOPTIONS /api HTTP/1.1Origin: https://app.example.comAccess-Control-Request-Method: PUTAccess-Control-Request-Headers: content-type

The server answers what it permits; Max-Age caches that answer:

terminal
{ printf 'HTTP/1.1 204 No Content\r\n'
  printf 'Access-Control-Allow-Origin: https://app.example.com\r\n'
  printf 'Access-Control-Allow-Methods: GET, PUT, DELETE\r\n'
  printf 'Access-Control-Allow-Headers: content-type\r\n'
  printf 'Access-Control-Max-Age: 86400\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI -X OPTIONS localhost:9000/api | grep -iE 'HTTP|access-control'
OutputHTTP/1.1 204 No ContentAccess-Control-Allow-Origin: https://app.example.comAccess-Control-Allow-Methods: GET, PUT, DELETEAccess-Control-Allow-Headers: content-typeAccess-Control-Max-Age: 86400

The credentials rule

Access-Control-Allow-Origin: * and credentials are mutually exclusive by specification:

Server sends Browser sends cookies Works
* no yes
* yes no
the exact origin yes yes, with Allow-Credentials: true

The wildcard is one header; the credentialed form is two:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Access-Control-Allow-Origin: *\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i access-control
OutputAccess-Control-Allow-Origin: *
terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Access-Control-Allow-Origin: https://app.example.com\r\n'
  printf 'Access-Control-Allow-Credentials: true\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -i access-control
OutputAccess-Control-Allow-Origin: https://app.example.comAccess-Control-Allow-Credentials: true

Reading a CORS failure

curl cannot reproduce one — no origin, no enforcement:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Content-Length: 3\r\n\r\nok\n'
} | nc -l 9000 >/dev/null &
curl -s -H 'Origin: https://evil.example' localhost:9000/
Outputok

No Access-Control-Allow-Origin anywhere, and the body printed regardless.

Security headers

Three worth setting, each one line:

terminal
{ printf 'HTTP/1.1 200 OK\r\n'
  printf 'Strict-Transport-Security: max-age=31536000; includeSubDomains\r\n'
  printf "Content-Security-Policy: default-src 'self'\r\n"
  printf 'X-Content-Type-Options: nosniff\r\n'
  printf 'Content-Length: 0\r\n\r\n'
} | nc -l 9000 >/dev/null &
curl -sI localhost:9000/ | grep -iE 'strict|content-security|x-content'
OutputStrict-Transport-Security: max-age=31536000; includeSubDomainsContent-Security-Policy: default-src 'self'X-Content-Type-Options: nosniff

default-src 'self' is the CSP directive to start from: scripts, styles and frames from your origin only.