From 3468a197afe87e65eb0d779fabd347fe683013ab Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Thu, 3 Aug 2023 01:28:06 +0200 Subject: [PATCH] fix(webtransport): properly handle WebTransport-only connections A WebTransport-only connection has no `request` attribute, so we need to handle that case. --- lib/socket.ts | 10 +++++----- test/socket.ts | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/socket.ts b/lib/socket.ts index f520605344..0d065c0263 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -292,16 +292,16 @@ export class Socket< */ private buildHandshake(auth: object): Handshake { return { - headers: this.request.headers, + headers: this.request?.headers || {}, time: new Date() + "", address: this.conn.remoteAddress, - xdomain: !!this.request.headers.origin, + xdomain: !!this.request?.headers.origin, // @ts-ignore - secure: !!this.request.connection.encrypted, + secure: !this.request || !!this.request.connection.encrypted, issued: +new Date(), - url: this.request.url!, + url: this.request?.url!, // @ts-ignore - query: this.request._query, + query: this.request?._query || {}, auth, }; } diff --git a/test/socket.ts b/test/socket.ts index 1fb2f9eafa..10e2924a42 100644 --- a/test/socket.ts +++ b/test/socket.ts @@ -684,6 +684,23 @@ describe("socket", () => { }); }); + it("should handshake a client without access to the Engine.IO request (WebTransport-only connection)", (done) => { + const io = new Server(0); + const clientSocket = createClient(io, "/"); + + io.engine.on("connection", (socket) => { + delete socket.request; + }); + + io.on("connection", (socket) => { + expect(socket.handshake.secure).to.be(true); + expect(socket.handshake.headers).to.eql({}); + expect(socket.handshake.query).to.eql({}); + + success(done, io, clientSocket); + }); + }); + it("should handle very large json", function (done) { this.timeout(30000); const io = new Server(0, { perMessageDeflate: false });