# csrf

Rule `sonarjs/csrf`. CSRF protections should not be disabled

- Source: sonarjs
- Technologies: JavaScript
- Kind: correctness
- Needs type information: no
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1200 by reach, #501 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/csrf` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-4502/) |

## Code examples

### Correct: the rule accepts this

```js
let csrf = require('csurf');
let express = require('express');

let csrfProtection = csrf({ cookie:  true });

let app = express();

app.post('/money_transfer', parseForm, csrfProtection, function (req, res) { // Compliant
  res.send('Money transferred')
});
```

```js
let csrf = require('csurf');
let express = require('express');

app.use(csrf({ cookie: true, ignoreMethods: ["GET"] })); // Compliant
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/csrf": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/cors](https://lintradar.dev/rules/sonarjs/cors.md): Cross-Origin Resource Sharing (CORS) policy should be restricted to trusted origins
- [sonarjs/deprecation](https://lintradar.dev/rules/sonarjs/deprecation.md): Deprecated APIs should not be used
- [sonarjs/cookie-no-httponly](https://lintradar.dev/rules/sonarjs/cookie-no-httponly.md): Cookies should have the "HttpOnly" flag
- [sonarjs/different-types-comparison](https://lintradar.dev/rules/sonarjs/different-types-comparison.md): Strict equality operators should not be used with dissimilar types
- [sonarjs/content-security-policy](https://lintradar.dev/rules/sonarjs/content-security-policy.md): Content security policy fetch directives should not be disabled
- [sonarjs/disabled-auto-escaping](https://lintradar.dev/rules/sonarjs/disabled-auto-escaping.md): Auto-escaping in HTML template engines should not be disabled

---

Web page: https://lintradar.dev/rules/sonarjs/csrf. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
