Skip to content

[PR] fix: reject all loopback aliases at the tunnel self check - #7022

Closed
Afloat16 wants to merge 2 commits into
ranxianglei:masterfrom
Afloat16:2026-11-17_tunnel-self-addresses
Closed

Afloat16 wants to merge 2 commits into
ranxianglei:masterfrom
Afloat16:2026-11-17_tunnel-self-addresses

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Problem

Tunnel admission's self check represents loopback by a finite interface set, adding only the first 256 IPv4 loopback addresses. Other 127/8 addresses can pass the serving-port check. DNS-returned IPv4-mapped addresses also use a different representation from ordinary interface addresses and miss the same check.

Change

Use the existing loopback range classifier for same-port self detection. Normalize resolver and interface addresses before testing membership. Remove the incomplete loopback enumeration. The self check remains ahead of allowlist handling; other ports and other machines preserve their existing behavior.

Verification

  • Base: 301e1a5459a77d5578206927c8062a58381af642 (0.1.215), confirmed current before implementation.
  • Five new unit cases plus a real HTTP admission assertion fail on the base; an other-port/other-machine control passes.
  • All six new unit tests pass directly.
  • Tunnel guards, resolver fallback, admin probes, and self-addressed requests: 37 tests passed with an explicit loopback-interface fixture. The environment denies os.networkInterfaces() (uv_interface_addresses), so local HTTP checks supply a known interface inventory; production behavior was not changed to accommodate the restriction.
  • npm run typecheck, npm run build, and git diff --check: passed.
  • Full CI remains the upstream verdict on the published head.

This restores the documented self-destination policy. It is distinct from #6354's inbound-Host marker exemption. No wire format, kernel, config-schema, or dependency changes.

Fixes #7021

@ranxianglei ranxianglei changed the title fix: reject all loopback aliases at the tunnel self check [PR] fix: reject all loopback aliases at the tunnel self check Nov 17, 2026
Merge upstream master 6a641f3, including the architecture module-map and schedule-independent ranxianglei#2929 probe fixes.

Preserve the existing PR head and exact contribution patch. Validation of this refreshed head runs in upstream CI; no local test rerun is claimed.
@ranxianglei

Copy link
Copy Markdown
Owner

Review 结论:tunnel 侧修法正确(枚举 127/8 列表 → classifyIp 分类判断,含 mapped IPv4 归一化),但 diff 外有一个连带行为变化,补一个 guard + 测试后即可收。

