Skip to content

Commit

Permalink
append/replace custom probe in ASTAnalyzer class
Browse files Browse the repository at this point in the history
  • Loading branch information
tchapacan committed Mar 10, 2024
1 parent 513231f commit bec2abb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/AstAnalyser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ export class AstAnalyser {
* @constructor
* @param { SourceParser } [parser]
*/
constructor(parser = new JsSourceParser(), customProbes = []) {
constructor(parser = new JsSourceParser(), customProbes = [], mergeMode = "append") {
this.parser = parser;
this.customProbes = customProbes;
this.mergeMode = mergeMode;
}

analyse(str, options = Object.create(null)) {
Expand All @@ -32,7 +33,7 @@ export class AstAnalyser {
isEcmaScriptModule: Boolean(module)
});

const source = new SourceFile(str, this.customProbes);
const source = new SourceFile(str, this.customProbes, this.mergeMode);

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

constructor(sourceCodeString, customProbes = []) {
constructor(sourceCodeString, customProbes = [], mergeMode = "append") {
this.tracer = new VariableTracer()
.enableDefaultTracing()
.trace("crypto.createHash", {
followConsecutiveAssignment: true, moduleName: "crypto"
});

let mergedProbes;
if (Array.isArray(customProbes) && customProbes.length > 0) {
this.probesRunner = new ProbeRunner(this, customProbes);
mergedProbes = mergeMode === "replace" ? customProbes : [...ProbeRunner.Defaults, ...customProbes];
}
else {
this.probesRunner = new ProbeRunner(this);
mergedProbes = ProbeRunner.Defaults;
}
this.probesRunner = new ProbeRunner(this, mergedProbes);

if (trojan.verify(sourceCodeString)) {
this.addWarning("obfuscated-code", "trojan-source");
Expand Down
49 changes: 32 additions & 17 deletions test/issues/221-inject-custom-probes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,45 @@ import { ProbeSignals } from "../../src/ProbeRunner.js";
* @see https://github.com/NodeSecure/js-x-ray/issues/221
*/
// CONSTANTS
const kIncriminedCodeSample = "const danger = 'danger';";
const kIncriminedCodeSample = "const danger = 'danger'; const stream = eval('require')('stream');";
const kWarningUnsafeDanger = "unsafe-danger";
const kWarningUnsafeImport = "unsafe-import";
const kWarningUnsafeStmt = "unsafe-stmt";

test("should detect a custom probe alert unsafe-danger", () => {
const customProbes = [
{
name: "customProbeUnsafeDanger",
validateNode: (node, sourceFile) => [true]
,
main: (node, options) => {
const { sourceFile, data: calleeName } = options;
if (node.declarations[0].init.value === "danger") {
sourceFile.addWarning("unsafe-danger", calleeName, node.loc);

return ProbeSignals.Skip;
}

return null;
const customProbes = [
{
name: "customProbeUnsafeDanger",
validateNode: (node, sourceFile) => {
return [node.type === "VariableDeclaration" && node.declarations[0].init.value === "danger"];
}
,
main: (node, options) => {
const { sourceFile, data: calleeName } = options;
if (node.declarations[0].init.value === "danger") {
sourceFile.addWarning("unsafe-danger", calleeName, node.loc);

return ProbeSignals.Skip;
}

return null;
}
];
}
];

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

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

test("should replace list of probes", () => {
const analyser = new AstAnalyser(new JsSourceParser(), customProbes, "replace");
const result = analyser.analyse(kIncriminedCodeSample);

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

0 comments on commit bec2abb

Please sign in to comment.