|
5 | 5 | private import python |
6 | 6 | private import experimental.dataflow.DataFlow |
7 | 7 | private import experimental.dataflow.RemoteFlowSources |
| 8 | +private import experimental.dataflow.TaintTracking |
8 | 9 | private import experimental.semmle.python.Concepts |
| 10 | +private import experimental.semmle.python.frameworks.Werkzeug |
9 | 11 |
|
10 | | -private module Flask { } |
| 12 | +// for old improved impl see |
| 13 | +// https://github.com/github/codeql/blob/9f95212e103c68d0c1dfa4b6f30fb5d53954ccef/python/ql/src/semmle/python/web/flask/Request.qll |
| 14 | +private module Flask { |
| 15 | + /** Gets a reference to the `flask` module. */ |
| 16 | + DataFlow::Node flask(DataFlow::TypeTracker t) { |
| 17 | + t.start() and |
| 18 | + result = DataFlow::importModule("flask") |
| 19 | + or |
| 20 | + exists(DataFlow::TypeTracker t2 | result = flask(t2).track(t2, t)) |
| 21 | + } |
| 22 | + |
| 23 | + /** Gets a reference to the `flask` module. */ |
| 24 | + DataFlow::Node flask() { result = flask(DataFlow::TypeTracker::end()) } |
| 25 | + |
| 26 | + module flask { |
| 27 | + /** Gets a reference to the `flask.request` object. */ |
| 28 | + DataFlow::Node request(DataFlow::TypeTracker t) { |
| 29 | + t.start() and |
| 30 | + result = DataFlow::importMember("flask", "request") |
| 31 | + or |
| 32 | + t.startInAttr("request") and |
| 33 | + result = flask() |
| 34 | + or |
| 35 | + exists(DataFlow::TypeTracker t2 | result = flask::request(t2).track(t2, t)) |
| 36 | + } |
| 37 | + |
| 38 | + /** Gets a reference to the `flask.request` object. */ |
| 39 | + DataFlow::Node request() { result = flask::request(DataFlow::TypeTracker::end()) } |
| 40 | + } |
| 41 | + |
| 42 | + // TODO: Do we even need this class? :| |
| 43 | + /** |
| 44 | + * A source of remote flow from a flask request. |
| 45 | + * |
| 46 | + * See https://flask.palletsprojects.com/en/1.1.x/api/#flask.Request |
| 47 | + */ |
| 48 | + private class RequestSource extends RemoteFlowSource::Range { |
| 49 | + RequestSource() { this = flask::request() } |
| 50 | + |
| 51 | + override string getSourceType() { result = "flask.request" } |
| 52 | + } |
| 53 | + |
| 54 | + private module FlaskRequestTracking { |
| 55 | + private DataFlow::Node tainted_methods(string attr_name, DataFlow::TypeTracker t) { |
| 56 | + attr_name in ["get_data", "get_json"] and |
| 57 | + t.startInAttr(attr_name) and |
| 58 | + result = flask::request() |
| 59 | + or |
| 60 | + exists(DataFlow::TypeTracker t2 | result = tainted_methods(attr_name, t2).track(t2, t)) |
| 61 | + } |
| 62 | + |
| 63 | + DataFlow::Node tainted_methods(string attr_name) { |
| 64 | + result = tainted_methods(attr_name, DataFlow::TypeTracker::end()) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * A source of remote flow from attributes from a flask request. |
| 70 | + * |
| 71 | + * See https://flask.palletsprojects.com/en/1.1.x/api/#flask.Request |
| 72 | + */ |
| 73 | + private class RequestInputAccess extends RemoteFlowSource::Range { |
| 74 | + string attr_name; |
| 75 | + |
| 76 | + RequestInputAccess() { |
| 77 | + // attributes |
| 78 | + exists(AttrNode attr | |
| 79 | + this.asCfgNode() = attr and attr.getObject(attr_name) = flask::request().asCfgNode() |
| 80 | + | |
| 81 | + attr_name in ["path", |
| 82 | + // str |
| 83 | + "full_path", "base_url", "url", "access_control_request_method", "content_encoding", |
| 84 | + "content_md5", "content_type", "data", "method", "mimetype", "origin", "query_string", |
| 85 | + "referrer", "remote_addr", "remote_user", "user_agent", |
| 86 | + // dict |
| 87 | + "environ", "cookies", "mimetype_params", "view_args", |
| 88 | + // json |
| 89 | + "json", |
| 90 | + // List[str] |
| 91 | + "access_route", |
| 92 | + // file-like |
| 93 | + "stream", "input_stream", |
| 94 | + // MultiDict[str, str] |
| 95 | + // https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.MultiDict |
| 96 | + "args", "values", "form", |
| 97 | + // MultiDict[str, FileStorage] |
| 98 | + // https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.FileStorage |
| 99 | + // TODO: FileStorage needs extra taint steps |
| 100 | + "files", |
| 101 | + // https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.HeaderSet |
| 102 | + "access_control_request_headers", "pragma", |
| 103 | + // https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.Accept |
| 104 | + // TODO: Kinda badly modeled for now -- has type List[Tuple[value, quality]], and some extra methods |
| 105 | + "accept_charsets", "accept_encodings", "accept_languages", "accept_mimetypes", |
| 106 | + // https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.Authorization |
| 107 | + // TODO: dict subclass with extra attributes like `username` and `password` |
| 108 | + "authorization", |
| 109 | + // https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.RequestCacheControl |
| 110 | + // TODO: has attributes like `no_cache`, and `to_header` method (actually, many of these models do) |
| 111 | + "cache_control", |
| 112 | + // https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.Headers |
| 113 | + // TODO: dict-like with wsgiref.headers.Header compatibility methods |
| 114 | + "headers"] |
| 115 | + ) |
| 116 | + or |
| 117 | + // methods (needs special handling to track bound-methods -- see `FlaskRequestMethodCallsAdditionalTaintStep` below) |
| 118 | + this = FlaskRequestTracking::tainted_methods(attr_name) |
| 119 | + } |
| 120 | + |
| 121 | + override string getSourceType() { result = "flask.request input" } |
| 122 | + } |
| 123 | + |
| 124 | + private class FlaskRequestMethodCallsAdditionalTaintStep extends TaintTracking::AdditionalTaintStep { |
| 125 | + override predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 126 | + // NOTE: `request -> request.tainted_method` part is handled as part of RequestInputAccess |
| 127 | + // tainted_method -> tainted_method() |
| 128 | + nodeFrom = FlaskRequestTracking::tainted_methods(_) and |
| 129 | + nodeTo.asCfgNode().(CallNode).getFunction() = nodeFrom.asCfgNode() |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private class RequestInputMultiDict extends RequestInputAccess, |
| 134 | + Werkzeug::Datastructures::MultiDict { |
| 135 | + RequestInputMultiDict() { attr_name in ["args", "values", "form", "files"] } |
| 136 | + } |
| 137 | + |
| 138 | + private class RequestInputFiles extends RequestInputMultiDict { |
| 139 | + RequestInputFiles() { attr_name = "files" } |
| 140 | + } |
| 141 | + // TODO: Somehow specify that elements of `RequestInputFiles` are |
| 142 | + // Werkzeug::Datastructures::FileStorage and should have those additional taint steps |
| 143 | + // AND that the 0-indexed argument to its' save method is a sink for path-injection. |
| 144 | + // https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.FileStorage.save |
| 145 | +} |
0 commit comments