Skip to content

fix: forward-proxy requests no longer misread as self, restoring per-upstream window config (#562) - #563

Merged
ranxianglei merged 2 commits into
masterfrom
2026-09-06_fix-forward-proxy-window
Sep 6, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-09-06_fix-forward-proxy-window

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #562.

What changed

resolveUpstream() (src/server.ts) decided whether an absolute-form request targeted the proxy itself by comparing the URL authority against the client-provided Host header. In a real HTTP forward proxy the client sets Host to the upstream (== the URL authority), so every legitimate forward-proxy request was misclassified as a self-request and dropped to undefined. Consequences:

  • Config resolution (embeddedUrl = route?.rewrittenUrl) received undefined → the per-upstream model-window config was silently missed (fell back to the global window).
  • The fallback in buildForwardTarget still forwarded via the full URL → chat kept working while the window config did not take effect.

Fix

Every well-formed absolute-form request is now marked a tunnel and routed through checkTunnelDestination, whose self-layer (destination port == our bound port AND a local-machine IP) 403s a genuine self-forward before any forwarding — no silent fall-through to the fallback path. Self-detection now uses the actual listening endpoint instead of the client-provided Host header. This preserves the original goals (prevent self-loop + keep the management plane unreachable through the tunnel) while fixing the misclassification. /bili/, CONNECT/MITM, and the admin gate are unchanged.

Regression coverage (mapped to the issue's list)

  1. A real forward-proxy request (full upstream URL + upstream Host) forwards correctly — the existing forward-absolute-url test now exercises the previously-broken branch (before, it only passed via the fallback forward).
  2. New unit test: that request resolves the per-upstream window (120000) via resolveConfiguredContextLimit on the resolved rewrittenUrl, not the global default.
  3. New unit test: the forward-proxy and /bili/ forms of the same upstream resolve to the same window.
  4. New integration test: a self-targeting absolute URL is denied with 403 (tunnel_destination_denied) instead of being forwarded; admin/tunnel protection does not regress (self-targets still carry the tunnel marker + admission gate).

Pre-flight

  • npm run typecheck — pass
  • npm test — 1049/1050. The single failure (tests/launcher.test.ts → resolveClientCommand: codex/claude resolve to themselves) is pre-existing and environment-specific: this sandbox has a real codex binary at /usr/bin/codex, so which codex resolves to the absolute path rather than the bare name the test expects. Verified it fails identically with these changes stashed. Unrelated to this change.
  • npm run build — pass
  • E2E (tests/e2e/e2e-codex.test.ts) not run here: it requires a live local sglang upstream at 127.0.0.1:8199, which is outside this sandbox's permitted network boundary. Note the e2e drives codex via origin-form / /bili/ requests, which do not exercise the forward-proxy (absolute-form) branch touched here; that branch is covered directly by the new unit + integration tests above.

…upstream window config (#562)

resolveUpstream() decided "is this the proxy itself?" by comparing the
absolute-URL authority against the client-provided Host header. In a real
forward proxy the client sets Host to the UPSTREAM (== the URL authority), so
every legitimate forward-proxy request was misclassified as a self-request and
dropped to undefined — losing per-upstream context-window config
(route?.rewrittenUrl undefined) while the fallback forward still reached the
upstream (chat kept working, the window silently did not).

Every well-formed absolute-form request is now marked a tunnel and routed
through checkTunnelDestination, whose self-layer (destination port == our bound
port AND a local-machine IP) 403s a genuine self-forward before any forwarding —
no silent fall-through to the fallback path. Self-detection uses the actual
listening endpoint instead of the client-provided Host header.

Tests: forward-proxy (Host == upstream authority) routes as a tunnel; the
forward-proxy and /bili/ forms of the same upstream resolve the same model
window; a self-targeting absolute URL is denied with 403 instead of forwarded.
@github-actions

github-actions Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-06_fix-forward-proxy-window (59f6cd7)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-563

Each push to this PR publishes a new version under the pr-563 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr563.tgz
npm install -g package

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] 🏷 Reviewed #563 (fix for the forward-proxy self-misclassification). Verdict: LGTM — correct, root-cause, and well-covered. No blocking issues.

Triage (bug verification)

Bug is real — confirmed at code level. In base resolveUpstream() (src/server.ts), the forward-proxy branch compared u.host against the client-provided Host header:

const ownHost = (req?.headers.host ?? "").toLowerCase();
if (u.host.toLowerCase() !== ownHost) { return { …, tunnel: true }; }
// else → fall through to `return undefined`

In a real HTTP forward proxy the client sets Host to the upstream (== the URL authority), so u.host === ownHost is true for every legitimate request → the guard never fired → route dropped to undefined. Downstream:

  • embeddedUrl = route?.rewrittenUrl → undefined (src/server.ts:1040)
  • resolveConfiguredContextLimit(routes, undefined, model) bails immediately at src/config.ts:205 (if (!model || !upstreamUrl) return undefined) → per-upstream window silently fell back to global/registry.
  • Meanwhile buildForwardTarget's fallback (isAbsoluteUrl ? reqUrl : …, src/server.ts:2445) still forwarded via the full URL → chat kept working while config didn't apply. Exactly as described.

Layer: the diagnosis hits the root cause, not a symptom. Using a client-supplied header as a self-detection signal is fundamentally unsound for a forward proxy (the header IS the upstream). The fix removes that signal and decides self-ness from the actual listening endpoint via checkTunnelDestination — the right layer.

Solution assessment

Correct and strictly safer than the status quo:

  • Self-targets are now marked tunnel: true and 403'd by checkTunnelDestination Layer 1 (dest port == bound port and local-machine IP, src/tunnel-guard.ts:136-141) before any forwarding — no silent fall-through.
  • As a side effect this also stamps x-bili-tunnel on all absolute-form forwards (src/server.ts:2462). Under the old bug those carried no marker (route was undefined), so the management-plane-through-tunnel guarantee was actually weaker than intended; the fix restores it for chained/NAT-hairpin cases too.
  • /bili/, CONNECT/MITM, and the admin gate are untouched. Single call site of resolveUpstream (handle, src/server.ts:922), so no other consumer depends on the old "self→undefined" behavior.

Pre-flight (ran locally on the PR head)

  • npm run typecheck — pass
  • npm test — 1049/1050. The one failure, tests/launcher.test.ts → resolveClientCommand: codex/claude resolve to themselves, is pre-existing & environment-specific: I stashed the PR's three changed files and re-ran tests/launcher.test.ts on the clean base — it fails identically there (this sandbox has a codex binary resolvable on PATH, so which returns an absolute path instead of the bare name the test expects). Unrelated to this change. Confirmed your note.
  • npm run build — pass (dist/index.js produced, acp-kernel inlined).
  • New tests pass individually: both tests/forward-absolute-url.test.ts cases (forward correctness; self-target → 403 + tunnel_destination_denied, which is only emitted at src/server.ts:936 so it definitively proves the tunnel self-guard fired) and the new upstream-proxy-routing assertions (per-upstream window 120000 resolved off rewrittenUrl; forward-proxy vs /bili/ parity).
  • E2E not run here — needs a live local sglang upstream outside this sandbox's network boundary. Agreed with your reasoning: e2e drives origin-form / /bili/ traffic, not the absolute-form branch touched here, so direct unit+integration coverage is the appropriate scope.

Minor notes (non-blocking)

  • The fix restores more than just the window: every per-upstream lookup keys off route?.rewrittenUrl (proxy decision at src/server.ts:2483, resolveCompressProtocol, compress-settings). Forward-proxy requests therefore regain their full per-upstream config, not only the context window. Worth a line in the PR body / changelog since it broadens the impact beyond "window config".
  • Self-guard depends on req.socket.localPort being the bound port (src/server.ts:929) — correct for a plain http.Server; fine as-is.

Recommendation: merge-ready. Per repo rules I can't merge PRs (human-only) — please merge when you're ready: #563

…s in tunnel-guard

WHATWG URL canonicalizes [::ffff:127.0.0.1] to the hex form
[::ffff:7f00:1], which sailed past classifyIp's dotted-only matcher as
'public' — a remote client could tunnel to loopback, RFC1918, or
169.254.169.254 metadata through the hex spelling (#409 follow-up found
while reviewing #562). Fold both mapped spellings to the dotted quad via
normalizeIpLiteral at the checkTunnelDestination entry, and treat
0.0.0.0/:: destinations on our own serving port as the self-loop they
route to (previously misrouted to the default upstream after one
hop-marker bounce).
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.

bug: 标准 HTTP 代理请求被误判为自身请求,导致上游模型窗口配置失效

1 participant