Socket
Socket
Sign inDemoInstall

@types/csurf

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@types/csurf

TypeScript definitions for csurf


Version published
Weekly downloads
209K
decreased by-2.34%
Maintainers
1
Weekly downloads
 
Created

What is @types/csurf?

@types/csurf provides TypeScript type definitions for the csurf middleware, which is used to protect against Cross-Site Request Forgery (CSRF) attacks in Node.js applications.

What are @types/csurf's main functionalities?

Basic CSRF Protection

This code demonstrates how to set up basic CSRF protection in an Express application using the csurf middleware. It includes setting up the middleware, generating a CSRF token, and using it in a form.

const express = require('express');
const csrf = require('csurf');
const cookieParser = require('cookie-parser');

const app = express();
const csrfProtection = csrf({ cookie: true });

app.use(cookieParser());
app.use(csrfProtection);

app.get('/form', (req, res) => {
  res.send(`<!DOCTYPE html>
  <html>
  <body>
    <form action="/process" method="POST">
      <input type="hidden" name="_csrf" value="${req.csrfToken()}">
      <button type="submit">Submit</button>
    </form>
  </body>
  </html>`);
});

app.post('/process', (req, res) => {
  res.send('Form processed');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Custom Error Handling

This code demonstrates how to handle CSRF token errors by setting up custom error handling middleware. If a CSRF token error occurs, it sends a 403 status code and a custom error message.

const express = require('express');
const csrf = require('csurf');
const cookieParser = require('cookie-parser');

const app = express();
const csrfProtection = csrf({ cookie: true });

app.use(cookieParser());
app.use(csrfProtection);

app.use((err, req, res, next) => {
  if (err.code !== 'EBADCSRFTOKEN') return next(err);
  res.status(403);
  res.send('Form tampered with');
});

app.get('/form', (req, res) => {
  res.send(`<!DOCTYPE html>
  <html>
  <body>
    <form action="/process" method="POST">
      <input type="hidden" name="_csrf" value="${req.csrfToken()}">
      <button type="submit">Submit</button>
    </form>
  </body>
  </html>`);
});

app.post('/process', (req, res) => {
  res.send('Form processed');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Other packages similar to @types/csurf

FAQs

Package last updated on 07 Nov 2023

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc