Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
@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 192,052 weekly downloads. As such, @types/csurf popularity was classified as popular.
We found that @types/csurf demonstrated a not healthy version release cadence and project activity because the last version was released 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.