Skip to content

Commit

Permalink
feat: trace assignation in ObjectExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-michelet committed May 16, 2024
1 parent a1972cf commit c5ed7b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/probes/isRegexObject.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import Third-party Dependencies
import { isLiteralRegex } from "@nodesecure/estree-ast-utils";
import { getMemberExpressionIdentifier, isLiteralRegex } from "@nodesecure/estree-ast-utils";
import safeRegex from "safe-regex";

/**
Expand Down Expand Up @@ -34,15 +34,24 @@ function main(node, options) {
}

function isRegexConstructor(node, tracer) {
if (node.type !== "NewExpression" || node.callee.type !== "Identifier") {
if (node.type !== "NewExpression") {
return false;
}

if (node.callee.name === "RegExp") {
let name = "";
if (node.callee.type === "Identifier") {
name = node.callee.name;
}
else {
name = [...getMemberExpressionIdentifier(node.callee)].join(".");
}

if (name === "RegExp") {
return true;
}

const data = tracer.getDataFromIdentifier(node.callee.name);
const data = tracer.getDataFromIdentifier(name);


return data?.superClassMemory.includes("RegExp");
}
Expand Down
7 changes: 7 additions & 0 deletions test/probes/isRegexObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ test("should throw a 'unsafe-regex' warning because the given RegExp Object is u
class MyRegExp2 extends MyRegExp {}
const d = MyRegExp2;
new d('(a+){10}');
`,
`
class MyRegExp extends RegExp {}
const y = { d: { z: MyRegExp } }
new y.d.z('(a+){10}');
`
];

Expand Down

0 comments on commit c5ed7b9

Please sign in to comment.