Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/tunnel-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ export function localMachineIps(): Set<string> {
if (ni.address) ips.add(ni.address.toLowerCase());
}
}
// The whole 127/8 loopback range routes to this machine.
for (let i = 0; i <= 255; i++) ips.add(`127.0.0.${i}`);
localAddrCache = { at: now, ips };
}
return localAddrCache.ips;
Expand Down Expand Up @@ -355,8 +353,13 @@ export async function checkTunnelDestination(origin: string, ctx: TunnelCheckCon
// destination routes to the local host, so our own port through them is
// a self-loop even though neither literal sits in the interface set.
if (ctx.selfPort !== undefined && port === ctx.selfPort) {
const mine = (ctx.localIps ?? localMachineIps)();
if (ips.some((ip) => ip === "0.0.0.0" || ip === "::" || mine.has(ip.toLowerCase()) || mine.has(`::ffff:${ip.toLowerCase()}`))) {
const mine = new Set(Array.from((ctx.localIps ?? localMachineIps)(), normalizeIpLiteral));
if (ips.some((address) => {
const ip = normalizeIpLiteral(address);
// Loopback is a range, not a list of configured interfaces. DNS
// answers also need the mapped-IPv4 normalization literals get.
return ip === "0.0.0.0" || classifyIp(ip) === "loopback" || mine.has(ip);
})) {
return { ok: false, code: "self", message: "the bili tunnel may not target the proxy itself" };
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/tunnel-guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,11 @@ test("integration: tunnel cannot reach the proxy's own management plane (#409 Po
assert.match(poc.body, /tunnel_destination_denied/);
const pocPut = await get(selfPort, `/bili/http://127.0.0.1:${selfPort}/__bili/config`);
assert.equal(pocPut.status, 403);
// Every 127/8 address denotes this machine, including aliases that do
// not appear in networkInterfaces(). Reject before opening a socket.
const alias = await get(selfPort, `/bili/http://127.42.19.8:${selfPort}/v1/models`);
assert.equal(alias.status, 403);
assert.match(alias.body, /"detail":"self"/);
// Direct loopback management access still works (marker not present).
const direct = await get(selfPort, "/__bili/config", { host: `127.0.0.1:${selfPort}` });
assert.equal(direct.status, 200);
Expand Down
64 changes: 64 additions & 0 deletions tests/tunnel-self-addresses.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import test from "node:test";
import assert from "node:assert/strict";
import { checkTunnelDestination } from "../src/tunnel-guard.ts";

const localIps = () => new Set(["127.0.0.1", "::1", "192.168.1.5"]);
const context = { selfPort: 8787, clientLoopback: true, allowlist: [], localIps };

test("self admission covers the whole IPv4 loopback range", async () => {
for (const host of ["127.0.1.1", "127.42.19.8", "127.255.255.254"]) {
const verdict = await checkTunnelDestination(`http://${host}:8787/v1/models`, context);
assert.equal(verdict.ok, false, host);
if (verdict.ok) assert.fail("self destination was admitted");
assert.equal(verdict.code, "self");
}
});

test("mapped IPv4 loopback aliases cannot reach the serving port", async () => {
for (const host of ["[::ffff:127.42.19.8]", "[::ffff:7f2a:1308]"]) {
const verdict = await checkTunnelDestination(`http://${host}:8787`, context);
assert.equal(verdict.ok, false, host);
if (verdict.ok) assert.fail("mapped self destination was admitted");
assert.equal(verdict.code, "self");
}
});

test("resolved loopback aliases receive the same self verdict as literals", async () => {
for (const address of ["127.42.19.8", "::ffff:127.42.19.8", "::ffff:7f2a:1308"]) {
const verdict = await checkTunnelDestination("http://model.test:8787", {
...context, resolveHost: async () => [address],
});
assert.equal(verdict.ok, false, address);
if (verdict.ok) assert.fail("resolved self destination was admitted");
assert.equal(verdict.code, "self");
}
});

test("mapped resolver answers match the machine's ordinary interface addresses", async () => {
for (const address of ["::ffff:192.168.1.5", "::ffff:c0a8:105"]) {
const verdict = await checkTunnelDestination("http://model.test:8787", {
...context, resolveHost: async () => [address],
});
assert.equal(verdict.ok, false, address);
if (verdict.ok) assert.fail("own interface was admitted");
assert.equal(verdict.code, "self");
}
});

test("an allowlist cannot exempt a remote client's loopback self destination", async () => {
const verdict = await checkTunnelDestination("http://127.42.19.8:8787", {
...context, clientLoopback: false, allowlist: ["127.42.19.8:8787"],
});
assert.equal(verdict.ok, false);
if (verdict.ok) assert.fail("allowlisted self destination was admitted");
assert.equal(verdict.code, "self");
});

test("other ports and other machines retain their self-hosted upstream behavior", async () => {
for (const origin of ["http://127.42.19.8:8199", "http://[::ffff:127.42.19.8]:8199", "http://192.168.1.6:8787"]) {
assert.deepEqual(await checkTunnelDestination(origin, context), { ok: true }, origin);
}
assert.deepEqual(await checkTunnelDestination("http://model.test:8199", {
...context, resolveHost: async () => ["::ffff:c0a8:105"],
}), { ok: true });
});
Loading