From bff082652aa110bbb4b6cbcff8b161da64eae200 Mon Sep 17 00:00:00 2001 From: Zaldimmar Date: Mon, 28 Sep 2026 22:20:53 -0500 Subject: [PATCH] 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 --- CLAUDE.md | 1 + docs/media-uploads.md | 3 ++ docs/security-headers.md | 68 ++++++++++++++++++++++++++++++++++++++++ docs/seo.md | 2 ++ server/src/index.js | 11 ++++--- 5 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 docs/security-headers.md diff --git a/CLAUDE.md b/CLAUDE.md index 753ad45..82d4c0f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -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 diff --git a/docs/media-uploads.md b/docs/media-uploads.md index 2352a3e..86cc907 100644 --- a/docs/media-uploads.md +++ b/docs/media-uploads.md @@ -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; } ``` diff --git a/docs/security-headers.md b/docs/security-headers.md new file mode 100644 index 0000000..b84647d --- /dev/null +++ b/docs/security-headers.md @@ -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/ | grep -i strict +``` + +Each should print the header once. diff --git a/docs/seo.md b/docs/seo.md index feaa726..cb2652f 100644 --- a/docs/seo.md +++ b/docs/seo.md @@ -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. diff --git a/server/src/index.js b/server/src/index.js index ed72ec4..e0ba46e 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -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, }), );