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();