Skip to content

Commit

Permalink
fix(isRequire): do not resolve CallExpr (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
fraxken authored Jan 14, 2024
1 parent a92a8df commit 8d8abe0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/probes/isRequire.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
import { ProbeSignals } from "../ProbeRunner.js";

function validateNodeRequire(node, { tracer }) {
const id = getCallExpressionIdentifier(node);
const id = getCallExpressionIdentifier(node, {
resolveCallExpression: false
});
if (id === null) {
return [false];
}
Expand Down
38 changes: 38 additions & 0 deletions test/issues/177-wrongUnsafeRequire.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Import Node.js Dependencies
import { test } from "node:test";
import assert from "node:assert";

// Import Internal Dependencies
import { runASTAnalysis } from "../../index.js";

/**
* @see https://github.com/NodeSecure/js-x-ray/issues/177
*/
test("should detect unsafe-import and unsafe-statement", () => {
const { warnings, dependencies } = runASTAnalysis(`const help = require('help-me')({
dir: path.join(__dirname, 'help'),
ext: '.txt'
})`);

assert.strictEqual(warnings.length, 0);
assert.ok(dependencies.has("help-me"));
const dependency = dependencies.get("help-me");

assert.deepEqual(
dependency,
{
unsafe: false,
inTry: false,
location: {
end: {
column: 31,
line: 1
},
start: {
column: 13,
line: 1
}
}
}
);
});
2 changes: 1 addition & 1 deletion test/issues/179-UnsafeEvalRequire.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { runASTAnalysis } from "../../index.js";
* @see https://github.com/NodeSecure/js-x-ray/issues/179
*/
// CONSTANTS
const kIncriminedCodeSample = `const stream = eval('require')('stream');`;
const kIncriminedCodeSample = "const stream = eval('require')('stream');";
const kWarningUnsafeImport = "unsafe-import";
const kWarningUnsafeStatement = "unsafe-stmt";

Expand Down

0 comments on commit 8d8abe0

Please sign in to comment.