|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the `asyncpg` PyPI package. |
| 3 | + * See https://magicstack.github.io/asyncpg/. |
| 4 | + */ |
| 5 | + |
| 6 | +private import python |
| 7 | +private import semmle.python.dataflow.new.DataFlow |
| 8 | +private import semmle.python.Concepts |
| 9 | +private import semmle.python.ApiGraphs |
| 10 | + |
| 11 | +/** Provides models for the `asyncpg` PyPI package. */ |
| 12 | +private module Asyncpg { |
| 13 | + private import semmle.python.internal.Awaited |
| 14 | + |
| 15 | + /** A `ConectionPool` is created when the result of `asyncpg.create_pool()` is awaited. */ |
| 16 | + API::Node connectionPool() { |
| 17 | + result = API::moduleImport("asyncpg").getMember("create_pool").getReturn().getAwaited() |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * A `Connection` is created when |
| 22 | + * - the result of `asyncpg.connect()` is awaited. |
| 23 | + * - the result of calling `aquire` on a `ConnectionPool` is awaited. |
| 24 | + */ |
| 25 | + API::Node connection() { |
| 26 | + result = API::moduleImport("asyncpg").getMember("connect").getReturn().getAwaited() |
| 27 | + or |
| 28 | + result = connectionPool().getMember("acquire").getReturn().getAwaited() |
| 29 | + } |
| 30 | + |
| 31 | + /** `Connection`s and `ConnectionPool`s provide some methods that execute SQL. */ |
| 32 | + class SqlExecutionOnConnection extends SqlExecution::Range, DataFlow::MethodCallNode { |
| 33 | + string methodName; |
| 34 | + |
| 35 | + SqlExecutionOnConnection() { |
| 36 | + methodName in ["copy_from_query", "execute", "fetch", "fetchrow", "fetchval", "executemany"] and |
| 37 | + this.calls([connectionPool().getAUse(), connection().getAUse()], methodName) |
| 38 | + } |
| 39 | + |
| 40 | + override DataFlow::Node getSql() { |
| 41 | + methodName in ["copy_from_query", "execute", "fetch", "fetchrow", "fetchval"] and |
| 42 | + result in [this.getArg(0), this.getArgByName("query")] |
| 43 | + or |
| 44 | + methodName = "executemany" and |
| 45 | + result in [this.getArg(0), this.getArgByName("command")] |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** `Connection`s and `ConnectionPool`s provide some methods that access the file system. */ |
| 50 | + class FileAccessOnConnection extends FileSystemAccess::Range, DataFlow::MethodCallNode { |
| 51 | + string methodName; |
| 52 | + |
| 53 | + FileAccessOnConnection() { |
| 54 | + methodName in ["copy_from_query", "copy_from_table", "copy_to_table"] and |
| 55 | + this.calls([connectionPool().getAUse(), connection().getAUse()], methodName) |
| 56 | + } |
| 57 | + |
| 58 | + // The path argument is keyword only. |
| 59 | + override DataFlow::Node getAPathArgument() { |
| 60 | + methodName in ["copy_from_query", "copy_from_table"] and |
| 61 | + result = this.getArgByName("output") |
| 62 | + or |
| 63 | + methodName = "copy_to_table" and |
| 64 | + result = this.getArgByName("source") |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Provides models of the `PreparedStatement` class in `asyncpg`. |
| 70 | + * `PreparedStatement`s are created when the result of calling `prepare(query)` on a connection is awaited. |
| 71 | + * The result of calling `prepare(query)` is a `PreparedStatementFactory` and the argument, `query` needs to |
| 72 | + * be tracked to the place where a `PreparedStatement` is created and then futher to any executing methods. |
| 73 | + * Hence the two type trackers. |
| 74 | + * |
| 75 | + * TODO: Rewrite this, once we have `API::CallNode` available. |
| 76 | + */ |
| 77 | + module PreparedStatement { |
| 78 | + class PreparedStatementConstruction extends SqlConstruction::Range, DataFlow::CallCfgNode { |
| 79 | + PreparedStatementConstruction() { this = connection().getMember("prepare").getACall() } |
| 80 | + |
| 81 | + override DataFlow::Node getSql() { result in [this.getArg(0), this.getArgByName("query")] } |
| 82 | + } |
| 83 | + |
| 84 | + private DataFlow::TypeTrackingNode preparedStatementFactory( |
| 85 | + DataFlow::TypeTracker t, DataFlow::Node sql |
| 86 | + ) { |
| 87 | + t.start() and |
| 88 | + sql = result.(PreparedStatementConstruction).getSql() |
| 89 | + or |
| 90 | + exists(DataFlow::TypeTracker t2 | result = preparedStatementFactory(t2, sql).track(t2, t)) |
| 91 | + } |
| 92 | + |
| 93 | + DataFlow::Node preparedStatementFactory(DataFlow::Node sql) { |
| 94 | + preparedStatementFactory(DataFlow::TypeTracker::end(), sql).flowsTo(result) |
| 95 | + } |
| 96 | + |
| 97 | + private DataFlow::TypeTrackingNode preparedStatement(DataFlow::TypeTracker t, DataFlow::Node sql) { |
| 98 | + t.start() and |
| 99 | + result = awaited(preparedStatementFactory(sql)) |
| 100 | + or |
| 101 | + exists(DataFlow::TypeTracker t2 | result = preparedStatement(t2, sql).track(t2, t)) |
| 102 | + } |
| 103 | + |
| 104 | + DataFlow::Node preparedStatement(DataFlow::Node sql) { |
| 105 | + preparedStatement(DataFlow::TypeTracker::end(), sql).flowsTo(result) |
| 106 | + } |
| 107 | + |
| 108 | + class PreparedStatementExecution extends SqlExecution::Range, DataFlow::MethodCallNode { |
| 109 | + DataFlow::Node sql; |
| 110 | + |
| 111 | + PreparedStatementExecution() { |
| 112 | + this.calls(preparedStatement(sql), ["executemany", "fetch", "fetchrow", "fetchval"]) |
| 113 | + } |
| 114 | + |
| 115 | + override DataFlow::Node getSql() { result = sql } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Provides models of the `Cursor` class in `asyncpg`. |
| 121 | + * `Cursor`s are created |
| 122 | + * - when the result of calling `cursor(query)` on a connection is awaited. |
| 123 | + * - when the result of calling `cursor()` on a prepared statement is awaited. |
| 124 | + * The result of calling `cursor` in either case is a `CursorFactory` and the argument, `query` needs to |
| 125 | + * be tracked to the place where a `Cursor` is created, hence the type tracker. |
| 126 | + * The creation of the `Cursor` executes the query. |
| 127 | + * |
| 128 | + * TODO: Rewrite this, once we have `API::CallNode` available. |
| 129 | + */ |
| 130 | + module Cursor { |
| 131 | + class CursorConstruction extends SqlConstruction::Range, DataFlow::CallCfgNode { |
| 132 | + CursorConstruction() { this = connection().getMember("cursor").getACall() } |
| 133 | + |
| 134 | + override DataFlow::Node getSql() { result in [this.getArg(0), this.getArgByName("query")] } |
| 135 | + } |
| 136 | + |
| 137 | + private DataFlow::TypeTrackingNode cursorFactory(DataFlow::TypeTracker t, DataFlow::Node sql) { |
| 138 | + // cursor created from connection |
| 139 | + t.start() and |
| 140 | + sql = result.(CursorConstruction).getSql() |
| 141 | + or |
| 142 | + // cursor created from prepared statement |
| 143 | + t.start() and |
| 144 | + result.(DataFlow::MethodCallNode).calls(PreparedStatement::preparedStatement(sql), "cursor") |
| 145 | + or |
| 146 | + exists(DataFlow::TypeTracker t2 | result = cursorFactory(t2, sql).track(t2, t)) |
| 147 | + } |
| 148 | + |
| 149 | + DataFlow::Node cursorFactory(DataFlow::Node sql) { |
| 150 | + cursorFactory(DataFlow::TypeTracker::end(), sql).flowsTo(result) |
| 151 | + } |
| 152 | + |
| 153 | + /** The creation of a `Cursor` executes the associated query. */ |
| 154 | + class CursorCreation extends SqlExecution::Range { |
| 155 | + DataFlow::Node sql; |
| 156 | + |
| 157 | + CursorCreation() { this = awaited(cursorFactory(sql)) } |
| 158 | + |
| 159 | + override DataFlow::Node getSql() { result = sql } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments