Send HSTS from nginx, not the API
nginx serves /assets, /uploads and the fallback index.html itself, so the API's HSTS header never reached them. nginx now sends it on everything and the API stops, so proxied pages don't get it twice. docs/security-headers.md says who sends which header and what nginx must pass (Host, X-Forwarded-For) for the CSRF check and rate limits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c0a5e3ceeb
commit
3406aa3312
5 changed files with 80 additions and 5 deletions
|
|
@ -34,6 +34,7 @@ In dev, Vite proxies `/api` to the target set in `vite.config.ts`, so the API mu
|
|||
- Uploaded media: `UPLOADS_DIR=/var/lib/ngu/uploads`, served by nginx at `/uploads/`. Setup and the nginx block are in `docs/media-uploads.md`.
|
||||
- Page HTML goes through the API, which fills in each page's title, description, preview image and structured data (`server/src/pageMeta.js`), and serves `/sitemap.xml`. It reads `SITE_CONFIG` (site.json) and `SITE_INDEX` (the built index.html), with Panel → Site settings (the `site_settings` table) laid over site.json, and serves `/robots.txt` from the same merge. nginx setup: `docs/seo.md`.
|
||||
- Video clips (Tools, admins and up): yt-dlp downloads our YouTube/Facebook videos into `CLIPS_DIR`, ffmpeg cuts them; one job at a time, files swept after a day (`server/src/clips.js`). Needs yt-dlp and ffmpeg on the VPS: `docs/video-clips.md`.
|
||||
- Security headers: the API sends them (Hono `secureHeaders()`, plus a CSRF check on `/api` writes); nginx sends HSTS on everything and must pass `Host` and `X-Forwarded-For` to the API: `docs/security-headers.md`.
|
||||
- Google sign-in for @nextgenerationofunity.org admin accounts: on when `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` are set on the service. Only existing accounts (made by admin-cli) get in; passwords still work. Setup: `docs/google-sign-in.md`.
|
||||
|
||||
## Git workflow
|
||||
|
|
|
|||
|
|
@ -72,6 +72,9 @@ which git ignores). The API serves `/uploads/` itself, and Vite proxies
|
|||
add_header X-Content-Type-Options nosniff;
|
||||
# An uploaded SVG opened on its own can't run script.
|
||||
add_header Content-Security-Policy "default-src 'none'; style-src 'unsafe-inline'; sandbox";
|
||||
# add_header here replaces the server-level HSTS; say it again
|
||||
# (docs/security-headers.md).
|
||||
add_header Strict-Transport-Security "max-age=31536000" always;
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
|||
68
docs/security-headers.md
Normal file
68
docs/security-headers.md
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
# 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) and no Content-Security-Policy yet
|
||||
(it needs trying in Report-Only first: pages carry an inline
|
||||
`window.__NGU_SITE__` script and YouTube, Facebook and Maps iframes).
|
||||
|
||||
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`.
|
||||
|
||||
## 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.
|
||||
|
|
@ -90,6 +90,8 @@ with it off it blocks everything.
|
|||
location @static {
|
||||
try_files /index.html =404;
|
||||
}
|
||||
# (A block that adds headers of its own, eg. Cache-Control, must
|
||||
# repeat the server-level HSTS line: docs/security-headers.md.)
|
||||
|
||||
# From the API, so Site settings' indexing switch applies; the
|
||||
# build's own robots.txt if the API is down.
|
||||
|
|
|
|||
|
|
@ -60,15 +60,16 @@ const app = new Hono();
|
|||
app.use("*", logger());
|
||||
|
||||
/* Headers for every response. Referrer kept to the origin (YouTube
|
||||
embeds refuse to play without one); HSTS without includeSubDomains
|
||||
so a plain-http dev subdomain keeps working. No CSP yet: pages carry
|
||||
an inline window.__NGU_SITE__ script and YouTube, Facebook and Maps
|
||||
iframes, so one needs trying in Report-Only first. */
|
||||
embeds refuse to play without one). No HSTS here: nginx sends it on
|
||||
everything, including the files it serves from disk
|
||||
(docs/security-headers.md). No CSP yet: pages carry an inline
|
||||
window.__NGU_SITE__ script and YouTube, Facebook and Maps iframes,
|
||||
so one needs trying in Report-Only first. */
|
||||
app.use(
|
||||
"*",
|
||||
secureHeaders({
|
||||
referrerPolicy: "strict-origin-when-cross-origin",
|
||||
strictTransportSecurity: "max-age=31536000",
|
||||
strictTransportSecurity: false,
|
||||
}),
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue