
Security News
CVE Volume Surges Past 48,000 in 2025 as WordPress Plugin Ecosystem Drives Growth
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.
Logic behind CSRF token creation and verification.
Read Understanding-CSRF for more information on CSRF. Use this module to create custom CSRF middleware.
Looking for a CSRF framework for your favorite framework that uses this module?
$ npm install csrf
This module includes a TypeScript declaration file to enable auto complete in compatible editors and type information for TypeScript projects.
var Tokens = require('csrf')
Create a new token generation/verification instance. The options argument is
optional and will just use all defaults if missing.
Tokens accepts these properties in the options object.
The length of the internal salt to use, in characters. Internally, the salt
is a base 62 string. Defaults to 8 characters.
The length of the secret to generate, in bytes. Note that the secret is
passed around base-64 encoded and that this length refers to the underlying
bytes, not the length of the base-64 string. Defaults to 18 bytes.
Create a new CSRF token attached to the given secret. The secret is a
string, typically generated from the tokens.secret() or tokens.secretSync()
methods. This token is what you should add into HTML <form> blocks and
expect the user's browser to provide back.
var secret = tokens.secretSync()
var token = tokens.create(secret)
Asynchronously create a new secret, which is a string. The secret is to
be kept on the server, typically stored in a server-side session for the
user. The secret should be at least per user.
tokens.secret(function (err, secret) {
if (err) throw err
// do something with the secret
})
Asynchronously create a new secret and return a Promise. Please see
tokens.secret(callback) documentation for full details.
Note: To use promises in Node.js prior to 0.12, promises must be
"polyfilled" using global.Promise = require('bluebird').
tokens.secret().then(function (secret) {
// do something with the secret
})
A synchronous version of tokens.secret(callback). Please see
tokens.secret(callback) documentation for full details.
var secret = tokens.secretSync()
Check whether a CSRF token is valid for the given secret, returning
a Boolean.
if (!tokens.verify(secret, token)) {
throw new Error('invalid token!')
}
The csurf package is another middleware for CSRF token creation and validation, specifically designed to work with Express.js. It provides similar functionality to csrf but is more tightly integrated with Express, making it easier to use in Express applications.
The csrf-csrf package is a lightweight alternative for CSRF protection. It offers similar functionalities to csrf but is designed to be simpler and more straightforward, making it a good choice for smaller projects or those that do not require the full feature set of csrf.
FAQs
primary logic behind csrf tokens
The npm package csrf receives a total of 331,451 weekly downloads. As such, csrf popularity was classified as popular.
We found that csrf demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.