Skip to content

Commit 1a8b6d7

Browse files
committed
recognize ranges without upper bounds
1 parent acaf294 commit 1a8b6d7

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
lines changed

javascript/ql/lib/semmle/javascript/security/performance/ReDoSUtil.qll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,10 @@ private class EffectivelyPlus extends RegExpTerm {
550550
EffectivelyPlus() {
551551
this instanceof RegExpPlus
552552
or
553-
exists(RegExpRange range | range.getLowerBound() = 1 and range.getUpperBound() >= 30 |
553+
exists(RegExpRange range |
554+
range.getLowerBound() = 1 and
555+
(range.getUpperBound() >= 30 or not exists(range.getUpperBound()))
556+
|
554557
this = range
555558
)
556559
}
@@ -564,7 +567,10 @@ private class EffectivelyStar extends RegExpTerm {
564567
EffectivelyStar() {
565568
this instanceof RegExpStar
566569
or
567-
exists(RegExpRange range | range.getLowerBound() = 0 and range.getUpperBound() >= 30 |
570+
exists(RegExpRange range |
571+
range.getLowerBound() = 0 and
572+
(range.getUpperBound() >= 30 or not exists(range.getUpperBound()))
573+
|
568574
this = range
569575
)
570576
}

javascript/ql/test/query-tests/Performance/ReDoS/PolynomialBackTracking.expected

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,10 @@
323323
| regexplib/strings.js:40:3:40:5 | \\w+ | Strings with many repetitions of 'a' can start matching anywhere after the start of the preceeding (\\w+)\\s+\\1 |
324324
| regexplib/strings.js:48:3:48:12 | [^\\.\\?\\!]* | Strings with many repetitions of ' ' can start matching anywhere after the start of the preceeding ([^\\.\\?\\!]*)[\\.\\?\\!] |
325325
| regexplib/strings.js:49:3:49:5 | \\S+ | Strings with many repetitions of '!' can start matching anywhere after the start of the preceeding (\\S+)\\x20{2,}(?=\\S+) |
326-
| regexplib/strings.js:53:25:53:33 | [a-z0-9]+ | Strings with many repetitions of '0' can start matching anywhere after the start of the preceeding [a-z0-9]+ |
326+
| regexplib/strings.js:53:4:53:12 | [a-z0-9]+ | Strings with many repetitions of '0.00' can start matching anywhere after the start of the preceeding [a-z0-9]+ |
327+
| regexplib/strings.js:53:25:53:33 | [a-z0-9]+ | Strings starting with '0' and with many repetitions of '0' can start matching anywhere after the start of the preceeding [a-z0-9]+ |
327328
| regexplib/strings.js:53:44:53:52 | [a-z0-9]+ | Strings with many repetitions of '00' can start matching anywhere after the start of the preceeding [a-z0-9]+ |
328-
| regexplib/strings.js:53:65:53:73 | [a-z0-9]+ | Strings with many repetitions of '0' can start matching anywhere after the start of the preceeding [a-z0-9]+ |
329+
| regexplib/strings.js:53:65:53:73 | [a-z0-9]+ | Strings starting with '0' and with many repetitions of '0' can start matching anywhere after the start of the preceeding [a-z0-9]+ |
329330
| regexplib/strings.js:54:20:54:22 | \\w+ | Strings with many repetitions of 'a' can start matching anywhere after the start of the preceeding (NOT)?(\\s*\\(*)\\s*(\\w+)\\s*(=\|<>\|<\|>\|LIKE\|IN)\\s*(\\(([^\\)]*)\\)\|'([^']*)'\|(-?\\d*\\.?\\d+))(\\s*\\)*\\s*)(AND\|OR)? |
330331
| regexplib/strings.js:56:52:56:53 | .+ | Strings starting with 'PRN.' and with many repetitions of '.' can start matching anywhere after the start of the preceeding .* |
331332
| regexplib/strings.js:57:36:57:38 | .*? | Strings starting with '?se[A' and with many repetitions of '?se[Aa' can start matching anywhere after the start of the preceeding (?s)(?:\\e\\[(?:(\\d+);?)*([A-Za-z])(.*?))(?=\\e\\[\|\\z) |
@@ -532,3 +533,4 @@
532533
| tst.js:404:6:405:7 | (g\|gg)* | Strings with many repetitions of 'g' can start matching anywhere after the start of the preceeding (g\|gg)*h$ |
533534
| tst.js:407:128:407:129 | * | Strings starting with '0/*' and with many repetitions of ' ' can start matching anywhere after the start of the preceeding \\s* |
534535
| tst.js:409:23:409:29 | [\\w.-]* | Strings starting with '//' and with many repetitions of '//' can start matching anywhere after the start of the preceeding (\\/(?:\\/[\\w.-]*)*){0,1}:([\\w.-]+) |
536+
| tst.js:411:15:411:19 | a{1,} | Strings with many repetitions of 'a' can start matching anywhere after the start of the preceeding (a{1,})* |

javascript/ql/test/query-tests/Performance/ReDoS/ReDoS.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
| regexplib/strings.js:19:31:19:57 | [a-z&#230;&#248;&#229;0-9]+ | This part of the regular expression may cause exponential backtracking on strings starting with '#@' and containing many repetitions of '#'. |
7171
| regexplib/strings.js:19:69:19:95 | [a-z&#230;&#248;&#229;0-9]+ | This part of the regular expression may cause exponential backtracking on strings starting with '#@#' and containing many repetitions of '##'. |
7272
| regexplib/strings.js:47:3:47:5 | \\S* | This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '!'. |
73+
| regexplib/strings.js:53:4:53:12 | [a-z0-9]+ | This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '00.'. |
74+
| regexplib/strings.js:53:14:53:24 | [\\-a-z0-9]* | This part of the regular expression may cause exponential backtracking on strings starting with '0' and containing many repetitions of '00.0'. |
7375
| regexplib/strings.js:57:17:57:19 | \\d+ | This part of the regular expression may cause exponential backtracking on strings starting with '?se[' and containing many repetitions of '9'. |
7476
| regexplib/strings.js:81:17:81:19 | \\d+ | This part of the regular expression may cause exponential backtracking on strings starting with '?se[' and containing many repetitions of '9'. |
7577
| regexplib/strings.js:91:3:91:5 | \\S* | This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '!'. |
@@ -196,3 +198,4 @@
196198
| tst.js:400:6:401:1 | (e\|ee)* | This part of the regular expression may cause exponential backtracking on strings containing many repetitions of 'ee'. |
197199
| tst.js:404:6:405:7 | (g\|gg)* | This part of the regular expression may cause exponential backtracking on strings containing many repetitions of 'gg'. |
198200
| tst.js:407:125:407:127 | \\s* | This part of the regular expression may cause exponential backtracking on strings starting with '0/*' and containing many repetitions of ' ;0'. |
201+
| tst.js:411:15:411:19 | a{1,} | This part of the regular expression may cause exponential backtracking on strings containing many repetitions of 'a'. |

javascript/ql/test/query-tests/Performance/ReDoS/tst.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,6 @@ var bad97 = new RegExp(
406406

407407
var bad98 = /^(?:\*\/\*|[a-zA-Z0-9][a-zA-Z0-9!\#\$&\-\^_\.\+]{0,126}\/(?:\*|[a-zA-Z0-9][a-zA-Z0-9!\#\$&\-\^_\.\+]{0,126})(?:\s* *; *[a-zA-Z0-9][a-zA-Z0-9!\#\$&\-\^_\.\+]{0,126}(?:="?[a-zA-Z0-9][a-zA-Z0-9!\#\$&\-\^_\.\+]{0,126}"?)?\s*)*)$/;
408408

409-
var good48 = /(\/(?:\/[\w.-]*)*){0,1}:([\w.-]+)/;
409+
var good48 = /(\/(?:\/[\w.-]*)*){0,1}:([\w.-]+)/;
410+
411+
var bad99 = /(a{1,})*b/;

python/ql/lib/semmle/python/security/performance/ReDoSUtil.qll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,10 @@ private class EffectivelyPlus extends RegExpTerm {
550550
EffectivelyPlus() {
551551
this instanceof RegExpPlus
552552
or
553-
exists(RegExpRange range | range.getLowerBound() = 1 and range.getUpperBound() >= 30 |
553+
exists(RegExpRange range |
554+
range.getLowerBound() = 1 and
555+
(range.getUpperBound() >= 30 or not exists(range.getUpperBound()))
556+
|
554557
this = range
555558
)
556559
}
@@ -564,7 +567,10 @@ private class EffectivelyStar extends RegExpTerm {
564567
EffectivelyStar() {
565568
this instanceof RegExpStar
566569
or
567-
exists(RegExpRange range | range.getLowerBound() = 0 and range.getUpperBound() >= 30 |
570+
exists(RegExpRange range |
571+
range.getLowerBound() = 0 and
572+
(range.getUpperBound() >= 30 or not exists(range.getUpperBound()))
573+
|
568574
this = range
569575
)
570576
}

ruby/ql/lib/codeql/ruby/security/performance/ReDoSUtil.qll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,10 @@ private class EffectivelyPlus extends RegExpTerm {
548548
EffectivelyPlus() {
549549
this instanceof RegExpPlus
550550
or
551-
exists(RegExpRange range | range.getLowerBound() = 1 and range.getUpperBound() >= 30 |
551+
exists(RegExpRange range |
552+
range.getLowerBound() = 1 and
553+
(range.getUpperBound() >= 30 or not exists(range.getUpperBound()))
554+
|
552555
this = range
553556
)
554557
}
@@ -562,7 +565,10 @@ private class EffectivelyStar extends RegExpTerm {
562565
EffectivelyStar() {
563566
this instanceof RegExpStar
564567
or
565-
exists(RegExpRange range | range.getLowerBound() = 0 and range.getUpperBound() >= 30 |
568+
exists(RegExpRange range |
569+
range.getLowerBound() = 0 and
570+
(range.getUpperBound() >= 30 or not exists(range.getUpperBound()))
571+
|
566572
this = range
567573
)
568574
}

0 commit comments

Comments
 (0)