|
1 | 1 | import * as net from 'net';
|
2 | 2 | import * as WebSocket from 'isomorphic-ws';
|
| 3 | +import * as http from 'http'; |
3 | 4 | import * as https from 'https';
|
4 | 5 | import HttpProxyAgent = require('http-proxy-agent');
|
5 | 6 | import HttpsProxyAgent = require('https-proxy-agent');
|
@@ -188,7 +189,6 @@ nodeOnly(() => {
|
188 | 189 | ]);
|
189 | 190 | });
|
190 | 191 |
|
191 |
| - |
192 | 192 | it("forwards the incoming requests' & resulting response's subprotocols", async () => {
|
193 | 193 | mockServer.forAnyWebSocket().thenPassThrough();
|
194 | 194 |
|
@@ -225,6 +225,54 @@ nodeOnly(() => {
|
225 | 225 | ]);
|
226 | 226 | });
|
227 | 227 |
|
| 228 | + it("ignores mildly invalid blank (empty string) subprotocol headers in incoming requests", async () => { |
| 229 | + await mockServer.forAnyWebSocket().thenPassThrough(); |
| 230 | + const request = https.request(`https://localhost:${wsPort}`, { |
| 231 | + agent: new HttpProxyAgent(`http://localhost:${mockServer.port}`), |
| 232 | + headers: { |
| 233 | + 'Connection': 'Upgrade', |
| 234 | + 'Upgrade': 'websocket', |
| 235 | + 'Sec-WebSocket-Version': 13, |
| 236 | + 'Sec-WebSocket-Key': 'DxfWc2xtQqmWYmU/n8WUWg==', |
| 237 | + 'Sec-WebSocket-Protocol': ' ' // Empty headers are invalid |
| 238 | + } |
| 239 | + }).end(); |
| 240 | + |
| 241 | + const response = await new Promise<http.IncomingMessage>((resolve, reject) => { |
| 242 | + request.on('response', resolve); |
| 243 | + request.on('upgrade', resolve); |
| 244 | + request.on('error', reject); |
| 245 | + }); |
| 246 | + |
| 247 | + expect(response.statusCode).to.equal(101); |
| 248 | + expect(response.headers['sec-websocket-protocol']).to.equal(undefined); |
| 249 | + }); |
| 250 | + |
| 251 | + it("handles mildly invalid non-empty subprotocol headers in incoming requests", async () => { |
| 252 | + await mockServer.forAnyWebSocket().thenPassThrough(); |
| 253 | + const request = https.request(`https://localhost:${wsPort}`, { |
| 254 | + agent: new HttpProxyAgent(`http://localhost:${mockServer.port}`), |
| 255 | + headers: { |
| 256 | + 'Connection': 'Upgrade', |
| 257 | + 'Upgrade': 'websocket', |
| 258 | + 'Sec-WebSocket-Version': 13, |
| 259 | + 'Sec-WebSocket-Key': 'DxfWc2xtQqmWYmU/n8WUWg==', |
| 260 | + 'Sec-WebSocket-Protocol': ' ', // Empty headers are invalid |
| 261 | + 'sec-webSocket-protocol': 'a,,b', // Badly formatted other protocols |
| 262 | + 'echo-ws-protocol-index': '0' |
| 263 | + } |
| 264 | + }).end(); |
| 265 | + |
| 266 | + const response = await new Promise<http.IncomingMessage>((resolve, reject) => { |
| 267 | + request.on('response', resolve); |
| 268 | + request.on('upgrade', resolve); |
| 269 | + request.on('error', reject); |
| 270 | + }); |
| 271 | + |
| 272 | + expect(response.statusCode).to.equal(101); |
| 273 | + expect(response.headers['sec-websocket-protocol']).to.equal('a'); |
| 274 | + }); |
| 275 | + |
228 | 276 | it("can handle & proxy invalid client frames upstream", async () => {
|
229 | 277 | mockServer.forAnyWebSocket().thenPassThrough();
|
230 | 278 |
|
|
0 commit comments