Skip to content

Commit

Permalink
fix(uws): prevent crash when using with middlewares
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed May 1, 2023
1 parent 9395782 commit 8b22162
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/userver.ts
Expand Up @@ -294,6 +294,7 @@ class ResponseWrapper {
this.res.writeStatus(status);
this.statusWritten = true;
this.writeBufferedHeaders();
return this;
}

public writeHeader(key: string, value: string) {
Expand Down
27 changes: 27 additions & 0 deletions test/middlewares.js
Expand Up @@ -4,6 +4,7 @@ const request = require("superagent");
const { WebSocket } = require("ws");
const helmet = require("helmet");
const session = require("express-session");
const { ClientSocket } = require("./common");

describe("middlewares", () => {
it("should apply middleware (polling)", (done) => {
Expand Down Expand Up @@ -291,4 +292,30 @@ describe("middlewares", () => {
});
});
});

it("should not be receiving data when getting a message longer than maxHttpBufferSize when polling (with a middleware)", (done) => {
const opts = {
allowUpgrades: false,
transports: ["polling"],
maxHttpBufferSize: 5,
};
const engine = listen(opts, (port) => {
engine.use((req, res, next) => {
next();
});

const socket = new ClientSocket(`ws://localhost:${port}`);
engine.on("connection", (conn) => {
conn.on("message", () => {
done(new Error("Test invalidation (message is longer than allowed)"));
});
});
socket.on("open", () => {
socket.send("aasdasdakjhasdkjhasdkjhasdkjhasdkjhasdkjhasdkjha");
});
socket.on("close", (reason) => {
done();
});
});
});
});

0 comments on commit 8b22162

Please sign in to comment.