Skip to content

Commit 0115836

Browse files
committed
start refacto options param
1 parent bec2abb commit 0115836

File tree

7 files changed

+74
-24
lines changed

7 files changed

+74
-24
lines changed

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ function runASTAnalysis(
99
) {
1010
const {
1111
customParser = new JsSourceParser(),
12+
astOptions = { isReplacing: false, customProbe: [] },
1213
...opts
1314
} = options;
1415

15-
const analyser = new AstAnalyser(customParser);
16+
const analyser = new AstAnalyser(customParser, options.astOptions);
1617

1718
return analyser.analyse(str, opts);
1819
}
@@ -23,10 +24,11 @@ async function runASTAnalysisOnFile(
2324
) {
2425
const {
2526
customParser = new JsSourceParser(),
27+
astOptions = { isReplacing: false, customProbe: [] },
2628
...opts
2729
} = options;
2830

29-
const analyser = new AstAnalyser(customParser);
31+
const analyser = new AstAnalyser(customParser, options.astOptions);
3032

3133
return analyser.analyseFile(pathToFile, opts);
3234
}

src/AstAnalyser.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import { JsSourceParser } from "./JsSourceParser.js";
1414
export class AstAnalyser {
1515
/**
1616
* @constructor
17-
* @param { SourceParser } [parser]
17+
* @param {SourceParser} [parser]
18+
* @param astOptions
1819
*/
19-
constructor(parser = new JsSourceParser(), customProbes = [], mergeMode = "append") {
20+
constructor(parser = new JsSourceParser(), astOptions = { isReplacing: false, customProbe: [] }) {
2021
this.parser = parser;
21-
this.customProbes = customProbes;
22-
this.mergeMode = mergeMode;
22+
this.astOptions = astOptions;
2323
}
2424

2525
analyse(str, options = Object.create(null)) {
@@ -33,7 +33,7 @@ export class AstAnalyser {
3333
isEcmaScriptModule: Boolean(module)
3434
});
3535

36-
const source = new SourceFile(str, this.customProbes, this.mergeMode);
36+
const source = new SourceFile(str, this.astOptions);
3737

3838
// we walk each AST Nodes, this is a purely synchronous I/O
3939
walk(body, {

src/SourceFile.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,18 @@ export class SourceFile {
2020
encodedLiterals = new Map();
2121
warnings = [];
2222

23-
constructor(sourceCodeString, customProbes = [], mergeMode = "append") {
23+
constructor(sourceCodeString, astOptions = { isReplacing: false, customProbes: [] }) {
2424
this.tracer = new VariableTracer()
2525
.enableDefaultTracing()
2626
.trace("crypto.createHash", {
2727
followConsecutiveAssignment: true, moduleName: "crypto"
2828
});
2929

30-
let mergedProbes;
31-
if (Array.isArray(customProbes) && customProbes.length > 0) {
32-
mergedProbes = mergeMode === "replace" ? customProbes : [...ProbeRunner.Defaults, ...customProbes];
30+
let probes = ProbeRunner.Defaults;
31+
if (Array.isArray(astOptions.customProbes) && astOptions.customProbes.length > 0) {
32+
probes = astOptions.isReplacing === true ? astOptions.customProbes : [...probes, ...astOptions.customProbes];
3333
}
34-
else {
35-
mergedProbes = ProbeRunner.Defaults;
36-
}
37-
this.probesRunner = new ProbeRunner(this, mergedProbes);
34+
this.probesRunner = new ProbeRunner(this, probes);
3835

3936
if (trojan.verify(sourceCodeString)) {
4037
this.addWarning("obfuscated-code", "trojan-source");

test/AstAnalyser.spec.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ describe("AstAnalyser", (t) => {
206206
const preparedSource = getAnalyser().prepareSource(`
207207
<!--
208208
// == fake comment == //
209-
209+
210210
const yo = 5;
211211
//-->
212212
`, {
@@ -236,6 +236,12 @@ describe("AstAnalyser", (t) => {
236236
assert.deepEqual([...result.dependencies.keys()], []);
237237
});
238238
});
239+
240+
it("should instantiate with correct default ASTOptions", () => {
241+
const analyser = new AstAnalyser();
242+
assert.strictEqual(analyser.astOptions.isReplacing, false);
243+
assert.deepStrictEqual(analyser.astOptions.customProbe, []);
244+
});
239245
});
240246
});
241247

test/issues/221-inject-custom-probes.spec.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import assert from "node:assert";
66
import { JsSourceParser } from "../../src/JsSourceParser.js";
77
import { AstAnalyser } from "../../src/AstAnalyser.js";
88
import { ProbeSignals } from "../../src/ProbeRunner.js";
9+
import { runASTAnalysis } from "../../index.js";
910

1011
/**
1112
* @see https://github.com/NodeSecure/js-x-ray/issues/221
@@ -37,7 +38,7 @@ const customProbes = [
3738
];
3839

3940
test("should append to list of probes (default)", () => {
40-
const analyser = new AstAnalyser(new JsSourceParser(), customProbes);
41+
const analyser = new AstAnalyser(new JsSourceParser(), { customProbes });
4142
const result = analyser.analyse(kIncriminedCodeSample);
4243

4344
assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
@@ -47,9 +48,26 @@ test("should append to list of probes (default)", () => {
4748
});
4849

4950
test("should replace list of probes", () => {
50-
const analyser = new AstAnalyser(new JsSourceParser(), customProbes, "replace");
51+
const analyser = new AstAnalyser(new JsSourceParser(), { customProbes, isReplacing: true });
5152
const result = analyser.analyse(kIncriminedCodeSample);
5253

5354
assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
5455
assert.equal(result.warnings.length, 1);
5556
});
57+
58+
59+
test("should append list of probes using runASTAnalysis", () => {
60+
const result = runASTAnalysis(kIncriminedCodeSample, { astOptions: { isReplacing: false, customProbes } });
61+
62+
assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
63+
assert.equal(result.warnings[1].kind, kWarningUnsafeImport);
64+
assert.equal(result.warnings[2].kind, kWarningUnsafeStmt);
65+
assert.equal(result.warnings.length, 3);
66+
});
67+
68+
test("should replace list of probes using runASTAnalysis", () => {
69+
const result = runASTAnalysis(kIncriminedCodeSample, { astOptions: { isReplacing: true, customProbes } });
70+
71+
assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
72+
assert.equal(result.warnings.length, 1);
73+
});

test/utils/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ export function getSastAnalysis(
3838
return this.sourceFile.dependencies;
3939
},
4040
execute(body) {
41-
const probes = Array.isArray(probe) ? probe : [probe];
42-
const probeRunner = new ProbeRunner(this.sourceFile, probes);
41+
const probeRunner = new ProbeRunner(this.sourceFile, [probe]);
4342
const self = this;
4443

4544
walk(body, {

types/api.d.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Warning } from "./warnings.js";
22
import { Statement } from "meriyah/dist/src/estree.js";
3+
import {validateFunctionName} from "meriyah/dist/src/common";
34

45
export {
56
AstAnalyser,
@@ -34,6 +35,17 @@ interface Dependency {
3435
location?: null | SourceLocation;
3536
}
3637

38+
interface RootOptions {
39+
/**
40+
* @default ASTOptions
41+
*/
42+
ASTOptions?: ASTOptions;
43+
/**
44+
* @default RuntimeOptions
45+
*/
46+
RuntimeOptions?: RuntimeOptions;
47+
}
48+
3749
interface RuntimeOptions {
3850
/**
3951
* @default true
@@ -47,10 +59,26 @@ interface RuntimeOptions {
4759
* @default false
4860
*/
4961
removeHTMLComments?: boolean;
50-
62+
5163
customParser?: SourceParser;
5264
}
5365

66+
interface ASTOptions {
67+
/**
68+
* @default false
69+
*/
70+
isReplacing?: boolean;
71+
/**
72+
* @default []
73+
*/
74+
customParser?: Probe[] | null;
75+
}
76+
77+
interface Probe {
78+
validate: Function[] | Function;
79+
main: Function[] | Function;
80+
}
81+
5482
interface Report {
5583
dependencies: Map<string, Dependency>;
5684
warnings: Warning[];
@@ -78,10 +106,10 @@ interface SourceParser {
78106
}
79107

80108
declare class AstAnalyser {
81-
constructor(parser?: SourceParser);
109+
constructor(parser?: SourceParser, astOptions?: ASTOptions);
82110
analyse: (str: string, options?: Omit<RuntimeOptions, "customParser">) => Report;
83111
analyzeFile(pathToFile: string, options?: Omit<RuntimeFileOptions, "customParser">): Promise<ReportOnFile>;
84112
}
85113

86-
declare function runASTAnalysis(str: string, options?: RuntimeOptions): Report;
87-
declare function runASTAnalysisOnFile(pathToFile: string, options?: RuntimeFileOptions): Promise<ReportOnFile>;
114+
declare function runASTAnalysis(str: string, options?: RootOptions): Report;
115+
declare function runASTAnalysisOnFile(pathToFile: string, options?: RootOptions): Promise<ReportOnFile>;

0 commit comments

Comments
 (0)