Files
outline/server/onupgrade.test.ts
T
Tom MoorandClaude Opus 5 15daf2e8a7 fix: Unhandled socket error during websocket upgrade crashes server (#13409)
* fix: Unhandled socket error during websocket upgrade crashes server

Node removes its own socket error handling before emitting the "upgrade"
event, so any socket detached from the HTTP server without a listener of
its own will terminate the process when the client goes away.

Handle errors for every upgraded socket in one place, and destroy sockets
we reject rather than leaving them half-open to be reset at any point in
the future.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Flush rejection response before destroying the socket

Destroying immediately after the write can truncate the response if the
socket's write buffer has not drained, leaving the client with a reset in
place of the rejection.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* noisy comment

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-13 18:59:06 -04:00

86 lines
2.5 KiB
TypeScript

import { once } from "node:events";
import http from "node:http";
import { Socket } from "node:net";
import { PassThrough } from "node:stream";
import Logger from "@server/logging/Logger";
import onupgrade, { rejectUnhandledUpgrades, rejectUpgrade } from "./onupgrade";
const request = () => new http.IncomingMessage(new Socket());
describe("onupgrade", () => {
it("should swallow expected disconnections without reporting them", () => {
const errorSpy = vi.spyOn(Logger, "error");
const server = http.createServer();
const socket = new PassThrough();
onupgrade(server);
server.emit("upgrade", request(), socket);
expect(() => {
socket.emit(
"error",
Object.assign(new Error("read ECONNRESET"), {
code: "ECONNRESET",
})
);
}).not.toThrow();
expect(errorSpy).not.toHaveBeenCalled();
errorSpy.mockRestore();
});
it("should report unexpected errors", () => {
const errorSpy = vi.spyOn(Logger, "error").mockImplementation(() => {});
const server = http.createServer();
const socket = new PassThrough();
onupgrade(server);
server.emit("upgrade", request(), socket);
socket.emit("error", new Error("something unexpected"));
expect(errorSpy).toHaveBeenCalled();
errorSpy.mockRestore();
});
});
describe("rejectUnhandledUpgrades", () => {
it("should close upgrade requests when no service handles them", async () => {
const server = http.createServer();
const socket = new PassThrough();
onupgrade(server);
rejectUnhandledUpgrades(server);
server.emit("upgrade", request(), socket);
await once(socket, "close");
expect(socket.destroyed).toEqual(true);
});
it("should leave upgrade requests alone when a service handles them", () => {
const server = http.createServer();
const socket = new PassThrough();
onupgrade(server);
server.on("upgrade", () => undefined);
rejectUnhandledUpgrades(server);
server.emit("upgrade", request(), socket);
expect(socket.destroyed).toEqual(false);
});
});
describe("rejectUpgrade", () => {
it("should write a complete response and destroy the socket", async () => {
const socket = new PassThrough();
const chunks: Buffer[] = [];
socket.on("data", (chunk: Buffer) => chunks.push(chunk));
rejectUpgrade(socket);
await once(socket, "close");
expect(Buffer.concat(chunks).toString()).toEqual(
"HTTP/1.1 400 Bad Request\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"
);
expect(socket.destroyed).toEqual(true);
});
});