# no-weak-keys

Rule `sonarjs/no-weak-keys`. Cryptographic keys should be robust

- 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: #1335 by reach, #636 by growth

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
var { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 1024,  // Noncompliant
  publicKeyEncoding:  { type: 'spki', format: 'pem' },
  privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
}); // Noncompliant: 1024 bits is too short for a RSA key pair

crypto.generateKeyPair('ec', {
  namedCurve: 'secp112r2',
  publicKeyEncoding:  { type: 'spki', format: 'pem' },
  privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
}, callback); // Noncompliant: secp112r2 curve doesn't provide enough security
```

### Correct: the rule accepts this

```js
crypto.generateKeyPair('rsa', {
  modulusLength: 2048,  // Compliant
  publicKeyEncoding:  { type: 'spki', format: 'pem' },
  privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
}, callback); // Compliant

crypto.generateKeyPair('ec', {
  namedCurve: 'secp224k1',
  publicKeyEncoding:  { type: 'spki', format: 'pem' },
  privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
}, callback); // compliant
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/no-weak-keys": "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/no-weak-cipher](https://lintradar.dev/rules/sonarjs/no-weak-cipher.md): Cipher algorithms should be robust
- [sonarjs/non-existent-operator](https://lintradar.dev/rules/sonarjs/non-existent-operator.md): Non-existent operators '=+', '=-' and '=!' should not be used
- [sonarjs/no-vue-mixins](https://lintradar.dev/rules/sonarjs/no-vue-mixins.md): Vue components should not use mixins
- [sonarjs/null-dereference](https://lintradar.dev/rules/sonarjs/null-dereference.md): Properties of variables with "null" or "undefined" values should not be accessed
- [sonarjs/no-vue-class-component](https://lintradar.dev/rules/sonarjs/no-vue-class-component.md): Vue components should not use the deprecated "vue-class-component" or "vue-property-decorator" libraries
- [sonarjs/object-alt-content](https://lintradar.dev/rules/sonarjs/object-alt-content.md): "<object>" tags should provide an alternative content

---

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