Skip to content

[PR] fix(wiring): restore a collapsed scheme slash in an embedded /bili/ URL - #8287

Open
yangwuan55 wants to merge 1 commit into
ranxianglei:masterfrom
yangwuan55:2026-12-05_bili-prefix-collapsed-slash
Open

yangwuan55 wants to merge 1 commit into
ranxianglei:masterfrom
yangwuan55:2026-12-05_bili-prefix-collapsed-slash

Conversation

@yangwuan55

Copy link
Copy Markdown

Closes #8286

What broke

resolveUpstream accepts an embedded /bili/ URL only when it already starts with http:// / https://. A client that normalises its base-URL path collapses the double slash, so the embedded URL arrives as /bili/http:/host/..., is not recognised as a tunnel, falls through all lanes and the request is answered with an opaque 403 {"error":{"type":"forbidden","message":"Request not allowed"}} — no log line names the cause.

Reproduced on 6d6ed9e (v0.1.218):

$ curl -s -o /dev/null -w '%{http_code}\n' 'http://127.0.0.1:8787/bili/https:/api.github.com/zen'
403
$ curl -s -o /dev/null -w '%{http_code}\n' 'http://127.0.0.1:8787/bili/https://api.github.com/zen'
200

The concrete client is the Antigravity CLI (agy 1.5.24): GOOGLE_GEMINI_BASE_URL=http://127.0.0.1:8787/bili/http://127.0.0.1:3425 is sent as POST /bili/http:/127.0.0.1:3425/v1beta/models/…:streamGenerateContent?alt=sse. This is the only blocker for the magpie agy lane (yetone/magpie#7663), which assumes the /bili/ prefix already works.

Fix

src/server.ts — normalise the scheme of the candidate before parsing, for the bare /bili/<url> form and for the <protocol>/-prefixed form (/bili/google/<url>):

const restoreSchemeSlashes = (s: string) => s.replace(/^(https?):\/*/, "$1://");

Well-formed input round-trips byte-identical (http:// → http://), so no existing lane changes behaviour: the strict parse still runs first and only the collapsed shape gains a path. not-a-url still has no scheme and stays unroutable, keeping the existing negative test green.

Tests

tests/zero-config-routing.test.ts — two cases: collapsed form on the bare prefix (asserts upstream, rewrittenUrl, tunnel) and collapsed form after the google/ protocol prefix (asserts upstream + explicitProtocol).

Pre-flight (local)

  • node --import tsx --test tests/zero-config-routing.test.ts → 7/7 pass (5 existing + 2 new)
  • npm run typecheck → clean
  • npm run build → OK (dist-import-annotations: OK, dist-external: OK)

Evidence (A/B, real traffic, not "should work")

Same bili build on --port 8787, magpie gateway on 127.0.0.1:3425:

through agy before after
agy … -p "reply exactly: collapsedok" with GOOGLE_GEMINI_BASE_URL=http://127.0.0.1:8787/bili/http://127.0.0.1:3425 403 "Request not allowed" collapsedok, new pfa-* session in bili sessions ls (10.7 KiB, prefix-affinity)

curl collapsed form flips 403 → 200 on the same build. No launch-side shim or rewrite was used — agy's own collapsed URL now folds normally.

A client that normalises its base-URL path (Antigravity CLI: GOOGLE_GEMINI_BASE_URL
`…/bili/http://host` reaches bili as `/bili/http:/host`) had its embedded URL
dropped by resolveUpstream, which only accepted `http://` / `https://`; the
request fell through to an opaque 403 "Request not allowed" with no log line
naming the cause.

Normalise `^(https?):\/*` to `$1://` before parsing, for both the bare
`/bili/<url>` form and the `<protocol>/`-prefixed form. Well-formed URLs are
returned byte-identical, so no existing lane changes behaviour.

Closes ranxianglei#8286
@ranxianglei

ranxianglei commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen4-35b-a3b

[bot] 🏷 (ai: qwen4-35b-a3b)

查重: 不重复。Searched open+closed issues & PRs for this topic — the only related item is #8286, the bug issue this PR closes (intended relationship, not a duplicate). No parallel fix PR exists.

Review verdict: 可合 (approve). I verified the fix independently end-to-end, not just by reading the diff.

What I ran:

  • Unit — tests/zero-config-routing.test.ts: 7/7 pass on the PR head. Teeth check: restoring src/server.ts from master makes the 2 new tests fail (5 pass), so the tests genuinely pin the fix.
  • E2E (real proxy + local dummy upstream) — on master the collapsed form /bili/http:/host/… returns 403 {"error":{"type":"forbidden","message":"Request not allowed"}} while the well-formed form returns 200. On the PR branch the collapsed form returns 200 and tunnels correctly; the well-formed form is unchanged; /bili/not-a-url is still rejected; the zero-slash form /bili/http:host also normalizes (bonus).
  • CI — all 30 checks green on head 6c16f528 (test lanes ubuntu/windows × node 22/24 all success).

结构化八问

  1. 是什么问题:客户端把内嵌 /bili/ URL 的双斜杠折叠成单斜杠(实证 Antigravity CLI agy 1.5.24 发出 /bili/http:/host)时,bili 不识别为隧道,落入 403「Request not allowed」,且无日志点明原因。
  2. 什么原因导致的:resolveUpstream(src/server.ts)只认已以 http:///https:// 开头的内嵌 URL;折叠形态不匹配,穿透所有车道后被拒。
  3. 影响范围与严重程度:仅影响会规范化/折叠 base-URL 路径的客户端;well-formed URL 不受影响。实证阻塞 magpie agy 车道。定级 p2(局部功能缺陷)。
  4. 怎么修复的:新增 restoreSchemeSlashes 助手,把 http(s): 后跟零或多个斜杠归一为 http:///https://,同时作用于裸 /bili/<url> 与 <protocol>/<url> 两种形态;well-formed 输入逐字节往返不变,严格解析仍先行。
  5. 是否有回归问题:无。well-formed 恒等、负例 not-a-url 仍不可路由、非 /bili/ 请求不受影响;e2e 实测折叠 403→200、wellformed 200→200、负例 403→403。唯一行为变化(折叠形态 403→200)即本 PR 目的,已在描述中披露。
  6. 是否破坏缓存:不涉及——纯 URL 路由,未触碰缓存/ref/id/账本逻辑。
  7. 是否改了设计方向:否——在既有 resolveUpstream 路由内的最小定向修复,无架构/边界变化。
  8. 是否引入了新的配置:否——无新 config/env/flag/默认值。

Non-blocking note: branch is behind master but mergeable with no conflict; CI is green on the head, so no rebase is required to merge.

判定:可合。修复正确、最小、单模块、测试与 CI 全绿、e2e 实证通过;无回归、无缓存影响、无新配置面。未向 PR 分支追加任何 commit(未发现缺陷)。合并由 owner 执行。

@ranxianglei ranxianglei added bug Something isn't working p2 scope/wiring labels Dec 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working p2 scope/wiring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Collapsed scheme slash in an embedded /bili/ URL 403s instead of tunnelling (agy × magpie, #7663)

2 participants