| 1 | # 0008 — A per-session CSRF token on every form, as the third layer |
| 2 | |
| 3 | **Status:** accepted · **Date:** 2026-09-08 |
| 4 | |
| 5 | > **Note, 2026-10-07.** Login CSRF, left open below, is closed: `/auth/login` refuses a |
| 6 | > POST carrying neither `Origin` nor `Sec-Fetch-Site`, and is no longer exempt. The |
| 7 | > pre-session cookie this record names was not chosen. How, and why, is in the note at |
| 8 | > the end; the original text is kept as written. |
| 9 | |
| 10 | ## Context |
| 11 | |
| 12 | Steid has thirty-eight POST routes, thirty-one of which are forms a signed-in owner |
| 13 | submits with an ambient session cookie. Cross-site request forgery is the classic hole |
| 14 | under that shape, and `current.md` had been carrying it as decidable-not-decided since |
| 15 | the forms first existed. |
| 16 | |
| 17 | Two defences were already in place, and finding the second is what made this a question |
| 18 | about *depth* rather than about coverage: |
| 19 | |
| 20 | - The session cookie is `SameSite=Lax`, which stops a cross-site POST in every current |
| 21 | browser. |
| 22 | - Topcoat's `sessions()` silently installs an `OriginLayer` — read in |
| 23 | `topcoat-session-0.5.0/src/origin.rs`, not assumed — which refuses any non-safe method |
| 24 | whose `Sec-Fetch-Site` is not `same-origin`/`none`, falling back to comparing |
| 25 | `Origin`'s host against the request's own. |
| 26 | |
| 27 | Both have the same gap, and it is deliberate in the layer: **a request carrying neither |
| 28 | header passes**, because that is what a non-browser client looks like and a non-browser |
| 29 | client has no ambient cookies to forge with. A browser old enough to send neither is in |
| 30 | that gap, and so is a proxy that strips `Origin`. And `SameSite=Lax` is one line in |
| 31 | `session_cookie.rs` that somebody could loosen for an unrelated reason. |
| 32 | |
| 33 | There is also no configured session secret to derive anything from: `main` builds |
| 34 | `SessionConfig::default()`. |
| 35 | |
| 36 | ## Decision |
| 37 | |
| 38 | **Every form carries a hidden `csrf_token` of `HMAC-SHA-256(per-boot secret, session |
| 39 | token hash)`, and every guarded POST handler verifies it in constant time before its use |
| 40 | case. A missing or wrong token is a 403 with one sentence. The secret is random per |
| 41 | boot.** |
| 42 | |
| 43 | Two exemption classes, enumerated with reasons in `csrf::EXEMPT` and asserted by a test: |
| 44 | routes with no cookie and no form (the git transport, `/api/setup`), and routes |
| 45 | submitted signed out (`/auth/setup`, `/auth/login`, `/auth/forgot`, |
| 46 | `/auth/reset/{token}`), which have no session to bind a token to. |
| 47 | |
| 48 | The obligation this creates is enforced by a test that reads |
| 49 | `src/infrastructure/web/*.rs` and fails when a POST route neither calls the check nor is |
| 50 | named in `EXEMPT`. |
| 51 | |
| 52 | ## Alternatives considered |
| 53 | |
| 54 | - **Nothing — rely on `SameSite=Lax` and the origin layer.** Cheapest, and correct for |
| 55 | every browser anyone actually uses. Rejected because both defences are one |
| 56 | configuration line from being off, both share a gap they cannot close by design, and |
| 57 | the cost of closing it is one hidden input. |
| 58 | - **A random token stored in the session row.** The conventional shape, and it needs a |
| 59 | column, a write on first render, and a decision about what happens when two tabs race. |
| 60 | The HMAC gets the same binding out of state that already exists on both sides. |
| 61 | - **A token derived from the session secret rather than random per boot.** Would survive |
| 62 | a restart, which is the one real cost of this choice. There is no session secret to |
| 63 | derive from, and inventing a persisted one is a bigger decision than this — it becomes |
| 64 | a thing to back up, rotate and configure. |
| 65 | - **A second CSRF cookie, so signed-out forms could be protected too.** The honest fix |
| 66 | for login CSRF, and rejected as scope: it is a cookie with its own lifetime and |
| 67 | `SameSite` question, and it protects `/auth/login` alone. |
| 68 | |
| 69 | ## Consequences |
| 70 | |
| 71 | - **A restart invalidates every form already open on someone's screen.** Submitting one |
| 72 | answers 403 and the person reloads and retypes. Acceptable for a form and not for a |
| 73 | session, which is why the session cookie is not treated the same way. It bites hardest |
| 74 | in development, where `topcoat dev` restarts on every edit. |
| 75 | - **Every new form is now three things, not two**: the field, the struct field, and the |
| 76 | check. The scan test is what makes forgetting the third a failing build rather than a |
| 77 | silent hole, and it is the only reason this is safe to spread across thirty-one |
| 78 | handlers. |
| 79 | - **Login CSRF is still open**, and knowingly. An attacker can force a victim's browser |
| 80 | to sign in as the attacker; `SameSite=Lax` and the origin layer still cover it in any |
| 81 | current browser, and closing it properly needs a pre-session cookie. |
| 82 | - **Reversing is cheap**: delete the module, the field, and the calls. Nothing is stored |
| 83 | and no URL or schema depends on it. |
| 84 | - **No new dependency.** `hmac`, `sha2`, `subtle` and `rand` were all already here. |
| 85 | |
| 86 | ## Note, 2026-10-07: login CSRF is closed by the request's provenance, not by a cookie |
| 87 | |
| 88 | Launch 2.18, decision 11. `POST /auth/login` is no longer in `EXEMPT`. Its handler calls |
| 89 | `csrf_verified_signed_out` after the rate limit, which refuses with the same 403 |
| 90 | sentence a POST carrying **neither** `Origin` nor `Sec-Fetch-Site`. That is exactly the |
| 91 | gap the context above names in the origin layer, which already refuses `same-site`, |
| 92 | `cross-site` and a foreign `Origin`. Together they refuse every forged sign-in a browser |
| 93 | can be made to send. The function also runs the layer's `verify_origin` itself, so it |
| 94 | does not lean on the layer staying installed. |
| 95 | |
| 96 | **A pre-session cookie, the alternative this record named, was not chosen:** |
| 97 | - **It is a second cookie.** It needs its own lifetime and its own `SameSite` answer, |
| 98 | and it protects one form. |
| 99 | - **It inherits the per-boot secret's cost.** Every restart would 403 the sign-in form |
| 100 | open on someone's screen. Nobody is signed in yet to mind losing a form, but it is the |
| 101 | form they need in order to get anywhere. |
| 102 | |
| 103 | **What it costs:** a client that sends neither header cannot sign in. Every browser that |
| 104 | can use the form sends at least one on a POST. A script signing in with a bare HTTP |
| 105 | client must add `Sec-Fetch-Site: same-origin` or its own `Origin`. Nothing in either |
| 106 | repository signs in that way. |
| 107 | |
| 108 | `/auth/setup`, `/auth/forgot` and `/auth/reset/{token}` stay exempt, each for the reason |
| 109 | in `EXEMPT`. The setup token and the reset URL's token are the unguessable parts there, |
| 110 | and a forgot request starts no session. |