Skip to content

Commit ce8cc23

Browse files
committed
improve precision of intersect
1 parent 44571ff commit ce8cc23

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

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

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ CharClass getCanonicalCharClass(RegExpTerm term) {
177177

178178
/**
179179
* Holds if `a` and `b` are input symbols from the same regexp.
180-
* (And not a `Dot()`, `Any()` or `Epsilon()`)
181180
*/
182181
private predicate sharesRoot(TInputSymbol a, TInputSymbol b) {
183182
exists(RegExpRoot root |
@@ -190,13 +189,10 @@ private predicate sharesRoot(TInputSymbol a, TInputSymbol b) {
190189
* Holds if the `a` is an input symbol from a regexp that has root `root`.
191190
*/
192191
private predicate belongsTo(TInputSymbol a, RegExpRoot root) {
193-
exists(RegExpTerm term | getRoot(term) = root |
194-
a = Char(term.(RegexpCharacterConstant).getValue().charAt(_))
195-
)
196-
or
197-
exists(string str, RegExpTerm term | a = CharClass(str) |
198-
term.getRawValue() = str and
199-
getRoot(term) = root
192+
exists(State s | getRoot(s.getRepr()) = root |
193+
delta(s, a, _)
194+
or
195+
delta(_, a, s)
200196
)
201197
}
202198

@@ -675,37 +671,28 @@ private string getAOverlapBetweenCharacterClasses(CharacterClass c, CharacterCla
675671
* Gets a character that is represented by both `c` and `d`.
676672
*/
677673
string intersect(InputSymbol c, InputSymbol d) {
678-
c = Char(result) and
679-
d = getAnInputSymbolMatching(result) and
680-
(
681-
sharesRoot(c, d)
682-
or
683-
d = Dot()
684-
or
685-
d = Any()
686-
)
687-
or
688-
result = getMinOverlapBetweenCharacterClasses(c, d)
689-
or
690-
result = c.(CharacterClass).choose() and
674+
(sharesRoot(c, d) or [c, d] = Any()) and
691675
(
692-
d = c
676+
c = Char(result) and
677+
d = getAnInputSymbolMatching(result)
693678
or
694-
d = Dot() and
695-
not (result = "\n" or result = "\r")
679+
result = getMinOverlapBetweenCharacterClasses(c, d)
696680
or
697-
d = Any()
698-
)
699-
or
700-
c = Dot() and
701-
(
702-
d = Dot() and result = "a"
681+
result = c.(CharacterClass).choose() and
682+
(
683+
d = c
684+
or
685+
d = Dot() and
686+
not (result = "\n" or result = "\r")
687+
or
688+
d = Any()
689+
)
703690
or
704-
d = Any() and result = "a"
691+
(c = Dot() or c = Any()) and
692+
(d = Dot() or d = Any()) and
693+
result = "a"
705694
)
706695
or
707-
c = Any() and d = Any() and result = "a"
708-
or
709696
result = intersect(d, c)
710697
}
711698

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@
349349
| tst.js:23:33:23:40 | [\\s\\S]*? | Strings starting with '(*(*' and with many repetitions of '(*' can start matching anywhere after the start of the preceeding (?:[\\s\\S]*?\\(\\*[\\s\\S]*?\\*\\))* |
350350
| tst.js:23:47:23:54 | [\\s\\S]*? | Strings starting with '(*' and with many repetitions of '(*a' can start matching anywhere after the start of the preceeding (?:[\\s\\S]*?\\(\\*[\\s\\S]*?\\*\\))* |
351351
| tst.js:28:24:28:25 | .* | Strings starting with '!\|' and with many repetitions of '\|' can start matching anywhere after the start of the preceeding .* |
352+
| tst.js:28:59:28:60 | .* | Strings starting with '!\|\\n-\|\\n\|' and with many repetitions of '\|' can start matching anywhere after the start of the preceeding .* |
352353
| tst.js:31:23:31:24 | .* | Strings starting with '!\|' and with many repetitions of '\|' can start matching anywhere after the start of the preceeding .* |
353354
| tst.js:31:54:31:55 | .* | Strings starting with '!\|\\n-\|\\n' and with many repetitions of '\|\\n\|' can start matching anywhere after the start of the preceeding .* |
354355
| tst.js:31:58:31:59 | .* | Strings starting with '!\|\\n-\|\\n\|' and with many repetitions of '\|' can start matching anywhere after the start of the preceeding .* |

0 commit comments

Comments
 (0)