WS breaks requests with http/2 upgrade headers
- [x] I've searched for any related issues and avoided creating a duplicate issue.
Description
When using ws, whenever a request comes in with upgrade headers that do not relate to websockets, a 400 Bad Request is returned.
Reproducible in:
- version: 7.2.1, 6.1.2
- Node.js version(s): 12.14.0, 10.18.0, 8.17.0
- OS version(s): Ubuntu 19.10
Steps to reproduce:
- Run the following script:
const http = require('http');
const ws = require('ws');
let server = http.createServer(((req, res, next) => res.end('OK')));
server.listen(1111);
let wss = new ws.Server({server: server});
- Send a request with upgrade headers:
curl http://localhost:1111/test -H 'Connection: Upgrade, HTTP2-Settings' -H 'Upgrade: h2c' -H 'HTTP2-Settings: '
Expected result:
WS should ignore the upgrade request, and a 'OK' response should be returned.
Actual result:
A 400 Bad Request is received instead.
Really dirty workaround:
// workaround
Object.defineProperty(http.IncomingMessage.prototype, "upgrade", {
get() {
return "connection" in this.headers && "upgrade" in this.headers && this.headers.connection.startsWith("Upgrade") && this.headers.upgrade.toLowerCase() == 'websocket';
},
set(v) {
}
});
This patches IncomingMessage from node's http module so it only indicates that it is an upgrade request if it's a websocket upgrade.
This is expected. You can use a custom 'upgrade' event handler if you want.
const http = require('http');
const WebSocket = require('ws');
const server = http.createServer();
const wss = new WebSocket.Server({ noServer: true });
wss.on('connection', function connection(ws) {
// ...
});
server.on('upgrade', function upgrade(request, socket, head) {
if (request.headers.upgrade.toLowerCase() !== 'websocket') {
socket.end('HTTP/1.1 200 OK\r\n\r\n');
return;
}
wss.handleUpgrade(request, socket, head, function done(ws) {
wss.emit('connection', ws, request);
});
});
server.listen(8080);
That's not an acceptable solution; that literally just ends the connection with a 200 whereas the expected behavior is for the request to be processed by whatever request handler is defined on the http server. You also can't just invoke the handler directly, because the response object isn't fully initialized at this point which breaks things like setting headers in express.
That's not an acceptable solution; that literally just ends the connection with a 200 whereas the expected behavior is for the request to be processed by whatever request handler is defined on the http server. You also can't just invoke the handler directly, because the response object isn't fully initialized at this point which breaks things like setting headers in express.
That's how Node.js works and not something changed by ws. You get an 'upgrade' event to handle "upgrade" requests and a 'request' event for "normal" requests. Those events are mutually exclusive.
You shouldn't handle "upgrade" requests like "normal" requests.
It is my understanding you should handle it like a normal request if you don't intend to honor the upgrade request; it is perfectly acceptable for a client to request an upgrade and for the server to ignore that request if it doesn't know how to handle it. In that case it should just handle the request as if the upgrade headers aren't there. But I realize this is more of an issue with NodeJS than it is a WS issue, so I'll move the discussion there.