连带变化:这个 PR 删掉了 localMachineIps() 里的 127.0.0.x 枚举(classify 已覆盖),但这个函数还有第二个消费方——selfAddressedLocalPath(src/server.ts,#562 self-layer:把绝对形式自访 URL 改写成 origin 形式本地处理)。它还在用"接口 IP 集合"判断,不认识未配置的 127/8 别名。结果:http://127.0.0.5:<自己的端口>/... 这种自访请求从"本地服务"变成"走 tunnel 分类 → 403"。改动前这类请求一直能用(枚举里有 127.0.0.5),改动后静默 403。

修法:selfAddressedLocalPath 的 mine 判断里加 classifyIp(host) === "loopback"(恢复改动前的完整 127/8 语义),并补两条测试锁住。

已验证:新测试在未修代码上红、修后绿;admin-probe + tunnel-self-addresses + tunnel-guard 共 29/29 通过;typecheck 通过。

我对该分支没有 push 权限,请应用以下补丁(或给我写权限):

From 9f594c159dfb824d2430c91fbca951bcb689c1a1 Mon Sep 17 00:00:00 2001
From: ranxianglei <ranxianglei@users.noreply.github.com>
Date: Wed, 18 Nov 2026 09:20:36 +0800
Subject: [PATCH] fix: keep unconfigured loopback aliases self-served in
 selfAddressedLocalPath
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The tunnel-side fix stops enumerating 127.0.0.x in localMachineIps()
(classifyIp covers the range now), but selfAddressedLocalPath still
relies on the interface set — an absolute-form self request via an
unconfigured alias such as 127.0.0.5:selfport would fall through to
tunnel classification and 403 instead of being served locally.

(cherry-pick pending: proposed fix for PR #7022)
---
 src/server.ts             | 7 ++++++-
 tests/admin-probe.test.ts | 2 ++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/server.ts b/src/server.ts
index 725588068..5984c6c28 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -233,7 +233,12 @@ export function selfAddressedLocalPath(reqUrl: string, localPort: number | undef
         const port = u.port !== "" ? Number(u.port) : u.protocol === "https:" ? 443 : 80;
         if (port !== localPort) return undefined;
         const host = u.hostname.replace(/^\[|\]$/g, "").toLowerCase();
-        const mine = host === "0.0.0.0" || host === "::" || host === "localhost" || localMachineIps().has(normalizeIpLiteral(host));
+        // #7022 review: classifyIp covers the whole loopback range, not just
+        // configured interfaces — the tunnel-side fix stopped enumerating
+        // 127.0.0.x in localMachineIps(), so an unconfigured alias like
+        // 127.0.0.5:selfport must still be recognized as self here (else it
+        // would fall through to tunnel classification and 403, #562 self-layer).
+        const mine = host === "0.0.0.0" || host === "::" || host === "localhost" || classifyIp(host) === "loopback" || localMachineIps().has(normalizeIpLiteral(host));
         if (!mine) return undefined;
         return u.pathname + u.search;
     } catch {
diff --git a/tests/admin-probe.test.ts b/tests/admin-probe.test.ts
index da931f9fe..b39838084 100644
--- a/tests/admin-probe.test.ts
+++ b/tests/admin-probe.test.ts
@@ -41,6 +41,8 @@ test("selfAddressedLocalPath: self-addressed absolute-form requests only", () =>
     assert.equal(selfAddressedLocalPath(`http://[::ffff:127.0.0.1]:${P}/__bili/health`, P), "/__bili/health", "hex-mapped loopback folds to 127.0.0.1");
     assert.equal(selfAddressedLocalPath(`http://0.0.0.0:${P}/__bili/health`, P), "/__bili/health");
     assert.equal(selfAddressedLocalPath(`http://[::]:${P}/__acp/x`, P), "/__acp/x", "v6 unspecified in brackets");
+    assert.equal(selfAddressedLocalPath(`http://127.0.0.5:${P}/__bili/health`, P), "/__bili/health", "unconfigured loopback alias is still self (#7022 review)");
+    assert.equal(selfAddressedLocalPath(`http://127.42.19.8:${P}/__bili/health`, P), "/__bili/health", "deep 127/8 alias is still self (#7022 review)");
     // #3245: ANY path qualifies — model-style paths and the /bili/ zero-config
     // entry are served locally exactly as a direct connection would send them.
     assert.equal(selfAddressedLocalPath(`http://127.0.0.1:${P}/v1/chat/completions`, P), "/v1/chat/completions");
-- 
2.43.0

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

已按你给的补丁在 PR 分支本地逐字节应用并验证通过,但推不上去——卡在写权限,和你撞的是同一道墙。

验证(本地已做)

  • 与你贴的补丁逐字节一致(blob 对上:src/server.ts 725588068→5984c6c28、tests/admin-probe.test.ts da931f9fe→b39838084)。
  • admin-probe + tunnel-self-addresses + tunnel-guard 共 29/29 通过;两条新断言在未修代码上红(expected '/__bili/health' / actual undefined)、修后绿——确认是 load-bearing,不是恒真。
  • npm run typecheck 通过。
  • 已备好 commit 11a86c5b39(author: ework-agent),是 PR head 93c8d45833 的直接子提交,fast-forward 可推,随时能上。

推送受阻
git push 到 Afloat16 的 fork 分支 2026-11-17_tunnel-self-addresses 被 GitHub 拒绝:permission denied。跨 fork PR 的 head 分支只有作者(Afloat16)或该 fork 的 collaborator 能推——ranxianglei 账号不是 Afloat16/billion-context 的 collaborator,所以我和你都推不上去(正是你说的「没 push 权限」)。

八问

  1. 是什么问题:PR 的 self-layer 连带回归——tunnel 侧去掉 127.0.0.x 枚举后,selfAddressedLocalPath 仍只认接口 IP 集合,未配置的 127/8 别名(如 127.0.0.5:自端口)从「本地自服务」掉进 tunnel 分类 → 静默 403。
  2. 什么原因:localMachineIps() 有两个消费方,PR 只改了 tunnel 侧那个;self-layer 那个没跟上。
  3. 影响范围/严重程度:技术面只影响「绝对形式自访 + 未配置 127/8 别名 + 自端口」这一窄场景(改动前一直能用,合入后静默 403);属功能回归,非数据/缓存。
  4. 怎么修复:mine 判断加 classifyIp(host) === "loopback",恢复改动前完整 127/8 语义 + 两条断言锁住(已逐字节对齐你的补丁)。
  5. 回归风险:无——只把 self-layer 判定恢复回 PR 前语义,不碰 tunnel 分类/allowlist/其他端口与机器行为(29/29 含 control 用例)。
  6. 破坏缓存:不涉及。
  7. 改设计方向:不涉及——复用现有 classifyIp 分类器,无新机制。
  8. 新配置:不涉及。

待你拍板(推送路径)

选项 做法 代价/波及
A(推荐) Afloat16 在 fork 设置里把 ranxianglei 加成 collaborator 一次授权;之后这个 PR 的后续迭代我都能直接推
B Afloat16 直接应用你贴的补丁(与我本地 11a86c5b39 一致) 无需授权,但依赖作者本人操作

选 A 的话,加完 collaborator 告诉我一声,我立刻把 11a86c5b39 推上去。

@ranxianglei

Copy link
Copy Markdown
Owner

Superseded by #7101 (batch-2) — this PR's content (and its review-patch fix where applicable) is now merged via #7101. Closing to avoid a duplicate merge. 本 PR 的内容(含评审补丁,如适用)已通过 #7101 合并,关闭以免重复合并。

Louie0806 pushed a commit to Louie0806/billion-context that referenced this pull request Nov 18, 2026
…ocalPath

The tunnel-side fix stops enumerating 127.0.0.x in localMachineIps()
(classifyIp covers the range now), but selfAddressedLocalPath still
relies on the interface set — an absolute-form self request via an
unconfigured alias such as 127.0.0.5:selfport would fall through to
tunnel classification and 403 instead of being served locally.

(cherry-pick pending: proposed fix for PR ranxianglei#7022)

(cherry picked from commit 9f594c159dfb824d2430c91fbca951bcb689c1a1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tunnel self-destination admission misses loopback aliases and mapped resolver answers

2 participants