From db643eb047a35cdcdb8810f35388ee43c004a1cf Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 8 Oct 2026 01:18:01 +0800 Subject: [PATCH] feat: #2359 intercept azure Responses WebSocket lanes; strip consumed x-bili-* at azure egress OpenCode V2's experimental.ws.handshake hook only admitted providerID "openai", so an azure providerID socket was never URL-rewritten/header-stamped and bypassed bili entirely (no compression, stale /acp carriage). Two coupled changes: - Admit "azure" alongside "openai" in the guard (src/agent/opencode-v2.ts), keeping the existing ws:// scheme + /responses pathname checks. Any other provider stays unintercepted. - Strip bili-internal x-bili-* headers at final upstream egress ONLY to hosts ending in .openai.azure.com (src/responses-ws.ts). Azure OpenAI caps custom request headers on the WS upgrade (documented 10); the forwarded x-bili-* metadata (already consumed at ingress) plus OpenCode's own x-* headers push a typical handshake past it, so the gateway closes with 1011 before any event. Auth + OpenCode headers stay intact; every other host keeps its existing forwarding byte-for-byte. Extracted into exported finalizeResponsesWsUpstreamHeaders so the exact wire decision is asserted directly rather than via a permissive round-trip mock. Tests: pure unit test of the egress decision (azure strips x-bili-*, non-azure retains them, hop markers always dropped); guard test proving azure is intercepted while an uncovered provider is left untouched; control assertion in the existing full-flow test that a non-azure upstream still receives x-bili-*. Closes #2359 --- src/agent/opencode-v2.ts | 6 +++- src/responses-ws.ts | 29 +++++++++++++++-- tests/responses-websocket.test.ts | 54 ++++++++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/src/agent/opencode-v2.ts b/src/agent/opencode-v2.ts index 7da14250b7..d8b89182e2 100644 --- a/src/agent/opencode-v2.ts +++ b/src/agent/opencode-v2.ts @@ -337,7 +337,11 @@ export function createOpencodeV2Setup(options: OpencodeV2SetupOptions = {}): (ct try { const wsReg = await ctx.session?.hook?.("experimental.ws.handshake", async (e) => { - if (pluginDisabled() || e.model?.providerID !== "openai" || typeof e.url !== "string" || !/^wss?:\/\//.test(e.url)) return; + // #2359: azure speaks the same OpenAI Responses WebSocket vocabulary; + // the egress filter in responses-ws keeps its handshake under Azure's + // custom-header cap. Any other provider stays unintercepted. + const providerID = e.model?.providerID; + if (pluginDisabled() || (providerID !== "openai" && providerID !== "azure") || typeof e.url !== "string" || !/^wss?:\/\//.test(e.url)) return; const httpUrl = e.url.replace(/^ws/, "http"); if (!new URL(httpUrl).pathname.endsWith("/responses")) return; const requestEvent: V2HttpRequestEvent = { sessionID: e.sessionID, agent: e.agent, model: e.model, request: new Request(httpUrl, { headers: e.headers }) }; diff --git a/src/responses-ws.ts b/src/responses-ws.ts index c4cd76602f..6f36627a38 100644 --- a/src/responses-ws.ts +++ b/src/responses-ws.ts @@ -24,6 +24,32 @@ function describeEventType(frame: unknown): string { return typeof frame.type === "string" ? frame.type : ""; } +// #2359: Azure OpenAI caps custom request headers on the WS upgrade (documented +// 10). Bili's forwarded x-bili-* metadata (already consumed at ingress) plus +// OpenCode's own x-* headers push a typical handshake past it, so the gateway +// closes with 1011 before any event. Strip bili-internal headers at final egress +// to *.openai.azure.com ONLY — auth + OpenCode headers stay intact, and every +// other host keeps its existing forwarding/chaining behavior byte-for-byte. +function isAzureOpenAiHost(url: string): boolean { + try { + return new URL(url).hostname.toLowerCase().endsWith(".openai.azure.com"); + } catch { + return false; + } +} + +/** Final wire header set for an upstream Responses WebSocket upgrade: drop + * hop-by-hop + sec-websocket- markers, then (azure only) the consumed x-bili-* + * metadata. Exported so the exact egress decision — including the #2359 azure + * custom-header-cap strip — is asserted directly rather than via a permissive + * round-trip mock. */ +export function finalizeResponsesWsUpstreamHeaders(url: string, rawHeaders: FetchOptions["headers"]): Record { + 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]; + if (isAzureOpenAiHost(url)) for (const key of Object.keys(headers)) if (key.startsWith("x-bili-")) delete headers[key]; + return headers; +} + // Transport options the WebSocket lane cannot honor. The lane forces stream:true // and owns streaming itself, so HTTP-side transport controls are refused — but // only the ones genuinely incompatible with a single WS exchange. stream_options @@ -168,8 +194,7 @@ export class ResponsesWsUpstream { } private async connect(url: string, options: FetchOptions): Promise> { - const headers = Object.fromEntries(new Headers(options.headers).entries()); - for (const key of Object.keys(headers)) if (UPSTREAM_HOP_HEADERS.has(key) || key.startsWith("sec-websocket-")) delete headers[key]; + const headers = finalizeResponsesWsUpstreamHeaders(url, options.headers); const wsUrl = url.replace(/^http/, "ws"); const key = `${wsUrl}:${canonical(headers)}`; if (this.key === key && this.socket?.readyState === UpstreamWebSocket.OPEN) return this.socket; diff --git a/tests/responses-websocket.test.ts b/tests/responses-websocket.test.ts index e60c72c965..f7c42df410 100644 --- a/tests/responses-websocket.test.ts +++ b/tests/responses-websocket.test.ts @@ -12,7 +12,7 @@ import WebSocket, { WebSocketServer } from "ws"; import { Agent } from "undici"; import { defaultConfig } from "acp-kernel"; import { startServer } from "../src/server.ts"; -import { ResponsesWsHistory, ResponsesWsUpstream } from "../src/responses-ws.ts"; +import { ResponsesWsHistory, ResponsesWsUpstream, finalizeResponsesWsUpstreamHeaders } from "../src/responses-ws.ts"; import { _liveUpstreamTimersForTest } from "../src/fetch-util.ts"; import { proxyDispatcher } from "../src/upstream-proxy.ts"; import { ensureRootCA, mintHostCert, rootCaPath } from "../src/ca.ts"; @@ -67,6 +67,56 @@ 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", () => { + const raw = { + host: "westus2.openai.azure.com", + connection: "Upgrade", + "content-length": "128", + "content-type": "application/json", + "sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==", + "sec-websocket-version": "13", + "api-key": "sk-azure-test-credential", + "x-openai-organization": "org-test", + "x-stainless-runtime": "node", + "x-bili-plugin": "opencode", + "x-bili-plugin-conversation": "ses_az", + "x-bili-plugin-model": "gpt-5.2", + "x-bili-plugin-context-window": "200000", + }; + const azure = finalizeResponsesWsUpstreamHeaders("https://westus2.openai.azure.com/openai/v1/responses?api-version=2024-08-01-preview", raw); + assert.equal(azure["api-key"], "sk-azure-test-credential"); + 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}`); + 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}`); +}); + +test("V2 handshake: azure Responses sockets are intercepted; uncovered providers stay unintercepted", async () => { + const hooks = new Map void | Promise>(); + const origin = "http://127.0.0.1:8787"; + const cleanup = await createOpencodeV2Setup({ route: createNativeRoute({ origin, ready: Promise.resolve(origin) }, { probe: async () => true }) })({ + session: { hook: async (name, cb) => { hooks.set(name, cb); return {}; } }, + }); + try { + const azureUrl = "wss://myres.openai.azure.com/openai/v1/responses?api-version=2024-08-01-preview"; + const azureEvent: V2HttpRequestEvent = { url: azureUrl, headers: { "api-key": "sk-azure-test" }, sessionID: "ses_az", agent: "build", model: { providerID: "azure", id: "gpt-5.2" } }; + await hooks.get("experimental.ws.handshake")!(azureEvent); + assert.equal(azureEvent.url, `ws://127.0.0.1:8787/bili/responses/${azureUrl.replace(/^ws/, "http")}`); + assert.equal(azureEvent.headers?.["x-bili-plugin"], "opencode"); + assert.equal(azureEvent.headers?.["x-bili-plugin-conversation"], "ses_az"); + const otherUrl = "wss://api.anthropic.com/v1/messages"; + const otherEvent: V2HttpRequestEvent = { url: otherUrl, headers: {}, sessionID: "ses_other", agent: "build", model: { providerID: "anthropic", id: "claude-x" } }; + await hooks.get("experimental.ws.handshake")!(otherEvent); + assert.equal(otherEvent.url, otherUrl); + assert.ok(otherEvent.headers?.["x-bili-plugin"] === undefined); + } finally { cleanup(); } +}); + async function fixture(terminalOutput: "full" | "empty" | "omitted" | "partial" | "suffix" = "full") { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "bili-responses-ws-")); _setStoreForTest(new SessionStore({ enabled: false })); @@ -216,6 +266,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"); assert.equal(second.status, "completed"); const session = peekSession(f.sid); assert.ok(session);