Skip to content

Commit 591b6ef

Browse files
committed
Python: Add ReDoS as identical files from JS
The library specific file is `RegExpTreeView`. The files are recorded as identical via the mapping in `identical-files.json`.
1 parent d2eeaff commit 591b6ef

File tree

9 files changed

+2093
-0
lines changed

9 files changed

+2093
-0
lines changed

config/identical-files.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,5 +448,17 @@
448448
"SensitiveDataHeuristics Python/JS": [
449449
"javascript/ql/src/semmle/javascript/security/internal/SensitiveDataHeuristics.qll",
450450
"python/ql/src/semmle/python/security/internal/SensitiveDataHeuristics.qll"
451+
],
452+
"ReDoS Util Python/JS": [
453+
"javascript/ql/src/semmle/javascript/security/performance/ReDoSUtil.qll",
454+
"python/ql/src/semmle/python/regex/ReDoSUtil.qll"
455+
],
456+
"ReDoS Exponential Python/JS": [
457+
"javascript/ql/src/semmle/javascript/security/performance/ExponentialBackTracking.qll",
458+
"python/ql/src/semmle/python/regex/ExponentialBackTracking.qll"
459+
],
460+
"ReDoS Polynomial Python/JS": [
461+
"javascript/ql/src/semmle/javascript/security/performance/SuperlinearBackTracking.qll",
462+
"python/ql/src/semmle/python/regex/SuperlinearBackTracking.qll"
451463
]
452464
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import python
2+
import semmle.python.regex.SuperlinearBackTracking
3+
4+
from PolynomialBackTrackingTerm t
5+
where t.getLocation().getFile().getBaseName() = "KnownCVEs.py"
6+
select t.getRegex(), t, t.getReason()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @name Polynomial regular expression used on uncontrolled data
3+
* @description A regular expression that can require polynomial time
4+
* to match may be vulnerable to denial-of-service attacks.
5+
* @kind path-problem
6+
* @problem.severity warning
7+
* @precision high
8+
* @id py/polynomial-redos
9+
* @tags security
10+
* external/cwe/cwe-730
11+
* external/cwe/cwe-400
12+
*/
13+
14+
import python
15+
import semmle.python.regex.SuperlinearBackTracking
16+
import semmle.python.security.dataflow.PolynomialReDoS
17+
import DataFlow::PathGraph
18+
19+
from
20+
PolynomialReDoSConfiguration config, DataFlow::PathNode source, DataFlow::PathNode sink,
21+
PolynomialReDoSSink sinkNode, PolynomialBackTrackingTerm regexp
22+
where
23+
config.hasFlowPath(source, sink) and
24+
sinkNode = sink.getNode() and
25+
regexp.getRootTerm() = sinkNode.getRegExp()
26+
// not (
27+
// source.getNode().(Source).getKind() = "url" and
28+
// regexp.isAtEndLine()
29+
// )
30+
select sinkNode.getHighlight(), source, sink,
31+
"This $@ that depends on $@ may run slow on strings " + regexp.getPrefixMessage() +
32+
"with many repetitions of '" + regexp.getPumpString() + "'.", regexp, "regular expression",
33+
source.getNode(), "a user-provided value"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @name Inefficient regular expression
3+
* @description A regular expression that requires exponential time to match certain inputs
4+
* can be a performance bottleneck, and may be vulnerable to denial-of-service
5+
* attacks.
6+
* @kind problem
7+
* @problem.severity error
8+
* @precision high
9+
* @id py/redos
10+
* @tags security
11+
* external/cwe/cwe-730
12+
* external/cwe/cwe-400
13+
*/
14+
15+
import python
16+
import semmle.python.regex.ExponentialBackTracking
17+
18+
from RegExpTerm t, string pump, State s, string prefixMsg
19+
where
20+
hasReDoSResult(t, pump, s, prefixMsg) and
21+
// exclude verbose mode regexes for now
22+
not t.getRegex().getAMode() = "VERBOSE"
23+
select t,
24+
"This part of the regular expression may cause exponential backtracking on strings " + prefixMsg +
25+
"containing many repetitions of '" + pump + "'."

0 commit comments

Comments
 (0)