NGU-Web/docs/security-headers.md
Zaldimmar be7e92417e Fix the Panel account bug and harden what the second audit found
- Panel: changing a role or disabling an account always failed
  (db.transaction doesn't exist on node:sqlite); it uses tx() now
- Feeds and link previews are scanned with indexOf, not whole-document
  regexes, so a hostile feed or page can't freeze the server; entity
  decoding can't throw; escaped-HTML descriptions make clean excerpts
- Feed reads share one fetch per feed and serve the stale copy while
  a refresh runs
- Deliberate HTTP errors (the CSRF 403) keep their status instead of
  becoming logged 500s; a JSON null body is handled
- url columns read the scheme the way the URL parser does, so
  control characters can't hide javascript:
- Google sign-in state cookie is __Host- and its return page is
  checked again; more reserved ranges blocked for previews and feeds,
  and a 15 s cap on each whole fetch
- Log lines from visitor text are made single-line and short; list
  limits can't go negative; entity names can't hit object prototypes
- A clip title can't point the clip tool at another file
- Uploads over 60 MP are refused before decoding; the Keep original
  help says a photo's location stays in it
- admin-cli add defaults to the editor role

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 02:34:26 -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-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.
- 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-ngu_oauth` (plain
`ngu_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.