Skip to content

Commit 8d8e92e

Browse files
committed
Python: Model execute on a DB connection
1 parent 18f7dbe commit 8d8e92e

File tree

2 files changed

+13
-5
lines changed
  • python/ql
    • src/semmle/python/frameworks
    • test/experimental/library-tests/frameworks/stdlib

2 files changed

+13
-5
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,31 @@ module cursor {
9090
}
9191

9292
/**
93-
* Gets a reference to the `execute` method on a cursor.
93+
* Gets a reference to the `execute` method on a cursor (or on a connection).
94+
*
95+
* Note: while `execute` method on a connection is not part of PEP249, if it is used, we
96+
* recognize it as an alias for constructing a cursor and calling `execute` on it.
97+
*
9498
* See https://www.python.org/dev/peps/pep-0249/#id15.
9599
*/
96100
private DataFlow::Node execute(DataFlow::TypeTracker t) {
97101
t.startInAttr("execute") and
98-
result = cursor::methodResult()
102+
result in [cursor::methodResult(), Connection::instance()]
99103
or
100104
exists(DataFlow::TypeTracker t2 | result = execute(t2).track(t2, t))
101105
}
102106

103107
/**
104-
* Gets a reference to the `execute` method on a cursor.
108+
* Gets a reference to the `execute` method on a cursor (or on a connection).
109+
*
110+
* Note: while `execute` method on a connection is not part of PEP249, if it is used, we
111+
* recognize it as an alias for constructing a cursor and calling `execute` on it.
112+
*
105113
* See https://www.python.org/dev/peps/pep-0249/#id15.
106114
*/
107115
DataFlow::Node execute() { result = execute(DataFlow::TypeTracker::end()) }
108116

109-
/** A call to the `execute` method on a cursor. */
117+
/** A call to the `execute` method on a cursor (or on a connection). */
110118
private class ExecuteCall extends SqlExecution::Range, DataFlow::CfgNode {
111119
override CallNode node;
112120

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
db = sqlite3.connect("example.db")
33

44
# non standard
5-
db.execute("some sql", (42,)) # $ MISSING: getSql="some sql"
5+
db.execute("some sql", (42,)) # $ getSql="some sql"
66

77
cursor = db.cursor()
88
cursor.execute("some sql", (42,)) # $ getSql="some sql"

0 commit comments

Comments
 (0)