Skip to content

Commit fc9fb59

Browse files
committed
Python: Add comments
1 parent 1151138 commit fc9fb59

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,23 @@ private import semmle.python.ApiGraphs
1010

1111
/** Provides models for the `asyncpg` PyPI package. */
1212
private module Asyncpg {
13+
/** A `ConectionPool` is created when the result of `asyncpg.create_pool()` is awaited. */
1314
API::Node connectionPool() {
1415
result = API::moduleImport("asyncpg").getMember("create_pool").getReturn().getAwaited()
1516
}
1617

18+
/**
19+
* A `Connection` is created when
20+
* - the result of `asyncpg.connect()` is awaited.
21+
* - the result of calling `aquire` on a c=`ConnectionPool` is awaited.
22+
*/
1723
API::Node connection() {
1824
result = API::moduleImport("asyncpg").getMember("connect").getReturn().getAwaited()
1925
or
2026
result = connectionPool().getMember("acquire").getReturn().getAwaited()
2127
}
2228

29+
/** Reverse lookup of the query argument name for a query method. */
2330
private string queryMethodName(string queryArg) {
2431
result in ["copy_from_query", "execute", "fetch", "fetchrow", "fetchval"] and
2532
queryArg = "query"
@@ -28,6 +35,7 @@ private module Asyncpg {
2835
queryArg = "command"
2936
}
3037

38+
/** `Connection`s and `ConnectionPool`s provide some methods that execute SQL. */
3139
class SqlExecutionOnConnection extends SqlExecution::Range, DataFlow::MethodCallNode {
3240
string queryArg;
3341

@@ -38,6 +46,7 @@ private module Asyncpg {
3846
override DataFlow::Node getSql() { result in [this.getArg(0), this.getArgByName(queryArg)] }
3947
}
4048

49+
/** Reverse lokup of the path argument name for a method accessing the file system. */
4150
private string fileAccessMethodName(string pathArg) {
4251
result in ["copy_from_query", "copy_from_table"] and
4352
pathArg = "output"
@@ -46,18 +55,20 @@ private module Asyncpg {
4655
pathArg = "source"
4756
}
4857

58+
/** `Connection`s and `ConnectionPool`s provide some methods that access the file system. */
4959
class FileAccessOnConnection extends FileSystemAccess::Range, DataFlow::MethodCallNode {
5060
string pathArg;
5161

5262
FileAccessOnConnection() {
5363
this.calls([connectionPool().getAUse(), connection().getAUse()], fileAccessMethodName(pathArg))
5464
}
5565

66+
// The path argument is keyword only.
5667
override DataFlow::Node getAPathArgument() { result in [this.getArgByName(pathArg)] }
5768
}
5869

5970
/**
60-
* Holds if `result` is the result of awaiting `n`.
71+
* Holds if `result` is the result of awaiting `awaitedValue`.
6172
*/
6273
pragma[inline]
6374
DataFlow::Node awaited(DataFlow::Node awaitedValue) {
@@ -88,6 +99,15 @@ private module Asyncpg {
8899
)
89100
}
90101

102+
/**
103+
* Provides models of the `PreparedStatement` class in `asyncpg`.
104+
* `PreparedStatement`s are created when the result of calling `prepare(query)` on a connection is awaited.
105+
* The result of calling `prepare(query)` is a `PreparedStatementFactory` and the argument, `query` needs to
106+
* be tracked to the place where a `PreparedStatement` is created and then futher to any executing methods.
107+
* Hence the two type trackers.
108+
*
109+
* TODO: Rewrite this, once we have `API::CallNode` available.
110+
*/
91111
module PreparedStatement {
92112
private DataFlow::TypeTrackingNode preparedStatementFactory(
93113
DataFlow::TypeTracker t, DataFlow::Node sql
@@ -128,6 +148,17 @@ private module Asyncpg {
128148
}
129149
}
130150

151+
/**
152+
* Provides models of the `Cursor` class in `asyncpg`.
153+
* `Cursor`s are created
154+
* - when the result of calling `cursor(query)` on a connection is awaited.
155+
* - when the result of calling `cursor()` on a prepared statement is awaited.
156+
* The result of calling `cursor` in either case is a `CursorFactory` and the argument, `query` needs to
157+
* be tracked to the place where a `Cursor` is created, hence the type tracker.
158+
* The creation of the `Cursor` executes the query.
159+
*
160+
* TODO: Rewrite this, once we have `API::CallNode` available.
161+
*/
131162
module Cursor {
132163
private DataFlow::TypeTrackingNode cursorFactory(DataFlow::TypeTracker t, DataFlow::Node sql) {
133164
// cursor created from connection
@@ -149,6 +180,7 @@ private module Asyncpg {
149180
cursorFactory(DataFlow::TypeTracker::end(), sql).flowsTo(result)
150181
}
151182

183+
/** The creation of a `Cursor` executes the associated query. */
152184
class CursorCreation extends SqlExecution::Range {
153185
DataFlow::Node sql;
154186

0 commit comments

Comments
 (0)