Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55f2f86

Browse files
committed
limit the search of state-pairs to the ones that are reachable within the given length
1 parent c4d7533 commit 55f2f86

4 files changed

Lines changed: 24 additions & 2 deletions

File tree

javascript/ql/src/Performance/ReDoS.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,18 +799,20 @@ string concretise(Trace t) {
799799
* a path from `r` back to `(fork, fork)` with `rem` steps.
800800
*/
801801
predicate isReachableFromFork(State fork, StatePair r, Trace w, int rem) {
802+
// base case
802803
exists(InputSymbol s1, InputSymbol s2, State q1, State q2 |
803804
isFork(fork, s1, s2, q1, q2) and
804805
r = MkStatePair(q1, q2) and
805806
w = Step(s1, s2, Nil()) and
806807
rem = statePairDist(r, MkStatePair(fork, fork))
807808
)
808809
or
810+
// recursive case
809811
exists(StatePair p, Trace v, InputSymbol s1, InputSymbol s2 |
810812
isReachableFromFork(fork, p, v, rem + 1) and
811813
step(p, s1, s2, r) and
812814
w = Step(s1, s2, v) and
813-
rem > 0
815+
rem >= statePairDist(r, MkStatePair(fork, fork))
814816
)
815817
}
816818

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,8 @@
263263
| tst.js:251:18:251:19 | A* | it can start matching anywhere |
264264
| tst.js:251:18:251:19 | A* | it can start matching anywhere after the start of the preceeding 'A*' |
265265
| tst.js:260:14:260:21 | (\\n\\s*)+ | it can start matching anywhere |
266+
| tst.js:266:14:266:91 | (\\w*foobarbaz\\w*foobarbaz\\w*foobarbaz\\w*foobarbaz\\s*foobarbaz\\d*foobarbaz\\w*)+ | it can start matching anywhere |
267+
| tst.js:266:15:266:17 | \\w* | it can start matching anywhere |
268+
| tst.js:269:14:269:116 | (.thisisagoddamnlongstringforstresstestingthequery\|\\sthisisagoddamnlongstringforstresstestingthequery)* | it can start matching anywhere |
269+
| tst.js:272:14:272:77 | (thisisagoddamnlongstringforstresstestingthequery\|this\\w+query)* | it can start matching anywhere |
270+
| tst.js:275:15:275:117 | (thisisagoddamnlongstringforstresstestingthequery\|imanotherbutunrelatedstringcomparedtotheotherstring)* | it can start matching anywhere |

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,6 @@
113113
| tst.js:254:17:254:21 | [^>]+ | This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '='. |
114114
| tst.js:257:16:257:21 | [^>a]+ | This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '='. |
115115
| tst.js:260:17:260:19 | \\s* | This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '\\n'. |
116+
| tst.js:266:87:266:89 | \\w* | This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '0foobarbazfoobarbazfoobarbazfoobarbazfoobarbazfoobarbaz'. |
117+
| tst.js:269:14:269:116 | (.thisisagoddamnlongstringforstresstestingthequery\|\\sthisisagoddamnlongstringforstresstestingthequery)* | This part of the regular expression may cause exponential backtracking on strings containing many repetitions of ' thisisagoddamnlongstringforstresstestingthequery'. |
118+
| tst.js:272:14:272:77 | (thisisagoddamnlongstringforstresstestingthequery\|this\\w+query)* | This part of the regular expression may cause exponential backtracking on strings containing many repetitions of 'thisisagoddamnlongstringforstresstestingthequery'. |

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,16 @@ var bad57 = /^([^>a]+)*(>|$)/;
260260
var bad58 = /(\n\s*)+$/;
261261

262262
// GOOD
263-
var good26 = /([^\\\]]+)*/
263+
var good26 = /([^\\\]]+)*/
264+
265+
// NOT GOOD
266+
var bad59 = /(\w*foobarbaz\w*foobarbaz\w*foobarbaz\w*foobarbaz\s*foobarbaz\d*foobarbaz\w*)+-/;
267+
268+
// NOT GOOD
269+
var bad60 = /(.thisisagoddamnlongstringforstresstestingthequery|\sthisisagoddamnlongstringforstresstestingthequery)*-/
270+
271+
// NOT GOOD
272+
var bad61 = /(thisisagoddamnlongstringforstresstestingthequery|this\w+query)*-/
273+
274+
// GOOD
275+
var good27 = /(thisisagoddamnlongstringforstresstestingthequery|imanotherbutunrelatedstringcomparedtotheotherstring)*-/

0 commit comments

Comments
 (0)