Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

elysia-csrf

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elysia-csrf

CSRF protection plugin for Elysia with cookie-based token storage

latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

build npm version license

Elysia CSRF

CSRF (Cross-Site Request Forgery) protection plugin for Elysia.

Installation

bun add elysia-csrf

Quick Start

import { Elysia } from "elysia";
import { csrf } from "elysia-csrf";

const app = new Elysia()
  .use(csrf({ cookie: true }))
  .get("/form", ({ csrfToken }) => {
    return `
      <form method="POST" action="/submit">
        <input type="hidden" name="_csrf" value="${csrfToken()}" />
        <input type="text" name="data" />
        <button type="submit">Submit</button>
      </form>
    `;
  })
  .post("/submit", ({ body }) => {
    return { success: true, data: body };
  })
  .listen(3000);

Configuration

csrf({
  cookie?: boolean | {
    key?: string;        // Cookie name (default: "_csrf")
    domain?: string;
    httpOnly?: boolean;  // Default: true
    maxAge?: number;
    path?: string;       // Default: "/"
    sameSite?: "lax" | "none" | "strict";  // Default: "lax"
    secure?: boolean;
    signed?: boolean;
  };
  ignoreMethods?: string[];  // Default: ["GET", "HEAD", "OPTIONS"]
  value?: (context: any) => string | undefined;  // Custom token extractor
  saltLength?: number;       // Default: 8
  secretLength?: number;     // Default: 18
  secret?: string;
})

Token Extraction

By default, tokens are extracted from (in order):

  • body._csrf
  • query._csrf
  • Headers: csrf-token, xsrf-token, x-csrf-token, x-xsrf-token

Customize with the value option.

Testing

Run tests to see examples of all features:

bun test

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Keywords

elysia

FAQs

Package last updated on 23 Oct 2025

Did you know?

Socket

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.

Install

Related posts