Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@types/csurf
Advanced tools
TypeScript definitions for 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.
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');
});
Helmet is a middleware for Express applications that helps secure them by setting various HTTP headers. While it does not specifically handle CSRF protection, it provides a broader range of security features compared to csurf.
Express-rate-limit is a middleware for rate limiting in Express applications. It helps protect against brute-force attacks by limiting the number of requests a client can make. While it does not provide CSRF protection, it complements csurf by adding another layer of security.
Express-validator is a middleware for validating and sanitizing user input in Express applications. It helps prevent various types of attacks, including injection attacks. While it does not provide CSRF protection, it works well alongside csurf to enhance overall security.
npm install --save @types/csurf
This package contains type definitions for csurf (https://www.npmjs.org/package/csurf).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/csurf.
import express = require("express-serve-static-core");
declare global {
namespace Express {
interface Request {
csrfToken(): string;
}
}
}
declare function csurf(options?: {
value?: ((req: express.Request) => string) | undefined;
/**
* @default false
*/
cookie?: csurf.CookieOptions | boolean | undefined;
ignoreMethods?: string[] | undefined;
sessionKey?: string | undefined;
}): express.RequestHandler;
declare namespace csurf {
interface CookieOptions extends express.CookieOptions {
/**
* @default '_csrf'
*/
key?: string | undefined;
}
}
export = csurf;
These definitions were written by Hiroki Horiuchi, and Piotr Błażejewicz.
FAQs
TypeScript definitions for csurf
The npm package @types/csurf receives a total of 175,824 weekly downloads. As such, @types/csurf popularity was classified as popular.
We found that @types/csurf demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.