CSRF Protection
This page explains ProxyAuth's CSRF (Cross-Site Request Forgery) protection in detail — token format, validation rules, and how it interacts with {{ csrf_token }} template injection (already covered briefly in Error Pages: How Templating & Injection Work).
CSRF protection only applies when both session_cookie and CSRF itself are enabled — the global session_cookie/csrf_token in config.json (see the config.json Reference), or their per-vhost overrides session_cookie/tag_csrf_token in routes.yml (see routes.yml Reference § Per-vhost behavior overrides) if that vhost sets its own. If either resolves to off, none of this runs.
Token format
A CSRF token is a compact, self-contained, signed string — not a server-side session lookup:
<nonce_b64>.<expiry_b64>.<signature_b64>
- nonce — 32 random bytes, unique per token.
- expiry — a Unix timestamp, 10 minutes from generation, big-endian encoded.
- signature —
blake3::keyed_hash(key, nonce || expiry), wherekey = blake3::hash(config.secret).
All three parts are base64 (URL-safe, no padding) encoded and joined with .. Because the signature is derived from config.secret, only ProxyAuth (or anyone who has your secret) can produce a token that verifies successfully — the token isn't looked up anywhere at generation time, it's just computed.
Validation: what has to be true
verify_csrf_token() checks, in order:
- Structurally valid — decodes to exactly a 32-byte nonce, 8-byte expiry, and 32-byte signature.
- Signature matches — recomputed with the current
config.secret, compared with a constant-time comparison. - Not expired — the embedded expiry must still be in the future.
- Not already used — see single-use enforcement below.
All four must pass. Any failure (including a token that was already consumed) means the request is rejected.
Single-use enforcement
Verifying a token's signature isn't enough on its own — CSRF tokens are also single-use. A shared, in-memory nonce store (CSRF_STORE, a DashMap<nonce, expiry>) records every nonce as it's consumed:
- The first time a valid, unexpired token is checked, its nonce is inserted into the store and validation succeeds.
- Any subsequent check of the same nonce fails immediately, even if the signature and expiry are still technically valid — replaying a captured CSRF token doesn't work.
- The store is purged of expired entries once per hour automatically, so it doesn't grow unbounded.
Practical implication: each CSRF token is good for exactly one request. If your frontend needs to submit multiple forms in sequence, fetch/inject a fresh token for each one (e.g. via the {{ csrf_token }} placeholder re-rendered on each page, or a dedicated endpoint if your app needs one) — reusing the same token for a second submission will fail.
Like the OTP hot-reload overlay, this nonce store is per-process, in-memory, and not shared across instances. In a multi-instance deployment behind a load balancer, a token issued by instance A must be validated by instance A specifically (or a load balancer with sticky sessions), unless you're also using the SSO key export/import mechanism to keep build constants in sync — CSRF's own nonce store isn't part of that sync, so strictly speaking a nonce consumed on A is still technically valid (unconsumed) on B. In practice this is rarely an issue since the token is short-lived (10 minutes) and single-use makes double-submission attempts fail closed rather than open, but keep it in mind if you see intermittent CSRF failures behind a non-sticky load balancer.
Which requests are checked
validate_csrf_token() is skipped entirely (request always allowed through, from a CSRF standpoint) for:
- Static assets — any path under
/assets/, or ending in.css,.js,.png,.jpg,.jpeg,.svg,.ico,.webp. - Safe HTTP methods —
GET,HEAD,OPTIONS.
For every other request, ProxyAuth looks for the token in, checked in this order:
- Header —
X-CSRF-TokenorX-CSRFToken. - Form body — if
Content-Typeisapplication/x-www-form-urlencoded, thecsrf_tokenfield. - JSON body — if
Content-Typeisapplication/json, the top-level"csrf_token"key.
multipart/form-data bodies are always rejected — there's no multipart parsing for the token, so file-upload forms can't currently carry a CSRF token in the body (send it as a header instead: X-CSRF-Token).
This check applies per-request across the whole proxy, but can be disabled per-route via routes.yml's need_csrf: false — see the routes.yml Reference.
Getting a token onto your page
Two ways:
- Automatic injection — if your login/error page HTML contains
{{ csrf_token }}or{{csrf_token}}, ProxyAuth replaces it with a fresh token whenever it renders that page (see Error Pages: How Templating & Injection Work for the full mechanism, including gzip/deflate/br-compressed pages, and Dynamic Tags in Pages for the same tag on static files and proxied responses). - Anywhere else in your app — since token generation doesn't require a server round-trip beyond having
config.secret(which only ProxyAuth has), there's currently no separate "get me a CSRF token" API route; token issuance is tied to the error-page rendering path above. If your frontend needs a token outside that flow, render it into the page server-side using the same placeholder convention.
Example form:
<form method="POST" action="/auth">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<input type="text" name="username">
<input type="password" name="password">
<button type="submit">Sign in</button>
</form>
Or, for a JSON/fetch-based client, send it as a header instead of a body field:
fetch("/auth", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRF-Token": token,
},
body: JSON.stringify({ username, password }),
});
Summary
| Property | Value |
|---|---|
| Token lifetime | 10 minutes |
| Reusable? | No — single-use, enforced server-side |
| Signature algorithm | blake3::keyed_hash, keyed by blake3::hash(config.secret) |
| Comparison | Constant-time |
| Accepted locations | X-CSRF-Token/X-CSRFToken header, form field, JSON field |
| Exempt | GET/HEAD/OPTIONS, static assets, routes with need_csrf: false |
| Not supported | multipart/form-data bodies |
See also
- config.json Reference —
session_cookie,csrf_token. - routes.yml Reference — per-route
need_csrfoverride, and the per-vhostsession_cookie/tag_csrf_tokenoverrides. - Error Pages: How Templating & Injection Work — where
{{ csrf_token }}gets filled in on error pages. - Dynamic Tags in Pages — where
{{ csrf_token }}gets filled in on static files and proxied responses.
📖 Wiki Home · 💻 Source Code · 🐛 Report an Issue · 📜 License
Found an error or something outdated on this page? Open an issue or submit a PR against the wiki repository.