Skip to content

Commit 24e84b2

Browse files
author
Adam Baldwin
committed
updated to check for new RegExp too
1 parent 005919a commit 24e84b2

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

rules/detect-unsafe-regex.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var safe = require('safe-regex');
22
/**
33
* Check if the regex is evil or not using the safe-regex module
4-
* @author Adam Baldwin
4+
* @author Adam Baldwin
55
*/
66

77
//------------------------------------------------------------------------------
@@ -11,9 +11,6 @@ var safe = require('safe-regex');
1111
module.exports = function(context) {
1212

1313
"use strict";
14-
var getSource = function(token) {
15-
return token.loc.start.line + ': ' + context.getSourceLines().slice(token.loc.start.line - 1, token.loc.end.line).join('\n\t');
16-
}
1714

1815
return {
1916
"Literal": function(node) {
@@ -23,7 +20,14 @@ module.exports = function(context) {
2320

2421
if (nodeType === "RegularExpression") {
2522
if (!safe(nodeValue)) {
26-
context.report(node, "Unsafe Regular Expression\n" + getSource(token));
23+
context.report(node, "Unsafe Regular Expression");
24+
}
25+
}
26+
},
27+
"NewExpression": function(node) {
28+
if (node.callee.name == "RegExp" && node.arguments && node.arguments.length > 0 && node.arguments[0].type == "Literal") {
29+
if (!safe(node.arguments[0].value)) {
30+
context.report(node, "Unsafe Regular Expression (new RegExp)");
2731
}
2832
}
2933
}

0 commit comments

Comments
 (0)