|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the `mysql-connector-python` package. |
| 3 | + * See https://dev.mysql.com/doc/dev/connector-python/. |
| 4 | + */ |
| 5 | + |
| 6 | +private import python |
| 7 | +private import experimental.dataflow.DataFlow |
| 8 | +private import experimental.dataflow.RemoteFlowSources |
| 9 | +private import experimental.semmle.python.Concepts |
| 10 | +private import PEP249 |
| 11 | + |
| 12 | +/** |
| 13 | + * Provides models for the `mysql-connector-python` package. |
| 14 | + * See https://dev.mysql.com/doc/dev/connector-python/. |
| 15 | + */ |
| 16 | +module MysqlConnectorPython { |
| 17 | + // --------------------------------------------------------------------------- |
| 18 | + // mysql |
| 19 | + // --------------------------------------------------------------------------- |
| 20 | + /** Gets a reference to the `mysql` module. */ |
| 21 | + private DataFlow::Node mysql(DataFlow::TypeTracker t) { |
| 22 | + t.start() and |
| 23 | + result = DataFlow::importNode("mysql") |
| 24 | + or |
| 25 | + exists(DataFlow::TypeTracker t2 | result = mysql(t2).track(t2, t)) |
| 26 | + } |
| 27 | + |
| 28 | + /** Gets a reference to the `mysql` module. */ |
| 29 | + DataFlow::Node mysql() { result = mysql(DataFlow::TypeTracker::end()) } |
| 30 | + |
| 31 | + /** |
| 32 | + * Gets a reference to the attribute `attr_name` of the `mysql` module. |
| 33 | + * WARNING: Only holds for a few predefined attributes. |
| 34 | + */ |
| 35 | + private DataFlow::Node mysql_attr(DataFlow::TypeTracker t, string attr_name) { |
| 36 | + attr_name in ["connector"] and |
| 37 | + ( |
| 38 | + t.start() and |
| 39 | + result = DataFlow::importNode("mysql" + "." + attr_name) |
| 40 | + or |
| 41 | + t.startInAttr(attr_name) and |
| 42 | + result = mysql() |
| 43 | + ) |
| 44 | + or |
| 45 | + // Due to bad performance when using normal setup with `mysql_attr(t2, attr_name).track(t2, t)` |
| 46 | + // we have inlined that code and forced a join |
| 47 | + exists(DataFlow::TypeTracker t2 | |
| 48 | + exists(DataFlow::StepSummary summary | |
| 49 | + mysql_attr_first_join(t2, attr_name, result, summary) and |
| 50 | + t = t2.append(summary) |
| 51 | + ) |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + pragma[nomagic] |
| 56 | + private predicate mysql_attr_first_join( |
| 57 | + DataFlow::TypeTracker t2, string attr_name, DataFlow::Node res, DataFlow::StepSummary summary |
| 58 | + ) { |
| 59 | + DataFlow::StepSummary::step(mysql_attr(t2, attr_name), res, summary) |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets a reference to the attribute `attr_name` of the `mysql` module. |
| 64 | + * WARNING: Only holds for a few predefined attributes. |
| 65 | + */ |
| 66 | + private DataFlow::Node mysql_attr(string attr_name) { |
| 67 | + result = mysql_attr(DataFlow::TypeTracker::end(), attr_name) |
| 68 | + } |
| 69 | + |
| 70 | + /** Provides models for the `mysql` module. */ |
| 71 | + module mysql { |
| 72 | + /** |
| 73 | + * The mysql.connector module |
| 74 | + * See https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html |
| 75 | + */ |
| 76 | + class MysqlConnector extends PEP249Module { |
| 77 | + MysqlConnector() { this = mysql_attr("connector") } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments