# csrf-protection

> Advanced CSRF Library

Latest version **0.2.0** (published 2024-02-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install csrf-protection
pnpm add csrf-protection
yarn add csrf-protection
bun add csrf-protection
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2024-02-05 |
| First published | 2024-01-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Umut Dag |
| Maintainers | umutdag1 |
| Keywords | csrf, middleware, security, protection |

## Links

- npm: https://www.npmjs.com/package/csrf-protection
- npm.io page: https://npm.io/package/csrf-protection

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2024-02-05
- 0.1.1 — 2024-01-28
- 0.1.0 — 2024-01-25
- 0.0.5 — 2024-01-24
- 0.0.4 — 2024-01-20
- 0.0.3 — 2024-01-20
- 0.0.2 — 2024-01-20
- 0.0.1 — 2024-01-20

## README

# Node CSRF Middleware

`csrf-protection` is a Node.js library that provides middleware functions for generating and validating CSRF tokens in web applications.

## Installation

To install the library, use the following npm command:

npm install csrf-protection

## Usage

### Importing the Library

const csrf = require('csrf-protection');

### CSRF Token Generation Middleware

This middleware generates a CSRF token and adds it to the response cookie. The token is also available in the res.locals.csrfToken variable for use in forms.

### CSRF Token Control Middleware

This middleware checks the CSRF token in the request against the one stored in the cookie. If the tokens match, the request is allowed to proceed; otherwise, a 403 Forbidden response is sent.

## Configuration

## Example
```javascript
const express = require('express');
const csrf = require('csrf-protection');
const cookieParser = require('cookie-parser');
const app = express();

app.use(express.urlencoded({ extended: true })); // Required
app.use(cookieParser()); // Required

const csrff = csrf({
	secret: 'Hello World!' // Your Secret Key
});

app.get('/', csrff.csrfCreate, (req, res) => {
  const csrfToken = res.locals.csrfToken;
  
  console.log(csrfToken);
  
  res.send(`
    <html>
      <body>
        <h1>Example</h1>
        <form action="/submit" method="post">
          <input type="hidden" name="_csrf" value="${csrfToken}">
          <label for="username">User Name:</label>
          <input type="text" id="username" name="username">
          <button type="submit">Send</button>
        </form>
      </body>
    </html>
  `);
});

app.post('/submit', csrff.csrfCheck, (req, res) => {
  res.send(`Hello World!`);
});

app.listen(3000, () => { console.log('Server is running on port 3000'); });
```

## License

This project is licensed under the [MIT License](LICENSE).

---
_Source: https://npm.io/package/csrf-protection · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
