Skip to content

Commit d5e0298

Browse files
committed
Python: Add support for Psycopg2 database connection pools
Our current modelling only treated `psycopg2` insofar as it implemented PEP 249 (which does not define any notion of connection pool), which meant we were missing database connections that arose from such pools. With these changes, we add support for the three classes relating to database pools that are defined in `psycopg2`. (Note that `getAnInstance` automatically looks at subclasses, which means this should also handle cases where the user has defined a new subclass that inherits from one of these three classes.)
1 parent 88059d9 commit d5e0298

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
5+
- The modelling of Psycopg2 now supports the use of `psycopg2.pool` connection pools for handling database connections.

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,17 @@ private module Psycopg2 {
2929
class Psycopg2 extends PEP249::PEP249ModuleApiNode {
3030
Psycopg2() { this = API::moduleImport("psycopg2") }
3131
}
32+
33+
/** A database connection obtained from a psycopg2 connection pool. */
34+
class Psycopg2ConnectionPoolMember extends PEP249::DatabaseConnection {
35+
Psycopg2ConnectionPoolMember() {
36+
this =
37+
any(Psycopg2 p)
38+
.getMember("pool")
39+
.getMember(["SimpleConnectionPool", "ThreadedConnectionPool", "AbstractConnectionPool"])
40+
.getAnInstance()
41+
.getMember("getconn")
42+
.getReturn()
43+
}
44+
}
3245
}

0 commit comments

Comments
 (0)