--- name: audit-csrf version: 1.2.1 description: | Check CSRF protection on every form and mutation API, on top of the built-in same-origin check (`api.requireSameOrigin`). Use for "audit CSRF", "check CSRF protection", "are my forms safe", "review session security". allowed-tools: - Bash - Read - Grep - Glob --- # Pracht Audit CSRF What the framework guarantees: by default (`ApiConfig.requireSameOrigin`, `true` unless explicitly disabled), the runtime rejects state-changing API requests (`POST`/`PUT`/`PATCH`/`DELETE`) with a 403 unless the browser signals an exact same-origin fetch (`Sec-Fetch-Site: same-origin`) or the request's `Origin`/`Referer` matches the request URL's origin. `same-site` is not accepted (sibling subdomains can be attacker-controlled). Requests with no browser provenance headers at all (curl, server-to-server) are allowed — the threat model is browser-form CSRF, which cannot strip those headers. Page routes reject unsafe methods outright, so the API surface is where mutations live. The same check also covers **WebSocket upgrade requests** (any request carrying an `Upgrade` header), even though they are `GET`. Browsers do not apply CORS to WebSocket, so an upgrade is a cross-site-reachable, cookie-carrying request — cross-site WebSocket hijacking. `requireSameOrigin: false` therefore opens sockets as well as mutations. The audit therefore targets the OPT-OUTS and the remaining layers, in order of preference: 1. **Built-in same-origin enforcement** (`requireSameOrigin`, on by default). 2. **`SameSite=Lax` (or `Strict`) on session cookies** — defense in depth. 3. **Origin-check middleware** — only needed when `requireSameOrigin` is disabled or for non-`/api` endpoints. 4. **Per-request tokens** — only when `SameSite=None` is required. Prerequisites: `pracht inspect` requires a vite config that registers the pracht plugin. ## Step 0: Check the built-in guard Read `src/routes.ts` (the app manifest) for `defineApp({ api: { requireSameOrigin } })`. Absent means `true` (the default). An explicit `requireSameOrigin: false` is a top finding (`error`): the project has opted out of the built-in CSRF protection and MUST show compensating layers (origin-check middleware or token protocol) — demand them in the report. ## Step 1: Inventory mutation surfaces ### Forms Grep for `