# session-regeneration

Rule `sonarjs/session-regeneration`. A new session should be created during user authentication

- 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: #1360 by reach, #661 by growth

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
app.post('/login',
  passport.authenticate('local', { failureRedirect: '/login' }),
  function(req, res) {
    // Sensitive - no session.regenerate after login
    res.redirect('/');
  });
```

### Correct: the rule accepts this

```js
app.post('/login',
  passport.authenticate('local', { failureRedirect: '/login' }),
  function(req, res) {
    let prevSession = req.session;
    req.session.regenerate((err) => {  // Compliant
      Object.assign(req.session, prevSession);
      res.redirect('/');
    });
  });
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/session-regeneration": "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/review-blockchain-mnemonic](https://lintradar.dev/rules/sonarjs/review-blockchain-mnemonic.md): Wallet phrases should not be hard-coded
- [sonarjs/single-char-in-character-classes](https://lintradar.dev/rules/sonarjs/single-char-in-character-classes.md): Character classes in regular expressions should not contain only one character
- [sonarjs/regex-complexity](https://lintradar.dev/rules/sonarjs/regex-complexity.md): Regular expressions should not be too complicated
- [sonarjs/single-character-alternation](https://lintradar.dev/rules/sonarjs/single-character-alternation.md): Single-character alternations in regular expressions should be replaced with character classes
- [sonarjs/redundant-type-aliases](https://lintradar.dev/rules/sonarjs/redundant-type-aliases.md): Redundant type aliases should not be used
- [sonarjs/slow-regex](https://lintradar.dev/rules/sonarjs/slow-regex.md): Regular expressions should not cause catastrophic backtracking

---

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