|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the `flask` PyPI package. |
| 3 | + * See https://flask.palletsprojects.com/en/1.1.x/. |
| 4 | + */ |
| 5 | + |
| 6 | +private import python |
| 7 | +private import semmle.python.frameworks.Flask |
| 8 | +private import semmle.python.dataflow.new.DataFlow |
| 9 | +private import experimental.semmle.python.Concepts |
| 10 | +private import semmle.python.ApiGraphs |
| 11 | + |
| 12 | +module ExperimentalFlask { |
| 13 | + /** |
| 14 | + * A reference to either `flask.make_response` function, or the `make_response` method on |
| 15 | + * an instance of `flask.Flask`. This creates an instance of the `flask_response` |
| 16 | + * class (class-attribute on a flask application), which by default is |
| 17 | + * `flask.Response`. |
| 18 | + * |
| 19 | + * See |
| 20 | + * - https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.make_response |
| 21 | + * - https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response |
| 22 | + */ |
| 23 | + private API::Node flaskMakeResponse() { |
| 24 | + result = |
| 25 | + [API::moduleImport("flask"), Flask::FlaskApp::instance()] |
| 26 | + .getMember(["make_response", "jsonify", "make_default_options_response"]) |
| 27 | + } |
| 28 | + |
| 29 | + /** Gets a reference to a header instance. */ |
| 30 | + private DataFlow::LocalSourceNode headerInstance(DataFlow::TypeTracker t) { |
| 31 | + t.start() and |
| 32 | + result.(DataFlow::AttrRead).getObject().getALocalSource() = |
| 33 | + [Flask::Response::classRef(), flaskMakeResponse()].getReturn().getAUse() |
| 34 | + or |
| 35 | + exists(DataFlow::TypeTracker t2 | result = headerInstance(t2).track(t2, t)) |
| 36 | + } |
| 37 | + |
| 38 | + /** Gets a reference to a header instance use. */ |
| 39 | + private DataFlow::Node headerInstance() { |
| 40 | + headerInstance(DataFlow::TypeTracker::end()).flowsTo(result) |
| 41 | + } |
| 42 | + |
| 43 | + /** Gets a reference to a header instance call/subscript */ |
| 44 | + private DataFlow::Node headerInstanceCall() { |
| 45 | + headerInstance() in [result.(DataFlow::AttrRead), result.(DataFlow::AttrRead).getObject()] or |
| 46 | + headerInstance().asExpr() = result.asExpr().(Subscript).getObject() |
| 47 | + } |
| 48 | + |
| 49 | + class FlaskHeaderDefinition extends DataFlow::Node, HeaderDeclaration::Range { |
| 50 | + DataFlow::Node headerInput; |
| 51 | + |
| 52 | + FlaskHeaderDefinition() { |
| 53 | + this.asCfgNode().(DefinitionNode) = headerInstanceCall().asCfgNode() and |
| 54 | + headerInput.asCfgNode() = this.asCfgNode().(DefinitionNode).getValue() |
| 55 | + } |
| 56 | + |
| 57 | + override DataFlow::Node getNameArg() { result.asExpr() = this.asExpr().(Subscript).getIndex() } |
| 58 | + |
| 59 | + override DataFlow::Node getValueArg() { result = headerInput } |
| 60 | + } |
| 61 | + |
| 62 | + private class FlaskMakeResponseExtend extends DataFlow::CallCfgNode, HeaderDeclaration::Range { |
| 63 | + KeyValuePair item; |
| 64 | + |
| 65 | + FlaskMakeResponseExtend() { |
| 66 | + this.getFunction() = headerInstanceCall() and |
| 67 | + item = this.getArg(_).asExpr().(Dict).getAnItem() |
| 68 | + } |
| 69 | + |
| 70 | + override DataFlow::Node getNameArg() { result.asExpr() = item.getKey() } |
| 71 | + |
| 72 | + override DataFlow::Node getValueArg() { result.asExpr() = item.getValue() } |
| 73 | + } |
| 74 | + |
| 75 | + private class FlaskResponse extends DataFlow::CallCfgNode, HeaderDeclaration::Range { |
| 76 | + KeyValuePair item; |
| 77 | + |
| 78 | + FlaskResponse() { this = Flask::Response::classRef().getACall() } |
| 79 | + |
| 80 | + override DataFlow::Node getNameArg() { result.asExpr() = item.getKey() } |
| 81 | + |
| 82 | + override DataFlow::Node getValueArg() { result.asExpr() = item.getValue() } |
| 83 | + } |
| 84 | +} |
0 commit comments