Skip to content

Commit bd410ea

Browse files
authored
refactor: fix deprecation in tests & update eslint (#292)
1 parent 86cb3d9 commit bd410ea

25 files changed

+133
-110
lines changed

.eslintignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

.eslintrc

Lines changed: 0 additions & 7 deletions
This file was deleted.

eslint.config.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ESLintConfig } from "@openally/config.eslint";
2+
3+
export default [
4+
{
5+
ignores: [
6+
"**/test/fixtures/**/*",
7+
"**/test/probes/fixtures/**/*.js"
8+
]
9+
},
10+
...ESLintConfig,
11+
{
12+
languageOptions: {
13+
sourceType: "module",
14+
parserOptions: {
15+
requireConfigFile: false
16+
}
17+
}
18+
}
19+
];

package.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
"node": ">=18.0.0"
99
},
1010
"scripts": {
11-
"lint": "eslint src test",
12-
"prepublishOnly": "pkg-ok",
11+
"lint": "eslint src workspaces test",
1312
"test-only": "glob -c \"node --test-reporter=spec --test\" \"./test/**/*.spec.js\"",
1413
"test": "c8 --all --src ./src -r html npm run test-only",
1514
"check": "npm run lint && npm run test-only"
@@ -54,13 +53,10 @@
5453
"ts-pattern": "^5.0.6"
5554
},
5655
"devDependencies": {
57-
"@nodesecure/eslint-config": "^1.6.0",
56+
"@openally/config.eslint": "^1.0.0",
5857
"@types/node": "^22.0.0",
5958
"c8": "^10.1.2",
60-
"cross-env": "^7.0.3",
61-
"eslint": "^9.0.0",
6259
"glob": "^11.0.0",
63-
"iterator-matcher": "^2.1.0",
64-
"pkg-ok": "^3.0.0"
60+
"iterator-matcher": "^2.1.0"
6561
}
6662
}

src/probes/isLiteral.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { builtinModules } from "repl";
55
import { Hex } from "@nodesecure/sec-literal";
66

77
const kMapRegexIps = Object.freeze({
8+
// eslint-disable-next-line @stylistic/max-len
89
regexIPv4: /^(https?:\/\/)(?!127\.)(?!.*:(?:0{1,3}|25[6-9])\.)(?!.*:(?:25[6-9])\.(?:0{1,3}|25[6-9])\.)(?!.*:(?:25[6-9])\.(?:25[6-9])\.(?:0{1,3}|25[6-9])\.)(?!.*:(?:25[6-9])\.(?:25[6-9])\.(?:25[6-9])\.(?:0{1,3}|25[6-9]))((?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d{2}|2[0-4]\d|25[0-5])(?::\d{1,5})?(\/[^\s]*)?$/,
910
regexIPv6: /^(https?:\/\/)(\[[0-9A-Fa-f:]+\])(?::\d{1,5})?(\/[^\s]*)?$/
1011
});

test/AstAnalyser.spec.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ describe("AstAnalyser", (t) => {
351351
});
352352
});
353353

354-
355354
it("intialize should be called before finalize", async() => {
356355
const calls = [];
357356

@@ -461,7 +460,6 @@ describe("AstAnalyser", (t) => {
461460
});
462461
});
463462

