Skip to content

Commit 8b4d6a8

Browse files
fix(uws): handle invalid websocket upgrades
When binding to an uWebSockets.js App, there was an unhandled case that could crash the server: ``` curl "http://localhost:3000/engine.io/?EIO=4&transport=websocket" ``` would result in: ``` Error: Returning from a request handler without responding or attaching an abort handler is forbidden! terminate called without an active exception ``` Note: this does not apply to the default server based on ws, because the error was caught elsewhere in the source code. Related: socketio/socket.io#4250
1 parent a84595a commit 8b4d6a8

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

lib/server.ts

+7
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ export abstract class BaseServer extends EventEmitter {
245245
});
246246
}
247247

248+
if (transport === "websocket" && !upgrade) {
249+
debug("invalid transport upgrade");
250+
return fn(Server.errors.BAD_REQUEST, {
251+
name: "TRANSPORT_HANDSHAKE_ERROR"
252+
});
253+
}
254+
248255
if (!this.opts.allowRequest) return fn();
249256

250257
return this.opts.allowRequest(req, (message, success) => {

lib/userver.ts

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export class uServer extends BaseServer {
2828
req.connection = {
2929
remoteAddress: Buffer.from(res.getRemoteAddressAsText()).toString()
3030
};
31+
32+
res.onAborted(() => {
33+
debug("response has been aborted");
34+
});
3135
}
3236

3337
protected createTransport(transportName, req) {

test/server.js

-5
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,6 @@ describe("server", () => {
603603
});
604604

605605
it("should disallow bad requests (handshake error)", function(done) {
606-
if (process.env.EIO_WS_ENGINE === "uws") {
607-
return this.skip();
608-
}
609606
const partialDone = createPartialDone(done, 2);
610607

611608
engine = listen(
@@ -618,8 +615,6 @@ describe("server", () => {
618615
expect(err.code).to.be(3);
619616
expect(err.message).to.be("Bad request");
620617
expect(err.context.name).to.be("TRANSPORT_HANDSHAKE_ERROR");
621-
expect(err.context.error).to.be.an(Error);
622-
expect(err.context.error.name).to.be("TypeError");
623618
partialDone();
624619
});
625620

0 commit comments

Comments
 (0)