Skip to content

Commit cce64d8

Browse files
fix: closing sockjs server
1 parent b86fc8d commit cce64d8

File tree

8 files changed

+1545
-93
lines changed

8 files changed

+1545
-93
lines changed

lib/Server.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ class Server {
719719

720720
setupHostHeaderCheck() {
721721
this.app.all("*", (req, res, next) => {
722-
if (this.checkHostHeader(req.headers)) {
722+
if (this.checkHeader(req.headers, "host")) {
723723
return next();
724724
}
725725

@@ -1157,8 +1157,8 @@ class Server {
11571157

11581158
if (
11591159
!headers ||
1160-
!this.checkHostHeader(headers) ||
1161-
!this.checkOriginHeader(headers)
1160+
!this.checkHeader(headers, "host") ||
1161+
!this.checkHeader(headers, "origin")
11621162
) {
11631163
this.sendMessage([client], "error", "Invalid Host/Origin header");
11641164

@@ -1603,25 +1603,13 @@ class Server {
16031603
next();
16041604
}
16051605

1606-
checkHostHeader(headers) {
1607-
return this.checkHeader(headers, "host");
1608-
}
1609-
1610-
checkOriginHeader(headers) {
1611-
return this.checkHeader(headers, "origin");
1612-
}
1613-
16141606
checkHeader(headers, headerToCheck) {
16151607
// allow user to opt out of this security check, at their own risk
16161608
// by explicitly enabling allowedHosts
16171609
if (this.options.allowedHosts === "all") {
16181610
return true;
16191611
}
16201612

1621-
if (!headerToCheck) {
1622-
headerToCheck = "host";
1623-
}
1624-
16251613
// get the Host header and extract hostname
16261614
// we don't care about port not matching
16271615
const hostHeader = headers[headerToCheck];

lib/servers/SockJSServer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ module.exports = class SockJSServer extends BaseServer {
6565
this.implementation.on("connection", (client) => {
6666
// Implement the the same API as for `ws`
6767
client.send = client.write;
68+
client.terminate = client.close;
6869

6970
this.clients.add(client);
7071

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`allowed hosts should connect web socket client using "[::1] host to web socket server with the "auto" value ("sockjs"): console messages 1`] = `
4+
Array [
5+
"[HMR] Waiting for update signal from WDS...",
6+
"Hey.",
7+
"[webpack-dev-server] Hot Module Replacement enabled.",
8+
"[webpack-dev-server] Live Reloading enabled.",
9+
]
10+
`;
11+
12+
exports[`allowed hosts should connect web socket client using "[::1] host to web socket server with the "auto" value ("sockjs"): page errors 1`] = `Array []`;
13+
14+
exports[`allowed hosts should connect web socket client using "[::1] host to web socket server with the "auto" value ("ws"): console messages 1`] = `
15+
Array [
16+
"[HMR] Waiting for update signal from WDS...",
17+
"Hey.",
18+
"[webpack-dev-server] Hot Module Replacement enabled.",
19+
"[webpack-dev-server] Live Reloading enabled.",
20+
]
21+
`;
22+
23+
exports[`allowed hosts should connect web socket client using "[::1] host to web socket server with the "auto" value ("ws"): page errors 1`] = `Array []`;
24+
25+
exports[`allowed hosts should connect web socket client using "127.0.0.1" host to web socket server with the "auto" value ("sockjs"): console messages 1`] = `
26+
Array [
27+
"[HMR] Waiting for update signal from WDS...",
28+
"Hey.",
29+
"[webpack-dev-server] Hot Module Replacement enabled.",
30+
"[webpack-dev-server] Live Reloading enabled.",
31+
]
32+
`;
33+
34+
exports[`allowed hosts should connect web socket client using "127.0.0.1" host to web socket server with the "auto" value ("sockjs"): page errors 1`] = `Array []`;
35+
36+
exports[`allowed hosts should connect web socket client using "127.0.0.1" host to web socket server with the "auto" value ("ws"): console messages 1`] = `
37+
Array [
38+
"[HMR] Waiting for update signal from WDS...",
39+
"Hey.",
40+
"[webpack-dev-server] Hot Module Replacement enabled.",
41+
"[webpack-dev-server] Live Reloading enabled.",
42+
]
43+
`;
44+
45+
exports[`allowed hosts should connect web socket client using "127.0.0.1" host to web socket server with the "auto" value ("ws"): page errors 1`] = `Array []`;
46+
47+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value ("sockjs"): console messages 1`] = `
48+
Array [
49+
"[HMR] Waiting for update signal from WDS...",
50+
"Hey.",
51+
"[webpack-dev-server] Hot Module Replacement enabled.",
52+
"[webpack-dev-server] Live Reloading enabled.",
53+
]
54+
`;
55+
56+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value ("sockjs"): page errors 1`] = `Array []`;
57+
58+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value ("ws"): console messages 1`] = `
59+
Array [
60+
"[HMR] Waiting for update signal from WDS...",
61+
"Hey.",
62+
"[webpack-dev-server] Hot Module Replacement enabled.",
63+
"[webpack-dev-server] Live Reloading enabled.",
64+
]
65+
`;
66+
67+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the "all" value ("ws"): page errors 1`] = `Array []`;
68+
69+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the custom hostname value ("sockjs"): console messages 1`] = `
70+
Array [
71+
"[HMR] Waiting for update signal from WDS...",
72+
"Hey.",
73+
"[webpack-dev-server] Hot Module Replacement enabled.",
74+
"[webpack-dev-server] Live Reloading enabled.",
75+
]
76+
`;
77+
78+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the custom hostname value ("sockjs"): page errors 1`] = `Array []`;
79+
80+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the custom hostname value ("ws"): console messages 1`] = `
81+
Array [
82+
"[HMR] Waiting for update signal from WDS...",
83+
"Hey.",
84+
"[webpack-dev-server] Hot Module Replacement enabled.",
85+
"[webpack-dev-server] Live Reloading enabled.",
86+
]
87+
`;
88+
89+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the custom hostname value ("ws"): page errors 1`] = `Array []`;
90+
91+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the custom hostname value starting with dot ("sockjs"): console messages 1`] = `
92+
Array [
93+
"[HMR] Waiting for update signal from WDS...",
94+
"Hey.",
95+
"[webpack-dev-server] Hot Module Replacement enabled.",
96+
"[webpack-dev-server] Live Reloading enabled.",
97+
]
98+
`;
99+
100+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the custom hostname value starting with dot ("sockjs"): page errors 1`] = `Array []`;
101+
102+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the custom hostname value starting with dot ("ws"): console messages 1`] = `
103+
Array [
104+
"[HMR] Waiting for update signal from WDS...",
105+
"Hey.",
106+
"[webpack-dev-server] Hot Module Replacement enabled.",
107+
"[webpack-dev-server] Live Reloading enabled.",
108+
]
109+
`;
110+
111+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the custom hostname value starting with dot ("ws"): page errors 1`] = `Array []`;
112+
113+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the multiple custom hostname values ("sockjs"): console messages 1`] = `
114+
Array [
115+
"[HMR] Waiting for update signal from WDS...",
116+
"Hey.",
117+
"[webpack-dev-server] Hot Module Replacement enabled.",
118+
"[webpack-dev-server] Live Reloading enabled.",
119+
]
120+
`;
121+
122+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the multiple custom hostname values ("sockjs"): page errors 1`] = `Array []`;
123+
124+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the multiple custom hostname values ("ws"): console messages 1`] = `
125+
Array [
126+
"[HMR] Waiting for update signal from WDS...",
127+
"Hey.",
128+
"[webpack-dev-server] Hot Module Replacement enabled.",
129+
"[webpack-dev-server] Live Reloading enabled.",
130+
]
131+
`;
132+
133+
exports[`allowed hosts should connect web socket client using custom hostname to web socket server with the multiple custom hostname values ("ws"): page errors 1`] = `Array []`;
134+
135+
exports[`allowed hosts should connect web socket client using custom sub hostname to web socket server with the custom hostname value ("sockjs"): console messages 1`] = `
136+
Array [
137+
"[HMR] Waiting for update signal from WDS...",
138+
"Hey.",
139+
"[webpack-dev-server] Hot Module Replacement enabled.",
140+
"[webpack-dev-server] Live Reloading enabled.",
141+
]
142+
`;
143+
144+
exports[`allowed hosts should connect web socket client using custom sub hostname to web socket server with the custom hostname value ("sockjs"): page errors 1`] = `Array []`;
145+
146+
exports[`allowed hosts should connect web socket client using custom sub hostname to web socket server with the custom hostname value ("ws"): console messages 1`] = `
147+
Array [
148+
"[HMR] Waiting for update signal from WDS...",
149+
"Hey.",
150+
"[webpack-dev-server] Hot Module Replacement enabled.",
151+
"[webpack-dev-server] Live Reloading enabled.",
152+
]
153+
`;
154+
155+
exports[`allowed hosts should connect web socket client using custom sub hostname to web socket server with the custom hostname value ("ws"): page errors 1`] = `Array []`;
156+
157+
exports[`allowed hosts should connect web socket client using localhost to web socket server with the "auto" value ("sockjs"): console messages 1`] = `
158+
Array [
159+
"[HMR] Waiting for update signal from WDS...",
160+
"Hey.",
161+
"[webpack-dev-server] Hot Module Replacement enabled.",
162+
"[webpack-dev-server] Live Reloading enabled.",
163+
]
164+
`;
165+
166+
exports[`allowed hosts should connect web socket client using localhost to web socket server with the "auto" value ("sockjs"): page errors 1`] = `Array []`;
167+
168+
exports[`allowed hosts should connect web socket client using localhost to web socket server with the "auto" value ("ws"): console messages 1`] = `
169+
Array [
170+
"[HMR] Waiting for update signal from WDS...",
171+
"Hey.",
172+
"[webpack-dev-server] Hot Module Replacement enabled.",
173+
"[webpack-dev-server] Live Reloading enabled.",
174+
]
175+
`;
176+
177+
exports[`allowed hosts should connect web socket client using localhost to web socket server with the "auto" value ("ws"): page errors 1`] = `Array []`;
178+
179+
exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("sockjs"): console messages 1`] = `Array []`;
180+
181+
exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("sockjs"): html 1`] = `"<html><head></head><body>Invalid Host header</body></html>"`;
182+
183+
exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("sockjs"): page errors 1`] = `Array []`;
184+
185+
exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("ws"): console messages 1`] = `Array []`;
186+
187+
exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("ws"): html 1`] = `"<html><head></head><body>Invalid Host header</body></html>"`;
188+
189+
exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("ws"): page errors 1`] = `Array []`;
190+
191+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header ("sockjs"): console messages 1`] = `
192+
Array [
193+
"[HMR] Waiting for update signal from WDS...",
194+
"Hey.",
195+
"[webpack-dev-server] Invalid Host/Origin header",
196+
"[webpack-dev-server] Disconnected!",
197+
]
198+
`;
199+
200+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header ("sockjs"): page errors 1`] = `Array []`;
201+
202+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header ("ws"): console messages 1`] = `
203+
Array [
204+
"[HMR] Waiting for update signal from WDS...",
205+
"Hey.",
206+
"[webpack-dev-server] Invalid Host/Origin header",
207+
"[webpack-dev-server] Disconnected!",
208+
]
209+
`;
210+
211+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "host" header ("ws"): page errors 1`] = `Array []`;
212+
213+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "origin" header ("sockjs"): console messages 1`] = `
214+
Array [
215+
"[HMR] Waiting for update signal from WDS...",
216+
"Hey.",
217+
"[webpack-dev-server] Invalid Host/Origin header",
218+
"[webpack-dev-server] Disconnected!",
219+
]
220+
`;
221+
222+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "origin" header ("sockjs"): page errors 1`] = `Array []`;
223+
224+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "origin" header ("ws"): console messages 1`] = `
225+
Array [
226+
"[HMR] Waiting for update signal from WDS...",
227+
"Hey.",
228+
"[webpack-dev-server] Invalid Host/Origin header",
229+
"[webpack-dev-server] Disconnected!",
230+
]
231+
`;
232+
233+
exports[`allowed hosts should disconnect web socket client using custom hostname from web socket server with the "auto" value based on the "origin" header ("ws"): page errors 1`] = `Array []`;

0 commit comments

Comments
 (0)