464-
465463
it("intialize should be called before finalize", () => {
466464
const calls = [];
467465

test/issues/109-html-comment-parsing.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ import { test } from "node:test";
44
import assert from "node:assert";
55

66
// Import Internal Dependencies
7-
import { runASTAnalysis } from "../../index.js";
7+
import { AstAnalyser } from "../../index.js";
88

99
// CONSTANTS
1010
const FIXTURE_URL = new URL("../fixtures/issues/", import.meta.url);
1111

1212
// Regression test for https://github.com/NodeSecure/js-x-ray/issues/109
1313
test("it should not crash for a JavaScript file containing HTML comments (and removeHTMLComments option enabled)", () => {
1414
const htmlComment = readFileSync(new URL("html-comments.js", FIXTURE_URL), "utf-8");
15-
runASTAnalysis(htmlComment, {
15+
new AstAnalyser().analyse(htmlComment, {
1616
removeHTMLComments: true
1717
});
1818
});
1919

2020
test("it should crash for a JavaScript file containing HTML comments", (t) => {
2121
const htmlComment = readFileSync(new URL("html-comments.js", FIXTURE_URL), "utf-8");
2222

23-
assert.throws(() => runASTAnalysis(htmlComment));
23+
assert.throws(() => new AstAnalyser().analyse(htmlComment));
2424
});

test/issues/163-illegalReturnStatement.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { test } from "node:test";
33
import assert from "node:assert";
44

55
// Import Internal Dependencies
6-
import { runASTAnalysis } from "../../index.js";
6+
import { AstAnalyser } from "../../index.js";
77

88
/**
99
* @see https://github.com/NodeSecure/js-x-ray/issues/163
@@ -23,9 +23,9 @@ if (!argv.length) {
2323

2424
test("it should not throw error whatever module is true or false", () => {
2525
assert.doesNotThrow(() => {
26-
runASTAnalysis(kIncriminedCodeSample, { module: false });
26+
new AstAnalyser().analyse(kIncriminedCodeSample, { module: false });
2727
});
2828
assert.doesNotThrow(() => {
29-
runASTAnalysis(kIncriminedCodeSample, { module: true });
29+
new AstAnalyser().analyse(kIncriminedCodeSample, { module: true });
3030
});
3131
});

test/issues/170-isOneLineRequire-logicalExpression-CJS-export.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { test } from "node:test";
33
import assert from "node:assert";
44

55
// Import Internal Dependencies
6-
import { runASTAnalysis } from "../../index.js";
6+
import { AstAnalyser } from "../../index.js";
77

88
const validTestCases = [
99
["module.exports = require('fs') || require('constants');", ["fs", "constants"]],
@@ -37,7 +37,7 @@ const validTestCases = [
3737
test("it should return isOneLineRequire true given a single line CJS export with a valid assignment", () => {
3838
validTestCases.forEach((test) => {
3939
const [source, modules] = test;
40-
const { dependencies, isOneLineRequire } = runASTAnalysis(source);
40+
const { dependencies, isOneLineRequire } = new AstAnalyser().analyse(source);
4141

4242
assert.ok(isOneLineRequire);
4343
assert.deepEqual([...dependencies.keys()], modules);
@@ -60,7 +60,7 @@ const invalidTestCases = [
6060
test("it should return isOneLineRequire false given a single line CJS export with illegal callees", () => {
6161
invalidTestCases.forEach((test) => {
6262
const [source, modules] = test;
63-
const { dependencies, isOneLineRequire } = runASTAnalysis(source);
63+
const { dependencies, isOneLineRequire } = new AstAnalyser().analyse(source);
6464

6565
assert.ok(isOneLineRequire === false);
6666
assert.deepEqual([...dependencies.keys()], modules);

test/issues/177-wrongUnsafeRequire.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { test } from "node:test";
33
import assert from "node:assert";
44

55
// Import Internal Dependencies
6-
import { runASTAnalysis } from "../../index.js";
6+
import { AstAnalyser } from "../../index.js";
77

88
/**
99
* @see https://github.com/NodeSecure/js-x-ray/issues/177
1010
*/
1111
test("should detect unsafe-import and unsafe-statement", () => {
12-
const { warnings, dependencies } = runASTAnalysis(`const help = require('help-me')({
12+
const { warnings, dependencies } = new AstAnalyser().analyse(`const help = require('help-me')({
1313
dir: path.join(__dirname, 'help'),
1414
ext: '.txt'
1515
})`);

test/issues/178-path-join-literal-args-is-not-unsafe.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { test } from "node:test";
33
import assert from "node:assert";
44

55
// Import Internal Dependencies
6-
import { runASTAnalysis } from "../../index.js";
6+
import { AstAnalyser } from "../../index.js";
77

88
/**
99
* @see https://github.com/NodeSecure/js-x-ray/issues/178
@@ -15,7 +15,7 @@ const validTestCases = [
1515

1616
test("should not detect unsafe-import for path.join if every argument is a string literal", () => {
1717
validTestCases.forEach((test) => {
18-
const { warnings, dependencies } = runASTAnalysis(test);
18+
const { warnings, dependencies } = new AstAnalyser().analyse(test);
1919

2020
assert.strictEqual(warnings.length, 0);
2121
assert.ok(dependencies.has("../bin.js"));
@@ -31,7 +31,7 @@ const invalidTestCases = [
3131

3232
test("should detect unsafe-import of path.join if not every argument is a string literal", () => {
3333
invalidTestCases.forEach((test) => {
34-
const { warnings } = runASTAnalysis(test);
34+
const { warnings } = new AstAnalyser().analyse(test);
3535

3636
assert.strictEqual(warnings.length, 1);
3737
});

test/issues/179-UnsafeEvalRequire.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { test } from "node:test";
33
import assert from "node:assert";
44

55
// Import Internal Dependencies
6-
import { runASTAnalysis } from "../../index.js";
6+
import { AstAnalyser } from "../../index.js";
77

88
/**
99
* @see https://github.com/NodeSecure/js-x-ray/issues/179
@@ -14,7 +14,7 @@ const kWarningUnsafeImport = "unsafe-import";
1414
const kWarningUnsafeStatement = "unsafe-stmt";
1515

1616
test("should detect unsafe-import and unsafe-statement", () => {
17-
const sastAnalysis = runASTAnalysis(kIncriminedCodeSample);
17+
const sastAnalysis = new AstAnalyser().analyse(kIncriminedCodeSample);
1818

1919
assert.equal(sastAnalysis.warnings.at(0).value, "stream");
2020
assert.equal(sastAnalysis.warnings.at(0).kind, kWarningUnsafeImport);

test/issues/180-logicalexpr-return-this.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { test } from "node:test";
33
import assert from "node:assert";
44

55
// Import Internal Dependencies
6-
import { runASTAnalysis } from "../../index.js";
6+
import { AstAnalyser } from "../../index.js";
77

88
/**
99
* @see https://github.com/NodeSecure/js-x-ray/issues/180
1010
*/
1111
test("should detect required core 'http' with a LogicalExpr containing Function('return this')()", () => {
12-
const { warnings, dependencies } = runASTAnalysis(`
12+
const { warnings, dependencies } = new AstAnalyser().analyse(`
1313
var root = freeGlobal || freeSelf || Function('return this')();
1414
const foo = root.require;
1515
foo("http");

test/issues/283-oneline-require-minified.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import { test } from "node:test";
33
import assert from "node:assert";
44

55
// Import Internal Dependencies
6-
import { runASTAnalysis } from "../../index.js";
6+
import { AstAnalyser } from "../../index.js";
77

88
// Regression test for https://github.com/NodeSecure/js-x-ray/issues/283
99
test("Given a one line require (with no module.exports) then isOneLineRequire must equal true", () => {
10-
const { isOneLineRequire } = runASTAnalysis(`require('foo.js');`);
10+
const { isOneLineRequire } = new AstAnalyser().analyse(`require('foo.js');`);
1111

1212
assert.ok(isOneLineRequire);
1313
});
1414

1515
test("Given an empty code then isOneLineRequire must equal false", () => {
16-
const { isOneLineRequire } = runASTAnalysis(``);
16+
const { isOneLineRequire } = new AstAnalyser().analyse(``);
1717

1818
assert.strictEqual(isOneLineRequire, false);
1919
});

test/issues/59-undefined-depName.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { readFileSync } from "node:fs";
33
import { test } from "node:test";
44

55
// Import Internal Dependencies
6-
import { runASTAnalysis } from "../../index.js";
6+
import { AstAnalyser } from "../../index.js";
77

88
// CONSTANTS
99
const FIXTURE_URL = new URL("../fixtures/issues/", import.meta.url);
@@ -14,5 +14,5 @@ test("it should not crash for prop-types", () => {
1414
new URL("prop-types.min.js", FIXTURE_URL),
1515
"utf-8"
1616
);
17-
runASTAnalysis(propTypes);
17+
new AstAnalyser().analyse(propTypes);
1818
});

test/obfuscated.spec.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import { test } from "node:test";
55
import assert from "node:assert";
66

77
// Import Internal Dependencies
8-
import { runASTAnalysis, runASTAnalysisOnFile } from "../index.js";
8+
import {
9+
AstAnalyser
10+
} from "../index.js";
911
import { getWarningKind } from "./utils/index.js";
1012

1113
// CONSTANTS
1214
const FIXTURE_URL = new URL("fixtures/obfuscated/", import.meta.url);
1315

1416
test("should detect 'jsfuck' obfuscation", () => {
1517
const trycatch = readFileSync(new URL("jsfuck.js", FIXTURE_URL), "utf-8");
16-
const { warnings } = runASTAnalysis(trycatch);
18+
const { warnings } = new AstAnalyser().analyse(trycatch);
1719

1820
assert.strictEqual(warnings.length, 1);
1921
assert.deepEqual(getWarningKind(warnings), ["obfuscated-code"].sort());
@@ -22,7 +24,7 @@ test("should detect 'jsfuck' obfuscation", () => {
2224

2325
test("should detect 'morse' obfuscation", () => {
2426
const trycatch = readFileSync(new URL("morse.js", FIXTURE_URL), "utf-8");
25-
const { warnings } = runASTAnalysis(trycatch);
27+
const { warnings } = new AstAnalyser().analyse(trycatch);
2628

2729
assert.strictEqual(warnings.length, 1);
2830
assert.deepEqual(getWarningKind(warnings), ["obfuscated-code"].sort());
@@ -31,14 +33,14 @@ test("should detect 'morse' obfuscation", () => {
3133

3234
test("should not detect 'morse' obfuscation", () => {
3335
const trycatch = readFileSync(new URL("notMorse.js", FIXTURE_URL), "utf-8");
34-
const { warnings } = runASTAnalysis(trycatch);
36+
const { warnings } = new AstAnalyser().analyse(trycatch);
3537

3638
assert.strictEqual(warnings.length, 0);
3739
});
3840

3941
test("should not detect 'morse' obfuscation for high number of doubles morse symbols", () => {
4042
const morseSymbolDoublesString = `const a = ${"'.' + '..' +".repeat(37)} '.'`;
41-
const { warnings } = runASTAnalysis(morseSymbolDoublesString);
43+
const { warnings } = new AstAnalyser().analyse(morseSymbolDoublesString);
4244

4345
assert.strictEqual(warnings.length, 0);
4446
});
@@ -48,7 +50,7 @@ test("should detect 'jjencode' obfuscation", () => {
4850
new URL("jjencode.js", FIXTURE_URL),
4951
"utf-8"
5052
);
51-
const { warnings } = runASTAnalysis(trycatch);
53+
const { warnings } = new AstAnalyser().analyse(trycatch);
5254

5355
assert.strictEqual(warnings.length, 1);
5456
assert.deepEqual(getWarningKind(warnings), ["obfuscated-code"].sort());
@@ -60,7 +62,7 @@ test("should detect 'freejsobfuscator' obfuscation", () => {
6062
new URL("freejsobfuscator.js", FIXTURE_URL),
6163
"utf-8"
6264
);
63-
const { warnings } = runASTAnalysis(trycatch);
65+
const { warnings } = new AstAnalyser().analyse(trycatch);
6466

6567
assert.deepEqual(getWarningKind(warnings), [
6668
"encoded-literal", "encoded-literal", "obfuscated-code"
@@ -73,7 +75,7 @@ test("should detect 'obfuscator.io' obfuscation (with hexadecimal generator)", (
7375
new URL("obfuscatorio-hexa.js", FIXTURE_URL),
7476
"utf-8"
7577
);
76-
const { warnings } = runASTAnalysis(trycatch);
78+
const { warnings } = new AstAnalyser().analyse(trycatch);
7779

7880
assert.strictEqual(warnings.length, 1);
7981
assert.deepEqual(getWarningKind(warnings), [
@@ -83,15 +85,15 @@ test("should detect 'obfuscator.io' obfuscation (with hexadecimal generator)", (
8385
});
8486

8587
test("should not detect 'trojan-source' when providing safe control character", () => {
86-
const { warnings } = runASTAnalysis(`
88+
const { warnings } = new AstAnalyser().analyse(`
8789
const simpleStringWithControlCharacters = "Its only a \u0008backspace";
8890
`);
8991

9092
assert.deepEqual([...warnings], []);
9193
});
9294

9395
test("should detect 'trojan-source' when there is one unsafe unicode control char", () => {
94-
const { warnings } = runASTAnalysis(`
96+
const { warnings } = new AstAnalyser().analyse(`
9597
const role = "ROLE_ADMIN⁦" // Dangerous control char;
9698
`);
9799

@@ -100,8 +102,8 @@ test("should detect 'trojan-source' when there is one unsafe unicode control cha
100102
assert.deepEqual(warnings[0].value, "trojan-source");
101103
});
102104

103-
test("should detect 'trojan-source' when there is atleast one unsafe unicode control char", async() => {
104-
const { warnings } = await runASTAnalysisOnFile(
105+
test("should detect 'trojan-source' when there is atleast one unsafe unicode control char", () => {
106+
const { warnings } = new AstAnalyser().analyseFileSync(
105107
fileURLToPath(new URL("unsafe-unicode-chars.js", FIXTURE_URL))
106108
);
107109

0 commit comments

Comments
 (0)