Skip to content

Commit

Permalink
refacto probesOptions ASTAnalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
tchapacan committed Mar 17, 2024
1 parent 8501003 commit 1f4f421
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 50 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ function runASTAnalysis(
) {
const {
customParser = new JsSourceParser(),
customProbe = [],
isReplacing = false,
customProbes = [],
skipDefaultProbes = false,
...opts
} = options;

Expand All @@ -25,8 +25,8 @@ async function runASTAnalysisOnFile(
) {
const {
customParser = new JsSourceParser(),
customProbe = [],
isReplacing = false,
customProbes = [],
skipDefaultProbes = false,
...opts
} = options;

Expand Down
26 changes: 9 additions & 17 deletions src/AstAnalyser.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,16 @@ import { JsSourceParser } from "./JsSourceParser.js";
export class AstAnalyser {
/**
* @constructor
* @param options
* @param {object} [options={}]
* @param {JsSourceParser} [options.customParser]
* @param {Array<object>} [options.customProbes]
* @param {boolean} [options.skipDefaultProbes=false]
*/
constructor(options = {}) {
if (options.customParser !== undefined) {
assert(options.customParser instanceof JsSourceParser || typeof options.customParser === "object",
`customParser must be an instance of JsSourceParser or an object`);
}
if (options.customProbe !== undefined) {
assert(Array.isArray(options.customProbe), `customProbe must be an array`);
}
if (options.isReplacing !== undefined) {
assert(typeof options.isReplacing === "boolean", `isReplacing must be a boolean`);
}

this.parser = options.customParser || new JsSourceParser();
this.options = {
isReplacing: options.isReplacing || false,
customProbe: options.customProbe || []
this.parser = options.customParser ?? new JsSourceParser();
this.probesOptions = {
customProbes: options.customProbes ?? [],
skipDefaultProbes: options.skipDefaultProbes ?? false
};
}

Expand All @@ -47,7 +39,7 @@ export class AstAnalyser {
isEcmaScriptModule: Boolean(module)
});

const source = new SourceFile(str, this.options);
const source = new SourceFile(str, this.probesOptions);

// we walk each AST Nodes, this is a purely synchronous I/O
walk(body, {
Expand Down
6 changes: 3 additions & 3 deletions src/SourceFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ export class SourceFile {
encodedLiterals = new Map();
warnings = [];

constructor(sourceCodeString, options = {}) {
constructor(sourceCodeString, probesOptions = {}) {
this.tracer = new VariableTracer()
.enableDefaultTracing()
.trace("crypto.createHash", {
followConsecutiveAssignment: true, moduleName: "crypto"
});

let probes = ProbeRunner.Defaults;
if (Array.isArray(options.customProbe) && options.customProbe.length > 0) {
probes = options.isReplacing === true ? options.customProbe : [...probes, ...options.customProbe];
if (Array.isArray(probesOptions.customProbes) && probesOptions.customProbes.length > 0) {
probes = probesOptions.skipDefaultProbes === true ? probesOptions.customProbes : [...probes, ...probesOptions.customProbes];
}
this.probesRunner = new ProbeRunner(this, probes);

Expand Down
22 changes: 2 additions & 20 deletions test/AstAnalyser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,26 +240,8 @@ describe("AstAnalyser", (t) => {
it("should instantiate with correct default ASTOptions", () => {
const analyser = new AstAnalyser();
assert(analyser.parser instanceof JsSourceParser || typeof analyser.parser.customParser === "object");
assert.deepStrictEqual(analyser.options.customProbe, []);
assert.strictEqual(analyser.options.isReplacing, false);
});

it("should throw an error if customParser is not an instance of JsSourceParser", () => {
assert.throws(() => {
new AstAnalyser({ customParser: "new JsSourceParser()" });
}, AssertionError);
});

it("should throw an error if customProbe is not an array", () => {
assert.throws(() => {
new AstAnalyser({ customProbe: "notArray" });
}, AssertionError);
});

it("should throw an error if isReplacing is not a boolean", () => {
assert.throws(() => {
new AstAnalyser({ isReplacing: "false" });
}, AssertionError);
assert.deepStrictEqual(analyser.probesOptions.customProbes, []);
assert.strictEqual(analyser.probesOptions.skipDefaultProbes, false);
});
});
});
Expand Down
22 changes: 18 additions & 4 deletions test/issues/221-inject-custom-probes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const customProbes = [
];

test("should append to list of probes (default)", () => {
const analyser = new AstAnalyser({ parser: new JsSourceParser(), customProbe: customProbes });
const analyser = new AstAnalyser({ customParser: new JsSourceParser(), customProbes });
const result = analyser.analyse(kIncriminedCodeSample);

assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
Expand All @@ -46,17 +46,27 @@ test("should append to list of probes (default)", () => {
});

test("should replace list of probes", () => {
const analyser = new AstAnalyser({ parser: new JsSourceParser(), customProbe: customProbes, isReplacing: true });
const analyser = new AstAnalyser({
parser: new JsSourceParser(),
customProbes,
skipDefaultProbes: true
});
const result = analyser.analyse(kIncriminedCodeSample);

console.log(analyser);

assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
assert.equal(result.warnings.length, 1);
});

test("should append list of probes using runASTAnalysis", () => {
const result = runASTAnalysis(
kIncriminedCodeSample,
{ parser: new JsSourceParser(), customProbe: customProbes, isReplacing: false }
{
parser: new JsSourceParser(),
customProbes,
skipDefaultProbes: false
}
);

assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
Expand All @@ -68,7 +78,11 @@ test("should append list of probes using runASTAnalysis", () => {
test("should replace list of probes using runASTAnalysis", () => {
const result = runASTAnalysis(
kIncriminedCodeSample,
{ parser: new JsSourceParser(), customProbe: customProbes, isReplacing: true }
{
parser: new JsSourceParser(),
customProbes,
skipDefaultProbes: true
}
);

assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
Expand Down
4 changes: 2 additions & 2 deletions types/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ interface RuntimeAnalyzerOptions {
/**
* @default []
*/
customProbe?: Probe[] | null;
customProbes?: Probe[] | null;
/**
* @default false
*/
isReplacing?: boolean;
skipDefaultProbes?: boolean;
}

type RuntimeOptions = RuntimeAnalyzerOptions & (RuntimeDefaultOptions | RuntimeFileOptions);
Expand Down

0 comments on commit 1f4f421

Please sign in to comment.