Skip to content

Commit 36e8ef5

Browse files
committed
Python: Model sqlite3 as SQL interface
1 parent 767a246 commit 36e8ef5

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* Added model of `sqlite3` as SQL interface following PEP249, resulting in additional sinks for `py/sql-injection`.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ private import semmle.python.dataflow.new.DataFlow
88
private import semmle.python.dataflow.new.TaintTracking
99
private import semmle.python.dataflow.new.RemoteFlowSources
1010
private import semmle.python.Concepts
11+
private import PEP249
1112

1213
/** Provides models for the Python standard library. */
1314
private module Stdlib {
@@ -1032,6 +1033,29 @@ private module Stdlib {
10321033

10331034
override string getFormat() { result = "JSON" }
10341035
}
1036+
1037+
// ---------------------------------------------------------------------------
1038+
// sqlite3
1039+
// ---------------------------------------------------------------------------
1040+
/** Gets a reference to the `sqlite3` module. */
1041+
private DataFlow::Node sqlite3(DataFlow::TypeTracker t) {
1042+
t.start() and
1043+
result = DataFlow::importNode("sqlite3")
1044+
or
1045+
exists(DataFlow::TypeTracker t2 | result = sqlite3(t2).track(t2, t))
1046+
}
1047+
1048+
/** Gets a reference to the `sqlite3` module. */
1049+
DataFlow::Node sqlite3() { result = sqlite3(DataFlow::TypeTracker::end()) }
1050+
1051+
/**
1052+
* sqlite3 implements PEP 249, providing ways to execute SQL statements against a database.
1053+
*
1054+
* See https://devdocs.io/python~3.9/library/sqlite3
1055+
*/
1056+
class Sqlite3 extends PEP249Module {
1057+
Sqlite3() { this = sqlite3() }
1058+
}
10351059
}
10361060

10371061
// ---------------------------------------------------------------------------

python/ql/test/experimental/library-tests/frameworks/stdlib/pep249.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
db.execute("some sql", (42,)) # $ MISSING: getSql="some sql"
66

77
cursor = db.cursor()
8-
cursor.execute("some sql", (42,)) # $ MISSING: getSql="some sql"
8+
cursor.execute("some sql", (42,)) # $ getSql="some sql"

0 commit comments

Comments
 (0)