Skip to content

Commit b66e5c5

Browse files
authored
Merge pull request #10634 from yoff/python/rewrite-typetrackers
Approved by tausbn
2 parents ee59bda + 84ab860 commit b66e5c5

File tree

2 files changed

+16
-43
lines changed
  • python/ql

2 files changed

+16
-43
lines changed

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

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

2829-
/** Helper module for tracking compiled regexes. */
2830-
private module CompiledRegexes {
2831-
private DataFlow::TypeTrackingNode compiledRegex(DataFlow::TypeTracker t, DataFlow::Node regex) {
2832-
t.start() and
2833-
result = API::moduleImport("re").getMember("compile").getACall() and
2834-
regex in [
2835-
result.(DataFlow::CallCfgNode).getArg(0),
2836-
result.(DataFlow::CallCfgNode).getArgByName("pattern")
2837-
]
2838-
or
2839-
exists(DataFlow::TypeTracker t2 | result = compiledRegex(t2, regex).track(t2, t))
2840-
}
2841-
2842-
DataFlow::Node compiledRegex(DataFlow::Node regex) {
2843-
compiledRegex(DataFlow::TypeTracker::end(), regex).flowsTo(result)
2844-
}
2829+
API::Node compiledRegex(API::Node regex) {
2830+
exists(API::CallNode compilation |
2831+
compilation = API::moduleImport("re").getMember("compile").getACall()
2832+
|
2833+
result = compilation.getReturn() and
2834+
regex = compilation.getParameter(0, "pattern")
2835+
)
28452836
}
28462837

2847-
private import CompiledRegexes
2848-
28492838
/**
28502839
* A call on compiled regular expression (obtained via `re.compile`) executing a
28512840
* regular expression.
@@ -2870,7 +2859,11 @@ private module StdlibPrivate {
28702859
DataFlow::Node regexNode;
28712860
RegexExecutionMethod method;
28722861

2873-
CompiledRegexExecution() { this.calls(compiledRegex(regexNode), method) }
2862+
CompiledRegexExecution() {
2863+
exists(API::Node regex | regexNode = regex.asSink() |
2864+
this.calls(compiledRegex(regex).getAValueReachableFromSource(), method)
2865+
)
2866+
}
28742867

28752868
override DataFlow::Node getRegex() { result = regexNode }
28762869

python/ql/src/experimental/semmle/python/frameworks/LDAP.qll

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ private module Ldap {
2626
API::Node ldapInitialize() { result = ldap().getMember("initialize") }
2727

2828
/** Gets a reference to a `ldap` operation. */
29-
private DataFlow::TypeTrackingNode ldapOperation(DataFlow::TypeTracker t) {
30-
t.start() and
31-
result.(DataFlow::AttrRead).getObject().getALocalSource() = ldapInitialize().getACall()
32-
or
33-
exists(DataFlow::TypeTracker t2 | result = ldapOperation(t2).track(t2, t))
29+
private API::Node ldapOperation(string name) {
30+
result = ldapInitialize().getReturn().getMember(name)
3431
}
3532

3633
/**
@@ -44,24 +41,13 @@ private module Ldap {
4441
}
4542
}
4643

47-
/** Gets a reference to a `ldap` operation. */
48-
private DataFlow::Node ldapOperation() {
49-
ldapOperation(DataFlow::TypeTracker::end()).flowsTo(result)
50-
}
51-
52-
/** Gets a reference to a `ldap` query. */
53-
private DataFlow::Node ldapQuery() {
54-
result = ldapOperation() and
55-
result.(DataFlow::AttrRead).getAttributeName() instanceof Ldap2QueryMethods
56-
}
57-
5844
/**
5945
* A class to find `ldap` methods executing a query.
6046
*
6147
* See `LDAP2QueryMethods`
6248
*/
6349
private class Ldap2Query extends DataFlow::CallCfgNode, LdapQuery::Range {
64-
Ldap2Query() { this.getFunction() = ldapQuery() }
50+
Ldap2Query() { this = ldapOperation(any(Ldap2QueryMethods m)).getACall() }
6551

6652
override DataFlow::Node getQuery() {
6753
result in [this.getArg(0), this.getArg(2), this.getArgByName("filterstr")]
@@ -82,12 +68,6 @@ private module Ldap {
8268
}
8369
}
8470

85-
/** Gets a reference to a `ldap` bind. */
86-
private DataFlow::Node ldapBind() {
87-
result = ldapOperation() and
88-
result.(DataFlow::AttrRead).getAttributeName() instanceof Ldap2BindMethods
89-
}
90-
9171
/**List of SSL-demanding options */
9272
private class LdapSslOptions extends DataFlow::Node {
9373
LdapSslOptions() {
@@ -101,7 +81,7 @@ private module Ldap {
10181
* See `LDAP2BindMethods`
10282
*/
10383
private class Ldap2Bind extends DataFlow::CallCfgNode, LdapBind::Range {
104-
Ldap2Bind() { this.getFunction() = ldapBind() }
84+
Ldap2Bind() { this = ldapOperation(any(Ldap2BindMethods m)).getACall() }
10585

10686
override DataFlow::Node getPassword() {
10787
result in [this.getArg(1), this.getArgByName("cred")]

0 commit comments

Comments
 (0)