npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nestjs-kitchen/csrf

v2.0.2

Published

A CSRF module in NextJS.

Downloads

11

Readme

@nestjs-kitchen/csrf

NPM Version NPM License codecov

A CSRF module in NextJS.


Feature

  • ✅ Supports double-csrf validation strategy (cookie-based, stateless)

  • ✅ Supports traditional session-based validation strategy (stateful)

  • ✅ Supports one-time-use CSRF tokens

  • ✅ Compatible with both @nestjs/platform-express and @nestjs/platform-fastify

Install

$ npm install --save @nestjs-kitchen/csrf

Usage

double-csrf Strategy (Cookie-Based)

This strategy stores the CSRF secret in the cookie and requires cookie support.

For @nestjs/platform-express:

npm install cookie-parser

For @nestjs/platform-fastify:

npm install @fastify/cookie

Example

Register module:

// in app.module.ts

@Module({
  imports: [
    // ...
    CsrfModule.register({
      type: 'double-csrf'
    })
    // ...
  ],
  controllers: [AppController],
  providers: [],
})
export class AppModule {}

Apply CSRF in a Controller:

// in app.controller.ts

import { Controller, Get, Post } from '@nestjs/common';
import { Csrf, CsrfInterceptor, CsrfGuard } from '@nestjs-kitchen/csrf';

@UseGuards(CsrfGuard)
@UseInterceptors(CsrfInterceptor)
@Controller()
export class AppController {

  @Csrf()
  @Post('/require-csrf')
  post(){
    // This handler will only execute if CSRF validation passes
    // ...
  }

  @Csrf.Sign()
  @Get('/csrf')
  csrf() {
    // Generates and stores a new CSRF token (skips CSRF validation)
    return true;
  }
}

session Strategy (Session-Based)

This strategy stores the CSRF secret in the session and requires session support.

For @nestjs/platform-express:

npm install express-session

For @nestjs/platform-fastify:

npm install @fastify/session

Alternatively, for Fastify, you can also use:

npm install @fastify/secure-session

⚠️ Note: When using @fastify/secure-session, one-time CSRF tokens are not supported because secure-session is stateless.

Example

Register module:

// in app.module.ts

import { Module } from "@nestjs/common";
import { CsrfModule } from '@nestjs-kitchen/csrf';
import { AppController } from './app.controller.ts';

@Module({
  imports: [
    // ...
    CsrfModule.register({
      type: 'session'
    })
    // ...
  ],
  controllers: [AppController],
  providers: [],
})
export class AppModule {}

Apply CSRF in a Controller:

Same as double-csrf example.

Options

Common options:

| Option | Type | Description | Default | | --------------- | ---------------------- | --------------------------------------------------------------------------- | -------------------------------------------------------- | | getToken | (req: any) => string | Function to extract CSRF token from the request. | (req) => req.headers['x-csrf-token'] | | headerKey | string | Response header name to expose the CSRF token. | 'x-csrf-token' | | verifyMethods | HttpMethod[] | List of HTTP methods that require CSRF validation. | ['PATCH', 'PUT', 'POST', 'DELETE', 'CONNECT', 'TRACE'] | | global | boolean | If set to true, automatically applies CsrfGuard and CsrfInterceptor globally. | false |

double-csrf options:

| Option | Type | Description | Default | | ------------------------ | --------------- | -------------------------------------------------------------------- | --------------- | | type | 'double-csrf' | Strategy type: stores token in cookie only. Requires cookie support. | 'double-csrf' | | cookieKey | string | Cookie name to store CSRF secret. | '_csrf' | | cookieOptions.path | string | Cookie path. | '/' | | cookieOptions.secure | boolean | Use secure cookie (only sent over HTTPS). | false | | cookieOptions.sameSite | string | SameSite policy. | 'strict' | | cookieOptions.httpOnly | boolean | Whether the cookie is HttpOnly. | true | | cookieOptions.signed | boolean | Whether the cookie is signed. | false | | cookieOptions.maxAge | number | Cookie expiration. | undefined | | cookieOptions[...] | any | Additional cookie options. | — |

session options:

| Option | Type | Description | Default | | ----------------- | --------------------------------------------------------------------- | ----------------------------------------------------------------- | ----------- | | type | 'session' | Strategy type: stores token in session. Requires session support. | 'session' | | sessionKey | string | Session key to store the CSRF secret. | '_csrf' | | oneTimeToken | boolean | Enable one-time-use tokens (valid for a single use only). | false | | oneTimeTokenTTL | number | TTL for one-time tokens in milliseconds. | undefined | | nonceStore.get | (key: string) => any \| Promise<any> | Retrieve one-time token from store. | In-memory | | nonceStore.set | (key: string, value: string, ttl?: number) => void \| Promise<void> | Store one-time token. | In-memory | | nonceStore.del | (key: string) => void \| Promise<void> | Delete one-time token. | In-memory |

License

MIT License