NGU-Web/docs/security-headers.md
Zaldimmar b165e612c2 Harden sign-in: __Host- session cookie and a per-email login lock
- The session cookie is __Host-ngu_session when Secure (production),
  so a sibling subdomain can't set or overwrite it; plain ngu_session
  over local http. Every admin signs in again once after deploying.
- Five wrong passwords for one email lock that email for 15 minutes,
  from any address; a correct password clears it. On top of the
  per-IP limit.
- CLAUDE.md: noImplicitAny stays off on purpose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 02:11:57 -05:00

87 lines
3.2 KiB
Markdown

# Security headers
Who sends what, and what nginx has to do for it.
## From the API
Every response the API sends (pages, `/api`, the sitemap, robots.txt)
carries Hono's `secureHeaders()` (`server/src/index.js`):
- `X-Frame-Options: SAMEORIGIN`: the site can't be framed by another site.
- `X-Content-Type-Options: nosniff`
- `Referrer-Policy: strict-origin-when-cross-origin`: kept to the origin,
because YouTube embeds refuse to play without a referrer.
- `Cross-Origin-Opener-Policy` and `Cross-Origin-Resource-Policy: same-origin`
- no HSTS (nginx sends it, below)
- `Content-Security-Policy-Report-Only`: blocks nothing yet. Browsers
post what it would have blocked to `/api/csp-report`, which logs one
line per report (`journalctl -u ngu-api | grep csp`). The allowed
embed hosts are `EMBEDS` in `server/src/index.js`. After a while of
real use with nothing unexpected in the log, rename
`contentSecurityPolicyReportOnly` to `contentSecurityPolicy` there to
enforce it. A new embed host (a new video site, say) has to be added
to `EMBEDS` first.
The API also refuses a form-style write (`text/plain`, form or
multipart body) sent from another site, including a sibling subdomain
(`hono/csrf`). Browsers mark their own requests
`Sec-Fetch-Site: same-origin`; for older ones it compares `Origin` with
the `Host` header, so **nginx must pass `Host`**:
```nginx
proxy_set_header Host $host;
```
Rate limits and stored IP hashes use the **last** `X-Forwarded-For`
entry, the one nginx adds from the connection, so each block that
proxies to the API needs:
```nginx
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
```
Both lines are already in every proxied block in `nginx-dev` and in
`docs/seo.md`.
## Sign-in
- The session cookie is `__Host-ngu_session`: the browser only
accepts it Secure, on path `/`, with no Domain, so another
subdomain can't set or overwrite it. With `COOKIE_SECURE=false`
(plain-http local work) it's `ngu_session`, since the prefix needs
Secure.
- Logins are limited per IP where `/api/auth/login` is mounted (10
per 15 minutes, `server/src/index.js`) and per email in
`routes/auth.js`: five wrong passwords for one address lock it for
the rest of 15 minutes, from anywhere; a correct one clears the
count. Both are in memory and reset when the service restarts.
## From nginx: HSTS
nginx terminates TLS and serves files from disk (`/assets/`,
`/uploads/`, `index.html` when the API is down), so it sends HSTS for
everything:
```nginx
# In the server block (the 443 one).
add_header Strict-Transport-Security "max-age=31536000" always;
```
`add_header` in a `location` replaces the server-level ones, so every
block with an `add_header` of its own repeats that line (and, on the
dev site, the `X-Robots-Tag` one). In `nginx-dev` that's `@file`,
`/uploads/`, `/assets/`, `@static` and `= /index.html`. Blocks with no
`add_header` (the proxied ones) inherit it.
No `includeSubDomains`: each subdomain (eg. dev) sends its own. Only
add it, or `preload`, once every subdomain is on HTTPS for good; both
are hard to undo.
Check it:
```sh
curl -sI https://nextgenerationofunity.org/ | grep -i strict
curl -sI https://nextgenerationofunity.org/assets/<any file> | grep -i strict
```
Each should print the header once.