Skip to content

Commit

Permalink
fix: Ensure everything works with ESLint v9
Browse files Browse the repository at this point in the history
fixes #144
  • Loading branch information
nzakas committed Apr 9, 2024
1 parent 7eddd87 commit bfc59e2
Show file tree
Hide file tree
Showing 20 changed files with 470 additions and 427 deletions.
756 changes: 393 additions & 363 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
"devDependencies": {
"@eslint/js": "^8.51.0",
"changelog": "1.3.0",
"eslint": "^8.51.0",
"eslint": "^9.0.0",
"eslint-config-nodesecurity": "^1.3.1",
"eslint-config-prettier": "^8.5.0",
"eslint-doc-generator": "^1.7.0",
"eslint-plugin-eslint-plugin": "^5.1.1",
"eslint-plugin-eslint-plugin": "^5.5.1",
"lint-staged": "^12.3.7",
"markdownlint-cli": "^0.32.2",
"mocha": "^9.2.2",
Expand Down
2 changes: 1 addition & 1 deletion rules/detect-bidi-characters.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = {
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-bidi-characters.md',
},
},
create: function (context) {
create(context) {
return {
Program: function (node) {
report({
Expand Down
2 changes: 1 addition & 1 deletion rules/detect-buffer-noassert.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = {
write,
},
},
create: function (context) {
create(context) {
return {
MemberExpression: function (node) {
let index;
Expand Down
7 changes: 4 additions & 3 deletions rules/detect-child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ module.exports = {
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-child-process.md',
},
},
create: function (context) {
create(context) {
const sourceCode = context.sourceCode;
return {
CallExpression: function (node) {
if (node.callee.name === 'require') {
Expand All @@ -46,14 +47,14 @@ module.exports = {
!node.arguments.length ||
isStaticExpression({
node: node.arguments[0],
scope: context.getScope(),
scope: sourceCode.getScope(node.arguments[0]),
})
) {
return;
}
const pathInfo = getImportAccessPath({
node: node.callee,
scope: context.getScope(),
scope: sourceCode.getScope(node.callee),
packageNames: childProcessPackageNames,
});
const fnName = pathInfo && pathInfo.path.length === 1 && pathInfo.path[0];
Expand Down
2 changes: 1 addition & 1 deletion rules/detect-disable-mustache-escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-disable-mustache-escape.md',
},
},
create: function (context) {
create(context) {
return {
AssignmentExpression: function (node) {
if (node.operator === '=') {
Expand Down
2 changes: 1 addition & 1 deletion rules/detect-new-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-new-buffer.md',
},
},
create: function (context) {
create(context) {
return {
NewExpression: function (node) {
if (node.callee.name === 'Buffer' && node.arguments[0] && node.arguments[0].type !== 'Literal') {
Expand Down
2 changes: 1 addition & 1 deletion rules/detect-no-csrf-before-method-override.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-no-csrf-before-method-override.md',
},
},
create: function (context) {
create(context) {
let csrf = false;

return {
Expand Down
7 changes: 4 additions & 3 deletions rules/detect-non-literal-fs-filename.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ module.exports = {
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-non-literal-fs-filename.md',
},
},
create: function (context) {
create(context) {
const sourceCode = context.sourceCode;
return {
CallExpression: function (node) {
// don't check require. If all arguments are Literals, it's surely safe!
Expand All @@ -36,7 +37,7 @@ module.exports = {

const pathInfo = getImportAccessPath({
node: node.callee,
scope: context.getScope(),
scope: sourceCode.getScope(node.callee),
packageNames: fsPackageNames,
});
if (!pathInfo) {
Expand Down Expand Up @@ -79,7 +80,7 @@ module.exports = {
continue;
}
const argument = node.arguments[index];
if (isStaticExpression({ node: argument, scope: context.getScope() })) {
if (isStaticExpression({ node: argument, scope: sourceCode.getScope(argument) })) {
continue;
}
indices.push(index);
Expand Down
4 changes: 2 additions & 2 deletions rules/detect-non-literal-regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-non-literal-regexp.md',
},
},
create: function (context) {
create(context) {
return {
NewExpression: function (node) {
if (node.callee.name === 'RegExp') {
Expand All @@ -31,7 +31,7 @@ module.exports = {
args.length > 0 &&
!isStaticExpression({
node: args[0],
scope: context.getScope(),
scope: context.sourceCode.getScope(args[0]),
})
) {
return context.report({ node: node, message: 'Found non-literal argument to RegExp Constructor' });
Expand Down
4 changes: 2 additions & 2 deletions rules/detect-non-literal-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-non-literal-require.md',
},
},
create: function (context) {
create(context) {
return {
CallExpression: function (node) {
if (node.callee.name === 'require') {
Expand All @@ -31,7 +31,7 @@ module.exports = {
args.length > 0 &&
!isStaticExpression({
node: args[0],
scope: context.getScope(),
scope: context.sourceCode.getScope(args[0]),
})
) {
return context.report({ node: node, message: 'Found non-literal argument in require' });
Expand Down
2 changes: 1 addition & 1 deletion rules/detect-object-injection.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = {
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-object-injection.md',
},
},
create: function (context) {
create(context) {
return {
MemberExpression: function (node) {
if (node.computed === true) {
Expand Down
2 changes: 1 addition & 1 deletion rules/detect-possible-timing-attacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = {
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-possible-timing-attacks.md',
},
},
create: function (context) {
create(context) {
return {
IfStatement: function (node) {
if (node.test && node.test.type === 'BinaryExpression') {
Expand Down
2 changes: 1 addition & 1 deletion rules/detect-pseudoRandomBytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = {
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-pseudoRandomBytes.md',
},
},
create: function (context) {
create(context) {
return {
MemberExpression: function (node) {
if (node.property.name === 'pseudoRandomBytes') {
Expand Down
2 changes: 1 addition & 1 deletion rules/detect-unsafe-regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
url: 'https://github.com/eslint-community/eslint-plugin-security/blob/main/docs/rules/detect-unsafe-regex.md',
},
},
create: function (context) {
create(context) {
return {
Literal: function (node) {
const token = context.getSourceCode().getTokens(node)[0];
Expand Down
7 changes: 1 addition & 6 deletions test/rules/detect-child-process.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
'use strict';

const RuleTester = require('eslint').RuleTester;
const tester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
},
});
const tester = new RuleTester();

const ruleName = 'detect-child-process';
const rule = require(`../../rules/${ruleName}`);
Expand Down
37 changes: 21 additions & 16 deletions test/rules/detect-non-literal-fs-filename.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
'use strict';

const RuleTester = require('eslint').RuleTester;
const tester = new RuleTester({
parserOptions: {
ecmaVersion: 13,
sourceType: 'module',
},
});
const tester = new RuleTester();

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

Expand All @@ -33,8 +28,10 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
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);`,
globals: {
__dirname: 'readonly',
languageOptions: {
globals: {
__dirname: 'readonly',
},
},
},
{
Expand All @@ -43,16 +40,20 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
import path from 'path';
const dirname = path.dirname(__filename)
const key = fs.readFileSync(path.resolve(dirname, './index.html'));`,
globals: {
__filename: 'readonly',
languageOptions: {
globals: {
__filename: 'readonly',
},
},
},
{
code: `
import fs from 'fs';
const key = fs.readFileSync(\`\${process.cwd()}/path/to/foo.json\`);`,
globals: {
process: 'readonly',
languageOptions: {
globals: {
process: 'readonly',
},
},
},
`
Expand All @@ -65,8 +66,10 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
code: `
import fs from 'fs';
const pkg = fs.readFileSync(require.resolve('eslint/package.json'), 'utf-8');`,
globals: {
require: 'readonly',
languageOptions: {
globals: {
require: 'readonly',
},
},
},
],
Expand Down Expand Up @@ -191,8 +194,10 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
import fs from 'fs';
import path from 'path';
const key = fs.readFileSync(path.resolve(__dirname, foo));`,
globals: {
__filename: 'readonly',
languageOptions: {
globals: {
__filename: 'readonly',
},
},
errors: [{ message: 'Found readFileSync from package "fs" with non literal argument at index 0' }],
},
Expand Down
8 changes: 5 additions & 3 deletions test/rules/detect-non-literal-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

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

Expand All @@ -17,8 +17,10 @@ tester.run(ruleName, require(`../../rules/${ruleName}`), {
},
{
code: "const utils = require(__dirname + '/utils');",
globals: {
__dirname: 'readonly',
languageOptions: {
globals: {
__dirname: 'readonly',
},
},
},
],
Expand Down
17 changes: 10 additions & 7 deletions test/utils/import-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Linter = require('eslint').Linter;
function getGetImportAccessPathResult(code) {
const linter = new Linter();
const result = [];
linter.defineRule('test-rule', {
const testRule = {
create(context) {
return {
'Identifier[name = target]'(node) {
Expand All @@ -18,7 +18,7 @@ function getGetImportAccessPathResult(code) {
}
const info = getImportAccessPath({
node: expr,
scope: context.getScope(),
scope: context.sourceCode.getScope(expr),
packageNames: ['target', 'target-foo', 'target-bar'],
});
if (!info) return;
Expand All @@ -30,15 +30,18 @@ function getGetImportAccessPathResult(code) {
},
};
},
});
};

const linterResult = linter.verify(code, {
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
plugins: {
test: {
rules: {
'test-rule': testRule,
},
},
},
rules: {
'test-rule': 'error',
'test/test-rule': 'error',
},
});
deepStrictEqual(linterResult, []);
Expand Down
28 changes: 17 additions & 11 deletions test/utils/is-static-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,41 @@ const Linter = require('eslint').Linter;
function getIsStaticExpressionResult(code) {
const linter = new Linter();
const result = [];
linter.defineRule('test-rule', {
const testRule = {
create(context) {
return {
'CallExpression[callee.name = target]'(node) {
result.push(
...node.arguments.map((expr) =>
isStaticExpression({
node: expr,
scope: context.getScope(),
scope: context.sourceCode.getScope(expr),
})
)
);
},
};
},
});
};

const linterResult = linter.verify(code, {
parserOptions: {
ecmaVersion: 11,
sourceType: 'module',
plugins: {
test: {
rules: {
'test-rule': testRule,
},
},
},
globals: {
__dirname: 'readonly',
__filename: 'readonly',
require: 'readonly',
languageOptions: {
sourceType: 'module',
globals: {
__dirname: 'readonly',
__filename: 'readonly',
require: 'readonly',
},
},
rules: {
'test-rule': 'error',
'test/test-rule': 'error',
},
});
deepStrictEqual(linterResult, []);
Expand Down

0 comments on commit bfc59e2

Please sign in to comment.