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

@lock-sdk/csrf

v1.0.0

Published

CSRF protection module for Lock security framework

Downloads

7

Readme

🛡️ CSRF Protection

A robust and flexible Cross-Site Request Forgery (CSRF) protection module for the Lock Security Framework. Supports token generation, header/cookie/session validation, double-submit patterns, and pluggable storage backends.

🚀 Features

  • 🔐 Supports token validation via cookie, header, session, or combo
  • ✌️ Double-submit cookie + header validation
  • 🧠 Smart auto-ignore for GET, OPTIONS, and form uploads
  • 🍪 Custom cookie options with secure, SameSite, and domain control
  • 🔄 Optional token refresh on every request
  • 📦 Pluggable token stores: memory or Redis
  • ⚙️ Fine-grained config for ignored methods, paths, content-types

🛠 Usage

Basic Middleware (Cookie + Header)

import { secure, csrfProtection } from '@lock-sdk/main';

const middleware = secure()(
  csrfProtection({
    enabled: true,
    tokenLocation: 'cookie-header',
    doubleSubmit: true,
  })
);

⚙️ Configuration

| Option | Type | Default | Description | | --------------------- | ------------------------------------------------------------ | -------------------------------- | -------------------------------------------------- | | enabled | boolean | true | Toggle protection | | tokenName | string | 'csrf-token' | Token name (used in cookie/header/session) | | tokenLength | number | 32 | Length of generated token | | headerName | string | 'x-csrf-token' | Header to check when using header or cookie-header | | cookieName | string | 'csrf-token' | Cookie name for token (if applicable) | | cookieOptions | CookieOptions | See below | Customization for Set-Cookie | | storage | 'memory' | 'redis' | 'memory' | Backend for token persistence | | tokenLocation | 'cookie' | 'header' | 'cookie-header' | 'session' | 'cookie-header' | Where to expect/return token | | ignoredMethods | string[] | ['GET','HEAD','OPTIONS'] | Skip token check for these methods | | ignoredPaths | (string \| RegExp)[] | [] | Skip token check for these routes | | ignoredContentTypes | string[] | ['multipart/form-data'] | Skip check for uploads | | failureStatusCode | number | 403 | Response status on failure | | failureMessage | string | 'CSRF token validation failed' | Response message | | refreshToken | boolean | true | Regenerate token on each request | | tokenTtl | number (seconds) | 86400 (24hr) | Expiry duration for stored tokens | | doubleSubmit | boolean | true | Enforce cookie + header match | | samesite | boolean | true | Apply SameSite cookie flags | | redisOptions | RedisOptions | – | Redis connection settings | | customStorage | TokenStorageProvider | – | Provide your own storage logic | | includeFormToken | boolean | – | (Coming soon) Inject token into forms | | angularCompatible | boolean | – | (Coming soon) Support Angular's $http token style |

🍪 Cookie Options

cookieOptions: {
  httpOnly: false,
  secure: true,
  sameSite: 'lax',
  path: '/',
  domain: 'example.com',
  maxAge: 3600,
}

📡 Token Sources

| Location | Description | | --------------- | ------------------------------------------------------- | | cookie | Token is read from a cookie | | header | Token is sent via header (x-csrf-token by default) | | cookie-header | Token must be in both header and cookie (double-submit) | | session | Token is stored in req.session[tokenName] |

📛 Event Types

| Event Code | Description | | ---------------------------- | ---------------------------------- | | csrf.token.missing | Token was not found in the request | | csrf.token.invalid | Token did not validate | | csrf.double.submit.failure | Cookie and header token mismatch | | csrf.validated | Request passed CSRF validation | | csrf.error | Unhandled exception in CSRF logic |

🧩 Storage Backends

Memory (default)

storage: 'memory',
tokenTtl: 86400

Redis

storage: 'redis',
redisOptions: {
  url: 'redis://localhost:6379',
  password: 'secret',
  keyPrefix: 'csrf:',
}

🛡 Maintained By

Lock Team