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.
Set-Cookie attributes
Everything after the value is an instruction, not data:
{ 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| 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:
{ 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-cookieSameSite=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:
nc -l 9000 | tr -d '\r' | grep -iE '^(GET|Origin)' &
curl -s --max-time 1 -H 'Origin: https://app.example.com' localhost:9000/xSimple requests
A GET with ordinary headers goes straight out:
{ 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-controlThe browser then decides whether the page may read it. CORS restricts the browser, not the network.
Preflight
Anything else gets an OPTIONS first:
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'The server answers what it permits; Max-Age caches that answer:
{ 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'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:
{ 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{ 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-controlReading a CORS failure
curl cannot reproduce one — no origin, no enforcement:
{ 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/No Access-Control-Allow-Origin anywhere, and the body printed regardless.
Security headers
Three worth setting, each one line:
{ 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'default-src 'self' is the CSP directive to start from: scripts, styles and frames from
your origin only.