From a8515497859101b99a800520111bd343c0283df3 Mon Sep 17 00:00:00 2001 From: Afloat Date: Tue, 17 Nov 2026 12:08:47 +0800 Subject: [PATCH] fix: reject all loopback aliases at the tunnel self check --- src/tunnel-guard.ts | 11 +++-- tests/tunnel-guard.test.ts | 5 +++ tests/tunnel-self-addresses.test.ts | 64 +++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 tests/tunnel-self-addresses.test.ts diff --git a/src/tunnel-guard.ts b/src/tunnel-guard.ts index 01965ae160..3c9b79924d 100644 --- a/src/tunnel-guard.ts +++ b/src/tunnel-guard.ts @@ -214,8 +214,6 @@ export function localMachineIps(): Set { 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; @@ -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" }; } } diff --git a/tests/tunnel-guard.test.ts b/tests/tunnel-guard.test.ts index d5806bb37a..c504351e07 100644 --- a/tests/tunnel-guard.test.ts +++ b/tests/tunnel-guard.test.ts @@ -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); diff --git a/tests/tunnel-self-addresses.test.ts b/tests/tunnel-self-addresses.test.ts new file mode 100644 index 0000000000..fc7cf8afb4 --- /dev/null +++ b/tests/tunnel-self-addresses.test.ts @@ -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 }); +});