diff --git a/PLUGIN.md b/PLUGIN.md index da29a87622..2fadda12d1 100644 --- a/PLUGIN.md +++ b/PLUGIN.md @@ -202,9 +202,9 @@ Lineage-only declaration (#2408): a host that cannot drive the full fork protoco Incremental turns (#3248; not yet advertised in the plugin manifest — detect by trying the endpoint and handling the 503): a host that keeps its own full local history may stop resending it. `POST /__bili/plugin/turn` (same `/__bili/` gate; headers `x-bili-plugin` + optional `x-bili-plugin-model`, `x-bili-plugin-context-window`, `x-bili-plugin-max-output`) with `{ conversationId, sinceSeq, resync?, system?, path?, request, messages }`. The first turn (or any recovery) sends `sinceSeq: 0` + the full `messages[]` (`resync: true` is implied on seed — a wholesale replacement never 409s and works whether the canonical is empty or already live); every later turn sends ONLY what the client added since the last ack — including the previous assistant reply (client-owned). `request` carries anthropic params (model, max_tokens, temperature, top_p, top_k, stop_sequences, stream, tools, tool_choice, metadata, thinking, service_tier, parallel_tool_calls); `system` is sticky (omit to keep the last one); `path` defaults to `/v1/messages` (bare relative rides the lane's default upstream; the `/bili//…` form is explicit). The response is the upstream body streamed through, with `x-bili-applied-seq` = the canonical length after applying the delta — echo that value as the next turn's `sinceSeq`. `409 { error: "seq-mismatch" | "turn-in-flight", serverSeq }` means the client MUST recover with a resync turn (`sinceSeq: serverSeq` if it still has the history, else `resync: true` full seed). `503` means the lane runs without `storage.unified` (the canonical log lives in the unified messages table). The lane owns no session identity of its own: the assembled request carries the client's own `x-bili-plugin-conversation`, so incremental and normal full-resend traffic for one conversation share one session, one fold ledger and one canonical — switching modes needs no ceremony, and a conversation may interleave them (e.g. fall back to full resend while a resync is pending). Folding, nudges, tools and the plugin tool endpoints all work unchanged on the assembled body; the model never sees the difference. v1 caveats: the canonical log is the session's messages table — a turn is acked after the assembled body is persisted (best-effort immediate flush; a crash in between surfaces as 409 → resync), and the pipeline still walks the assembled prefix (O(full) CPU, cheap and zero-hit — the server-authored fast path is tracked on #3248). A `resync: true` that replaces a diverged canonical records a base-seq epoch (`session_ext` key `inc:base-seq`): the stale branch's rows stay in the append-only log for audit, but cold rebuilds slice past them — one resync heals, restarts included. On legacy-only storage (no ext surface) the marker degrades and cold rebuilds serve the full row union (409 → resync once, as before). -Marker divert (#3248 phase 2 — native extensions): instead of switching the provider URL to the turn endpoint, a host may send the incremental body on its ORDINARY model request. Stamp `x-bili-plugin-turn: 1` on the request (the lane requires `x-bili-plugin` too, as for every plugin signal); the body shape — top-level `conversationId` + `sinceSeq` + `messages`, no top-level `model` — is the final word: a marker'd body that is NOT incremental-shaped rides the normal pipeline byte-identically, so a host may stamp the marker whenever the feature is enabled and only convert turns it wants. The lane forwards the assembled request to the ORIGINAL request path (bare or `/bili/…` — no provider reconfiguration). Capability probe: `GET /__bili/plugin/turn` → `{ ok, protocol: 1, feature: "pluginIncrementalTurns", enabled, storage }` (loopback-gated like every `/__bili/` surface; a 404 = older server, stay full-resend). Gating: rollout feature `pluginIncrementalTurns` (default 0% = every install byte-identical to before) — enable with `rollout.features.pluginIncrementalTurns: true` (or a percent) in the bili config, or `BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS=1`; the feature additionally requires a live unified sidecar (shadow or full). When disabled mid-flight the lane answers 503 on incremental-shaped turns — fall back to a plain full-resend request. Reference host wiring: pi rides its `before_provider_headers` (marker) + `before_provider_request` (payload replacement) + `after_provider_response` (ack) extension events; dsh-native rides the fetch patch's `beforeSend` (body conversion) + `headersFor` (marker) + `onResponse` (ack). pi-native inherits the pi wiring unchanged (same extension factory — only proxy discovery differs). opencode rides its `http.request` hook (an `incremental` adapter callback appended after routing/stamping swaps the request body via `replaceRequestPayload` and stamps the marker on the replaced request; acks land on the fetch intercept's `onResponse`). omp has no header events at all — its native adapter reads the extension-stamped `prompt_cache_key` body field as the conversation id inside `beforeSend`, stamps the plugin headers directly on the request init (identity upgrades, never forks: wire mode already resolves the same value via `preferPromptCacheKeyIdentity`), and acks on `onResponse`. Non-Anthropic wire shapes (OpenAI `tool_calls`/`role:"tool"`) are detected by `wireMessagesLookAnthropic` and left on full resend — the lane's message ingestion is the anthropic wire. Planned turns are single-in-flight per URL and age out after 30s (a network-dead request must not block the URL forever). The shared client state machine lives in `src/agent/incremental-client.ts` (probe TTL cache, prefix-hash diffing, sticky system, per-conversation dead flag) — new native hosts should reuse it rather than re-implement the diffing. +Marker divert (#3248 phase 2 — native extensions): instead of switching the provider URL to the turn endpoint, a host may send the incremental body on its ORDINARY model request. Stamp `x-bili-plugin-turn: 1` on the request (the lane requires `x-bili-plugin` too, as for every plugin signal); the body shape — top-level `conversationId` + `sinceSeq` + `messages`, no top-level `model` — is the final word: a marker'd body that is NOT incremental-shaped rides the normal pipeline byte-identically, so a host may stamp the marker whenever the feature is enabled and only convert turns it wants. Incremental-SHAPED turns are lane traffic and carry the same trust class as the direct turn endpoint (#3343): non-loopback / tunnel / untrusted-origin callers get 403 on them (their capability probe already 403s, so a well-behaved host never sends deltas from afar), and management prefixes (`/__bili/`, `/__acp/`) never ride the divert. The lane forwards the assembled request to the ORIGINAL request path (bare or `/bili/…` — no provider reconfiguration). Capability probe: `GET /__bili/plugin/turn` → `{ ok, protocol: 1, feature: "pluginIncrementalTurns", enabled, storage }` (loopback-gated like every `/__bili/` surface; a 404 = older server, stay full-resend). Gating: rollout feature `pluginIncrementalTurns` (default 0% = every install byte-identical to before) — enable with `rollout.features.pluginIncrementalTurns: true` (or a percent) in the bili config, or `BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS=1`; the feature additionally requires a live unified sidecar (shadow or full). When disabled mid-flight the lane answers 503 on incremental-shaped turns — fall back to a plain full-resend request. Reference host wiring: pi rides its `before_provider_headers` (marker) + `before_provider_request` (payload replacement) + `after_provider_response` (ack) extension events; dsh-native rides the fetch patch's `beforeSend` (body conversion) + `headersFor` (marker) + `onResponse` (ack). pi-native inherits the pi wiring unchanged (same extension factory — only proxy discovery differs). opencode rides its `http.request` hook (an `incremental` adapter callback appended after routing/stamping swaps the request body via `replaceRequestPayload` and stamps the marker on the replaced request; acks land on the fetch intercept's `onResponse`). omp has no header events at all — its native adapter reads the extension-stamped `prompt_cache_key` body field as the conversation id inside `beforeSend`, stamps the plugin headers directly on the request init (identity upgrades, never forks: wire mode already resolves the same value via `preferPromptCacheKeyIdentity`), and acks on `onResponse`. Non-Anthropic wire shapes (OpenAI `tool_calls`/`role:"tool"`) are detected by `wireMessagesLookAnthropic` and left on full resend — the lane's message ingestion is the anthropic wire. Planned turns are single-in-flight per URL and age out after 30s (a network-dead request must not block the URL forever). The shared client state machine lives in `src/agent/incremental-client.ts` (probe TTL cache, prefix-hash diffing, sticky system, per-conversation dead flag) — new native hosts should reuse it rather than re-implement the diffing. -中文增量要点(#3248):`POST /__bili/plugin/turn`,首次/恢复用 `sinceSeq:0` 全量(种子隐含 `resync:true`,永不 409);此后每轮只发新增(含上一轮 assistant 回复,客户端自持)。响应即上游原文,`x-bili-applied-seq` 为应用后的长度,下一轮作 `sinceSeq` 回传。409 必须按 serverSeq 对齐或全量重同步;503 表示未开 storage.unified。会话身份沿用客户端自己的 conversation id——增量与全量重发同一会话可任意切换,折叠/工具/状态栏不变。阶段 2(标记分流):普通模型请求上盖 `x-bili-plugin-turn: 1` 头 + 增量形状 body(顶层 conversationId/sinceSeq/messages、无顶层 model)即可,lane 按原始 URL 转发;探针 `GET /__bili/plugin/turn`;开关 rollout `pluginIncrementalTurns`(默认 0%=零变化,`BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS=1` 或配置强制开)。客户端状态机在 src/agent/incremental-client.ts,新宿主复用勿重写。已接宿主:pi/pi-native(同一扩展工厂,后者零代码继承)、dsh-native(fetch 补丁 beforeSend/headersFor/onResponse)、opencode(http.request 钩子 incremental 适配回调换 body+盖章)、omp(无头事件通道,读 body 里 prompt_cache_key 当会话 id,直接在 init 上盖章);OpenAI 工具形状(tool_calls/role:tool)自动回退全量重发;每 URL 单飞行+30s 过期。 +中文增量要点(#3248):`POST /__bili/plugin/turn`,首次/恢复用 `sinceSeq:0` 全量(种子隐含 `resync:true`,永不 409);此后每轮只发新增(含上一轮 assistant 回复,客户端自持)。响应即上游原文,`x-bili-applied-seq` 为应用后的长度,下一轮作 `sinceSeq` 回传。409 必须按 serverSeq 对齐或全量重同步;503 表示未开 storage.unified。会话身份沿用客户端自己的 conversation id——增量与全量重发同一会话可任意切换,折叠/工具/状态栏不变。阶段 2(标记分流):普通模型请求上盖 `x-bili-plugin-turn: 1` 头 + 增量形状 body(顶层 conversationId/sinceSeq/messages、无顶层 model)即可,增量形状 turn 属 lane 流量,与直连 turn 端点同信任级(#3343):非回环/隧道/不可信源一律 403(其探针本就 403,正常宿主不会远程发增量);管理前缀(`/__bili/`、`/__acp/`)永不走分流。lane 按原始 URL 转发;探针 `GET /__bili/plugin/turn`;开关 rollout `pluginIncrementalTurns`(默认 0%=零变化,`BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS=1` 或配置强制开)。客户端状态机在 src/agent/incremental-client.ts,新宿主复用勿重写。已接宿主:pi/pi-native(同一扩展工厂,后者零代码继承)、dsh-native(fetch 补丁 beforeSend/headersFor/onResponse)、opencode(http.request 钩子 incremental 适配回调换 body+盖章)、omp(无头事件通道,读 body 里 prompt_cache_key 当会话 id,直接在 init 上盖章);OpenAI 工具形状(tool_calls/role:tool)自动回退全量重发;每 URL 单飞行+30s 过期。 中文集成要点:fork 协议现为 2(服务端同时接受 1);先读取 manifest 能力及 snapshot,按原文、role、工具身份确认匹配前缀,仅提交该前缀的 orderedMessages,不猜测多模态分支点。被 `compress.protectedTools` 硬排除的消息在两个数组中均为 `"ref": null`(参与顺序与身份、无地址 ref),与其他前缀条目一样逐字提交;ref 合法值仅 `mNNNNN` 字符串或 `null`。父 revision 变化必须重新取快照并重新匹配。子会话使用独立稳定 id,首次请求可追加尚未出站的模型回复,不能重置继承的 refs/blocks。手动摘要使用 snapshot 的 refs 和 `expectedRevision: snapshot.parentRevision` 调用公开 tool;摘要期间历史发生变化时返回 409,不执行旧摘要。状态栏读取 `contextTokens`、`contextTokensSource`、`contextTokensAt`、`model`、`contextLimit`、`contextGeneration`、`sessionId` 与 `sessionRevision`,不得用累计账单 input/cache 冒充有效上下文。手动 compress/decompress 后为独立时间及 generation 的 estimate,不改累计账单或 last-usage 基线;下一次正数 usage 才恢复 usage,零值或缺失 usage 不得复活旧观测。未知来源返回 unavailable,tokens/time/generation 为 null。 diff --git a/src/incremental-lane.ts b/src/incremental-lane.ts index 28e178e85a..018b6b659b 100644 --- a/src/incremental-lane.ts +++ b/src/incremental-lane.ts @@ -431,7 +431,12 @@ export async function handleIncrementalTurn( method: "POST", url: path, headers, - socket: { localPort: (req.socket as { localPort?: number } | undefined)?.localPort, remoteAddress: "127.0.0.1", destroy: (): void => { synthetic.destroy?.(); } }, + // #3343: the REAL caller's socket, never a forged loopback one — + // every downstream gate (admin plane, tunnel admission, provenance) + // must judge the actual peer. Loopback topology is unchanged (real + // == 127.0.0.1); a non-loopback peer degrades to ordinary model + // traffic and can no longer ride onto the management plane. + socket: req.socket, }); // Ack header: inject ahead of whatever status the pipeline writes. diff --git a/src/server/admin.ts b/src/server/admin.ts index 434b8982f0..610a21b982 100644 --- a/src/server/admin.ts +++ b/src/server/admin.ts @@ -130,6 +130,16 @@ function adminTrustedHostnames(bindHost: string): Set { return new Set(names); } +/** #3343: the management-plane prefix test — the two internal prefixes the + * loopback + trusted-origin + anti-tunnel gate protects (handleAdminRoute). + * Shared with the incremental-lane marker divert in handle.ts, which must + * never accept these paths: a diverted turn re-enters the pipeline carrying + * the ORIGINAL request URL, so an unfiltered divert hands a non-loopback + * caller a request onto the management prefixes. */ +export function isManagementPath(url: string | undefined): boolean { + return url !== undefined && (url === "/__bili/" || url.startsWith("/__bili/") || url === "/__acp/" || url.startsWith("/__acp/")); +} + // #2170 split-session canary: one log line per conversation base per process. const splitWarnedBases = new Set(); @@ -145,7 +155,7 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv // #1073/#3245: self-addressed absolute-form requests arrive already // rewritten to origin form by handle() (selfAddressedLocalPath), so the // gates below always judge a path, never a full URL. - const isAdminPath = req.url === "/__bili/" || req.url?.startsWith("/__bili/") || req.url === "/__acp/" || req.url?.startsWith("/__acp/"); + const isAdminPath = isManagementPath(req.url); // #409: management must never be reachable THROUGH the bili tunnel, not // even from a loopback client: the tunnel's inner connection originates // from the proxy itself, so the remoteAddress gate alone is satisfied and diff --git a/src/server/handle.ts b/src/server/handle.ts index fbe688ed38..abe7895ee2 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -60,7 +60,7 @@ import { prepareOpenai } from "./prepare-openai.js"; import { prepareGoogle, prepareGoogleCountTokens } from "./prepare-google.js"; import { prepareResponses, prepareResponsesCompact } from "./prepare-responses.js"; import { bodyDumpEnabled, isModelDiscoveryPath, isV1InternalControlPlanePath, logDumpFailure, logUnrecognizedPath } from "./observability.js"; -import { adminGateDenial, handleAdminRoute } from "./admin.js"; +import { adminGateDenial, handleAdminRoute, isManagementPath } from "./admin.js"; import { handleIncrementalTurn, handleIncrementalProbe, incrementalTurnBodyShape, incrementalTurnsEnabled, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_PATH } from "../incremental-lane.js"; import { ccrPluginWireOk, storeEffectiveCcr, type CcrSettings } from "../store.js"; import { DEFAULT_DECIDE_MAX_TOKENS } from "../nudge-decide.js"; @@ -133,16 +133,23 @@ export async function handle( // #3248 marker divert: a native extension may stamp // `x-bili-plugin-turn: 1` on an ORDINARY model request (any /bili/… // or bare path, provider baseURL untouched) to send only the delta. - // Trust class = the plugin marker header (x-bili-plugin must be - // present too, same as every plugin-lane signal). The marker only - // declares intent — the BODY SHAPE is the final word, because a - // marker'd host may still send full-resend turns (retries, dead - // conversations, side lanes); those must ride the normal pipeline. - // So: buffer once (the pipeline re-buffers every JSON body anyway), - // shape-test, then divert or replay. The synthetic re-entries above - // call handleRequest DIRECTLY (never this divert), so no loops. + // Trust class = the plugin marker headers (x-bili-plugin must be + // present too) AND the management-plane gate (adminGateDenial — same + // loopback + anti-tunnel + trusted-origin checks as the direct + // /__bili/plugin/turn entry): the diverted turn re-enters the + // pipeline carrying the ORIGINAL request URL and writes canonical + // session state under a caller-chosen conversationId, so a header + // alone could not bound who reaches applyTurn (#3343). Management + // prefixes (/__bili/, /__acp/) never ride the divert at all. The + // marker only declares intent for MODEL paths — the BODY SHAPE is + // the final word, because a marker'd host may still send full-resend + // turns (retries, dead conversations, side lanes); those ride the + // normal pipeline regardless of source address. So: buffer once (the + // pipeline re-buffers every JSON body anyway), shape-test, then + // divert or replay. The synthetic re-entries above call handleRequest + // DIRECTLY (never this divert), so no loops. if (req.method === "POST" - && !(req.url ?? "").startsWith("/__bili/") + && !isManagementPath((req.url ?? "").split("?", 2)[0]) && req.headers["x-bili-plugin"] !== undefined && req.headers[INCREMENTAL_TURN_MARKER_HEADER] !== undefined) { let raw: Buffer; @@ -166,6 +173,19 @@ export async function handle( return; } if (incrementalTurnBodyShape(raw)) { + // #3343: shaped turns are lane traffic — loopback-only, same + // trust class as the direct /__bili/plugin/turn entry. A + // non-loopback / tunnel / untrusted-origin caller gets 403 + // here instead of reaching applyTurn (which writes canonical + // session state under a caller-chosen conversationId). + // Well-behaved remote hosts never send deltas: their GET + // probe already 403s, so they stay full-resend (replay below). + const denial = adminGateDenial(req, opts.host); + if (denial) { + res.writeHead(403, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: denial })); + return; + } if (!incrementalTurnsEnabled()) { res.writeHead(503, { "content-type": "application/json" }); res.end(JSON.stringify({ error: "incremental turns disabled on this lane (rollout feature pluginIncrementalTurns off, or unified sidecar unavailable)" })); diff --git a/tests/issue3343-marker-divert-gate.test.ts b/tests/issue3343-marker-divert-gate.test.ts new file mode 100644 index 0000000000..2e33afbab3 --- /dev/null +++ b/tests/issue3343-marker-divert-gate.test.ts @@ -0,0 +1,242 @@ +// #3343: the marker divert must not be reachable from outside the loopback, +// and the lane's synthetic re-entry must carry the REAL caller's socket — +// never a forged 127.0.0.1. Non-loopback peers are simulated deterministically +// by connecting through a unix socket: remoteAddress comes back undefined, +// which isLoopbackAddress classifies as non-loopback (no network topology luck). +import assert from "node:assert/strict"; +import http from "node:http"; +import net from "node:net"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { Readable } from "node:stream"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; +import { _resetSessionsForTest, peekSession } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { __resetIncrementalLaneForTests, handleIncrementalTurn } from "../src/incremental-lane.ts"; +import { adminGateDenial } from "../src/server/admin.ts"; +import { BILI_TUNNEL_HEADER } from "../src/tunnel-guard.ts"; + +process.env.NODE_ENV = "test"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-3343-")); +test.after(() => rmSync(testRoot, { recursive: true, force: true })); + +const userTurn = (n: number, text = "hello") => ({ id: `u${n}`, role: "user", contentType: "text", content: text.repeat(400) }); + +interface Harness { + proxy: http.Server; + proxyUrl: string; + dir: string; + forwarded: Record[]; + /** Close the TCP listener and re-listen the SAME server on a unix + * socket — subsequent connections present remoteAddress undefined + * (= non-loopback to every gate) without depending on machine topology. */ + toUnixSocket: () => Promise; + close: () => Promise; +} + +async function harness(): Promise { + const dir = mkdtempSync(join(testRoot, "run-")); + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; + process.env.BILI_STORAGE_UNIFIED = "full"; + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + __resetIncrementalLaneForTests(); + const store = new SessionStore({ dir: dir + "/sessions", enabled: true, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + const forwarded: Record[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + forwarded.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "msg_test", role: "assistant", content: [{ type: "text", text: "answer" }], usage: { input_tokens: 10000, output_tokens: 10 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const addr = upstream.address(); + assert(addr && typeof addr === "object"); + const upstreamUrl = `http://127.0.0.1:${addr.port}`; + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "claude-test": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); + if (!(proxy as unknown as { listening?: boolean }).listening) await once(proxy, "listening"); + const proxyUrl = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + const toUnixSocket = async (): Promise => { + proxy.closeAllConnections?.(); + await new Promise((resolve) => proxy.close(() => resolve())); + const sockPath = join(dir, "proxy.sock"); + await new Promise((resolve, reject) => { + proxy.once("error", reject); + proxy.listen(sockPath, () => resolve()); + }); + return sockPath; + }; + return { proxy, proxyUrl, dir, forwarded, toUnixSocket, close: async () => { + proxy.closeAllConnections?.(); + await new Promise((resolve) => proxy.close(() => resolve())); + store.closeQuietly(); + await new Promise((resolve) => upstream.close(() => resolve())); + } }; +} + +/** Raw request over a unix socket (fetch/undici cannot dial socketPaths); + * the Host header is set explicitly because it is part of the gate input. */ +function udsRequest(sockPath: string, method: "GET" | "POST", path: string, headers: Record, body?: string): Promise<{ status: number; headers: http.IncomingHttpHeaders; body: string }> { + return new Promise((resolve, reject) => { + const req = http.request({ socketPath: sockPath, host: "127.0.0.1", method, path, headers: { host: "127.0.0.1", ...headers } }, (res) => { + const chunks: Buffer[] = []; + res.on("data", (c: Buffer) => chunks.push(c)); + res.on("end", () => resolve({ status: res.statusCode ?? 0, headers: res.headers, body: Buffer.concat(chunks).toString("utf8") })); + }); + req.on("error", reject); + if (body !== undefined) req.write(body); + req.end(); + }); +} + +const MARKER_HEADERS = { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-turn": "1" }; + +test("#3343 unit: adminGateDenial — the checks the divert now shares", () => { + const mkReq = (remoteAddress: string | undefined, headers: Record = {}): http.IncomingMessage => + Object.assign(new Readable({ read: (): void => {} }), { method: "GET", url: "/__bili/sessions", headers, socket: { remoteAddress } }) as unknown as http.IncomingMessage; + const deny = (req: http.IncomingMessage) => adminGateDenial(req, "127.0.0.1"); + assert.equal(deny(mkReq("127.0.0.1", { host: "127.0.0.1" })), undefined, "loopback + trusted Host, no Origin (curl class) passes"); + assert.equal(deny(mkReq("::1", { host: "localhost" })), undefined, "::1 + localhost passes"); + assert.match(deny(mkReq("192.168.1.50", { host: "127.0.0.1" })) ?? "", /loopback-only/, "a FORGED Host does not help a remote peer — the socket check precedes origin"); + assert.equal(deny(mkReq(undefined, { host: "127.0.0.1" })) !== undefined, true, "unix-socket peer (remoteAddress undefined) is non-loopback"); + assert.match(deny(mkReq("127.0.0.1", { host: "evil.example" })) ?? "", /origin/, "loopback peer with an untrusted Host is denied"); + assert.match(deny(mkReq("127.0.0.1", { host: "127.0.0.1", [BILI_TUNNEL_HEADER]: "1" })) ?? "", /tunnel/, "anti-tunnel holds even on loopback"); +}); + +test("#3343: loopback marker'd shaped turn still works (fix must not break the lane)", async () => { + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + let h: Harness | undefined; + try { + h = await harness(); + const res = await fetch(`${h.proxyUrl}/v1/messages`, { + method: "POST", + headers: { ...MARKER_HEADERS, "x-bili-plugin-conversation": "conv-3343-local", "x-acp-session": "conv-3343-local" }, + body: JSON.stringify({ conversationId: "conv-3343-local", sinceSeq: 0, request: { model: "claude-test", max_tokens: 512 }, messages: [userTurn(1)] }), + }); + assert.equal(res.status, 200); + assert.ok(res.headers.get("x-bili-applied-seq"), "loopback diversion carries the ack"); + const upstream = h.forwarded.at(-1) as { model?: string; messages?: unknown[] }; + assert.equal(upstream.model, "claude-test", "assembled request reached the upstream"); + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + await h?.close(); + } +}); + +test("#3343: non-loopback marker'd shaped turn → 403, no lane state written", async (t) => { + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + let h: Harness | undefined; + try { + h = await harness(); + const sockPath = await h.toUnixSocket(); + // The exact exploit shape from the issue: empty delta + sticky-system + // update aimed at a foreign conversation. Pre-fix this seeded the + // victim session with the injected system prompt. + const victim = "conv-3343-victim"; + const res = await udsRequest(sockPath, "POST", "/v1/messages", { ...MARKER_HEADERS, "x-bili-plugin-conversation": victim, "x-acp-session": victim }, + JSON.stringify({ conversationId: victim, sinceSeq: 0, system: "INJECTED-SYSTEM", request: { model: "claude-test", max_tokens: 512 }, messages: [] })); + assert.equal(res.status, 403, `denied before the lane (${res.body.slice(0, 120)})`); + assert.match(res.body, /loopback-only/); + assert.equal(h.forwarded.length, 0, "nothing reached the upstream"); + assert.equal(peekSession(victim), undefined, "no session state was created for the foreign conversation"); + // The capability probe stays loopback-gated too — a remote host sees + // 403 and correctly concludes "no incremental support", staying full-resend. + const probe = await udsRequest(sockPath, "GET", "/__bili/plugin/turn", { "x-bili-plugin": "dsh" }); + assert.equal(probe.status, 403); + void t; + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + await h?.close(); + } +}); + +test("#3343: non-loopback marker'd shaped turn on /__acp/* → 403 admin gate, never diverted", async (t) => { + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + let h: Harness | undefined; + try { + h = await harness(); + const sockPath = await h.toUnixSocket(); + // Pre-fix this rode the divert into the pipeline with a forged + // 127.0.0.1 socket, passed the admin gate, and answered 404 after + // writing lane state under the caller-chosen conversation id. + const conv = "conv-3343-acp"; + const res = await udsRequest(sockPath, "POST", "/__acp/status", { ...MARKER_HEADERS, "x-bili-plugin-conversation": conv, "x-acp-session": conv }, + JSON.stringify({ conversationId: conv, sinceSeq: 0, request: { model: "claude-test", max_tokens: 512 }, messages: [userTurn(1)] })); + assert.equal(res.status, 403, `management prefix rejected by the real gate (${res.body.slice(0, 120)})`); + assert.match(res.body, /loopback-only/); + assert.equal(h.forwarded.length, 0); + assert.equal(peekSession(conv), undefined, "no lane state under the caller-chosen id"); + void t; + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + await h?.close(); + } +}); + +test("#3343: non-loopback marker'd FULL-RESEND body still rides the normal pipeline (availability)", async () => { + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + let h: Harness | undefined; + try { + h = await harness(); + const sockPath = await h.toUnixSocket(); + // A remote host whose probe 403s stays full-resend but keeps stamping + // the marker — those turns must keep working byte-for-byte semantics. + const res = await udsRequest(sockPath, "POST", "/v1/messages", { ...MARKER_HEADERS, "x-acp-session": "conv-3343-plain" }, + JSON.stringify({ model: "claude-test", max_tokens: 512, messages: [{ role: "user", content: "remote plain turn" }] })); + assert.equal(res.status, 200, res.body.slice(0, 120)); + const upstream = h.forwarded.at(-1) as { model?: string; conversationId?: string }; + assert.equal(upstream.model, "claude-test"); + assert.equal("conversationId" in upstream, false, "not diverted — ordinary model traffic"); + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + await h?.close(); + } +}); + +test("#3343 unit: the synthetic forward carries the REAL socket, never a forged loopback", async () => { + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + let h: Harness | undefined; + try { + h = await harness(); + const remoteSocket = { remoteAddress: "192.168.1.50", localPort: 43210 } as const; + const req = Readable.from([]) as unknown as http.IncomingMessage; + Object.assign(req, { + method: "POST", + url: "/v1/messages", + headers: { host: "127.0.0.1", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": "conv-3343-sock" }, + socket: remoteSocket, + }); + const fakeRes = { + writeHead: (): void => {}, + setHeader: (): void => {}, + end: (): void => {}, + on: (): unknown => fakeRes, + } as unknown as http.ServerResponse; + let captured: http.IncomingMessage | undefined; + await handleIncrementalTurn(req, fakeRes, (): void => {}, (synthetic) => { + captured = synthetic; + return Promise.resolve(); + }, "/v1/messages", Buffer.from(JSON.stringify({ conversationId: "conv-3343-sock", sinceSeq: 0, resync: true, request: { model: "claude-test", max_tokens: 512 }, messages: [userTurn(1)] }))); + assert.ok(captured, "the turn reached the forward step"); + assert.equal(captured?.socket, remoteSocket, "identity: the caller's actual socket object, not a stand-in"); + assert.notEqual((captured?.socket as { remoteAddress?: string } | undefined)?.remoteAddress, "127.0.0.1", "no forged loopback identity downstream"); + } finally { + delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + await h?.close(); + } +});