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

Skip to content

Commit 1605d36

Browse files
Refine polynomial redos sources to exclude length limited methods
1 parent 04edc10 commit 1605d36

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

java/ql/lib/semmle/code/java/security/performance/PolynomialReDosQuery.qll

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** Definitions and configurations for the Polynomial ReDos query */
1+
/** Definitions and configurations for the Polynomial ReDoS query */
22

33
import semmle.code.java.security.performance.SuperlinearBackTracking
44
import semmle.code.java.dataflow.DataFlow
@@ -16,17 +16,40 @@ class PolynomialRedosSink extends DataFlow::Node {
1616
RegExpTerm getRegExp() { result.getParent() = reg }
1717
}
1818

19+
/**
20+
* A method whose result typically has a limited length,
21+
* such as HTTP headers, and values derrived from them.
22+
*/
23+
private class LengthRestrictedMethod extends Method {
24+
LengthRestrictedMethod() {
25+
this.getName().toLowerCase().matches(["%header%", "%requesturi%", "%requesturl%", "%cookie%"])
26+
or
27+
this.getDeclaringType().getName().toLowerCase().matches("%cookie%") and
28+
this.getName().matches("get%")
29+
or
30+
this.getDeclaringType().getName().toLowerCase().matches("%request%") and
31+
this.getName().toLowerCase().matches(["%get%path%", "get%user%", "%querystring%"])
32+
}
33+
}
34+
1935
/** A configuration for Polynomial ReDoS queries. */
2036
class PolynomialRedosConfig extends TaintTracking::Configuration {
2137
PolynomialRedosConfig() { this = "PolynomialRedosConfig" }
2238

2339
override predicate isSource(DataFlow::Node src) { src instanceof RemoteFlowSource }
2440

2541
override predicate isSink(DataFlow::Node sink) { sink instanceof PolynomialRedosSink }
42+
43+
override predicate isSanitizer(DataFlow::Node node) {
44+
super.isSanitizer(node) or
45+
node.getType() instanceof PrimitiveType or
46+
node.getType() instanceof BoxedType or
47+
node.asExpr().(MethodAccess).getMethod() instanceof LengthRestrictedMethod
48+
}
2649
}
2750

2851
/** Holds if there is flow from `source` to `sink` that is matched against the regexp term `regexp` that is vulnerable to Polynomial ReDoS. */
29-
predicate hasPolynomialReDosResult(
52+
predicate hasPolynomialReDoSResult(
3053
DataFlow::PathNode source, DataFlow::PathNode sink, PolynomialBackTrackingTerm regexp
3154
) {
3255
any(PolynomialRedosConfig config).hasFlowPath(source, sink) and

java/ql/src/Security/CWE/CWE-730/PolynomialReDoS.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
*/
1313

1414
import java
15-
import semmle.code.java.security.performance.PolynomialReDosQuery
15+
import semmle.code.java.security.performance.PolynomialReDoSQuery
1616
import DataFlow::PathGraph
1717

1818
from DataFlow::PathNode source, DataFlow::PathNode sink, PolynomialBackTrackingTerm regexp
19-
where hasPolynomialReDosResult(source, sink, regexp)
19+
where hasPolynomialReDoSResult(source, sink, regexp)
2020
select sink, source, sink,
2121
"This $@ that depends on $@ may run slow on strings " + regexp.getPrefixMessage() +
2222
"with many repetitions of '" + regexp.getPumpString() + "'.", regexp, "regular expression",

java/ql/test/query-tests/security/CWE-730/PolyRedosTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,13 @@ void test5(HttpServletRequest request) {
7272
p3.asMatchPredicate().test(tainted);
7373
p4.asPredicate().test(tainted); // $ hasPolyRedos
7474
}
75+
76+
void test6(HttpServletRequest request) {
77+
Pattern p = Pattern.compile("^a*a*$");
78+
79+
p.matcher(request.getParameter("inp")).matches(); // $ hasPolyRedos
80+
p.matcher(request.getHeader("If-None-Match")).matches();
81+
p.matcher(request.getRequestURI()).matches();
82+
p.matcher(request.getCookies()[0].getName()).matches();
83+
}
7584
}

java/ql/test/query-tests/security/CWE-730/PolynomialReDoS.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import java
22
import TestUtilities.InlineExpectationsTest
3-
import semmle.code.java.security.performance.PolynomialReDosQuery
3+
import semmle.code.java.security.performance.PolynomialReDoSQuery
44

55
class HasPolyRedos extends InlineExpectationsTest {
66
HasPolyRedos() { this = "HasPolyRedos" }
@@ -10,7 +10,7 @@ class HasPolyRedos extends InlineExpectationsTest {
1010
override predicate hasActualResult(Location location, string element, string tag, string value) {
1111
tag = "hasPolyRedos" and
1212
exists(DataFlow::PathNode source, DataFlow::PathNode sink, PolynomialBackTrackingTerm regexp |
13-
hasPolynomialReDosResult(source, sink, regexp) and
13+
hasPolynomialReDoSResult(source, sink, regexp) and
1414
location = sink.getNode().getLocation() and
1515
element = sink.getNode().toString() and
1616
value = ""

0 commit comments

Comments
 (0)