@wardenjs/ssr-csrf
v0.2.0
Published
CSRF protection for Fetch-API servers: exact Origin checks plus signed double-submit tokens, as a decorator.
Maintainers
Readme
@wardenjs/ssr-csrf
Part of warden's server track: security controls as decorators for any Fetch-API handler (Next.js route handlers, Hono, Bun, Deno, Workers). Here SSR means server side, not server-side rendering.
CSRF protection for cookie-authenticated writes.
npm i @wardenjs/ssr @wardenjs/ssr-csrfimport { csrf } from '@wardenjs/ssr-csrf';
const guard = csrf({ secret: process.env.CSRF_SECRET!, origin: 'https://app.example.com' });
// At sign-in: set the cookie, give the token to the client.
const { token, cookie } = await guard.issue();
// On writes:
export const POST = chain().use(guard.protect()).use(session.required()).handle(update);Three layers, because each alone has a gap:
- Your session cookie's SameSite.
Sec-Fetch-Site, then exactOrigin, thenReferer.- A signed double-submit token (
x-csrf-tokenheader must equal the__Host-csrfcookie, and carry a valid HMAC). The signature defeats cookie tossing from a sibling subdomain.
Safe methods pass. Not needed for Bearer-token APIs or webhooks, where the browser attaches nothing. Use a secret distinct from the session secret.
