Skip to content

Commit abcdfe3

Browse files
committed
use LibraryName class for websocket library names
1 parent f7faaa6 commit abcdfe3

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

javascript/ql/src/semmle/javascript/frameworks/WebSocket.qll

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ private module LibraryNames {
2727
string websocket() { result = "WebSocket" }
2828

2929
string ws() { result = "ws" }
30+
31+
class LibraryName extends string {
32+
LibraryName() {
33+
this = sockjs() or this = websocket() or this = ws()
34+
}
35+
}
3036
}
3137

3238
/**
3339
* Holds if the websocket library named `client` can send a message to the library named `server`.
3440
* Both `client` and `server` are library names defined in `LibraryNames`.
3541
*/
36-
private predicate areLibrariesCompatible(string client, string server) {
42+
private predicate areLibrariesCompatible(LibraryNames::LibraryName client, LibraryNames::LibraryName server) {
3743
// sockjs is a WebSocket emulating library, but not actually an implementation of WebSockets.
3844
client = LibraryNames::sockjs() and server = LibraryNames::sockjs()
3945
or
@@ -51,7 +57,7 @@ module ClientWebSocket {
5157
* A class that can be used to instantiate a WebSocket instance.
5258
*/
5359
class SocketClass extends DataFlow::SourceNode {
54-
string library; // the name of the WebSocket library. Can be one of the libraries defined in `LibraryNames`.
60+
LibraryName library; // the name of the WebSocket library. Can be one of the libraries defined in `LibraryNames`.
5561

5662
SocketClass() {
5763
this = DataFlow::globalVarRef("WebSocket") and library = websocket()
@@ -69,7 +75,7 @@ module ClientWebSocket {
6975
/**
7076
* Gets the WebSocket library name.
7177
*/
72-
string getLibrary() { result = library }
78+
LibraryName getLibrary() { result = library }
7379
}
7480

7581
/**
@@ -83,7 +89,7 @@ module ClientWebSocket {
8389
/**
8490
* Gets the WebSocket library name.
8591
*/
86-
string getLibrary() { result = socketClass.getLibrary() }
92+
LibraryName getLibrary() { result = socketClass.getLibrary() }
8793
}
8894

8995
/**
@@ -166,7 +172,7 @@ module ServerWebSocket {
166172
/**
167173
* Gets a server created by a library named `library`.
168174
*/
169-
DataFlow::SourceNode getAServer(string library) {
175+
DataFlow::SourceNode getAServer(LibraryName library) {
170176
library = ws() and
171177
result = DataFlow::moduleImport("ws").getAConstructorInvocation("Server")
172178
or
@@ -178,7 +184,7 @@ module ServerWebSocket {
178184
* A server WebSocket instance.
179185
*/
180186
class ServerSocket extends EventEmitter::Range, DataFlow::SourceNode {
181-
string library;
187+
LibraryName library;
182188

183189
ServerSocket() {
184190
exists(DataFlow::CallNode onCall |
@@ -192,7 +198,7 @@ module ServerWebSocket {
192198
/**
193199
* Gets the name of the library that created this server socket.
194200
*/
195-
string getLibrary() { result = library }
201+
LibraryName getLibrary() { result = library }
196202
}
197203

198204
/**

0 commit comments

Comments
 (0)