From 9326946fcaf57fe518c295bd0c80c7ee779d9dfe Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 13:23:34 +0800 Subject: [PATCH 1/2] fix: stop leaking x-bili-plugin* host headers to the model endpoint (#3905) The main HTTP forward path copied every inbound header to the upstream except content-length/host, so all host-private x-bili-plugin* convention headers (agent marker, conversation id, window/model declarations, and the declared- tools channel from #3906/#3907) rode through to the model provider. Add stripBiliPluginHeaders (src/server/headers.ts) and apply it at both outbound model-endpoint derivations: the HTTP forward choke point (buildForwardTarget, which every retry/refetch reuses) and the WS lane final egress (finalizeResponsesWsUpstreamHeaders, non-Azure). The Azure WS branch keeps stripping every x-bili-* for the documented custom-header cap (#2359). The bili->bili protocol markers (x-bili-hop / x-bili-tunnel / x-bili-ws-lane) are deliberately left untouched: their consumer is a downstream bili's ingress decision (#300/#1086), so they must survive transit even though they leak harmlessly onto a terminal provider. A namespace-wide strip was tried first and broke x-bili-ws-lane transit (codex-responses-ws #3073), so the fix is scoped to the plugin family only. Regression-pinned both directions: host-private x-bili-plugin* no longer reach the model endpoint (proxy + plugin lanes, incl. the new exposed-tools channel); the chain markers still propagate; the WS lane presents the same surface. Fixes #3905 --- src/responses-ws.ts | 13 +- src/server.ts | 6 +- src/server/headers.ts | 19 ++ tests/issue3905-egress-bili-strip.test.ts | 228 ++++++++++++++++++++++ 4 files changed, 263 insertions(+), 3 deletions(-) create mode 100644 tests/issue3905-egress-bili-strip.test.ts diff --git a/src/responses-ws.ts b/src/responses-ws.ts index 688b679094..9c38408619 100644 --- a/src/responses-ws.ts +++ b/src/responses-ws.ts @@ -4,7 +4,7 @@ import WebSocket from "ws"; import { MAX_REQUEST_BYTES, fetchWithTimeout, type FetchOptions } from "./fetch-util.js"; import { withFetchTransport } from "./fetch-transport.js"; import { BILI_HOP_HEADER } from "./server/context-window.js"; -import { connectionNamedHeaders, stripRemoteCompactionBetaFeature, UPSTREAM_HOP_HEADERS } from "./server/headers.js"; +import { connectionNamedHeaders, stripBiliPluginHeaders, stripRemoteCompactionBetaFeature, UPSTREAM_HOP_HEADERS } from "./server/headers.js"; import { BILI_TUNNEL_HEADER } from "./tunnel-guard.js"; import { normalizeSseLineEndings, finalizeSseLineEndings } from "./sse-util.js"; import type { WsBridgeCodec, WsBridgeContext, WsBridgeSession } from "./ws-bridge.js"; @@ -85,7 +85,16 @@ export function finalizeResponsesWsUpstreamHeaders(url: string, rawHeaders: Fetc const headers = Object.fromEntries(new Headers(rawHeaders).entries()); for (const key of Object.keys(headers)) if (UPSTREAM_HOP_HEADERS.has(key) || key.startsWith("sec-websocket-")) delete headers[key]; stripRemoteCompactionBetaFeature(headers); - if (isAzureOpenAiHost(url)) for (const key of Object.keys(headers)) if (key.startsWith("x-bili-")) delete headers[key]; + // #3905: parity with the HTTP forward path. Azure caps custom WS headers + // (#2359), so there EVERY x-bili-* goes (protocol markers included — azure is + // a terminal provider that never reads them). Elsewhere strip only the + // host-private x-bili-plugin* family, keeping hop/tunnel/ws-lane alive for + // downstream-bili transit (#300/#1086). + if (isAzureOpenAiHost(url)) { + for (const key of Object.keys(headers)) if (key.startsWith("x-bili-")) delete headers[key]; + } else { + stripBiliPluginHeaders(headers); + } return headers; } diff --git a/src/server.ts b/src/server.ts index a35e0831a2..c4cac20c3d 100644 --- a/src/server.ts +++ b/src/server.ts @@ -146,7 +146,7 @@ import { streamKeepAliveMs as knobStreamKeepAliveMs, } from "./knobs.js"; import { BILI_HOP_HEADER, anthropicBetaContextWindow, capRegistryWindowByStandard, expandedContextSuffixWindow, LAUNCHER_MODEL_WINDOWS, LAUNCHER_MODEL_MAX_OUTPUTS, launcherContextWindow, launcherMaxOutput, parseLauncherModelWindows, windowSourceLogged } from "./server/context-window.js"; -import { connectionNamedHeaders, NO_IDENTITY_MESSAGE, stripRemoteCompactionBetaFeature, UPSTREAM_HOP_HEADERS } from "./server/headers.js"; +import { connectionNamedHeaders, NO_IDENTITY_MESSAGE, stripBiliPluginHeaders, stripRemoteCompactionBetaFeature, UPSTREAM_HOP_HEADERS } from "./server/headers.js"; import { installWebSocketBridge } from "./ws-bridge.js"; import { codexResponsesCodec, responsesCodec } from "./responses-ws.js"; import { currentFetchTransport } from "./fetch-transport.js"; @@ -2509,6 +2509,10 @@ export function buildForwardTarget( } if (!(isLoopbackAddress(req.socket.remoteAddress) && destLoopback && destAdminPath)) headers[BILI_TUNNEL_HEADER] = "1"; } + // #3905: strip the host-private x-bili-plugin* convention family so none of + // it reaches the model endpoint. The bili→bili protocol markers (hop/tunnel/ + // ws-lane) are intentionally left for downstream-bili transit (#300/#1086). + stripBiliPluginHeaders(headers); headers["host"] = new URL(upstreamUrl).host; // #3073: shared with the WS lane's final egress so probe and turn carry // the same beta surface (see stripRemoteCompactionBetaFeature). diff --git a/src/server/headers.ts b/src/server/headers.ts index dbc56efe92..18d5705709 100644 --- a/src/server/headers.ts +++ b/src/server/headers.ts @@ -43,6 +43,25 @@ export const UPSTREAM_HOP_HEADERS = new Set([ // binary bodies (#677). export const RESPONSE_ONLY_STRIP_HEADERS = new Set(["content-encoding"]); +// #3905: the `x-bili-plugin*` family is host-private convention metadata — the +// agent marker, conversation id, window/model declarations, and the declared- +// tools channel (#3906/#3907). Each is consumed ONCE at this instance's ingress +// to configure how the request is handled; none is meaningful to a real model +// endpoint, yet the main HTTP lane used to copy every inbound header through, +// leaking the whole family to the provider. Strip it at every outbound +// model-endpoint derivation. The bili→bili PROTOCOL markers (x-bili-hop, +// x-bili-tunnel, x-bili-ws-lane) are deliberately left alone: their consumer is +// a DOWNSTREAM bili's ingress decision (#300/#1086), so they must survive +// transit even though they leak harmlessly onto a terminal provider. +/** #3905: remove every `x-bili-plugin*` header. Applied at each outbound + * model-endpoint derivation (HTTP forward + WS egress) so host-private + * convention metadata never rides to the provider. Mutates in place. */ +export function stripBiliPluginHeaders(headers: Record): void { + for (const k of Object.keys(headers)) { + if (k.toLowerCase().startsWith("x-bili-plugin")) delete headers[k]; + } +} + // RFC 7230 §6.1: the Connection header names additional hop-by-hop headers // that must be stripped per-message. Returns their lowercased names. export function connectionNamedHeaders(conn: string | string[] | undefined): Set { diff --git a/tests/issue3905-egress-bili-strip.test.ts b/tests/issue3905-egress-bili-strip.test.ts new file mode 100644 index 0000000000..207750a85c --- /dev/null +++ b/tests/issue3905-egress-bili-strip.test.ts @@ -0,0 +1,228 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { defaultConfig } from "acp-kernel"; +import { startServer, BILI_HOP_HEADER } from "../src/server.ts"; +import { BILI_TUNNEL_HEADER } from "../src/tunnel-guard.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { finalizeResponsesWsUpstreamHeaders } from "../src/responses-ws.ts"; +import { type FetchOptions } from "../src/fetch-util.ts"; + +/** #3905: bili's private protocol surface (the whole `x-bili-*` namespace) must + * never reach a real model endpoint. Hosts stamp `x-bili-plugin*` convention + * headers (marker / conversation id / window / model / the new declared-tools + * channel from #3906/#3907) that are consumed ONCE at this instance's ingress — + * forwarding them onward leaks host-private state to the provider. The main + * HTTP lane built its outbound headers by copying every inbound header except + * content-length/host, so the entire family rode through to the model endpoint. + * + * The two EXCEPTIONS are the bili→bili chain markers (`x-bili-hop`, + * `x-bili-tunnel`): their sole consumer is a DOWNSTREAM bili's ingress + * pass-through decision (#300/#1086), so they must survive transit even though + * they leak harmlessly onto a terminal provider (providers ignore unknown + * headers; hop carries only an opaque instance id, tunnel literally "1"). We + * cannot reliably tell "next hop is a bili" from "next hop is a real provider" + * at egress, so stripping them would risk re-opening the double-compression bug + * the marker exists to prevent. + * + * Pinned directions: + * - host path no longer emits any `x-bili-plugin*` (incl. the new exposed-tools); + * - the chain markers survive transit (inbound hop still present on egress); + * - WS lane presents the same surface (non-Azure keeps chain markers, Azure + * strips everything incl. markers for the documented custom-header cap). */ + +const MODEL = "gpt-test"; + +function close(server: http.Server): Promise { + return new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); +} + +function makeOpts(upstream: string): ProxyOptions { + return { + port: 0, + host: "127.0.0.1", + upstream, + routes: { [upstream]: { models: { [MODEL]: { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + }; +} + +type Captured = { url: string; headers: http.IncomingHttpHeaders }; + +function makeUpstream(captured: Captured[]): http.Server { + return http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + captured.push({ url: req.url ?? "", headers: req.headers }); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "chatcmpl-test", + object: "chat.completion", + model: MODEL, + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], + usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 }, + })); + }); + }); +} + +const OPENAI_BODY = JSON.stringify({ + model: MODEL, + stream: false, + messages: [ + { role: "system", content: "You are a test assistant." }, + { role: "user", content: "hello world" }, + ], +}); + +async function withRig(fn: (port: number, captured: Captured[]) => Promise): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const captured: Captured[] = []; + const upstream = makeUpstream(captured); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const llmUrl = `http://127.0.0.1:${(upstream.address() as { port: number }).port}`; + const srv = await startServer(makeOpts(llmUrl)); + await once(srv, "listening"); + try { + await fn((srv.address() as { port: number }).port, captured); + } finally { + srv.closeAllConnections?.(); + await close(srv); + upstream.closeAllConnections?.(); + await close(upstream); + } +} + +test("#3905: proxy lane — stray x-bili-plugin* metadata never reaches the model endpoint", async () => { + await withRig(async (port, captured) => { + const resp = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, { + method: "POST", + headers: { + "content-type": "application/json", + "x-acp-session": "issue3905-proxy-stray", + // No x-bili-plugin marker → stays a plain proxy-lane request. + "x-bili-plugin-conversation": "conv-abc123", + "x-bili-plugin-model": MODEL, + "x-bili-plugin-context-window": "400000", + "x-bili-plugin-exposed-tools": "compress,decompress,acp_status", + // End-to-end header the client genuinely wants upstream. + "x-client-keep": "yes", + }, + body: OPENAI_BODY, + }); + assert.equal(resp.status, 200); + await resp.text(); + assert.equal(captured.length, 1, "the model endpoint was contacted"); + const h = captured[0]!.headers; + assert.equal(h["x-bili-plugin-conversation"], undefined, "host conversation id must not leak"); + assert.equal(h["x-bili-plugin-model"], undefined, "host model declaration must not leak"); + assert.equal(h["x-bili-plugin-context-window"], undefined, "host window declaration must not leak"); + assert.equal(h["x-bili-plugin-exposed-tools"], undefined, "#3906 declared-tools channel must not leak upstream"); + assert.equal(h["x-client-keep"], "yes", "client end-to-end headers are unaffected"); + }); +}); + +test("#3905: plugin lane — the full x-bili-plugin* family a host stamps is stripped at egress", async () => { + await withRig(async (port, captured) => { + const resp = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, { + method: "POST", + headers: { + "content-type": "application/json", + "x-bili-plugin": "dsh", + "x-bili-plugin-conversation": "sess-host-1", + "x-bili-plugin-model": MODEL, + "x-bili-plugin-max-output": "8192", + "x-bili-plugin-exposed-tools": "compress", + }, + body: OPENAI_BODY, + }); + assert.equal(resp.status, 200); + await resp.text(); + assert.equal(captured.length, 1, "plugin-lane model call still reaches the model endpoint"); + const h = captured[0]!.headers; + for (const k of Object.keys(h)) { + assert.ok(!k.toLowerCase().startsWith("x-bili-plugin"), `host-private ${k} leaked to the model endpoint`); + } + }); +}); + +test("#3905: the strip preserves the bili→bili chain marker (inbound x-bili-hop survives egress)", async () => { + await withRig(async (port, captured) => { + // A request already carrying x-bili-hop means an upstream bili processed + // it — this instance passes through. The marker MUST still be on the + // outbound bytes so a further downstream bili keeps detecting the chain. + const resp = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, { + method: "POST", + headers: { + "content-type": "application/json", + "x-acp-session": "issue3905-chain-hop", + [BILI_HOP_HEADER]: "upstream-bili-instance", + "x-bili-plugin-conversation": "conv-should-still-be-stripped", + }, + body: OPENAI_BODY, + }); + assert.equal(resp.status, 200); + await resp.text(); + assert.equal(captured.length, 1); + const h = captured[0]!.headers; + assert.equal(h[BILI_HOP_HEADER], "upstream-bili-instance", "chain marker survives transit (#300/#1086)"); + assert.equal(h["x-bili-plugin-conversation"], undefined, "...while host-private metadata is still stripped"); + }); +}); + +test("#3905: WS egress — non-Azure strips x-bili-plugin* but keeps the chain markers", async () => { + const raw: FetchOptions["headers"] = { + "content-type": "application/json", + authorization: "Bearer secret-token", + "x-bili-plugin": "opencode", + "x-bili-plugin-conversation": "ses_abc", + "x-bili-plugin-exposed-tools": "compress,decompress", + [BILI_HOP_HEADER]: "instance-xyz", + [BILI_TUNNEL_HEADER]: "1", + }; + const out = finalizeResponsesWsUpstreamHeaders("wss://api.openai.com/v1/responses", raw); + assert.equal(out["authorization"], "Bearer secret-token", "auth stays intact"); + assert.equal(out["x-bili-plugin"], undefined, "host marker stripped"); + assert.equal(out["x-bili-plugin-conversation"], undefined, "host conversation stripped"); + assert.equal(out["x-bili-plugin-exposed-tools"], undefined, "declared-tools channel stripped"); + assert.equal(out[BILI_HOP_HEADER], "instance-xyz", "chain hop marker kept for transit"); + assert.equal(out[BILI_TUNNEL_HEADER], "1", "tunnel marker kept for transit"); +}); + +test("#3905: WS egress — Azure keeps stripping EVERY x-bili-* (custom-header cap, #2359)", async () => { + const raw: FetchOptions["headers"] = { + "content-type": "application/json", + authorization: "Bearer secret-token", + "x-bili-plugin": "opencode", + "x-bili-plugin-conversation": "ses_abc", + [BILI_HOP_HEADER]: "instance-xyz", + [BILI_TUNNEL_HEADER]: "1", + }; + const out = finalizeResponsesWsUpstreamHeaders("wss://myres.openai.azure.com/v1/responses", raw); + assert.equal(out["authorization"], "Bearer secret-token", "auth stays intact"); + for (const k of Object.keys(out)) { + assert.ok(!k.toLowerCase().startsWith("x-bili-"), `azure caps custom WS headers — ${k} must be stripped`); + } +}); From 9c17df1f4a7d82e4cc2b78bc2f02d02f9cad9a39 Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 15:57:22 +0800 Subject: [PATCH 2/2] test: align three stale egress pins with the #3905 strip (#3915) #3915 intentionally stops forwarding x-bili-plugin* to model endpoints, but three pre-existing tests still pinned the old leak behavior and turned CI red: - responses-websocket.test.ts "egress headers": non-Azure egress asserted the x-bili-plugin* family arrives verbatim -> now asserts it is stripped (Azure's full x-bili-* strip #2359 unchanged); test name updated to match. - responses-websocket.test.ts "incremental tool continuation": the #2359 control line asserted x-bili-plugin reaches a non-Azure mock -> now asserts it does not. - plugin-bypass.test.ts "forwarded verbatim": asserted the bypass marker header reaches upstream; the marker is host-private convention metadata and is stripped at egress like the rest of the family (the body stays verbatim - tools/messages assertions unchanged). Targeted local: these files + issue3905 + hop-by-hop + chain suites green, typecheck clean; full-suite verdict = remote CI. --- tests/plugin-bypass.test.ts | 4 +++- tests/responses-websocket.test.ts | 13 ++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/plugin-bypass.test.ts b/tests/plugin-bypass.test.ts index 00d4680661..1a5a71622e 100644 --- a/tests/plugin-bypass.test.ts +++ b/tests/plugin-bypass.test.ts @@ -103,7 +103,9 @@ describe("plugin bypass header (#920)", () => { const seen = JSON.parse(h!.captured[0]!.body) as { tools?: unknown[]; messages: unknown[] }; assert.equal(seen.tools, undefined); assert.deepEqual(seen.messages, body.messages); - assert.equal(h!.captured[0]!.headers["x-bili-plugin-bypass"], "1"); + // #3905: the bypass MARKER itself is host-private convention metadata — stripped at + // egress like the rest of the x-bili-plugin* family; the BODY above stays verbatim. + assert.equal(h!.captured[0]!.headers["x-bili-plugin-bypass"], undefined); }); it("without the header the same request gets the plugin pipeline (tools injected)", { timeout: 60_000 }, async () => { diff --git a/tests/responses-websocket.test.ts b/tests/responses-websocket.test.ts index 07a3f218e9..d9003da005 100644 --- a/tests/responses-websocket.test.ts +++ b/tests/responses-websocket.test.ts @@ -67,7 +67,7 @@ test("V2 handshake: routes OAuth/API Responses sockets and stamps native identit } finally { cleanup(); } }); -test("Responses WS egress headers: azure strips consumed x-bili-* under the custom-header cap; other hosts unchanged", () => { +test("Responses WS egress headers: azure strips every x-bili-* under the custom-header cap; other hosts strip the plugin family (#2359/#3905)", () => { const raw = { host: "westus2.openai.azure.com", connection: "Upgrade", @@ -88,12 +88,11 @@ test("Responses WS egress headers: azure strips consumed x-bili-* under the cust assert.equal(azure["x-openai-organization"], "org-test"); assert.equal(azure["content-type"], "application/json"); for (const dropped of ["host", "connection", "content-length", "sec-websocket-key", "sec-websocket-version", "x-bili-plugin", "x-bili-plugin-conversation", "x-bili-plugin-model", "x-bili-plugin-context-window"]) assert.ok(!(dropped in azure), `azure egress must not carry ${dropped}`); + // #3905: the host-private x-bili-plugin* family is stripped at egress on EVERY host + // (previously forwarded verbatim outside azure); bili→bili protocol markers are unaffected. const openai = finalizeResponsesWsUpstreamHeaders("https://api.openai.com/v1/responses", raw); - assert.equal(openai["x-bili-plugin"], "opencode"); - assert.equal(openai["x-bili-plugin-conversation"], "ses_az"); - assert.equal(openai["x-bili-plugin-model"], "gpt-5.2"); assert.equal(openai["api-key"], "sk-azure-test-credential"); - for (const dropped of ["host", "connection", "content-length", "sec-websocket-key"]) assert.ok(!(dropped in openai), `non-azure egress must still drop hop marker ${dropped}`); + for (const dropped of ["host", "connection", "content-length", "sec-websocket-key", "sec-websocket-version", "x-bili-plugin", "x-bili-plugin-conversation", "x-bili-plugin-model", "x-bili-plugin-context-window"]) assert.ok(!(dropped in openai), `non-azure egress must not carry ${dropped}`); }); // #3073: the codex remote_compaction_v2 beta feature is stripped on EVERY @@ -290,8 +289,8 @@ test("Responses WS: both ends use sockets; incremental tool continuation reaches assert.equal((f.rows[1].request.input as Item[]).length, 2); assert.ok(JSON.stringify(f.rows[1].full).includes("first-round-sentinel")); assert.equal(f.rows[0].headers.authorization, "Bearer fake-credential"); - // #2359 control: a non-azure upstream still receives the consumed x-bili-* metadata verbatim — only *.openai.azure.com egress strips it. - assert.equal(f.rows[0].headers["x-bili-plugin"], "opencode"); + // #3905: the host-private x-bili-plugin* marker is stripped at egress on every host (was verbatim pre-#3905; see tests/issue3905-egress-bili-strip.test.ts). + assert.equal(f.rows[0].headers["x-bili-plugin"], undefined); assert.equal(second.status, "completed"); const session = peekSession(f.sid); assert.ok(session);