|
| 1 | +/** |
| 2 | + * Provides definitions and modeling for the `websockets` PyPI package. |
| 3 | + * |
| 4 | + * See https://websockets.readthedocs.io/en/stable/ |
| 5 | + */ |
| 6 | + |
| 7 | +private import python |
| 8 | +private import semmle.python.dataflow.new.RemoteFlowSources |
| 9 | +private import semmle.python.Concepts |
| 10 | +private import semmle.python.ApiGraphs |
| 11 | +private import semmle.python.frameworks.internal.PoorMansFunctionResolution |
| 12 | +private import semmle.python.frameworks.internal.InstanceTaintStepsHelper |
| 13 | + |
| 14 | +/** |
| 15 | + * Provides models for the `websockets` PyPI package. |
| 16 | + * See https://websockets.readthedocs.io/en/stable/ |
| 17 | + */ |
| 18 | +module Websockets { |
| 19 | + private class HandlerArg extends DataFlow::Node { |
| 20 | + HandlerArg() { |
| 21 | + exists(DataFlow::CallCfgNode c | |
| 22 | + c = |
| 23 | + API::moduleImport("websockets") |
| 24 | + .getMember(["asyncio", "sync"]) |
| 25 | + .getMember("server") |
| 26 | + .getMember(["serve", "unix_serve"]) |
| 27 | + .getACall() |
| 28 | + | |
| 29 | + (this = c.getArg(0) or this = c.getArgByName("handler")) |
| 30 | + ) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /** A websocket handler that is passed to `serve`. */ |
| 35 | + // TODO: handlers defined via route maps, e.g. through `websockets.asyncio.router.route`, are more complex to handle. |
| 36 | + class WebSocketHandler extends Http::Server::RequestHandler::Range { |
| 37 | + WebSocketHandler() { poorMansFunctionTracker(this) = any(HandlerArg a) } |
| 38 | + |
| 39 | + override Parameter getARoutedParameter() { result = this.getAnArg() } |
| 40 | + |
| 41 | + override string getFramework() { result = "websockets" } |
| 42 | + } |
| 43 | + |
| 44 | + /** Provides taint models for instances of `ServerConnection` objects passed to websocket handlers. */ |
| 45 | + module ServerConnection { |
| 46 | + /** |
| 47 | + * A source of instances of `websockets.asyncio.ServerConnection` and `websockets.sync.ServerConnection`, extend this class to model new instances. |
| 48 | + * |
| 49 | + * This can include instantiations of the class, return values from function |
| 50 | + * calls, or a special parameter that will be set when functions are called by an external |
| 51 | + * library. |
| 52 | + * |
| 53 | + * Use the predicate `ServerConnection::instance()` to get references to instances of `websockets.asyncio.ServerConnection` and `websockets.sync.ServerConnection`. |
| 54 | + */ |
| 55 | + abstract class InstanceSource extends DataFlow::LocalSourceNode { } |
| 56 | + |
| 57 | + /** Gets a reference to an instance of `websockets.asyncio.ServerConnection` or `websockets.sync.ServerConnection`. */ |
| 58 | + private DataFlow::TypeTrackingNode instance(DataFlow::TypeTracker t) { |
| 59 | + t.start() and |
| 60 | + result instanceof InstanceSource |
| 61 | + or |
| 62 | + exists(DataFlow::TypeTracker t2 | result = instance(t2).track(t2, t)) |
| 63 | + } |
| 64 | + |
| 65 | + /** Gets a reference to an instance of `websockets.asyncio.ServerConnection` or `websockets.sync.ServerConnection`. */ |
| 66 | + DataFlow::Node instance() { instance(DataFlow::TypeTracker::end()).flowsTo(result) } |
| 67 | + |
| 68 | + private class HandlerParam extends DataFlow::Node, InstanceSource { |
| 69 | + HandlerParam() { exists(WebSocketHandler h | this = DataFlow::parameterNode(h.getArg(0))) } |
| 70 | + } |
| 71 | + |
| 72 | + private class InstanceTaintSteps extends InstanceTaintStepsHelper { |
| 73 | + InstanceTaintSteps() { this = "websockets.asyncio.ServerConnection" } |
| 74 | + |
| 75 | + override DataFlow::Node getInstance() { result = instance() } |
| 76 | + |
| 77 | + override string getAttributeName() { none() } |
| 78 | + |
| 79 | + override string getAsyncMethodName() { result = ["recv", "recv_streaming"] } |
| 80 | + |
| 81 | + override string getMethodName() { result = ["recv", "recv_streaming"] } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments