Gateway: ALLOWED_SERVERS ignored text after a bracketed host

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
This commit is contained in:
MoepchiandClaude Opus 5.5 committed 2026-11-25 12:49:15 +00:00
1 parent 5f9950a96b
commit 512366bc94
2 files changed
+39 -2

No files matched your search

+4 -2
View File
@@ -50,8 +50,10 @@ const allowedServers = () =>
// as-is. Deliberately not full URL parsing - `host` here is never a URL.
function stripPort(host: string): string {
if (host.startsWith("[")) {
const end = host.indexOf("]");
return end === -1 ? host : host.slice(1, end);
// Only "[v6]" or "[v6]:port"; anything after "]" would otherwise be
// ignored here but still reach the connector ("[allowed]evil.tld").
const m = /^\[([^\]]*)\](?::\d+)?$/.exec(host);
return m ? m[1] : host;
}
const i = host.lastIndexOf(":");
return i !== -1 && /^\d+$/.test(host.slice(i + 1)) ? host.slice(0, i) : host;
@@ -59,3 +59,38 @@ test("no connector outlives its WebSocket", { timeout: 60_000 }, async () => {
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 });
}
});