Skip to content

Commit 1e93e68

Browse files
committed
fix: Ensure everything works with ESLint v9
fixes #144
1 parent 1b56aa2 commit 1e93e68

20 files changed

+470
-427
lines changed

package-lock.json

+393-363
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@
4848
"devDependencies": {
4949
"@eslint/js": "^8.51.0",
5050
"changelog": "1.3.0",
51-
"eslint": "^8.51.0",
51+
"eslint": "^9.0.0",
5252
"eslint-config-nodesecurity": "^1.3.1",
5353
"eslint-config-prettier": "^8.5.0",
5454
"eslint-doc-generator": "^1.0.2",
55-
"eslint-plugin-eslint-plugin": "^5.1.1",
55+
"eslint-plugin-eslint-plugin": "^5.5.1",
5656
"lint-staged": "^12.3.7",
5757
"markdownlint-cli": "^0.32.2",
5858
"mocha": "^9.2.2",

rules/detect-bidi-characters.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module.exports = {
7878
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-bidi-characters.md',
7979
},
8080
},
81-
create: function (context) {
81+
create(context) {
8282
return {
8383
Program: function (node) {
8484
report({

rules/detect-buffer-noassert.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports = {
6161
write,
6262
},
6363
},
64-
create: function (context) {
64+
create(context) {
6565
return {
6666
MemberExpression: function (node) {
6767
let index;

rules/detect-child-process.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module.exports = {
2323
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-child-process.md',
2424
},
2525
},
26-
create: function (context) {
26+
create(context) {
27+
const sourceCode = context.sourceCode;
2728
return {
2829
CallExpression: function (node) {
2930
if (node.callee.name === 'require') {
@@ -46,14 +47,14 @@ module.exports = {
4647
!node.arguments.length ||
4748
isStaticExpression({
4849
node: node.arguments[0],
49-
scope: context.getScope(),
50+
scope: sourceCode.getScope(node.arguments[0]),
5051
})
5152
) {
5253
return;
5354
}
5455
const pathInfo = getImportAccessPath({
5556
node: node.callee,
56-
scope: context.getScope(),
57+
scope: sourceCode.getScope(node.callee),
5758
packageNames: childProcessPackageNames,
5859
});
5960
const fnName = pathInfo && pathInfo.path.length === 1 && pathInfo.path[0];

rules/detect-disable-mustache-escape.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-disable-mustache-escape.md',
1111
},
1212
},
13-
create: function (context) {
13+
create(context) {
1414
return {
1515
AssignmentExpression: function (node) {
1616
if (node.operator === '=') {

rules/detect-new-buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-new-buffer.md',
1111
},
1212
},
13-
create: function (context) {
13+
create(context) {
1414
return {
1515
NewExpression: function (node) {
1616
if (node.callee.name === 'Buffer' && node.arguments[0] && node.arguments[0].type !== 'Literal') {

rules/detect-no-csrf-before-method-override.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-no-csrf-before-method-override.md',
2020
},
2121
},
22-
create: function (context) {
22+
create(context) {
2323
let csrf = false;
2424

2525
return {

rules/detect-non-literal-fs-filename.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ module.exports = {
2626
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-non-literal-fs-filename.md',
2727
},
2828
},
29-
create: function (context) {
29+
create(context) {
30+
const sourceCode = context.sourceCode;
3031
return {
3132
CallExpression: function (node) {
3233
// don't check require. If all arguments are Literals, it's surely safe!
@@ -36,7 +37,7 @@ module.exports = {
3637

3738
const pathInfo = getImportAccessPath({
3839
node: node.callee,
39-
scope: context.getScope(),
40+
scope: sourceCode.getScope(node.callee),
4041
packageNames: fsPackageNames,
4142
});
4243
if (!pathInfo) {
@@ -79,7 +80,7 @@ module.exports = {
7980
continue;
8081
}
8182
const argument = node.arguments[index];
82-
if (isStaticExpression({ node: argument, scope: context.getScope() })) {
83+
if (isStaticExpression({ node: argument, scope: sourceCode.getScope(argument) })) {
8384
continue;
8485
}
8586
indices.push(index);

rules/detect-non-literal-regexp.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-non-literal-regexp.md',
2222
},
2323
},
24-
create: function (context) {
24+
create(context) {
2525
return {
2626
NewExpression: function (node) {
2727
if (node.callee.name === 'RegExp') {
@@ -31,7 +31,7 @@ module.exports = {
3131
args.length > 0 &&
3232
!isStaticExpression({
3333
node: args[0],
34-
scope: context.getScope(),
34+
scope: context.sourceCode.getScope(args[0]),
3535
})
3636
) {
3737
return context.report({ node: node, message: 'Found non-literal argument to RegExp Constructor' });

rules/detect-non-literal-require.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-non-literal-require.md',
2222
},
2323
},
24-
create: function (context) {
24+
create(context) {
2525
return {
2626
CallExpression: function (node) {
2727
if (node.callee.name === 'require') {
@@ -31,7 +31,7 @@ module.exports = {
3131
args.length > 0 &&
3232
!isStaticExpression({
3333
node: args[0],
34-
scope: context.getScope(),
34+
scope: context.sourceCode.getScope(args[0]),
3535
})
3636
) {
3737
return context.report({ node: node, message: 'Found non-literal argument in require' });

rules/detect-object-injection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports = {
6161
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-object-injection.md',
6262
},
6363
},
64-
create: function (context) {
64+
create(context) {
6565
return {
6666
MemberExpression: function (node) {
6767
if (node.computed === true) {

rules/detect-possible-timing-attacks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = {
3232
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-possible-timing-attacks.md',
3333
},
3434
},
35-
create: function (context) {
35+
create(context) {
3636
return {
3737
IfStatement: function (node) {
3838
if (node.test && node.test.type === 'BinaryExpression') {

rules/detect-pseudoRandomBytes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-pseudoRandomBytes.md',
2020
},
2121
},
22-
create: function (context) {
22+
create(context) {
2323
return {
2424
MemberExpression: function (node) {
2525
if (node.property.name === 'pseudoRandomBytes') {

rules/detect-unsafe-regex.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = {
2525
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-unsafe-regex.md',
2626
},
2727
},
28-
create: function (context) {
28+
create(context) {
2929
return {
3030
Literal: function (node) {
3131
const token = context.getSourceCode().getTokens(node)[0];

test/rules/detect-child-process.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
'use strict';
22

33
const RuleTester = require('eslint').RuleTester;
4-
const tester = new RuleTester({
5-
parserOptions: {
6-
ecmaVersion: 6,
7-
sourceType: 'module',
8-
},
9-
});
4+
const tester = new RuleTester();
105

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

test/rules/detect-non-literal-fs-filename.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
'use strict';
22

33
const RuleTester = require('eslint').RuleTester;
4-
const tester = new RuleTester({
5-
parserOptions: {
6-
ecmaVersion: 13,
7-
sourceType: 'module',
8-
},
9-
});
4+
const tester = new RuleTester();
105

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

@@ -33,8 +28,10 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
3328
const index = await fsp.readFile(path.resolve(__dirname, './index.html'), 'utf-8');
3429
const key = fs.readFileSync(path.join(__dirname, './ssl.key'));
3530
await fsp.writeFile(path.resolve(__dirname, './sitemap.xml'), sitemap);`,
36-
globals: {
37-
__dirname: 'readonly',
31+
languageOptions: {
32+
globals: {
33+
__dirname: 'readonly',
34+
},
3835
},
3936
},
4037
{
@@ -43,16 +40,20 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
4340
import path from 'path';
4441
const dirname = path.dirname(__filename)
4542
const key = fs.readFileSync(path.resolve(dirname, './index.html'));`,
46-
globals: {
47-
__filename: 'readonly',
43+
languageOptions: {
44+
globals: {
45+
__filename: 'readonly',
46+
},
4847
},
4948
},
5049
{
5150
code: `
5251
import fs from 'fs';
5352
const key = fs.readFileSync(\`\${process.cwd()}/path/to/foo.json\`);`,
54-
globals: {
55-
process: 'readonly',
53+
languageOptions: {
54+
globals: {
55+
process: 'readonly',
56+
},
5657
},
5758
},
5859
`
@@ -65,8 +66,10 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
6566
code: `
6667
import fs from 'fs';
6768
const pkg = fs.readFileSync(require.resolve('eslint/package.json'), 'utf-8');`,
68-
globals: {
69-
require: 'readonly',
69+
languageOptions: {
70+
globals: {
71+
require: 'readonly',
72+
},
7073
},
7174
},
7275
],
@@ -191,8 +194,10 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
191194
import fs from 'fs';
192195
import path from 'path';
193196
const key = fs.readFileSync(path.resolve(__dirname, foo));`,
194-
globals: {
195-
__filename: 'readonly',
197+
languageOptions: {
198+
globals: {
199+
__filename: 'readonly',
200+
},
196201
},
197202
errors: [{ message: 'Found readFileSync from package "fs" with non literal argument at index 0' }],
198203
},

test/rules/detect-non-literal-require.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const RuleTester = require('eslint').RuleTester;
44

5-
const tester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
5+
const tester = new RuleTester({ languageOptions: { sourceType: 'commonjs' } });
66

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

@@ -17,8 +17,10 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
1717
},
1818
{
1919
code: "const utils = require(__dirname + '/utils');",
20-
globals: {
21-
__dirname: 'readonly',
20+
languageOptions: {
21+
globals: {
22+
__dirname: 'readonly',
23+
},
2224
},
2325
},
2426
],

test/utils/import-utils.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Linter = require('eslint').Linter;
88
function getGetImportAccessPathResult(code) {
99
const linter = new Linter();
1010
const result = [];
11-
linter.defineRule('test-rule', {
11+
const testRule = {
1212
create(context) {
1313
return {
1414
'Identifier[name = target]'(node) {
@@ -18,7 +18,7 @@ function getGetImportAccessPathResult(code) {
1818
}
1919
const info = getImportAccessPath({
2020
node: expr,
21-
scope: context.getScope(),
21+
scope: context.sourceCode.getScope(expr),
2222
packageNames: ['target', 'target-foo', 'target-bar'],
2323
});
2424
if (!info) return;
@@ -30,15 +30,18 @@ function getGetImportAccessPathResult(code) {
3030
},
3131
};
3232
},
33-
});
33+
};
3434

3535
const linterResult = linter.verify(code, {
36-
parserOptions: {
37-
ecmaVersion: 6,
38-
sourceType: 'module',
36+
plugins: {
37+
test: {
38+
rules: {
39+
'test-rule': testRule,
40+
},
41+
},
3942
},
4043
rules: {
41-
'test-rule': 'error',
44+
'test/test-rule': 'error',
4245
},
4346
});
4447
deepStrictEqual(linterResult, []);

test/utils/is-static-expression.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,41 @@ const Linter = require('eslint').Linter;
1212
function getIsStaticExpressionResult(code) {
1313
const linter = new Linter();
1414
const result = [];
15-
linter.defineRule('test-rule', {
15+
const testRule = {
1616
create(context) {
1717
return {
1818
'CallExpression[callee.name = target]'(node) {
1919
result.push(
2020
...node.arguments.map((expr) =>
2121
isStaticExpression({
2222
node: expr,
23-
scope: context.getScope(),
23+
scope: context.sourceCode.getScope(expr),
2424
})
2525
)
2626
);
2727
},
2828
};
2929
},
30-
});
30+
};
3131

3232
const linterResult = linter.verify(code, {
33-
parserOptions: {
34-
ecmaVersion: 11,
35-
sourceType: 'module',
33+
plugins: {
34+
test: {
35+
rules: {
36+
'test-rule': testRule,
37+
},
38+
},
3639
},
37-
globals: {
38-
__dirname: 'readonly',
39-
__filename: 'readonly',
40-
require: 'readonly',
40+
languageOptions: {
41+
sourceType: 'module',
42+
globals: {
43+
__dirname: 'readonly',
44+
__filename: 'readonly',
45+
require: 'readonly',
46+
},
4147
},
4248
rules: {
43-
'test-rule': 'error',
49+
'test/test-rule': 'error',
4450
},
4551
});
4652
deepStrictEqual(linterResult, []);

0 commit comments

Comments
 (0)