API Reference

csrf-csrf

Double-submit CSRF protection for Express with signed cookies, per-request tokens, and explicit validation for cookie-authenticated mutations.

Official Documentation ↗
CompatibilityThis documentation covers version csrf-csrf 3.x/4.x; token delivery and cookie names must match the client implementation. Check the official docs for the latest API surface.

Installation

npmnpm install csrf-csrf
pnpmpnpm add csrf-csrf
yarnyarn add csrf-csrf

Methods

Cross-site request forgery abuses a logged-in user’s cookies to submit an unwanted state-changing request. csrf-csrf implements a double-submit token: the server issues a token, the client sends it back in a header or body, and the server validates it against the session-bound secret.

Protect mutations, not reads

Apply validation to POST, PUT, PATCH, DELETE, and any GET that changes state. Safe reads do not need a token and adding one can break caching. Combine the token with SameSite cookies and an origin allowlist; each control covers different gaps.

Operate the secret

Use a strong secret from the environment, rotate it with a plan for existing sessions, and keep the cookie Secure and HttpOnly where the pattern allows. Issue a fresh token after login and on session changes. Log validation failures with a request id, but do not log the token.

Errors

The following points are quoted verbatim from the official documentation (https://github.com/Psifi-Solutions/csrf-csrf). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted from the official documentation (https://github.com/Psifi-Solutions/csrf-csrf); this is not a complete threat model.

  • Double CSRF A utility package to help implement stateless CSRF (Cross-Site Request Forgery) protection using the signed Double Submit Cookie Pattern in express. (https://github.com/Psifi-Solutions/csrf-csrf)
  • Getting Started • Configuration • Utilities • FAQ • Support Background This module provides the necessary pieces required to implement CSRF protection using the signed Double Submit Cookie Pattern . (https://github.com/Psifi-Solutions/csrf-csrf)
  • This is a stateless CSRF protection pattern, if you are using sessions it is highly recommended that you use csrf-sync for the Synchroniser Token Pattern instead. (https://github.com/Psifi-Solutions/csrf-csrf)

FAQ

Is SameSite enough to prevent CSRF?
SameSite helps but is not a complete defense across all browsers, subdomains, and deployment topologies. Use a CSRF token or origin check for cookie-authenticated mutations.
Which requests need CSRF protection?
State-changing requests authenticated by cookies: POST, PUT, PATCH, DELETE, and any GET that changes state. Pure JSON APIs using bearer tokens are not vulnerable to classic CSRF.
Can I put the CSRF token in a cookie and read it with JavaScript?
The double-submit pattern uses a readable token, but the security depends on the cookie's SameSite and HttpOnly settings and on binding the token to the session. Follow the library's recommended pattern rather than inventing one.