Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/responses-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 5 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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).
Expand Down
19 changes: 19 additions & 0 deletions src/server/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>): 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<string> {
Expand Down
228 changes: 228 additions & 0 deletions tests/issue3905-egress-bili-strip.test.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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<void>): Promise<void> {
_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`);
}
});
4 changes: 3 additions & 1 deletion tests/plugin-bypass.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
13 changes: 6 additions & 7 deletions tests/responses-websocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Loading