|
| 1 | +/** |
| 2 | + * Provides classes for working with [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) and [ws](https://github.com/websockets/ws). |
| 3 | + * |
| 4 | + * The model is based on the EventEmitter model, and there is therefore a |
| 5 | + * data-flow step from where a WebSocket event is sent to where the message |
| 6 | + * is received. |
| 7 | + * |
| 8 | + * Data flow is modeled both from clients to servers, and from servers to clients. |
| 9 | + * The model models that clients can send messages to all servers, and servers can send messages to all clients. |
| 10 | + */ |
| 11 | + |
| 12 | +import javascript |
| 13 | + |
| 14 | +/** |
| 15 | + * Gets the channel name used throughout this WebSocket model. |
| 16 | + * WebSockets don't have a concept of channels, and therefore a singleton name is used. |
| 17 | + * The name can be anything, as long as it is used consistently in this WebSocket model. |
| 18 | + */ |
| 19 | +private string channelName() { result = "message" } |
| 20 | + |
| 21 | +/** |
| 22 | + * Provides classes that model WebSockets clients. |
| 23 | + */ |
| 24 | +module ClientWebSocket { |
| 25 | + /** |
| 26 | + * A class that can be used to instantiate a WebSocket instance. |
| 27 | + */ |
| 28 | + class SocketClass extends DataFlow::SourceNode { |
| 29 | + boolean isNode; |
| 30 | + |
| 31 | + SocketClass() { |
| 32 | + this = DataFlow::globalVarRef("WebSocket") and isNode = false |
| 33 | + or |
| 34 | + this = DataFlow::moduleImport("ws") and isNode = true |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Holds if this class is an import of the "ws" module. |
| 39 | + */ |
| 40 | + predicate isNode() { isNode = true } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * A client WebSocket instance. |
| 45 | + */ |
| 46 | + class ClientSocket extends EventEmitter::Range, DataFlow::SourceNode { |
| 47 | + SocketClass socketClass; |
| 48 | + |
| 49 | + ClientSocket() { this = socketClass.getAnInstantiation() } |
| 50 | + |
| 51 | + /** |
| 52 | + * Holds if this ClientSocket is created from the "ws" module. |
| 53 | + * |
| 54 | + * The predicate is used to differentiate where the behavior of the "ws" module differs from the native WebSocket in browsers. |
| 55 | + */ |
| 56 | + predicate isNode() { socketClass.isNode() } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * A message sent from a WebSocket client. |
| 61 | + */ |
| 62 | + class SendNode extends EventDispatch::Range, DataFlow::CallNode { |
| 63 | + override ClientSocket emitter; |
| 64 | + |
| 65 | + SendNode() { this = emitter.getAMemberCall("send") } |
| 66 | + |
| 67 | + override string getChannel() { result = channelName() } |
| 68 | + |
| 69 | + override DataFlow::Node getSentItem(int i) { i = 0 and result = this.getArgument(0) } |
| 70 | + |
| 71 | + override ServerWebSocket::ReceiveNode getAReceiver() { any() } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * A handler that is registered to receive messages from a WebSocket. |
| 76 | + */ |
| 77 | + abstract class ReceiveNode extends EventRegistration::Range, DataFlow::FunctionNode { |
| 78 | + override ClientSocket emitter; |
| 79 | + |
| 80 | + override string getChannel() { result = channelName() } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Gets a handler, that is registered using method `methodName` and receives messages sent to `emitter`. |
| 85 | + */ |
| 86 | + private DataFlow::FunctionNode getAMessageHandler(ClientWebSocket::ClientSocket emitter, string methodName) { |
| 87 | + exists(DataFlow::CallNode call | |
| 88 | + call = emitter.getAMemberCall(methodName) and |
| 89 | + call.getArgument(0).mayHaveStringValue("message") and |
| 90 | + result = call.getCallback(1) |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * A handler that receives a message using the WebSocket API. |
| 96 | + * The WebSocket API is used both by the WebSocket library in browsers, and the same API is also implemented as part of the "ws" library. |
| 97 | + * This class therefore models both the WebSocket library, and a subset of the "ws" library. |
| 98 | + */ |
| 99 | + private class WebSocketReceiveNode extends ClientWebSocket::ReceiveNode { |
| 100 | + WebSocketReceiveNode() { |
| 101 | + this = getAMessageHandler(emitter, "addEventListener") |
| 102 | + or |
| 103 | + this = emitter.getAPropertyWrite("onmessage").getRhs() |
| 104 | + } |
| 105 | + |
| 106 | + override DataFlow::Node getReceivedItem(int i) { |
| 107 | + i = 0 and result = this.getParameter(0).getAPropertyRead("data") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * A handler that receives a message using the API from the "ws" library. |
| 113 | + * The "ws" library additionally implements the WebSocket API, which is modeled in the `WebSocketReceiveNode` class. |
| 114 | + */ |
| 115 | + private class WSReceiveNode extends ClientWebSocket::ReceiveNode { |
| 116 | + WSReceiveNode () { |
| 117 | + emitter.isNode() and |
| 118 | + this = getAMessageHandler(emitter, EventEmitter::on()) |
| 119 | + } |
| 120 | + |
| 121 | + override DataFlow::Node getReceivedItem(int i) { |
| 122 | + i = 0 and result = this.getParameter(0) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Provides classes that model WebSocket servers. |
| 129 | + */ |
| 130 | +module ServerWebSocket { |
| 131 | + /** |
| 132 | + * A server WebSocket instance. |
| 133 | + */ |
| 134 | + class ServerSocket extends EventEmitter::Range, DataFlow::SourceNode { |
| 135 | + ServerSocket() { |
| 136 | + exists(DataFlow::CallNode onCall | |
| 137 | + onCall = DataFlow::moduleImport("ws") |
| 138 | + .getAConstructorInvocation("Server") |
| 139 | + .getAMemberCall(EventEmitter::on()) and |
| 140 | + onCall.getArgument(0).mayHaveStringValue("connection") |
| 141 | + | |
| 142 | + this = onCall.getCallback(1).getParameter(0) |
| 143 | + ) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * A message sent from a WebSocket server. |
| 149 | + */ |
| 150 | + class SendNode extends EventDispatch::Range, DataFlow::CallNode { |
| 151 | + override ServerSocket emitter; |
| 152 | + |
| 153 | + SendNode() { this = emitter.getAMemberCall("send") } |
| 154 | + |
| 155 | + override string getChannel() { result = channelName() } |
| 156 | + |
| 157 | + override DataFlow::Node getSentItem(int i) { |
| 158 | + i = 0 and |
| 159 | + result = getArgument(0) |
| 160 | + } |
| 161 | + |
| 162 | + override ClientWebSocket::ReceiveNode getAReceiver() { any() } |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * A registration of an event handler that receives data from a WebSocket. |
| 167 | + */ |
| 168 | + class ReceiveNode extends EventRegistration::Range, DataFlow::CallNode { |
| 169 | + override ServerSocket emitter; |
| 170 | + |
| 171 | + ReceiveNode() { |
| 172 | + this = emitter.getAMemberCall(EventEmitter::on()) and |
| 173 | + this.getArgument(0).mayHaveStringValue("message") |
| 174 | + } |
| 175 | + |
| 176 | + override string getChannel() { result = channelName() } |
| 177 | + |
| 178 | + override DataFlow::Node getReceivedItem(int i) { |
| 179 | + i = 0 and |
| 180 | + result = this.getCallback(1).getParameter(0) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * A data flow node representing data received from a client, viewed as remote user input. |
| 186 | + */ |
| 187 | + private class ReceivedItemAsRemoteFlow extends RemoteFlowSource { |
| 188 | + ReceivedItemAsRemoteFlow() { this = any(ReceiveNode rercv).getReceivedItem(_) } |
| 189 | + |
| 190 | + override string getSourceType() { result = "WebSocket client data" } |
| 191 | + |
| 192 | + override predicate isUserControlledObject() { any() } |
| 193 | + } |
| 194 | +} |
0 commit comments