Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
companion: drop Connection-named headers and end replies cut short
Bugfixer 2d1500a801a2d
diff --git a/companion/src/session-route.ts b/companion/src/session-route.tsindex edd489dc1e..05ae044721 100644--- a/companion/src/session-route.ts+++ b/companion/src/session-route.ts@@ -1,7 +1,7 @@// The full web app over the desktop's encrypted connection. A paired device that also holds a session of the web server// (`omb_sess_…`, issued by the server's own pairing) reaches every route that session may use, not only the phone app's.// The server decides what the session may do; the companion only carries the request, as a remote one.-import { request as httpRequest, type IncomingMessage, type ServerResponse } from "node:http";+import { request as httpRequest, type IncomingHttpHeaders, type IncomingMessage, type ServerResponse } from "node:http";import type { Duplex } from "node:stream";import { bearerToken } from "./devices.ts";@@ -9,10 +9,19 @@ import type { E2EContext } from "./e2e/registry.ts";/** What a page sends to sign in with the server's pairing code; the reply is the session token it uses from then on. */const SERVER_PAIRING = "POST /api/auth/pair";-/** Never forwarded: hop-by-hop headers, and everything that names a client or carries this machine's trust. */+const HOP_BY_HOP = ["connection", "keep-alive", "proxy-connection", "te", "trailer", "transfer-encoding", "upgrade"];+/** Hop-by-hop headers of one message (RFC 9110 section 7.6.1): the fixed ones and every name its Connection header lists. */+function hopByHop(headers: IncomingHttpHeaders): Set<string> {+ const names = new Set(HOP_BY_HOP);+ for (const name of String(headers.connection ?? "").split(",")) {+ const nominated = name.trim().toLowerCase();+ if (nominated) names.add(nominated);+ }+ return names;+}+/** Never forwarded besides the hop-by-hop headers: everything that names a client or carries this machine's trust. */const DROPPED = new Set([- "host", "origin", "cookie", "connection", "keep-alive", "proxy-authorization", "proxy-connection", "te", "trailer",- "transfer-encoding", "upgrade",+ "host", "origin", "cookie", "proxy-authorization","x-forwarded-for", "x-forwarded-proto", "x-forwarded-host", "forwarded", "x-real-ip", "cf-connecting-ip","true-client-ip", "x-client-ip",]);@@ -30,8 +39,9 @@ export function webSessionPeer(context: E2EContext | null, req: IncomingMessage)* its session) and keeps nothing the device wrote that names a client, a cookie or the desktop's own credentials. */export function webSessionHeaders(req: IncomingMessage, deviceId: string, upgrade = false): Record<string, string | string[]> {const out: Record<string, string | string[]> = {};+ const hop = hopByHop(req.headers);for (const [name, value] of Object.entries(req.headers)) {- if (value === undefined || DROPPED.has(name) || name.startsWith("x-openmausbot-")) continue;+ if (value === undefined || hop.has(name) || DROPPED.has(name) || name.startsWith("x-openmausbot-")) continue;out[name] = value;}out.host = HOST;@@ -64,9 +74,12 @@ export function forwardWebSession(target: WebSessionTarget, deviceId: string, re{ hostname: "127.0.0.1", port: target.harnessPort, path: req.url, method: req.method, headers: webSessionHeaders(req, deviceId) },(harness) => {clearTimeout(deadline);- const { connection: _connection, "keep-alive": _keepAlive, ...headers } = harness.headers;+ const hop = hopByHop(harness.headers);+ const headers = Object.fromEntries(Object.entries(harness.headers).filter(([name]) => !hop.has(name)));res.writeHead(harness.statusCode ?? 502, harness.statusMessage, headers);harness.pipe(res);+ // pipe() ends the reply only on a complete body: a harness that drops mid-reply must not leave the device waiting.+ harness.once("close", () => { if (!harness.complete) res.destroy(); });},);const { deadline, timedOut } = headersDeadline(upstream, target.headersTimeoutMs);@@ -90,7 +103,6 @@ function responseHead(status: number, message: string, rawHeaders: string[], skiif (extra) lines.push(extra);return `${lines.join("\r\n")}\r\n\r\n`;}-const REFUSED_UPGRADE_SKIP = new Set(["connection", "transfer-encoding", "keep-alive"]);export function forwardWebSessionUpgrade(target: WebSessionTarget, deviceId: string, req: IncomingMessage, socket: Duplex, head: Buffer): void {const upstream = httpRequest({ hostname: "127.0.0.1", port: target.harnessPort, path: req.url, method: req.method, headers: webSessionHeaders(req, deviceId, true) });@@ -117,8 +129,9 @@ export function forwardWebSessionUpgrade(target: WebSessionTarget, deviceId: strclearTimeout(deadline);answered = true;// Refused: the body is read to its end, then the socket closes (no chunked framing is passed on).- socket.write(responseHead(harness.statusCode ?? 502, harness.statusMessage ?? "", harness.rawHeaders, REFUSED_UPGRADE_SKIP, "connection: close"));+ socket.write(responseHead(harness.statusCode ?? 502, harness.statusMessage ?? "", harness.rawHeaders, hopByHop(harness.headers), "connection: close"));harness.pipe(socket);+ harness.once("close", () => { if (!harness.complete) socket.destroy(); });});upstream.once("error", () => {clearTimeout(deadline);diff --git a/companion/test/session-route.test.ts b/companion/test/session-route.test.tsindex 51ec99774f..557852df51 100644--- a/companion/test/session-route.test.ts+++ b/companion/test/session-route.test.ts@@ -1,4 +1,4 @@-import { createServer, request, type IncomingHttpHeaders, type Server } from "node:http";+import { createServer, request, type IncomingHttpHeaders, type IncomingMessage, type Server } from "node:http";import { once } from "node:events";import type { Duplex } from "node:stream";import { afterEach, expect, it } from "vitest";@@ -42,6 +42,16 @@ async function rig(harnessPortOverride?: number) {let hungClosed!: () => void;const hung = new Promise<void>((resolve) => { hungClosed = resolve; });const harness = createServer(async (req, res) => {+ if (req.url === "/api/cut") {+ // The harness goes away after half its reply.+ res.writeHead(200, { "content-length": "100" });+ res.write("partial");+ return setImmediate(() => req.socket.destroy());+ }+ if (req.url === "/api/hop") {+ received.push({ method: req.method, url: req.url, headers: req.headers, body: "" });+ return res.writeHead(200, { connection: "x-reply-hop", "x-reply-hop": "harness-local", "x-from": "server" }).end("ok");+ }if (req.url === "/api/hang") {req.socket.once("close", hungClosed);return received.push({ method: req.method, url: req.url, headers: req.headers, body: "" });@@ -53,6 +63,10 @@ async function rig(harnessPortOverride?: number) {});const wss = new WebSocketServer({ noServer: true });harness.on("upgrade", (req, socket, head) => {+ if (req.url === "/api/cut-upgrade") {+ socket.write("HTTP/1.1 403 Forbidden\r\nContent-Length: 100\r\n\r\npartial");+ return setImmediate(() => socket.destroy());+ }received.push({ method: req.method, url: req.url, headers: req.headers, body: "" });wss.handleUpgrade(req, socket, head, (ws) => ws.on("message", (data) => ws.send(`echo:${data}`)));});@@ -177,3 +191,40 @@ it("says so when OpenMausBot is not running, for requests and live sockets", asyconst [, refused] = await once(ws, "unexpected-response");expect((refused as { statusCode: number }).statusCode).toBe(502);});++it("drops the headers a Connection header names, both ways", async () => {+ const { received, origin } = await rig();+ const res = await send(await origin.connect(DEVICE), null, "GET", "/api/hop", {+ authorization: "Bearer omb_sess_abc", connection: "x-hop-state", "x-hop-state": "device-local",+ });+ expect(res.status).toBe(200);+ expect(received[0].headers["x-hop-state"]).toBeUndefined();+ expect(received[0].headers.authorization).toBe("Bearer omb_sess_abc");+ expect(res.headers["x-reply-hop"]).toBeUndefined();+ expect(res.headers["x-from"]).toBe("server");+});++it("ends the device's reply when the harness drops mid-reply", async () => {+ const { origin } = await rig();+ const stream = await origin.connect(DEVICE);+ const req = request({ method: "GET", path: "/api/cut", headers: { authorization: "Bearer omb_sess_abc" }, createConnection: () => stream as never });+ req.once("error", () => {});+ req.end();+ const [res] = await once(req, "response") as [IncomingMessage];+ res.once("error", () => {}); // "aborted": the reply ends short+ res.resume();+ await new Promise((resolve) => res.once("close", resolve));+ expect(res.complete).toBe(false);+});++it("ends a refused live socket when the harness drops mid-reply", async () => {+ const { origin } = await rig();+ const stream = await origin.connect(DEVICE);+ let text = "";+ stream.on("data", (chunk) => { text += chunk; });+ stream.once("error", () => {});+ const ended = new Promise((resolve) => { stream.once("end", resolve); stream.once("close", resolve); });+ stream.write("GET /api/cut-upgrade HTTP/1.1\r\nHost: desktop.invalid\r\nAuthorization: Bearer omb_sess_abc\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n");+ await ended;+ expect(text).toMatch(/^HTTP\/1\.1 403/);+});