Skip to content

Commit 9bec9b4

Browse files
authored
Merge pull request #4801 from RasmusWL/sqlite3-support
Python: Add sqlite3 support
2 parents 0210c1f + 36e8ef5 commit 9bec9b4

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
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 {
@@ -1080,6 +1081,29 @@ private module Stdlib {
10801081

10811082
override string getFormat() { result = "JSON" }
10821083
}
1084+
1085+
// ---------------------------------------------------------------------------
1086+
// sqlite3
1087+
// ---------------------------------------------------------------------------
1088+
/** Gets a reference to the `sqlite3` module. */
1089+
private DataFlow::Node sqlite3(DataFlow::TypeTracker t) {
1090+
t.start() and
1091+
result = DataFlow::importNode("sqlite3")
1092+
or
1093+
exists(DataFlow::TypeTracker t2 | result = sqlite3(t2).track(t2, t))
1094+
}
1095+
1096+
/** Gets a reference to the `sqlite3` module. */
1097+
DataFlow::Node sqlite3() { result = sqlite3(DataFlow::TypeTracker::end()) }
1098+
1099+
/**
1100+
* sqlite3 implements PEP 249, providing ways to execute SQL statements against a database.
1101+
*
1102+
* See https://devdocs.io/python~3.9/library/sqlite3
1103+
*/
1104+
class Sqlite3 extends PEP249Module {
1105+
Sqlite3() { this = sqlite3() }
1106+
}
10831107
}
10841108

10851109
// ---------------------------------------------------------------------------
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import sqlite3
2+
db = sqlite3.connect("example.db")
3+
4+
# non standard
5+
db.execute("some sql", (42,)) # $ MISSING: getSql="some sql"
6+
7+
cursor = db.cursor()
8+
cursor.execute("some sql", (42,)) # $ getSql="some sql"

0 commit comments

Comments
 (0)