From 1cf5156c77dcdffb4eb2c77a3d41518ada11c0d8 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Tue, 20 Oct 2026 15:48:02 +0100 Subject: [PATCH] security: validate browser origins at the authenticated websocket proxy --- web/serve.mjs | 6 ++++++ web/src/proxySecurity.js | 12 ++++++++++++ web/test/proxySecurity.test.mjs | 13 +++++++++++++ web/test/run-admission-probe.mjs | 9 +++++++++ 4 files changed, 40 insertions(+) create mode 100644 web/src/proxySecurity.js create mode 100644 web/test/proxySecurity.test.mjs diff --git a/web/serve.mjs b/web/serve.mjs index b650a92d..fd9766db 100644 --- a/web/serve.mjs +++ b/web/serve.mjs @@ -23,6 +23,7 @@ import { readFile, stat } from "node:fs/promises"; import { extname, join, normalize, resolve, sep } from "node:path"; import { fileURLToPath } from "node:url"; import net from "node:net"; +import { sameOriginUpgrade } from "./src/proxySecurity.js"; const ROOT = resolve(fileURLToPath(new URL(".", import.meta.url))); @@ -466,6 +467,11 @@ function proxyUpgrade(req, clientSocket, clientHead) { server.on("upgrade", (req, clientSocket, clientHead) => { const pathname = (req.url ?? "").split("?")[0]; + if (!sameOriginUpgrade(req.headers.origin, req.headers.host)) { + clientSocket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); + clientSocket.destroy(); + return; + } if (auth && !isAuthed(req)) { clientSocket.write("HTTP/1.1 401 Unauthorized\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); clientSocket.destroy(); diff --git a/web/src/proxySecurity.js b/web/src/proxySecurity.js new file mode 100644 index 00000000..06f2622f --- /dev/null +++ b/web/src/proxySecurity.js @@ -0,0 +1,12 @@ +// The upstream has an Origin check, but a same-origin proxy must remove Origin +// before forwarding. Enforce it at the browser-facing boundary first instead +// of accidentally trusting any cookie-bearing WebSocket upgrade. +export function sameOriginUpgrade(origin, host) { + if (origin == null) return true; // non-browser clients have no Origin + try { + const url = new URL(origin); + return (url.protocol === "http:" || url.protocol === "https:") && url.host.toLowerCase() === String(host ?? "").toLowerCase(); + } catch { + return false; + } +} diff --git a/web/test/proxySecurity.test.mjs b/web/test/proxySecurity.test.mjs new file mode 100644 index 00000000..903cf78b --- /dev/null +++ b/web/test/proxySecurity.test.mjs @@ -0,0 +1,13 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { sameOriginUpgrade } from "../src/proxySecurity.js"; + +test("the authenticated proxy checks Origin before discarding it upstream", () => { + assert.equal(sameOriginUpgrade("http://localhost:8095", "localhost:8095"), true); + assert.equal(sameOriginUpgrade("https://pipelines.example", "pipelines.example"), true); + assert.equal(sameOriginUpgrade("https://untrusted.example", "pipelines.example"), false); + assert.equal(sameOriginUpgrade("http://localhost:8094", "localhost:8095"), false); + assert.equal(sameOriginUpgrade("null", "localhost:8095"), false); + assert.equal(sameOriginUpgrade("file:///report.html", "localhost:8095"), false); + assert.equal(sameOriginUpgrade(undefined, "localhost:8095"), true); +}); diff --git a/web/test/run-admission-probe.mjs b/web/test/run-admission-probe.mjs index 98465b75..0bf3fe08 100644 --- a/web/test/run-admission-probe.mjs +++ b/web/test/run-admission-probe.mjs @@ -5,6 +5,7 @@ import { spawn } from "node:child_process"; import { mkdir } from "node:fs/promises"; import { join } from "node:path"; import net from "node:net"; +import { request } from "node:http"; import { fileURLToPath } from "node:url"; import { eventually, launchChrome } from "./browser.mjs"; @@ -28,6 +29,14 @@ try { if (serve.exitCode != null) throw Error(logs); return fetch(`http://127.0.0.1:${port}/`).then((r) => r.ok).catch(() => false); }); + const refused = await new Promise((resolve, reject) => { + const req = request({ host: "127.0.0.1", port, path: "/connect", headers: { Connection: "Upgrade", Upgrade: "websocket", Origin: "https://untrusted.example", "Sec-WebSocket-Version": "13", "Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==" } }); + req.on("response", (res) => { res.resume(); resolve(res.statusCode); }); + req.on("upgrade", (_res, socket) => { socket.destroy(); resolve(101); }); + req.on("error", reject); + req.end(); + }); + assert.equal(refused, 403, "proxy must reject foreign Origin before stripping it upstream"); browser = await launchChrome({ width: 390, height: 844 }); const page = await browser.openPage("about:blank"); await page.addInitScript(`(() => {