fix(ws): reject malformed upgrades instead of crashing the server
The Node 'upgrade' listener matched the raw request path without the normalization HTTP runs. A bad percent escape in a param segment threw URIError synchronously inside the listener, an uncaught exception that ends the process, and '//' silently matched a route HTTP refuses. Upgrades now normalize the path first: malformed escapes and repeated slashes get a 400, an unknown route a written 404 instead of a bare destroy, and any other throw in the listener a 500. app.upgrade (Deno/Bun/edge) closes 1008 'invalid request path' rather than 1011. On Deno the adapter's socket is still CONNECTING when an early 1008 is sent, and that close was a no-op that left the peer hanging; eventSocket now defers such a close to 'open'. Closes #104 Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
10 files changed
+309
-23
No files matched your search
@@ -20,6 +20,13 @@ class Rt {
|
||||
for await (const msg of inbound) yield `echo:${msg}`;
|
||||
})();
|
||||
}
|
||||
|
||||
@Ws('/room/:name')
|
||||
room(@ctx() { inbound }: any) {
|
||||
return (async function* () {
|
||||
for await (const msg of inbound) yield `room:${msg}`;
|
||||
})();
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', controllers: [Rt] })
|
||||
class M {}
|
||||
@@ -47,3 +54,22 @@ test('serveBun: HTTP + WebSocket round-trip', async () => {
|
||||
server.stop(true);
|
||||
}
|
||||
});
|
||||
|
||||
// #104: a malformed escape in a param segment used to reject out of the matcher and close 1011.
|
||||
test('serveBun: a malformed upgrade path closes 1008 and the server keeps serving', async () => {
|
||||
const app = createApp({ modules: [M] });
|
||||
const server = await serveBun(app, { port: 0 });
|
||||
const port = server.port;
|
||||
try {
|
||||
const ws = new WebSocket(`ws://127.0.0.1:${port}/rt/room/%E0%A4%A`);
|
||||
const closed = await new Promise<{ code: number; reason: string }>((resolve) => {
|
||||
ws.onclose = (e) => resolve({ code: e.code, reason: e.reason });
|
||||
});
|
||||
expect(closed).toEqual({ code: 1008, reason: 'invalid request path' });
|
||||
|
||||
const httpRes = await fetch(`http://127.0.0.1:${port}/rt/hello`);
|
||||
expect(httpRes.status).toBe(200);
|
||||
} finally {
|
||||
server.stop(true);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user