|
| 1 | +/** |
| 2 | + * Provides classes for working with [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket). |
| 3 | + * |
| 4 | + * The model is based on the EventEmitter model, and there a therefore a |
| 5 | + * data-flow step from where a WebSocket event is send to where the message |
| 6 | + * is received. |
| 7 | + * |
| 8 | + * WebSockets include no concept of channels, therefore every client can send |
| 9 | + * to every server (and vice versa). |
| 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 | +module ClientWebSocket { |
| 22 | + /** |
| 23 | + * A class that can be used to instantiate a WebSocket instance. |
| 24 | + */ |
| 25 | + class SocketClass extends DataFlow::SourceNode { |
| 26 | + boolean isNode; |
| 27 | + |
| 28 | + SocketClass() { |
| 29 | + this = DataFlow::globalVarRef("WebSocket") and isNode = false |
| 30 | + or |
| 31 | + this = DataFlow::moduleImport("ws") and isNode = true |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Holds if this class an import of the "ws" module. |
| 36 | + */ |
| 37 | + predicate isNode() { isNode = true } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * A client WebSocket instance. |
| 42 | + */ |
| 43 | + class ClientSocket extends EventEmitter::Range, DataFlow::SourceNode { |
| 44 | + SocketClass socketClass; |
| 45 | + |
| 46 | + ClientSocket() { this = socketClass.getAnInstantiation() } |
| 47 | + |
| 48 | + /** |
| 49 | + * Holds if this ClientSocket is created from the "ws" module. |
| 50 | + * |
| 51 | + * The predicate is used to differentiate where the behavior of the "ws" module differs from the native WebSocket in browsers. |
| 52 | + */ |
| 53 | + predicate isNode() { socketClass.isNode() } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * A message sent from a WebSocket client. |
| 58 | + */ |
| 59 | + class SendNode extends EventDispatch::Range, DataFlow::CallNode { |
| 60 | + override ClientSocket emitter; |
| 61 | + |
| 62 | + SendNode() { this = emitter.getAMemberCall("send") } |
| 63 | + |
| 64 | + override string getChannel() { result = channelName() } |
| 65 | + |
| 66 | + override DataFlow::Node getSentItem(int i) { i = 0 and result = this.getArgument(0) } |
| 67 | + |
| 68 | + override ServerWebSocket::ReceiveNode getAReceiver() { any() } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Gets a methodName that can be used to register a listener for WebSocket messages on a given socket. |
| 73 | + */ |
| 74 | + string getARegisterMethodName(ClientSocket socket) { |
| 75 | + result = "addEventListener" |
| 76 | + or |
| 77 | + socket.isNode() and |
| 78 | + result = EventEmitter::on() |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * A handler that is registered to receive messages from a WebSocket. |
| 83 | + * |
| 84 | + * If the registration happens with the "addEventListener" method or the "onmessage" setter property, then the handler receives an event with a "data" property. |
| 85 | + * Otherwise the handler receives the data directly. |
| 86 | + * |
| 87 | + * This confusing API is caused by the "ws" library only mostly using their own API, where event objects are not used. |
| 88 | + * But the "ws" library additionally supports the WebSocket API from browsers, which exclusively use event objects with a "data" property. |
| 89 | + */ |
| 90 | + class ReceiveNode extends EventRegistration::Range, DataFlow::FunctionNode { |
| 91 | + override ClientSocket emitter; |
| 92 | + boolean receivesEvent; |
| 93 | + |
| 94 | + ReceiveNode() { |
| 95 | + exists(DataFlow::CallNode call, string methodName | |
| 96 | + methodName = getARegisterMethodName(emitter) and |
| 97 | + call = emitter.getAMemberCall(methodName) and |
| 98 | + call.getArgument(0).mayHaveStringValue("message") and |
| 99 | + this = call.getCallback(1) and |
| 100 | + if methodName = "addEventListener" then receivesEvent = true else receivesEvent = false |
| 101 | + ) |
| 102 | + or |
| 103 | + this = emitter.getAPropertyWrite("onmessage").getRhs() and |
| 104 | + receivesEvent = true |
| 105 | + } |
| 106 | + |
| 107 | + override string getChannel() { result = channelName() } |
| 108 | + |
| 109 | + override DataFlow::Node getReceivedItem(int i) { |
| 110 | + i = 0 and |
| 111 | + result = this.getParameter(0).getAPropertyRead("data") and |
| 112 | + receivesEvent = true |
| 113 | + or |
| 114 | + i = 0 and |
| 115 | + result = this.getParameter(0) and |
| 116 | + receivesEvent = false |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +module ServerWebSocket { |
| 122 | + /** |
| 123 | + * A server WebSocket instance. |
| 124 | + */ |
| 125 | + class ServerSocket extends EventEmitter::Range, DataFlow::SourceNode { |
| 126 | + ServerSocket() { |
| 127 | + exists(DataFlow::CallNode onCall | |
| 128 | + onCall = DataFlow::moduleImport("ws") |
| 129 | + .getAConstructorInvocation("Server") |
| 130 | + .getAMemberCall(EventEmitter::on()) and |
| 131 | + onCall.getArgument(0).mayHaveStringValue("connection") |
| 132 | + | |
| 133 | + this = onCall.getCallback(1).getParameter(0) |
| 134 | + ) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * A message sent from a WebSocket server. |
| 140 | + */ |
| 141 | + class SendNode extends EventDispatch::Range, DataFlow::CallNode { |
| 142 | + override ServerSocket emitter; |
| 143 | + |
| 144 | + SendNode() { this = emitter.getAMemberCall("send") } |
| 145 | + |
| 146 | + override string getChannel() { result = channelName() } |
| 147 | + |
| 148 | + override DataFlow::Node getSentItem(int i) { |
| 149 | + i = 0 and |
| 150 | + result = getArgument(0) |
| 151 | + } |
| 152 | + |
| 153 | + override ClientWebSocket::ReceiveNode getAReceiver() { any() } |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * A registration of an event handler that receives data from a WebSocket. |
| 158 | + */ |
| 159 | + class ReceiveNode extends EventRegistration::Range, DataFlow::CallNode { |
| 160 | + override ServerSocket emitter; |
| 161 | + |
| 162 | + ReceiveNode() { |
| 163 | + this = emitter.getAMemberCall(EventEmitter::on()) and |
| 164 | + this.getArgument(0).mayHaveStringValue("message") |
| 165 | + } |
| 166 | + |
| 167 | + override string getChannel() { result = channelName() } |
| 168 | + |
| 169 | + override DataFlow::Node getReceivedItem(int i) { |
| 170 | + i = 0 and |
| 171 | + result = this.getCallback(1).getParameter(0) |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * A data flow node representing data received from a client, viewed as remote user input. |
| 177 | + */ |
| 178 | + private class ReceivedItemAsRemoteFlow extends RemoteFlowSource { |
| 179 | + ReceivedItemAsRemoteFlow() { this = any(ReceiveNode rercv).getReceivedItem(_) } |
| 180 | + |
| 181 | + override string getSourceType() { result = "WebSocket client data" } |
| 182 | + |
| 183 | + override predicate isUserControlledObject() { any() } |
| 184 | + } |
| 185 | +} |
0 commit comments