Log notification subscription lifecycle and websocket rejections
This commit is contained in:
@@ -705,6 +705,12 @@ function proxyUpgrade(req, clientSocket, clientHead) {
|
||||
clientSocket.on("error", () => upstream.destroy());
|
||||
}
|
||||
|
||||
function logRejectedUpgrade(status, pathname, reason) {
|
||||
// pathname excludes the query string so cookies/tokens can never enter this
|
||||
// diagnostic line.
|
||||
console.warn(`websocket upgrade rejected status=${status} path=${pathname} reason=${reason}`);
|
||||
}
|
||||
|
||||
server.on("upgrade", (req, clientSocket, clientHead) => {
|
||||
try { refreshAuth(); } catch {
|
||||
clientSocket.end("HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\n\r\n");
|
||||
@@ -712,11 +718,13 @@ server.on("upgrade", (req, clientSocket, clientHead) => {
|
||||
}
|
||||
const pathname = (req.url ?? "").split("?")[0];
|
||||
if (access.plainLanBlocked(req) || !sameOriginUpgrade(req.headers.origin, req.headers.host)) {
|
||||
logRejectedUpgrade(403, pathname, access.plainLanBlocked(req) ? "LAN access refused" : "origin mismatch");
|
||||
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)) {
|
||||
logRejectedUpgrade(401, pathname, "not authenticated");
|
||||
clientSocket.write("HTTP/1.1 401 Unauthorized\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
|
||||
clientSocket.destroy();
|
||||
return;
|
||||
|
||||
@@ -30,6 +30,46 @@ async function start(args) {
|
||||
return { base: `http://127.0.0.1:${port}`, stop: () => child.kill() };
|
||||
}
|
||||
|
||||
async function rawUpgrade(port, path, headers = {}) {
|
||||
const socket = net.createConnection(port, "127.0.0.1");
|
||||
await once(socket, "connect");
|
||||
const request = [
|
||||
`GET ${path} HTTP/1.1`,
|
||||
`Host: 127.0.0.1:${port}`,
|
||||
"Connection: Upgrade",
|
||||
"Upgrade: websocket",
|
||||
"Sec-WebSocket-Version: 13",
|
||||
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==",
|
||||
...Object.entries(headers).map(([name, value]) => `${name}: ${value}`),
|
||||
"",
|
||||
"",
|
||||
].join("\r\n");
|
||||
const reply = new Promise((resolve) => {
|
||||
const done = (data = "") => {
|
||||
socket.destroy();
|
||||
resolve(data.toString());
|
||||
};
|
||||
socket.once("data", done);
|
||||
socket.once("close", () => done());
|
||||
socket.once("error", () => done());
|
||||
});
|
||||
socket.end(request);
|
||||
return reply;
|
||||
}
|
||||
|
||||
function nextLog(stderr, predicate) {
|
||||
return new Promise((resolve) => {
|
||||
let output = "";
|
||||
const onData = (chunk) => {
|
||||
output += chunk.toString();
|
||||
if (!predicate(output)) return;
|
||||
stderr.off("data", onData);
|
||||
resolve(output);
|
||||
};
|
||||
stderr.on("data", onData);
|
||||
});
|
||||
}
|
||||
|
||||
test("serves the app root, deep routes and the update feed", async (t) => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "serve-"));
|
||||
const root = join(dir, "app");
|
||||
@@ -92,6 +132,40 @@ test("icons and the manifest need no sign-in; the app does", async (t) => {
|
||||
assert.equal((await fetch(`${base}/icons/../main.dart.js`)).status, 401);
|
||||
});
|
||||
|
||||
test("rejected websocket upgrades log status and a redacted reason", async (t) => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "serve-upgrade-log-"));
|
||||
const auth = join(dir, "auth.json");
|
||||
await writeFile(auth, JSON.stringify({ password: "password-1", secret: "s".repeat(40) }));
|
||||
const port = await freePort();
|
||||
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port)], {
|
||||
cwd: here,
|
||||
env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth },
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
t.after(async () => {
|
||||
child.kill();
|
||||
await once(child, "exit").catch(() => {});
|
||||
});
|
||||
await once(child.stdout, "data");
|
||||
|
||||
const forbiddenLog = nextLog(child.stderr, (output) => output.includes("status=403"));
|
||||
const forbidden = await rawUpgrade(port, "/connect?token=forbidden-secret", {
|
||||
Origin: "http://evil.example",
|
||||
Cookie: "session=private-cookie",
|
||||
});
|
||||
assert.match(forbidden, /^HTTP\/1\.1 403 Forbidden/m);
|
||||
const forbiddenOutput = await forbiddenLog;
|
||||
assert.match(forbiddenOutput, /websocket upgrade rejected status=403 path=\/connect reason=origin mismatch/);
|
||||
assert.doesNotMatch(forbiddenOutput, /forbidden-secret|private-cookie/);
|
||||
|
||||
const unauthorizedLog = nextLog(child.stderr, (output) => output.includes("status=401"));
|
||||
const unauthorized = await rawUpgrade(port, "/private?token=unauthorized-secret");
|
||||
assert.match(unauthorized, /^HTTP\/1\.1 401 Unauthorized/m);
|
||||
const unauthorizedOutput = await unauthorizedLog;
|
||||
assert.match(unauthorizedOutput, /websocket upgrade rejected status=401 path=\/private reason=not authenticated/);
|
||||
assert.doesNotMatch(unauthorizedOutput, /unauthorized-secret/);
|
||||
});
|
||||
|
||||
test("files require sign-in and proxy query, ranges and attachment headers", async (t) => {
|
||||
const requests = [];
|
||||
const upstream = createServer((req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user