Skip to content

Commit 5255a6d

Browse files
committed
kickstart custom probe as param
1 parent 9e4ad1f commit 5255a6d

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/AstAnalyser.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ export class AstAnalyser {
1616
* @constructor
1717
* @param { SourceParser } [parser]
1818
*/
19-
constructor(parser = new JsSourceParser()) {
19+
constructor(parser = new JsSourceParser(), customProbes = []) {
2020
this.parser = parser;
21+
this.customProbes = customProbes;
2122
}
2223

2324
analyse(str, options = Object.create(null)) {
@@ -31,7 +32,7 @@ export class AstAnalyser {
3132
isEcmaScriptModule: Boolean(module)
3233
});
3334

34-
const source = new SourceFile(str);
35+
const source = new SourceFile(str, this.customProbes);
3536

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

src/SourceFile.js

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

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

30-
this.probesRunner = new ProbeRunner(this);
30+
if (Array.isArray(customProbes) && customProbes.length > 0) {
31+
this.probesRunner = new ProbeRunner(this, customProbes);
32+
}
33+
else {
34+
this.probesRunner = new ProbeRunner(this);
35+
}
36+
3137
if (trojan.verify(sourceCodeString)) {
3238
this.addWarning("obfuscated-code", "trojan-source");
3339
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Import Node.js Dependencies
2+
import { test } from "node:test";
3+
import assert from "node:assert";
4+
5+
// Import Internal Dependencies
6+
import { JsSourceParser } from "../../src/JsSourceParser.js";
7+
import { AstAnalyser } from "../../src/AstAnalyser.js";
8+
import { ProbeSignals } from "../../src/ProbeRunner.js";
9+
10+
/**
11+
* @see https://github.com/NodeSecure/js-x-ray/issues/221
12+
*/
13+
// CONSTANTS
14+
const kIncriminedCodeSample = "const danger = 'danger';";
15+
const kWarningUnsafeDanger = "unsafe-danger";
16+
17+
test("should detect a custom probe alert unsafe-danger", () => {
18+
const customProbes = [
19+
{
20+
name: "customProbeUnsafeDanger",
21+
validateNode: (node, sourceFile) => [true]
22+
,
23+
main: (node, options) => {
24+
const { sourceFile, data: calleeName } = options;
25+
if (node.declarations[0].init.value === "danger") {
26+
sourceFile.addWarning("unsafe-danger", calleeName, node.loc);
27+
28+
return ProbeSignals.Skip;
29+
}
30+
31+
return null;
32+
}
33+
}
34+
];
35+
36+
const analyser = new AstAnalyser(new JsSourceParser(), customProbes);
37+
const result = analyser.analyse(kIncriminedCodeSample);
38+
39+
assert.equal(result.warnings[0].kind, kWarningUnsafeDanger);
40+
});

test/utils/index.js

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

4445
walk(body, {

0 commit comments

Comments
 (0)