security: validate browser origins at the authenticated websocket proxy
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user