Skip to content

Commit 618d135

Browse files
authored
Merge pull request #7060 from RasmusWL/hashlib-new-type-tracker
Approved by yoff
2 parents 77aca0a + 9e2bc41 commit 618d135

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

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

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,15 +1534,36 @@ private module StdlibPrivate {
15341534
// ---------------------------------------------------------------------------
15351535
// hashlib
15361536
// ---------------------------------------------------------------------------
1537+
/** Gets a back-reference to the hashname argument `arg` that was used in a call to `hashlib.new`. */
1538+
private DataFlow::TypeTrackingNode hashlibNewCallNameBacktracker(
1539+
DataFlow::TypeBackTracker t, DataFlow::Node arg
1540+
) {
1541+
t.start() and
1542+
hashlibNewCallImpl(_, arg) and
1543+
result = arg.getALocalSource()
1544+
or
1545+
exists(DataFlow::TypeBackTracker t2 |
1546+
result = hashlibNewCallNameBacktracker(t2, arg).backtrack(t2, t)
1547+
)
1548+
}
1549+
1550+
/** Gets a back-reference to the hashname argument `arg` that was used in a call to `hashlib.new`. */
1551+
private DataFlow::LocalSourceNode hashlibNewCallNameBacktracker(DataFlow::Node arg) {
1552+
result = hashlibNewCallNameBacktracker(DataFlow::TypeBackTracker::end(), arg)
1553+
}
1554+
1555+
/** Holds when `call` is a call to `hashlib.new` with `nameArg` as the first argument. */
1556+
private predicate hashlibNewCallImpl(DataFlow::CallCfgNode call, DataFlow::Node nameArg) {
1557+
call = API::moduleImport("hashlib").getMember("new").getACall() and
1558+
nameArg in [call.getArg(0), call.getArgByName("name")]
1559+
}
1560+
15371561
/** Gets a call to `hashlib.new` with `algorithmName` as the first argument. */
15381562
private DataFlow::CallCfgNode hashlibNewCall(string algorithmName) {
1539-
exists(DataFlow::Node nameArg |
1540-
result = API::moduleImport("hashlib").getMember("new").getACall() and
1541-
nameArg in [result.getArg(0), result.getArgByName("name")] and
1542-
exists(StrConst str |
1543-
nameArg.getALocalSource() = DataFlow::exprNode(str) and
1544-
algorithmName = str.getText()
1545-
)
1563+
exists(DataFlow::Node origin, DataFlow::Node nameArg |
1564+
origin = hashlibNewCallNameBacktracker(nameArg) and
1565+
algorithmName = origin.asExpr().(StrConst).getText() and
1566+
hashlibNewCallImpl(result, nameArg)
15461567
)
15471568
}
15481569

python/ql/test/library-tests/frameworks/stdlib/test_md5.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@
2727
hasher.update(b"secret") # $ CryptographicOperation CryptographicOperationInput=b"secret" CryptographicOperationAlgorithm=MD5
2828
hasher.update(b" message") # $ CryptographicOperation CryptographicOperationInput=b" message" CryptographicOperationAlgorithm=MD5
2929
print(hasher.hexdigest())
30+
31+
32+
def foo(arg):
33+
hasher = hashlib.new(arg)
34+
hasher.update(b"secret") # $ CryptographicOperation CryptographicOperationInput=b"secret" CryptographicOperationAlgorithm=MD5
35+
hasher.update(b" message") # $ CryptographicOperation CryptographicOperationInput=b" message" CryptographicOperationAlgorithm=MD5
36+
print(hasher.hexdigest())
37+
38+
foo("md5")

0 commit comments

Comments
 (0)