security: validate browser origins at the authenticated websocket proxy

This commit is contained in:
2026-10-20 15:48:02 +01:00
parent c6695e5e90
commit 1cf5156c77
4 changed files with 40 additions and 0 deletions
+6
View File
@@ -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();
+12
View File
@@ -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;
}
}
+13
View File
@@ -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);
});
+9
View File
@@ -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(`(() => {