NGU-Web/docs/security-headers.md
Zaldimmar 2b009beac1 Use organization-neutral names in code and browser keys
- The timeline's ngu-tl classes and keyframes are site-tl; nguLogo is
  siteLogo; the event card's ngu grid area is org; window.__NGU_SITE__
  is window.__SITE__; QR downloads are qr-<page>.png
- Browser keys (theme, closed banner and privacy notice, chunk reload)
  start site-; the page's first script moves any old ngu- values over
  once, so nobody loses their theme or sees a closed notice again
- Session and Google sign-in cookies are site_session and site_oauth
  (__Host- over https); everyone signs in again once

Record ids, server paths, the service and the database file keep
their names.

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

99 lines
3.9 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-site_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 `site_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.
- The per-email lock is a trade-off: anyone who knows an admin's
address can keep password sign-in for it locked by guessing wrong
five times every 15 minutes. Google sign-in still works for that
account, and the lock clears on a restart.
- `clientIp` trusts the last `X-Forwarded-For` entry, the one nginx
adds. If a CDN or another proxy is ever put in front of nginx, that
entry becomes the proxy's address and every visitor shares one
rate-limit bucket: set nginx's `real_ip_header` and
`set_real_ip_from` for the proxy first.
- The Google sign-in state cookie is `__Host-site_oauth` (plain
`site_oauth` over local http), and the page it returns to is checked
again when the browser comes back.
## 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.