@fastify/csrf

中文文档
8.1.1发布于 2026/10/09MITTypeScriptCommonJS零依赖构建来源已验证近 30 天下载 75.8万

primary logic behind csrf tokens

原始使用文档
文档目录5 节
  1. Install
  2. TypeScript
  3. API
  4. new Tokens([options])
  5. License

CSRF

CI NPM version neostandard javascript style

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?

This module is a fork of https://github.com/pillarjs/csrf at f0d66c91ea4be6d30a03bd311ed9518951d9c3e4.

Install

$ npm i @fastify/csrf

TypeScript

This module includes a TypeScript declaration file to enable auto-complete in compatible editors and type information for TypeScript projects.

API

const Tokens = require('@fastify/csrf')

new Tokens([options])

Create a new token generation/verification instance. The options argument is optional and will just use all defaults if missing.

Options

Tokens accept these properties in the options object.

algorithm

The hash-algorithm to generate the token. Defaults to sha256.

saltLength

The length of the internal salt to use, in characters. Internally, the salt is a base 62 string. Defaults to 8 characters.

secretLength

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.

userInfo

Require user-specific information in tokens.create() and tokens.verify().

hmacKey

When set, the hmacKey is used to generate the cryptographic HMAC hash instead of the default hash function.

validity

The maximum validity of the token to generate, in milliseconds. Note that the epoch is passed around base-36 encoded. Defaults to 0 milliseconds (disabled).

tokens.create(secret[, userInfo])

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.

const secret = tokens.secretSync()
const token = tokens.create(secret)

The userInfo parameter can be used to protect against cookie tossing attacks (and similar) when the application is deployed with untrusted subdomains. It will encode some user-specific information within the token. It is used only if userInfo: true is passed to the constructor.

tokens.secret(callback)

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
})

tokens.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
})

tokens.secretSync()

A synchronous version of tokens.secret(callback). Please see tokens.secret(callback) documentation for full details.

const secret = tokens.secretSync()

tokens.verify(secret, token[, userInfo])

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 userInfo parameter is required if userInfo: true was configured during initialization. The user-specific information must match what was passed in tokens.create().

License

Licensed under MIT.

版本与发布记录

@fastify/csrf 共发布 16 个版本,首次发布于 2021/05/13;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.1.1latest2026/10/091 个月前
8.1.02026/08/083 个月前
8.0.12025/05/111.5 年前
8.0.02025/01/111.8 年前
7.1.12025/01/021.9 年前
7.1.02024/12/141.9 年前

其他发布通道:next → 7.0.0。

关于 @fastify/csrf 的常见问题

@fastify/csrf 是什么?

primary logic behind csrf tokens

如何安装 @fastify/csrf?

在项目目录运行 npm install @fastify/csrf,或使用 pnpm add / yarn add / bun add。

@fastify/csrf 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fastify/csrf 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fastify/csrf 可以商用吗?

@fastify/csrf 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fastify/csrf 的下载量有多少?

根据 npm 官方下载统计,@fastify/csrf 在 2026/10/15 – 2026/11/13 这 30 天内被下载 758,251 次,日均约 25,275 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fastify/csrf 还在维护吗?

最新版本 8.1.1 发布于 2026/10/09(1 个月前);至今共发布 16 个版本,其中过去 12 个月发布了 2 个;首次发布于 2021/05/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。