mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
stripPort("[allowed]evil.tld") returned "allowed", so the allowlist
accepted the host while the connector resolved and contacted evil.tld.
A bracketed host may now only be followed by :port.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
97 lines
4.5 KiB
JavaScript
97 lines
4.5 KiB
JavaScript
// Connect/close races on one WebSocket must not leave connector processes
|
|
// behind. Uses a fake connector that records its PID and lives until its
|
|
// stdin closes (like the real one), against the built gateway.
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { spawn } from "node:child_process";
|
|
import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
import { createServer } from "node:net";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
|
|
const INDEX = path.resolve(import.meta.dirname, "../dist/index.js");
|
|
const wait = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
const alive = (pid) => { try { process.kill(pid, 0); return true; } catch { return false; } };
|
|
const freePort = () => new Promise((ok) => {
|
|
const s = createServer().listen(0, () => { const p = s.address().port; s.close(() => ok(p)); });
|
|
});
|
|
|
|
test("no connector outlives its WebSocket", { timeout: 60_000 }, async () => {
|
|
const dir = mkdtempSync(path.join(tmpdir(), "ws3-orphans-"));
|
|
const pids = path.join(dir, "pids");
|
|
const fake = path.join(dir, "fake-connector.sh");
|
|
writeFileSync(fake, `#!/bin/sh\necho $$ >> '${pids}'\ncat > /dev/null\nsleep 1\n`);
|
|
chmodSync(fake, 0o755);
|
|
const port = await freePort();
|
|
const gw = spawn(process.execPath, [INDEX], {
|
|
cwd: dir,
|
|
env: { PATH: process.env.PATH, PORT: String(port), WEB_STATIC: "0", TELEMETRY: "0", CONNECTOR_BIN: fake },
|
|
stdio: "ignore",
|
|
});
|
|
const recorded = () => (existsSync(pids) ? readFileSync(pids, "utf8").split("\n").filter(Boolean).map(Number) : []);
|
|
try {
|
|
for (let i = 0; i < 50; i++) { try { await fetch(`http://127.0.0.1:${port}/api/features`); break; } catch { await wait(100); } }
|
|
const connect = JSON.stringify({ type: "connect", host: "example.invalid", nickname: "dummy" });
|
|
const open = async () => {
|
|
const ws = new WebSocket(`ws://127.0.0.1:${port}/ws`);
|
|
await new Promise((ok, fail) => { ws.onopen = ok; ws.onerror = fail; });
|
|
return ws;
|
|
};
|
|
|
|
// connect, connect while the first connector shuts down, close
|
|
let ws = await open();
|
|
ws.send(connect); await wait(300);
|
|
ws.send(connect); await wait(100);
|
|
ws.close();
|
|
// connect, then two connects back to back, close later
|
|
ws = await open();
|
|
ws.send(connect); await wait(300);
|
|
ws.send(connect); ws.send(connect);
|
|
await wait(4000);
|
|
ws.close();
|
|
|
|
await wait(4000);
|
|
assert.ok(recorded().length >= 2, "fake connector was spawned");
|
|
assert.deepEqual(recorded().filter(alive), [], "connector processes left behind");
|
|
} finally {
|
|
for (const pid of recorded()) if (alive(pid)) process.kill(pid, "SIGKILL");
|
|
await new Promise((ok) => { gw.once("exit", ok); gw.kill("SIGTERM"); });
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("ALLOWED_SERVERS can't be bypassed with a bracketed prefix", { timeout: 30_000 }, async () => {
|
|
const dir = mkdtempSync(path.join(tmpdir(), "ws3-allow-"));
|
|
const pids = path.join(dir, "pids");
|
|
const fake = path.join(dir, "fake-connector.sh");
|
|
writeFileSync(fake, `#!/bin/sh\necho $$ >> '${pids}'\ncat > /dev/null\n`);
|
|
chmodSync(fake, 0o755);
|
|
const port = await freePort();
|
|
const gw = spawn(process.execPath, [INDEX], {
|
|
cwd: dir,
|
|
env: { PATH: process.env.PATH, PORT: String(port), WEB_STATIC: "0", TELEMETRY: "0", CONNECTOR_BIN: fake, ALLOWED_SERVERS: "ts.example.com" },
|
|
stdio: "ignore",
|
|
});
|
|
try {
|
|
for (let i = 0; i < 50; i++) { try { await fetch(`http://127.0.0.1:${port}/api/features`); break; } catch { await wait(100); } }
|
|
const reply = async (host) => {
|
|
const ws = new WebSocket(`ws://127.0.0.1:${port}/ws`);
|
|
const msgs = [];
|
|
ws.onmessage = (e) => msgs.push(JSON.parse(e.data));
|
|
await new Promise((ok, fail) => { ws.onopen = ok; ws.onerror = fail; });
|
|
ws.send(JSON.stringify({ type: "connect", host, nickname: "dummy" }));
|
|
await wait(500);
|
|
ws.close();
|
|
return msgs.find((m) => m.type === "error")?.message ?? "";
|
|
};
|
|
for (const host of ["[ts.example.com]evil.tld", "[ts.example.com]evil.tld:9987", "evil.tld"]) {
|
|
assert.match(await reply(host), /not allowed/, host);
|
|
}
|
|
assert.doesNotMatch(await reply("ts.example.com:9987"), /not allowed/);
|
|
assert.equal(existsSync(pids) ? readFileSync(pids, "utf8").split("\n").filter(Boolean).length : 0, 1, "only the allowed host spawned a connector");
|
|
} finally {
|
|
await new Promise((ok) => { gw.once("exit", ok); gw.kill("SIGTERM"); });
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|