Commit 9fab60a
authored
feat(server): replace simple CSRF protection with GET method CSRF protection (#1846)
Adds `GetMethodCsrfProtectionHandlerPlugin`, a zero-config safeguard
that makes enabling `GET` on handlers safe with `SameSite=Lax` cookie
authentication, and removes `SimpleCsrfProtectionHandlerPlugin`
entirely. The new plugin rejects `GET` requests arriving as cross-site
or browser-initiated top-level navigations, the only context where
another site can make a browser attach explicitly marked `SameSite=Lax`
cookies to a safe-method request. Cookie-less cross-site requests keep
working, so public APIs need no origin allowlist.
## Behavior
- `GET` requests get a `403` before routing when `Sec-Fetch-Site`
reports `cross-site` or `none` on a top-level navigation
(`Sec-Fetch-Mode: navigate` targeting `Sec-Fetch-Dest: document`); a
request stripped of either header is rejected rather than passed.
- Only `GET` is guarded: navigations can use no method besides `GET` and
the unsafe `POST` per the HTML spec, so `HEAD`, `QUERY`, and every other
method pass untouched.
- Same-origin and same-site requests always pass: the site is the
`SameSite` trust boundary, so sibling-subdomain fetches and dev setups
need no configuration.
- Cross-site `fetch`, `<img>`, `<iframe>`, and other non-top-level
contexts pass, since browsers never attach `Lax` cookies to them.
- Non-browser clients and plain-HTTP deployments pass unchanged (no
Fetch Metadata), and batch requests are judged before splitting, so
forged sub-request headers cannot overturn the verdict.
## Breaking changes
- `SimpleCsrfProtectionHandlerPlugin` no longer exists; imports fail at
compile time. The replacement for cookie-based apps is
`GetMethodCsrfProtectionHandlerPlugin` with cookies explicitly marked
`SameSite=Lax` or `Strict`; apps that relied on rejecting all cross-site
traffic need their own safeguard, such as a synchronizer token.
- The v1 migration guide now maps the old CSRF plugin pair to the new
plugin.
## Docs
- New page documents which request contexts send `SameSite=Lax` cookies
(links, address bar, email links, `<img>`, `<iframe>`, `fetch`, forms)
and the resulting verdicts, plus cookie requirements: only Chrome
defaults unmarked cookies to `Lax`, so the attribute must be set
explicitly.
- The RPC handler page's "Enabling the GET Method" section now
recommends the new plugin.
## Testing
- Tests cover every navigation vector, cookie-less context, ignored
methods, stripped and repeated headers, and batch forgery, with 100%
statement and branch coverage on the plugin.
- Full suite green: root vitest, `pnpm type:check`, and `pnpm
docs:validate` (JSDoc backlinks + strict link check) all pass.1 parent 18576d4 commit 9fab60a
10 files changed
Lines changed: 393 additions & 619 deletions
File tree
- apps/content/docs
- migrations
- plugins
- rpc
- packages/server/src/plugins
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
517 | 517 | | |
518 | 518 | | |
519 | 519 | | |
520 | | - | |
| 520 | + | |
521 | 521 | | |
522 | 522 | | |
523 | 523 | | |
524 | 524 | | |
525 | | - | |
| 525 | + | |
526 | 526 | | |
527 | 527 | | |
528 | 528 | | |
| |||
538 | 538 | | |
539 | 539 | | |
540 | 540 | | |
541 | | - | |
| 541 | + | |
542 | 542 | | |
543 | 543 | | |
544 | 544 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
| 84 | + | |
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
| 89 | + | |
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
| 98 | + | |
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
104 | | - | |
| 104 | + | |
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
108 | 108 | | |
109 | | - | |
| 109 | + | |
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| |||
0 commit comments