Skip to content

Commit

Permalink
feat: add config recommended-legacy
Browse files Browse the repository at this point in the history
it also moves rule tests to `./test/rules`, and adds a test for the configs.

fixes #131

Signed-off-by: 唯然 <weiran.zsd@outlook.com>
  • Loading branch information
aladdin-add committed Dec 14, 2023
1 parent 27cd160 commit 69cf370
Show file tree
Hide file tree
Showing 31 changed files with 80 additions and 47 deletions.
44 changes: 28 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ yarn add --dev eslint-plugin-security

## Usage

### flat config(requires eslint >= v8.23.0)

Add the following to your `eslint.config.js` file:

```js
Expand All @@ -28,6 +30,16 @@ const pluginSecurity = require('eslint-plugin-security');
module.exports = [pluginSecurity.configs.recommended];
```

### eslintrc config(legacy)

Add the following to your `.eslintrc` file:

```js
"extends": [
"plugin:security/recommended-legacy"
]
```

## Developer guide

- Use [GitHub pull requests](https://help.github.com/articles/using-pull-requests).
Expand All @@ -52,21 +64,21 @@ npm test
⚠️ Configurations set to warn in.\
✅ Set in the `recommended` configuration.

| Name                                  | Description | ⚠️ |
| :------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------- | :-- |
| [detect-bidi-characters](docs/rules/detect-bidi-characters.md) | Detects trojan source attacks that employ unicode bidi attacks to inject malicious code. ||
| [detect-buffer-noassert](docs/rules/detect-buffer-noassert.md) | Detects calls to "buffer" with "noAssert" flag set. ||
| [detect-child-process](docs/rules/detect-child-process.md) | Detects instances of "child_process" & non-literal "exec()" calls. ||
| [detect-disable-mustache-escape](docs/rules/detect-disable-mustache-escape.md) | Detects "object.escapeMarkup = false", which can be used with some template engines to disable escaping of HTML entities. ||
| [detect-eval-with-expression](docs/rules/detect-eval-with-expression.md) | Detects "eval(variable)" which can allow an attacker to run arbitrary code inside your process. ||
| [detect-new-buffer](docs/rules/detect-new-buffer.md) | Detects instances of new Buffer(argument) where argument is any non-literal value. ||
| [detect-no-csrf-before-method-override](docs/rules/detect-no-csrf-before-method-override.md) | Detects Express "csrf" middleware setup before "method-override" middleware. ||
| [detect-non-literal-fs-filename](docs/rules/detect-non-literal-fs-filename.md) | Detects variable in filename argument of "fs" calls, which might allow an attacker to access anything on your system. ||
| [detect-non-literal-regexp](docs/rules/detect-non-literal-regexp.md) | Detects "RegExp(variable)", which might allow an attacker to DOS your server with a long-running regular expression. ||
| [detect-non-literal-require](docs/rules/detect-non-literal-require.md) | Detects "require(variable)", which might allow an attacker to load and run arbitrary code, or access arbitrary files on disk. ||
| [detect-object-injection](docs/rules/detect-object-injection.md) | Detects "variable[key]" as a left- or right-hand assignment operand. ||
| [detect-possible-timing-attacks](docs/rules/detect-possible-timing-attacks.md) | Detects insecure comparisons (`==`, `!=`, `!==` and `===`), which check input sequentially. ||
| [detect-pseudoRandomBytes](docs/rules/detect-pseudoRandomBytes.md) | Detects if "pseudoRandomBytes()" is in use, which might not give you the randomness you need and expect. ||
| [detect-unsafe-regex](docs/rules/detect-unsafe-regex.md) | Detects potentially unsafe regular expressions, which may take a very long time to run, blocking the event loop. ||
| Name                                  | Description | ⚠️ |
| :------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------- | :------------------------------- |
| [detect-bidi-characters](docs/rules/detect-bidi-characters.md) | Detects trojan source attacks that employ unicode bidi attacks to inject malicious code. |![badge-recommended-legacy][] |
| [detect-buffer-noassert](docs/rules/detect-buffer-noassert.md) | Detects calls to "buffer" with "noAssert" flag set. |![badge-recommended-legacy][] |
| [detect-child-process](docs/rules/detect-child-process.md) | Detects instances of "child_process" & non-literal "exec()" calls. |![badge-recommended-legacy][] |
| [detect-disable-mustache-escape](docs/rules/detect-disable-mustache-escape.md) | Detects "object.escapeMarkup = false", which can be used with some template engines to disable escaping of HTML entities. |![badge-recommended-legacy][] |
| [detect-eval-with-expression](docs/rules/detect-eval-with-expression.md) | Detects "eval(variable)" which can allow an attacker to run arbitrary code inside your process. |![badge-recommended-legacy][] |
| [detect-new-buffer](docs/rules/detect-new-buffer.md) | Detects instances of new Buffer(argument) where argument is any non-literal value. |![badge-recommended-legacy][] |
| [detect-no-csrf-before-method-override](docs/rules/detect-no-csrf-before-method-override.md) | Detects Express "csrf" middleware setup before "method-override" middleware. |![badge-recommended-legacy][] |
| [detect-non-literal-fs-filename](docs/rules/detect-non-literal-fs-filename.md) | Detects variable in filename argument of "fs" calls, which might allow an attacker to access anything on your system. |![badge-recommended-legacy][] |
| [detect-non-literal-regexp](docs/rules/detect-non-literal-regexp.md) | Detects "RegExp(variable)", which might allow an attacker to DOS your server with a long-running regular expression. |![badge-recommended-legacy][] |
| [detect-non-literal-require](docs/rules/detect-non-literal-require.md) | Detects "require(variable)", which might allow an attacker to load and run arbitrary code, or access arbitrary files on disk. |![badge-recommended-legacy][] |
| [detect-object-injection](docs/rules/detect-object-injection.md) | Detects "variable[key]" as a left- or right-hand assignment operand. |![badge-recommended-legacy][] |
| [detect-possible-timing-attacks](docs/rules/detect-possible-timing-attacks.md) | Detects insecure comparisons (`==`, `!=`, `!==` and `===`), which check input sequentially. |![badge-recommended-legacy][] |
| [detect-pseudoRandomBytes](docs/rules/detect-pseudoRandomBytes.md) | Detects if "pseudoRandomBytes()" is in use, which might not give you the randomness you need and expect. |![badge-recommended-legacy][] |
| [detect-unsafe-regex](docs/rules/detect-unsafe-regex.md) | Detects potentially unsafe regular expressions, which may take a very long time to run, blocking the event loop. |![badge-recommended-legacy][] |

<!-- end auto-generated rules list -->
2 changes: 1 addition & 1 deletion docs/rules/detect-bidi-characters.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Detects trojan source attacks that employ unicode bidi attacks to inject malicious code (`security/detect-bidi-characters`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/detect-buffer-noassert.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Detects calls to "buffer" with "noAssert" flag set (`security/detect-buffer-noassert`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/detect-child-process.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Detects instances of "child_process" & non-literal "exec()" calls (`security/detect-child-process`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/detect-disable-mustache-escape.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Detects "object.escapeMarkup = false", which can be used with some template engines to disable escaping of HTML entities (`security/detect-disable-mustache-escape`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/detect-eval-with-expression.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Detects "eval(variable)" which can allow an attacker to run arbitrary code inside your process (`security/detect-eval-with-expression`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/detect-new-buffer.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Detects instances of new Buffer(argument) where argument is any non-literal value (`security/detect-new-buffer`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->
2 changes: 1 addition & 1 deletion docs/rules/detect-no-csrf-before-method-override.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Detects Express "csrf" middleware setup before "method-override" middleware (`security/detect-no-csrf-before-method-override`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/detect-non-literal-fs-filename.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Detects variable in filename argument of "fs" calls, which might allow an attacker to access anything on your system (`security/detect-non-literal-fs-filename`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/detect-non-literal-regexp.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Detects "RegExp(variable)", which might allow an attacker to DOS your server with a long-running regular expression (`security/detect-non-literal-regexp`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/detect-non-literal-require.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Detects "require(variable)", which might allow an attacker to load and run arbitrary code, or access arbitrary files on disk (`security/detect-non-literal-require`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/detect-object-injection.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Detects "variable[key]" as a left- or right-hand assignment operand (`security/detect-object-injection`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/detect-possible-timing-attacks.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Detects insecure comparisons (`==`, `!=`, `!==` and `===`), which check input sequentially (`security/detect-possible-timing-attacks`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->
2 changes: 1 addition & 1 deletion docs/rules/detect-pseudoRandomBytes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Detects if "pseudoRandomBytes()" is in use, which might not give you the randomness you need and expect (`security/detect-pseudoRandomBytes`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->
2 changes: 1 addition & 1 deletion docs/rules/detect-unsafe-regex.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Detects potentially unsafe regular expressions, which may take a very long time to run, blocking the event loop (`security/detect-unsafe-regex`)

⚠️ This rule _warns_ in the ✅ `recommended` config.
⚠️ This rule _warns_ in the following configs: `recommended`, `recommended-legacy`.

<!-- end auto-generated rule header -->

Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ const recommended = {
},
};

Object.assign(plugin.configs, { recommended });
const recommendedLegacy = {
plugins: ['security'],
rules: recommended.rules,
};

Object.assign(plugin.configs, { recommended, 'recommended-legacy': recommendedLegacy });

module.exports = plugin;
16 changes: 16 additions & 0 deletions test/configs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const plugin = require('../../index.js');
const assert = require('assert').strict;

describe('export plugin object', () => {
it('should export rules', () => {
assert(plugin.rules);
assert(typeof plugin.rules['detect-unsafe-regex'] === 'object');
});

it('should export configs', () => {
assert(plugin.configs);
assert(plugin.configs['recommended']);
assert(plugin.configs['recommended-legacy']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const RuleTester = require('eslint').RuleTester;
const tester = new RuleTester();

const ruleName = 'detect-bidi-characters';
const Rule = require(`../rules/${ruleName}`);
const Rule = require(`../../rules/${ruleName}`);

tester.run(ruleName, Rule, {
valid: [
Expand Down Expand Up @@ -54,7 +54,7 @@ tester.run(`${ruleName} in comment-line`, Rule, {
console.log("You are an admin.");
/* end admins only ‮
⁦*/
/* end admins only ‮
/* end admins only ‮
{ ⁦*/
`,
errors: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const RuleTester = require('eslint').RuleTester;
const tester = new RuleTester();

const ruleName = 'detect-buffer-noassert';
const rule = require(`../rules/${ruleName}`);
const rule = require(`../../rules/${ruleName}`);

const allMethodNames = [...rule.meta.__methodsToCheck.read, ...rule.meta.__methodsToCheck.write];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const tester = new RuleTester({
});

const ruleName = 'detect-child-process';
const rule = require(`../rules/${ruleName}`);
const rule = require(`../../rules/${ruleName}`);

tester.run(ruleName, rule, {
valid: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const tester = new RuleTester();

const ruleName = 'detect-disable-mustache-escape';

tester.run(ruleName, require(`../rules/${ruleName}`), {
tester.run(ruleName, require(`../../rules/${ruleName}`), {
valid: [{ code: 'escapeMarkup = false' }],
invalid: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const tester = new RuleTester();

const ruleName = 'detect-eval-with-expression';

tester.run(ruleName, require(`../rules/${ruleName}`), {
tester.run(ruleName, require(`../../rules/${ruleName}`), {
valid: [{ code: "eval('alert()')" }],
invalid: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const tester = new RuleTester();
const ruleName = 'detect-new-buffer';
const invalid = 'var a = new Buffer(c)';

tester.run(ruleName, require(`../rules/${ruleName}`), {
tester.run(ruleName, require(`../../rules/${ruleName}`), {
valid: [{ code: "var a = new Buffer('test')" }],
invalid: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const tester = new RuleTester();

const ruleName = 'detect-no-csrf-before-method-override';

tester.run(ruleName, require(`../rules/${ruleName}`), {
tester.run(ruleName, require(`../../rules/${ruleName}`), {
valid: [{ code: 'express.methodOverride();express.csrf()' }],
invalid: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const tester = new RuleTester({

const ruleName = 'detect-non-literal-fs-filename';

tester.run(ruleName, require(`../rules/${ruleName}`), {
tester.run(ruleName, require(`../../rules/${ruleName}`), {
valid: [
{
code: `var fs = require('fs');
Expand All @@ -29,7 +29,7 @@ tester.run(ruleName, require(`../rules/${ruleName}`), {
import { promises as fsp } from 'fs';
import fs from 'fs';
import path from 'path';
const index = await fsp.readFile(path.resolve(__dirname, './index.html'), 'utf-8');
const key = fs.readFileSync(path.join(__dirname, './ssl.key'));
await fsp.writeFile(path.resolve(__dirname, './sitemap.xml'), sitemap);`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const tester = new RuleTester();
const ruleName = 'detect-non-literal-regexp';
const invalid = "var a = new RegExp(c, 'i')";

tester.run(ruleName, require(`../rules/${ruleName}`), {
tester.run(ruleName, require(`../../rules/${ruleName}`), {
valid: [
{ code: "var a = new RegExp('ab+c', 'i')" },
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const tester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });

const ruleName = 'detect-non-literal-require';

tester.run(ruleName, require(`../rules/${ruleName}`), {
tester.run(ruleName, require(`../../rules/${ruleName}`), {
valid: [
{ code: "var a = require('b')" },
{ code: 'var a = require(`b`)' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const tester = new RuleTester();

const ruleName = 'detect-object-injection';

const Rule = require(`../rules/${ruleName}`);
const Rule = require(`../../rules/${ruleName}`);

const valid = 'var a = {};';
// const invalidVariable = "TODO";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const RuleTester = require('eslint').RuleTester;
const tester = new RuleTester();

const ruleName = 'detect-possible-timing-attacks';
const Rule = require(`../rules/${ruleName}`);
const Rule = require(`../../rules/${ruleName}`);

const valid = 'if (age === 5) {}';
const invalidLeft = "if (password === 'mypass') {}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const tester = new RuleTester();
const ruleName = 'detect-pseudoRandomBytes';
const invalid = 'crypto.pseudoRandomBytes';

tester.run(ruleName, require(`../rules/${ruleName}`), {
tester.run(ruleName, require(`../../rules/${ruleName}`), {
valid: [{ code: 'crypto.randomBytes' }],
invalid: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const RuleTester = require('eslint').RuleTester;
const tester = new RuleTester();

const ruleName = 'detect-unsafe-regex';
const Rule = require(`../rules/${ruleName}`);
const Rule = require(`../../rules/${ruleName}`);

tester.run(ruleName, Rule, {
valid: [{ code: '/^d+1337d+$/' }],
Expand Down

0 comments on commit 69cf370

Please sign in to comment.