Skip to content

Commit 3c7c3ff

Browse files
fix: refuse an event-stream client, not just a WebSocket one
`allowedHosts` and the origin check were not being enforced on the `sse` transport. The guard ran and decided to refuse, and then neither half of the refusal landed: the message went through `sendMessage`, which tests `readyState === 1` and calls `client.send` — a `ServerResponse` has neither, so the loop skipped it — and `client.close()`, which a `ServerResponse` also does not have, threw and was swallowed. The stream stayed open and kept receiving builds. The message goes through the middleware's `publishTo` now, which puts it on the wire the client is actually on, and the connection is dropped the way that client is dropped: a WebSocket closes, a response ends. Verified on both transports: a disallowed origin is told `Invalid Host/Origin header` and dropped, over a WebSocket and over a stream alike — and a same-origin page, which `EventSource` gives no `Origin` at all, is still let through. Found via a CodeRabbit review of the middleware's README example, which had the same two mistakes in it. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
1 parent 05f7f57 commit 3c7c3ff

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

‎lib/Server.js‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ function loadWebpackPeer() {
157157
* @typedef {{ implementation: WebSocketServer, clients: ClientConnection[] }} WebSocketServerImplementation
158158
*/
159159

160+
/**
161+
* A client the hot endpoint handed over, of either transport: a WebSocket, or
162+
* the event stream's response. Neither one's write or close method exists on
163+
* the other, which is why anything handling both goes through the middleware
164+
* or asks before calling.
165+
* @typedef {ClientConnection | ServerResponse} HotClient
166+
*/
167+
160168
/**
161169
* @typedef {{ path?: HttpProxyMiddlewareOptionsFilter | undefined, context?: HttpProxyMiddlewareOptionsFilter | undefined } & HttpProxyMiddlewareOptions} ProxyConfigArrayItem
162170
*/
@@ -2472,7 +2480,7 @@ class Server {
24722480

24732481
middleware.onConnect(
24742482
/**
2475-
* @param {ClientConnection} client the client
2483+
* @param {HotClient} client the client
24762484
* @param {IncomingMessage} request the request it joined with
24772485
*/
24782486
(client, request) => {
@@ -2512,12 +2520,26 @@ class Server {
25122520
// Said to the client before it is closed, so the reason reaches the
25132521
// page rather than only this server's log — a refusal that closes
25142522
// silently is one the developer has nowhere to read.
2515-
this.sendMessage([client], "error", "Invalid Host/Origin header");
2523+
//
2524+
// Through the middleware rather than written here: the two
2525+
// transports hand over different kinds of client, and neither
2526+
// one's write method exists on the other. Writing directly reached
2527+
// only the WebSocket, so a stream client was refused in silence —
2528+
// and `close()`, which a `ServerResponse` does not have, threw
2529+
// before it could be dropped, leaving it connected.
2530+
middleware.publishTo(client, {
2531+
action: "error",
2532+
message: "Invalid Host/Origin header",
2533+
});
25162534

2517-
// With https enabled, the message above is encrypted asynchronously
2518-
// so not yet sent. Terminate would prevent it sending, so use close
2519-
// to allow it to be sent.
2520-
client.close();
2535+
// A WebSocket closes; the event stream's response ends. Closing
2536+
// rather than terminating so the message above still goes out —
2537+
// over https it is encrypted asynchronously and not yet sent.
2538+
if ("close" in client && typeof client.close === "function") {
2539+
client.close();
2540+
} else if ("end" in client) {
2541+
client.end();
2542+
}
25212543
}
25222544
},
25232545
);

0 commit comments

Comments
 (0)