Skip to content

Commit 8b22162

Browse files
fix(uws): prevent crash when using with middlewares
The class used to accumulate the response headers did not expose the exact same API as its wrapped type, which could lead to the following error in some rare cases: > TypeError: Cannot read properties of undefined (reading 'end') > at Polling.onDataRequest (build/transports-uws/polling.js:109:53) > at Polling.onRequest (build/transports-uws/polling.js:47:18) > at callback (build/userver.js:94:56) > at uServer.verify (build/server.js:152:9) Related: socketio/socket.io#4643
1 parent 9395782 commit 8b22162

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/userver.ts

+1
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ class ResponseWrapper {
294294
this.res.writeStatus(status);
295295
this.statusWritten = true;
296296
this.writeBufferedHeaders();
297+
return this;
297298
}
298299

299300
public writeHeader(key: string, value: string) {

test/middlewares.js

+27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const request = require("superagent");
44
const { WebSocket } = require("ws");
55
const helmet = require("helmet");
66
const session = require("express-session");
7+
const { ClientSocket } = require("./common");
78

89
describe("middlewares", () => {
910
it("should apply middleware (polling)", (done) => {
@@ -291,4 +292,30 @@ describe("middlewares", () => {
291292
});
292293
});
293294
});
295+
296+
it("should not be receiving data when getting a message longer than maxHttpBufferSize when polling (with a middleware)", (done) => {
297+
const opts = {
298+
allowUpgrades: false,
299+
transports: ["polling"],
300+
maxHttpBufferSize: 5,
301+
};
302+
const engine = listen(opts, (port) => {
303+
engine.use((req, res, next) => {
304+
next();
305+
});
306+
307+
const socket = new ClientSocket(`ws://localhost:${port}`);
308+
engine.on("connection", (conn) => {
309+
conn.on("message", () => {
310+
done(new Error("Test invalidation (message is longer than allowed)"));
311+
});
312+
});
313+
socket.on("open", () => {
314+
socket.send("aasdasdakjhasdkjhasdkjhasdkjhasdkjhasdkjhasdkjha");
315+
});
316+
socket.on("close", (reason) => {
317+
done();
318+
});
319+
});
320+
});
294321
});

0 commit comments

Comments
 (0)