diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index fdff375e06..1fc45c89a6 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -333,13 +333,15 @@ export async function prepareOpenai( // through: upstreams that ignore it lose nothing, upstreams that use it // get a per-conversation routing hint. delete (rebuilt as Record).prompt_cache_retention; - // #2218: body-only relay affinity for plugin hosts. dsh desktop's only - // identity signal is the bili-internal x-bili-plugin-conversation header - // (already forwarded as x-session-id by buildForwardTarget); body-keyed - // relays (workbuddy panel) can't read headers, so stamp the conversation - // id into prompt_cache_key too. Gated to admin-rewritten or loopback - // destinations — strict direct APIs can 400 unknown body params (#1403). - if (shouldStampRelayAffinityPck(conversationHeaderSource(req.headers)?.name, parsed.prompt_cache_key, billingUpstream, upstreamOrigin)) { + // #2218/#2645: body-only relay affinity for plugin hosts. dsh desktop's + // only identity signal is the bili-internal x-bili-plugin-conversation + // header (already forwarded as x-session-id by buildForwardTarget); + // body-keyed relays (workbuddy panel) can't read headers, so stamp the + // conversation id into prompt_cache_key too. Gated to LOOPBACK + // destinations only — strict direct APIs can 400 unknown body params + // (#1403), and the /bili/ tunnel sets a route for the user's own REMOTE + // destination too (#2645), so "a URL was embedded" must NOT imply stamping. + if (shouldStampRelayAffinityPck(conversationHeaderSource(req.headers)?.name, parsed.prompt_cache_key, upstreamOrigin)) { rebuilt.prompt_cache_key = session.id; } // OpenAI Chat Completions only emits a usage object in the final stream diff --git a/src/session-id.ts b/src/session-id.ts index bc7799b444..c04ddccbef 100644 --- a/src/session-id.ts +++ b/src/session-id.ts @@ -214,20 +214,25 @@ export function affinityToken(identity: ConversationIdentity): string | undefine * - the plugin lane owns the identity (the winning conversation header is * the bili-internal one, not a header the upstream could read itself); * - the client sent no prompt_cache_key of its own (client value wins); - * - the destination is admin-configured (provider-rewritten route) or - * loopback — deliberate relay/local deployments. Strict direct APIs can - * 400 unknown body params (#1403), so unrouted non-loopback forwards - * stay unstamped. + * - the DESTINATION is loopback — a deliberate local relay deployment. + * Strict direct APIs can 400 unknown body params (#1403), so any remote + * forward stays unstamped. + * + * #2645 correction: this gate used to short-circuit to `true` whenever a URL + * had been embedded/rewritten (`route.rewrittenUrl`). The /bili/ tunnel and + * forward-proxy lanes set rewrittenUrl for the user's OWN remote destination + * too, so that leg stamped strict remote APIs (NVIDIA NIM answers 400 on + * `prompt_cache_key`) and broke every turn. rewrittenUrl always co-references + * route.upstream, so the destination origin is the only trustworthy signal — + * gate on IT being loopback, never on whether a URL was embedded. */ export function shouldStampRelayAffinityPck( identityHeaderName: string | undefined, clientPck: unknown, - routeRewrittenUrl: string | undefined, upstreamOrigin: string, ): boolean { if (identityHeaderName !== "x-bili-plugin-conversation") return false; if (clientPck !== undefined) return false; - if (routeRewrittenUrl !== undefined) return true; try { const hostname = new URL(upstreamOrigin).hostname; // "localhost" is loopback but not matched by isLoopbackAddress (127.x/::1 diff --git a/tests/issue2218-relay-affinity-signal.test.ts b/tests/issue2218-relay-affinity-signal.test.ts index 3acd707aef..8b53fe4ed8 100644 --- a/tests/issue2218-relay-affinity-signal.test.ts +++ b/tests/issue2218-relay-affinity-signal.test.ts @@ -1,6 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; +import os from "node:os"; import { once } from "node:events"; import { startServer } from "../src/server.ts"; import { defaultConfig } from "acp-kernel"; @@ -98,23 +99,26 @@ function chatBody(extra: Record = {}): Record }; } -test("#2218 unit: shouldStampRelayAffinityPck gate matrix", () => { +test("#2218/#2645 unit: shouldStampRelayAffinityPck gate matrix", () => { const loop = "http://127.0.0.1:7864/v1"; - const remote = "https://relay.example.com/v1"; - // plugin lane + no client pck + admin-rewritten route → stamp - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, loop, remote), true); - // plugin lane + no client pck + loopback destination without a route → stamp - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, undefined, loop), true); - // plugin lane + no client pck + unrouted non-loopback (strict direct API risk, #1403) → no stamp - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, undefined, remote), false); + const localhost = "http://localhost:7864/v1"; + const nvidia = "https://integrate.api.nvidia.com/v1/chat/completions"; + // plugin lane + no client pck + loopback destination → stamp + assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, loop), true); + // literal "localhost" counts as loopback (#2218) + assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, localhost), true); + // plugin lane + no client pck + REMOTE destination (strict direct API, NVIDIA NIM #2645) → no stamp + assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, nvidia), false); + // the tunnel / forward-proxy lane sets a full destination URL (path included) for the + // user's OWN remote host too — that must NOT flip the gate to stamp (#2645) + assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, "https://relay.example.com/v1"), false); // client's own prompt_cache_key always wins - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", "pck-own", loop, loop), false); + assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", "pck-own", loop), false); // non-plugin identity sources (codex session-id header, plain clients) stay unstamped - assert.equal(shouldStampRelayAffinityPck("session-id", undefined, loop, loop), false); - assert.equal(shouldStampRelayAffinityPck(undefined, undefined, loop, loop), false); - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, undefined, "http://localhost:7864/v1"), true); + assert.equal(shouldStampRelayAffinityPck("session-id", undefined, loop), false); + assert.equal(shouldStampRelayAffinityPck(undefined, undefined, loop), false); // unparseable origin is not a deliberate deployment → no stamp - assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, undefined, "not-a-url"), false); + assert.equal(shouldStampRelayAffinityPck("x-bili-plugin-conversation", undefined, "not-a-url"), false); }); test("#2218 regression: plain client body pck still forwards x-session-id + body pck", async () => { @@ -215,3 +219,99 @@ test("#2218 regression: readable client conversation headers still suppress the await h.close(); } }); + +// #2645 regression: a dsh plugin-lane request tunneled to a REMOTE (non-loopback) +// destination must NOT carry a stamped prompt_cache_key — strict-schema upstreams +// (NVIDIA NIM) answer 400 on the unknown field. The mock below enforces exactly +// that schema, so the pre-fix behaviour reproduces as a real 400 rather than a +// mere missing-field check. +function nonLoopbackIPv4(): string | undefined { + const ifaces = os.networkInterfaces(); + for (const name of Object.keys(ifaces)) { + for (const i of ifaces[name] ?? []) { + if (i.family === "IPv4" && !i.internal) return i.address; + } + } + return undefined; +} + +test("#2645 regression: dsh plugin lane to a strict REMOTE upstream is not stamped (no 400)", async (t) => { + const destIp = nonLoopbackIPv4(); + if (!destIp) { t.skip("no non-loopback IPv4 interface available in this environment"); return; } + + const captured: Captured[] = []; + // Strict upstream: rejects the unknown param, mirroring NVIDIA NIM's + // `Validation: Unsupported parameter(s): prompt_cache_key` 400. + const strictUpstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + let body: Record = {}; + try { body = JSON.parse(Buffer.concat(chunks).toString("utf8")); } catch { /* keep {} */ } + captured.push({ headers: req.headers, body }); + if ("prompt_cache_key" in body) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { message: "Validation: Unsupported parameter(s): `prompt_cache_key`", type: "Bad Request", code: 400 } })); + return; + } + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + res.end(chatSse()); + }); + }); + await new Promise((resolve) => strictUpstream.listen(0, "0.0.0.0", resolve)); + const upstreamPort = (strictUpstream.address() as { port: number }).port; + + _resetSessionsForTest(); + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy: Server = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: {}, + modelContextLimit: 100_000, + kernelConfig: defaultConfig(100_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + log: false, + sessionHeader: "x-acp-session", + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + }); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + + try { + // Tunnel to the machine's NON-loopback address: bili sees a remote + // destination (isLoopbackAddress=false) even though the socket lands on + // this same host — exactly the NVIDIA NIM shape from the issue. + const base = `http://127.0.0.1:${proxyPort}/bili/http://${destIp}:${upstreamPort}/v1/chat/completions`; + const r = await fetch(base, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "dsh-nvidia-1" }, + body: JSON.stringify(chatBody()), + }); + // Before the fix bili stamped prompt_cache_key → the strict upstream 400'd. + assert.equal(r.status, 200); + await r.text(); + const [b] = captured; + assert.ok(b, "request reached the strict upstream"); + assert.ok(!("prompt_cache_key" in b.body), "prompt_cache_key must NOT be stamped onto a remote destination"); + // header relay still works (workbuddy hub / sub2api read x-session-id) + assert.equal(b.headers["x-session-id"], "dsh-nvidia-1"); + } finally { + proxy.close(); + await once(proxy, "close"); + strictUpstream.close(); + await once(strictUpstream, "close"); + } +});