csrf-csrf
Double-submit CSRF protection for Express with signed cookies, per-request tokens, and explicit validation for cookie-authenticated mutations.
Official Documentation ↗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
npm install csrf-csrfpnpm add csrf-csrfyarn add csrf-csrfMethods
No methods match this filter.
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.
- For additional information on figuring out the error see the “Dealing with ‘ForbiddenError: invalid csrf token’” section of the FAQ. (https://github.com/Psifi-Solutions/csrf-csrf)
- If overwrite is true a new token will always be generated, even if the current one is invalid. (https://github.com/Psifi-Solutions/csrf-csrf)
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.