Skip to content

Commit 0654e39

Browse files
committed
python: rewrite type tracker for compiled regexes
we have the option to use `regex.getAValueReachingSink` rather than `regex.asSink`, but it will likely be used as a sink for data flow.
1 parent 296c0a7 commit 0654e39

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

python/ql/lib/semmle/python/frameworks/Stdlib.qll

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,26 +2842,15 @@ private module StdlibPrivate {
28422842
override string getName() { result = "re." + method }
28432843
}
28442844

2845-
/** Helper module for tracking compiled regexes. */
2846-
private module CompiledRegexes {
2847-
private DataFlow::TypeTrackingNode compiledRegex(DataFlow::TypeTracker t, DataFlow::Node regex) {
2848-
t.start() and
2849-
result = API::moduleImport("re").getMember("compile").getACall() and
2850-
regex in [
2851-
result.(DataFlow::CallCfgNode).getArg(0),
2852-
result.(DataFlow::CallCfgNode).getArgByName("pattern")
2853-
]
2854-
or
2855-
exists(DataFlow::TypeTracker t2 | result = compiledRegex(t2, regex).track(t2, t))
2856-
}
2857-
2858-
DataFlow::Node compiledRegex(DataFlow::Node regex) {
2859-
compiledRegex(DataFlow::TypeTracker::end(), regex).flowsTo(result)
2860-
}
2845+
API::Node compiledRegex(API::Node regex) {
2846+
exists(API::CallNode compilation |
2847+
compilation = API::moduleImport("re").getMember("compile").getACall()
2848+
|
2849+
result = compilation.getReturn() and
2850+
regex = compilation.getParameter(0, "pattern")
2851+
)
28612852
}
28622853

2863-
private import CompiledRegexes
2864-
28652854
/**
28662855
* A call on compiled regular expression (obtained via `re.compile`) executing a
28672856
* regular expression.
@@ -2886,7 +2875,11 @@ private module StdlibPrivate {
28862875
DataFlow::Node regexNode;
28872876
RegexExecutionMethod method;
28882877

2889-
CompiledRegexExecution() { this.calls(compiledRegex(regexNode), method) }
2878+
CompiledRegexExecution() {
2879+
exists(API::Node regex | regexNode = regex.asSink() |
2880+
this.calls(compiledRegex(regex).getAValueReachableFromSource(), method)
2881+
)
2882+
}
28902883

28912884
override DataFlow::Node getRegex() { result = regexNode }
28922885

0 commit comments

Comments
 (0)