|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the `starlette` PyPI package. |
| 3 | + * |
| 4 | + * See |
| 5 | + * - https://pypi.org/project/starlette/ |
| 6 | + * - https://www.starlette.io/ |
| 7 | + */ |
| 8 | + |
| 9 | +private import python |
| 10 | +private import semmle.python.dataflow.new.DataFlow |
| 11 | +private import semmle.python.dataflow.new.TaintTracking |
| 12 | +private import semmle.python.Concepts |
| 13 | +private import semmle.python.ApiGraphs |
| 14 | +private import semmle.python.frameworks.internal.InstanceTaintStepsHelper |
| 15 | +private import semmle.python.frameworks.Stdlib |
| 16 | + |
| 17 | +/** |
| 18 | + * INTERNAL: Do not use. |
| 19 | + * |
| 20 | + * Provides models for `starlette` PyPI package. |
| 21 | + * |
| 22 | + * See |
| 23 | + * - https://pypi.org/project/starlette/ |
| 24 | + * - https://www.starlette.io/ |
| 25 | + */ |
| 26 | +module Starlette { |
| 27 | + /** |
| 28 | + * Provides models for the `starlette.websockets.WebSocket` class |
| 29 | + * |
| 30 | + * See https://www.starlette.io/websockets/. |
| 31 | + */ |
| 32 | + module WebSocket { |
| 33 | + /** Gets a reference to the `starlette.websockets.WebSocket` class. */ |
| 34 | + API::Node classRef() { |
| 35 | + result = API::moduleImport("starlette").getMember("websockets").getMember("WebSocket") |
| 36 | + or |
| 37 | + result = API::moduleImport("fastapi").getMember("WebSocket") |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * A source of instances of `starlette.websockets.WebSocket`, extend this class to model new instances. |
| 42 | + * |
| 43 | + * This can include instantiations of the class, return values from function |
| 44 | + * calls, or a special parameter that will be set when functions are called by an external |
| 45 | + * library. |
| 46 | + * |
| 47 | + * Use the predicate `WebSocket::instance()` to get references to instances of `starlette.websockets.WebSocket`. |
| 48 | + */ |
| 49 | + abstract class InstanceSource extends DataFlow::LocalSourceNode { } |
| 50 | + |
| 51 | + /** A direct instantiation of `starlette.websockets.WebSocket`. */ |
| 52 | + private class ClassInstantiation extends InstanceSource, DataFlow::CallCfgNode { |
| 53 | + ClassInstantiation() { this = classRef().getACall() } |
| 54 | + } |
| 55 | + |
| 56 | + /** Gets a reference to an instance of `starlette.websockets.WebSocket`. */ |
| 57 | + private DataFlow::TypeTrackingNode instance(DataFlow::TypeTracker t) { |
| 58 | + t.start() and |
| 59 | + result instanceof InstanceSource |
| 60 | + or |
| 61 | + exists(DataFlow::TypeTracker t2 | result = instance(t2).track(t2, t)) |
| 62 | + } |
| 63 | + |
| 64 | + /** Gets a reference to an instance of `starlette.websockets.WebSocket`. */ |
| 65 | + DataFlow::Node instance() { instance(DataFlow::TypeTracker::end()).flowsTo(result) } |
| 66 | + |
| 67 | + /** |
| 68 | + * Taint propagation for `starlette.websockets.WebSocket`. |
| 69 | + */ |
| 70 | + private class InstanceTaintSteps extends InstanceTaintStepsHelper { |
| 71 | + InstanceTaintSteps() { this = "starlette.websockets.WebSocket" } |
| 72 | + |
| 73 | + override DataFlow::Node getInstance() { result = instance() } |
| 74 | + |
| 75 | + override string getAttributeName() { result in ["url", "headers", "query_params", "cookies"] } |
| 76 | + |
| 77 | + override string getMethodName() { none() } |
| 78 | + |
| 79 | + override string getAsyncMethodName() { |
| 80 | + result in [ |
| 81 | + "receive", "receive_bytes", "receive_text", "receive_json", "iter_bytes", "iter_text", |
| 82 | + "iter_json" |
| 83 | + ] |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** An attribute read on a `starlette.websockets.WebSocket` instance that is a `starlette.requests.URL` instance. */ |
| 88 | + private class UrlInstances extends URL::InstanceSource { |
| 89 | + UrlInstances() { |
| 90 | + this.(DataFlow::AttrRead).getObject() = instance() and |
| 91 | + this.(DataFlow::AttrRead).getAttributeName() = "url" |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Provides models for the `starlette.requests.URL` class |
| 98 | + * |
| 99 | + * See the URL part of https://www.starlette.io/websockets/. |
| 100 | + */ |
| 101 | + module URL { |
| 102 | + /** Gets a reference to the `starlette.requests.URL` class. */ |
| 103 | + private API::Node classRef() { |
| 104 | + result = API::moduleImport("starlette").getMember("requests").getMember("URL") |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * A source of instances of `starlette.requests.URL`, extend this class to model new instances. |
| 109 | + * |
| 110 | + * This can include instantiations of the class, return values from function |
| 111 | + * calls, or a special parameter that will be set when functions are called by an external |
| 112 | + * library. |
| 113 | + * |
| 114 | + * Use the predicate `URL::instance()` to get references to instances of `starlette.requests.URL`. |
| 115 | + */ |
| 116 | + abstract class InstanceSource extends DataFlow::LocalSourceNode { } |
| 117 | + |
| 118 | + /** A direct instantiation of `starlette.requests.URL`. */ |
| 119 | + private class ClassInstantiation extends InstanceSource, DataFlow::CallCfgNode { |
| 120 | + ClassInstantiation() { this = classRef().getACall() } |
| 121 | + } |
| 122 | + |
| 123 | + /** Gets a reference to an instance of `starlette.requests.URL`. */ |
| 124 | + private DataFlow::TypeTrackingNode instance(DataFlow::TypeTracker t) { |
| 125 | + t.start() and |
| 126 | + result instanceof InstanceSource |
| 127 | + or |
| 128 | + exists(DataFlow::TypeTracker t2 | result = instance(t2).track(t2, t)) |
| 129 | + } |
| 130 | + |
| 131 | + /** Gets a reference to an instance of `starlette.requests.URL`. */ |
| 132 | + DataFlow::Node instance() { instance(DataFlow::TypeTracker::end()).flowsTo(result) } |
| 133 | + |
| 134 | + /** |
| 135 | + * Taint propagation for `starlette.requests.URL`. |
| 136 | + */ |
| 137 | + private class InstanceTaintSteps extends InstanceTaintStepsHelper { |
| 138 | + InstanceTaintSteps() { this = "starlette.requests.URL" } |
| 139 | + |
| 140 | + override DataFlow::Node getInstance() { result = instance() } |
| 141 | + |
| 142 | + override string getAttributeName() { |
| 143 | + result in [ |
| 144 | + "components", "netloc", "path", "query", "fragment", "username", "password", "hostname", |
| 145 | + "port" |
| 146 | + ] |
| 147 | + } |
| 148 | + |
| 149 | + override string getMethodName() { none() } |
| 150 | + |
| 151 | + override string getAsyncMethodName() { none() } |
| 152 | + } |
| 153 | + |
| 154 | + /** An attribute read on a `starlette.requests.URL` instance that is a `urllib.parse.SplitResult` instance. */ |
| 155 | + private class UrlSplitInstances extends Stdlib::SplitResult::InstanceSource { |
| 156 | + UrlSplitInstances() { |
| 157 | + this.(DataFlow::AttrRead).getObject() = instance() and |
| 158 | + this.(DataFlow::AttrRead).getAttributeName() = "components" |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments