|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the 'SqlAlchemy' package. |
| 3 | + * See https://pypi.org/project/SQLAlchemy/. |
| 4 | + */ |
| 5 | + |
| 6 | +private import python |
| 7 | +private import semmle.python.dataflow.new.DataFlow |
| 8 | +private import semmle.python.dataflow.new.TaintTracking |
| 9 | +private import semmle.python.ApiGraphs |
| 10 | +private import semmle.python.Concepts |
| 11 | +private import experimental.semmle.python.Concepts |
| 12 | + |
| 13 | +private module SqlAlchemy { |
| 14 | + /** |
| 15 | + * Returns an instantization of a SqlAlchemy Session object. |
| 16 | + * See https://docs.sqlalchemy.org/en/14/orm/session_api.html#sqlalchemy.orm.Session and |
| 17 | + * https://docs.sqlalchemy.org/en/14/orm/session_api.html#sqlalchemy.orm.sessionmaker |
| 18 | + */ |
| 19 | + private API::Node getSqlAlchemySessionInstance() { |
| 20 | + result = API::moduleImport("sqlalchemy.orm").getMember("Session").getReturn() or |
| 21 | + result = API::moduleImport("sqlalchemy.orm").getMember("sessionmaker").getReturn().getReturn() |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns an instantization of a SqlAlchemy Engine object. |
| 26 | + * See https://docs.sqlalchemy.org/en/14/core/engines.html#sqlalchemy.create_engine |
| 27 | + */ |
| 28 | + private API::Node getSqlAlchemyEngineInstance() { |
| 29 | + result = API::moduleImport("sqlalchemy").getMember("create_engine").getReturn() |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Returns an instantization of a SqlAlchemy Query object. |
| 34 | + * See https://docs.sqlalchemy.org/en/14/orm/query.html?highlight=query#sqlalchemy.orm.Query |
| 35 | + */ |
| 36 | + private API::Node getSqlAlchemyQueryInstance() { |
| 37 | + result = getSqlAlchemySessionInstance().getMember("query").getReturn() |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * A call to `execute` meant to execute an SQL expression |
| 42 | + * See the following links: |
| 43 | + * - https://docs.sqlalchemy.org/en/14/core/connections.html?highlight=execute#sqlalchemy.engine.Connection.execute |
| 44 | + * - https://docs.sqlalchemy.org/en/14/core/connections.html?highlight=execute#sqlalchemy.engine.Engine.execute |
| 45 | + * - https://docs.sqlalchemy.org/en/14/orm/session_api.html?highlight=execute#sqlalchemy.orm.Session.execute |
| 46 | + */ |
| 47 | + private class SqlAlchemyExecuteCall extends DataFlow::CallCfgNode, SqlExecution::Range { |
| 48 | + SqlAlchemyExecuteCall() { |
| 49 | + // new way |
| 50 | + this = getSqlAlchemySessionInstance().getMember("execute").getACall() or |
| 51 | + this = |
| 52 | + getSqlAlchemyEngineInstance() |
| 53 | + .getMember(["connect", "begin"]) |
| 54 | + .getReturn() |
| 55 | + .getMember("execute") |
| 56 | + .getACall() |
| 57 | + } |
| 58 | + |
| 59 | + override DataFlow::Node getSql() { result = this.getArg(0) } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * A call to `scalar` meant to execute an SQL expression |
| 64 | + * See https://docs.sqlalchemy.org/en/14/orm/session_api.html#sqlalchemy.orm.Session.scalar and |
| 65 | + * https://docs.sqlalchemy.org/en/14/core/connections.html?highlight=scalar#sqlalchemy.engine.Engine.scalar |
| 66 | + */ |
| 67 | + private class SqlAlchemyScalarCall extends DataFlow::CallCfgNode, SqlExecution::Range { |
| 68 | + SqlAlchemyScalarCall() { |
| 69 | + this = |
| 70 | + [getSqlAlchemySessionInstance(), getSqlAlchemyEngineInstance()] |
| 71 | + .getMember("scalar") |
| 72 | + .getACall() |
| 73 | + } |
| 74 | + |
| 75 | + override DataFlow::Node getSql() { result = this.getArg(0) } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * A call on a Query object |
| 80 | + * See https://docs.sqlalchemy.org/en/14/orm/query.html?highlight=query#sqlalchemy.orm.Query |
| 81 | + */ |
| 82 | + private class SqlAlchemyQueryCall extends DataFlow::CallCfgNode, SqlExecution::Range { |
| 83 | + SqlAlchemyQueryCall() { |
| 84 | + this = |
| 85 | + getSqlAlchemyQueryInstance() |
| 86 | + .getMember(any(SqlAlchemyVulnerableMethodNames methodName)) |
| 87 | + .getACall() |
| 88 | + } |
| 89 | + |
| 90 | + override DataFlow::Node getSql() { result = this.getArg(0) } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * This class represents a list of methods vulnerable to sql injection. |
| 95 | + * |
| 96 | + * See https://github.com/jty-team/codeql/pull/2#issue-611592361 |
| 97 | + */ |
| 98 | + private class SqlAlchemyVulnerableMethodNames extends string { |
| 99 | + SqlAlchemyVulnerableMethodNames() { this in ["filter", "filter_by", "group_by", "order_by"] } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Additional taint-steps for `sqlalchemy.text()` |
| 104 | + * |
| 105 | + * See https://docs.sqlalchemy.org/en/14/core/sqlelement.html#sqlalchemy.sql.expression.text |
| 106 | + * See https://docs.sqlalchemy.org/en/14/core/sqlelement.html#sqlalchemy.sql.expression.TextClause |
| 107 | + */ |
| 108 | + class SqlAlchemyTextAdditionalTaintSteps extends TaintTracking::AdditionalTaintStep { |
| 109 | + override predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 110 | + exists(DataFlow::CallCfgNode call | |
| 111 | + ( |
| 112 | + call = API::moduleImport("sqlalchemy").getMember("text").getACall() |
| 113 | + or |
| 114 | + call = API::moduleImport("sqlalchemy").getMember("sql").getMember("text").getACall() |
| 115 | + or |
| 116 | + call = |
| 117 | + API::moduleImport("sqlalchemy") |
| 118 | + .getMember("sql") |
| 119 | + .getMember("expression") |
| 120 | + .getMember("text") |
| 121 | + .getACall() |
| 122 | + or |
| 123 | + call = |
| 124 | + API::moduleImport("sqlalchemy") |
| 125 | + .getMember("sql") |
| 126 | + .getMember("expression") |
| 127 | + .getMember("TextClause") |
| 128 | + .getACall() |
| 129 | + ) and |
| 130 | + nodeFrom in [call.getArg(0), call.getArgByName("text")] and |
| 131 | + nodeTo = call |
| 132 | + ) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Gets a reference to `sqlescapy.sqlescape`. |
| 138 | + * |
| 139 | + * See https://pypi.org/project/sqlescapy/ |
| 140 | + */ |
| 141 | + class SQLEscapySanitizerCall extends DataFlow::CallCfgNode, SQLEscape::Range { |
| 142 | + SQLEscapySanitizerCall() { |
| 143 | + this = API::moduleImport("sqlescapy").getMember("sqlescape").getACall() |
| 144 | + } |
| 145 | + |
| 146 | + override DataFlow::Node getAnInput() { result = this.getArg(0) } |
| 147 | + } |
| 148 | +} |
0 commit comments