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
bff082652a
5 changed files with 80 additions and 5 deletions
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue