diff --git a/scripts/live-files.mjs b/scripts/live-files.mjs index 7bd93fe7d3..30c4a60135 100644 --- a/scripts/live-files.mjs +++ b/scripts/live-files.mjs @@ -147,6 +147,9 @@ export const LIVE_FILES = [ // Native-zstd absence is simulated in an isolated Node child; it binds // no sockets and changes only that child's built-in module exports. "tests/content-encoding-zstd.test.ts", + // One Linux-only Node child reads fixture system CAs and exits within 10s; + // no child ports or network I/O. In-process HTTPS fixtures use listen(0). + "tests/upstream-ca-default-trust.test.ts", ]; /** Band width per live file: the zone ladder drifts +1 per EADDRINUSE retry; diff --git a/src/loop/adapter-google.ts b/src/loop/adapter-google.ts index 327c75344c..9e9fe8c6dc 100644 --- a/src/loop/adapter-google.ts +++ b/src/loop/adapter-google.ts @@ -624,13 +624,16 @@ export function createGoogleAdapter( }); continue; } - const clean = tagFilter.push(part.text); + // A signature may belong to ordinary text, without thought:true. + // Preserve its bytes and part boundary on both replay paths. + const signed = typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0; + const clean = signed ? part.text : tagFilter.push(part.text); if (clean !== part.text) { editedParts ??= [...parts]; editedParts[i] = { ...part, text: clean }; } if (clean.length === 0) continue; - partEvents.push({ kind: "text", delta: clean, rawHandled: true }); + partEvents.push({ kind: "text", delta: clean, rawHandled: true, ...(signed ? { rawGooglePart: part } : {}) }); } // Decide wire output only after every prose sibling has been // cleaned. Each complete frame belongs to one event, regardless diff --git a/src/loop/core.ts b/src/loop/core.ts index 5f73b25bea..269bb7a600 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -15,7 +15,7 @@ import { maybeJudgeContinuity } from "../invariant-judge.js"; import type { SeparatorSlot } from "../server/responses-ordering.js"; import { countImagesInParsedBody, countImagesInRawBody } from "../image-tokens.js"; import { diagnoseSuccessWithoutUsage, lastCompressSuffix, releaseNudgeHold, withSessionLock, type Session } from "../session.js"; -import type { BiliMessage } from "acp-kernel/wire"; +import type { BiliMessage, GooglePart } from "acp-kernel/wire"; import { coreToAnthropic } from "acp-kernel/wire"; import { parseCompressInput, @@ -329,7 +329,7 @@ export interface RequestOptions { // arrive instead of being re-laid out as [reasoning…, text, tool…]. type RoundPart = | { kind: "reasoning"; text: string; signature: string } - | { kind: "text"; text: string } + | { kind: "text"; text: string; rawGooglePart?: GooglePart } | { kind: "redacted"; data: string } // #3158: a Gemini part whose only payload is a thoughtSignature (no text, // no functionCall). Recorded in stream order so a proxy-tool rebuild puts @@ -340,7 +340,7 @@ export type ParsedStreamEvent = // rawHandled: the adapter carries this delta in another event's complete // frame (or buffers that frame until tool settlement). Keep accounting it, // but do not synthesize a second client frame in continuation rounds. - | { kind: "text"; delta: string; raw?: Buffer; rawHandled?: boolean } + | { kind: "text"; delta: string; raw?: Buffer; rawHandled?: boolean; rawGooglePart?: GooglePart } // round2-hold: withheld marks a reasoning PAYLOAD the adapter kept off the // client wire (bili-owned round ≥ 2 on a signing upstream — see // ParseStreamOpts.upstreamSignsThinking). It still feeds the loop's @@ -970,8 +970,8 @@ export async function* runCompressLoop( if (ev.kind === "text") { assistantText += ev.delta; let tp = roundParts[roundParts.length - 1]; - if (!tp || tp.kind !== "text") { - tp = { kind: "text", text: "" }; + if (!tp || tp.kind !== "text" || tp.rawGooglePart !== undefined || ev.rawGooglePart !== undefined) { + tp = { kind: "text", text: "", ...(ev.rawGooglePart ? { rawGooglePart: ev.rawGooglePart } : {}) }; roundParts.push(tp); } tp.text += ev.delta; @@ -1538,6 +1538,10 @@ export async function* runCompressLoop( role: "assistant", contentType: "text", text: part.text, + ...(part.rawGooglePart ? { + googleThoughtSignature: part.rawGooglePart.thoughtSignature, + rawGoogleParts: [part.rawGooglePart], + } : {}), }); } else if (part.kind === "standalone_signature") { // #3158: put the standalone thoughtSignature back at its diff --git a/src/plugin.ts b/src/plugin.ts index d58d0ffa8b..867038aef8 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3766,10 +3766,11 @@ export async function pipePluginChatWithStrip( // field, so an interleaved thought/text pair in one frame never // shares held-back state. const field = p["thought"] === true ? "thinking" : "text"; + const signed = typeof p["thoughtSignature"] === "string" && p["thoughtSignature"].length > 0; // Whole-field tool-call emission on an absorb-instructed // request (m00885) — see processOpenai. Degenerate-close // residue (#2190) rides the same routing. - if (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !mayStartDegenerateRenderTag(raw) && !(absorbInstructed === true && mayStartToolCallEmission(raw)) && !anyPending()) { + if (signed || (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !mayStartDegenerateRenderTag(raw) && !(absorbInstructed === true && mayStartToolCallEmission(raw)) && !anyPending())) { if (raw.length > 0) { keptText = true; appendReply(field, index, raw); @@ -5207,12 +5208,16 @@ function stripGoogleChunk(obj: T, drop: boolean = false, requestText?: string ...cand, content: { ...cont, - parts: (cont["parts"] as unknown[]).map((p) => - // #1960/KDD#10: thought parts carry a thoughtSignature — leave them byte-for-byte. - p && typeof p === "object" && (p as Record)["thought"] !== true && typeof (p as Record)["text"] === "string" - ? { ...(p as Record), text: stripAcpTags((p as Record)["text"] as string, drop, requestText) } - : p, - ), + parts: (cont["parts"] as unknown[]).map((p) => { + if (!p || typeof p !== "object") return p; + const part = p as Record; + // #1960/KDD#10: ordinary text can carry a signature too. + return part["thought"] !== true + && !(typeof part["thoughtSignature"] === "string" && part["thoughtSignature"].length > 0) + && typeof part["text"] === "string" + ? { ...part, text: stripAcpTags(part["text"], drop, requestText) } + : p; + }), }, }; }); diff --git a/src/server.ts b/src/server.ts index 9fd2f0e84d..7e1ea4eb6b 100644 --- a/src/server.ts +++ b/src/server.ts @@ -233,7 +233,12 @@ export function selfAddressedLocalPath(reqUrl: string, localPort: number | undef const port = u.port !== "" ? Number(u.port) : u.protocol === "https:" ? 443 : 80; if (port !== localPort) return undefined; const host = u.hostname.replace(/^\[|\]$/g, "").toLowerCase(); - const mine = host === "0.0.0.0" || host === "::" || host === "localhost" || localMachineIps().has(normalizeIpLiteral(host)); + // #7022 review: classifyIp covers the whole loopback range, not just + // configured interfaces — the tunnel-side fix stopped enumerating + // 127.0.0.x in localMachineIps(), so an unconfigured alias like + // 127.0.0.5:selfport must still be recognized as self here (else it + // would fall through to tunnel classification and 403, #562 self-layer). + const mine = host === "0.0.0.0" || host === "::" || host === "localhost" || classifyIp(host) === "loopback" || localMachineIps().has(normalizeIpLiteral(host)); if (!mine) return undefined; return u.pathname + u.search; } catch { @@ -1564,8 +1569,8 @@ export async function startServer(opts: ProxyOptions): Promise { // callback stacks on the server and a later successful bind fires ALL // accumulated claimOrAnnounce closures at once (double announce, // double registration, MaxListenersExceededWarning on long waits). - // Only our own callbacks ever use this event. server.removeAllListeners("listening"); + server.on("listening", installProcessHandlers); server.listen(port, opts.host, claimOrAnnounce); }; attemptListen(opts.port); @@ -1714,7 +1719,7 @@ export async function startServer(opts: ProxyOptions): Promise { // Node 20+ aborts the process on these by default. Log loudly and flush. let suppressedWriteErrors = 0; let suppressedRaceErrors = 0; - process.on("uncaughtException", (err) => { + const onUncaughtException = (err: Error): void => { if (isStreamWriteError(err)) { // Belt-and-suspenders for #1233: the logger's own stderr path is // guarded and never reaches here; a stream-write error that does @@ -1739,10 +1744,10 @@ export async function startServer(opts: ProxyOptions): Promise { return; } log("error", `uncaughtException: ${String(err?.stack ?? err)}`); - }); - process.on("unhandledRejection", (reason) => { + }; + const onUnhandledRejection = (reason: unknown): void => { log("error", `unhandledRejection: ${String(reason)}`); - }); + }; // Graceful shutdown: flush all dirty sessions to disk so a restart does // not lose recent compression state. SIGKILL/power loss cannot flush, but // debounced writes keep disk within ~500ms of in-memory state. @@ -1816,14 +1821,40 @@ export async function startServer(opts: ProxyOptions): Promise { // #3544: POST /__bili/shutdown (stale-proxy rotation from a newer // generation's bring-up) takes this exact graceful path. engine.lifecycleHooks.requestShutdown = (reason) => shutdown(reason); - process.on("SIGTERM", () => shutdown("SIGTERM")); - process.on("SIGINT", () => shutdown("SIGINT")); - // Windows never delivers SIGTERM (Node can listen but the kernel won't - // raise it). Ctrl+Break (and most service managers / `taskkill` / NSSM) - // raise SIGBREAK, so hook it to the same graceful-shutdown path there. - if (process.platform === "win32") { - process.on("SIGBREAK", () => shutdown("SIGBREAK")); + const onSigterm = () => shutdown("SIGTERM"); + const onSigint = () => shutdown("SIGINT"); + const onSigbreak = () => shutdown("SIGBREAK"); + let processHandlersInstalled = false; + function installProcessHandlers(): void { + if (processHandlersInstalled) return; + processHandlersInstalled = true; + process.on("uncaughtException", onUncaughtException); + process.on("unhandledRejection", onUnhandledRejection); + process.on("SIGTERM", onSigterm); + process.on("SIGINT", onSigint); + // Windows service managers deliver SIGBREAK instead of SIGTERM. + if (process.platform === "win32") process.on("SIGBREAK", onSigbreak); } + const removeProcessHandlers = (): void => { + if (!processHandlersInstalled) return; + // #7026 review: the close event fires when the listener stops accepting, + // i.e. BEFORE the session flush in finishDrain. Stripping the handlers + // here would leave a second Ctrl+C during the flush window unhandled — + // the process would die with unflushed compression state. Keep them + // until the process actually exits; a closed embedded server that never + // shut down (port-ladder retry path) still cleans up normally. + if (shuttingDown) return; + processHandlersInstalled = false; + process.removeListener("uncaughtException", onUncaughtException); + process.removeListener("unhandledRejection", onUnhandledRejection); + process.removeListener("SIGTERM", onSigterm); + process.removeListener("SIGINT", onSigint); + if (process.platform === "win32") process.removeListener("SIGBREAK", onSigbreak); + }; + // A closed embedded server must not retain signal-driven shutdown of the + // hosting process. Port-claim retries can close and re-listen this server. + installProcessHandlers(); + server.on("close", removeProcessHandlers); // Launcher children have no console and TerminateProcess leaves no room // for a flush (#414): they watch the launcher pid and run the graceful // path themselves when it disappears (≤2s after the parent exits). diff --git a/src/stream-google.ts b/src/stream-google.ts index fbe585404c..a722e2119a 100644 --- a/src/stream-google.ts +++ b/src/stream-google.ts @@ -31,7 +31,9 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator containsRenderTagText(text) || containsMarkerLineText(text) || containsBiliInternalText(text) || (absorbArmed && containsToolCallEmissionText(text)); // Capture request evidence before a yielded proxy tool can change the context. const requestText = absorbArmed && parts.some(part => part && typeof part === "object" - && (part as { thought?: boolean }).thought !== true && typeof part.text === "string" && containsEcho(part.text)) + && (part as { thought?: boolean }).thought !== true + && !(typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0) + && typeof part.text === "string" && containsEcho(part.text)) ? JSON.stringify(ctx.messages) : undefined; for (const part of parts) { if (!part || typeof part !== "object") continue; @@ -46,10 +48,12 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator 0) + && typeof part.text === "string" && containsEcho(part.text)) { ctx.log(`[tag-echo] stripped: non-stream google output ACP echo (render tags/markers/internal artifacts): ${part.text.slice(0, 120).replace(/\n/g, " ")}`); part.text = stripAcpTags(part.text, absorbArmed, requestText); } diff --git a/src/tunnel-guard.ts b/src/tunnel-guard.ts index 01965ae160..3c9b79924d 100644 --- a/src/tunnel-guard.ts +++ b/src/tunnel-guard.ts @@ -214,8 +214,6 @@ export function localMachineIps(): Set { if (ni.address) ips.add(ni.address.toLowerCase()); } } - // The whole 127/8 loopback range routes to this machine. - for (let i = 0; i <= 255; i++) ips.add(`127.0.0.${i}`); localAddrCache = { at: now, ips }; } return localAddrCache.ips; @@ -355,8 +353,13 @@ export async function checkTunnelDestination(origin: string, ctx: TunnelCheckCon // destination routes to the local host, so our own port through them is // a self-loop even though neither literal sits in the interface set. if (ctx.selfPort !== undefined && port === ctx.selfPort) { - const mine = (ctx.localIps ?? localMachineIps)(); - if (ips.some((ip) => ip === "0.0.0.0" || ip === "::" || mine.has(ip.toLowerCase()) || mine.has(`::ffff:${ip.toLowerCase()}`))) { + const mine = new Set(Array.from((ctx.localIps ?? localMachineIps)(), normalizeIpLiteral)); + if (ips.some((address) => { + const ip = normalizeIpLiteral(address); + // Loopback is a range, not a list of configured interfaces. DNS + // answers also need the mapped-IPv4 normalization literals get. + return ip === "0.0.0.0" || classifyIp(ip) === "loopback" || mine.has(ip); + })) { return { ok: false, code: "self", message: "the bili tunnel may not target the proxy itself" }; } } diff --git a/src/upstream-ca.ts b/src/upstream-ca.ts index b021f135dc..9831cbd9eb 100644 --- a/src/upstream-ca.ts +++ b/src/upstream-ca.ts @@ -1,10 +1,9 @@ // #3776 sub-defect 2: CA trust material for bili's OWN outbound upstream fetch. -// Node's default TLS trust is the built-in Mozilla bundle + NODE_EXTRA_CA_CERTS; -// it does NOT read the OS certificate store, so a security-software MITM root -// (Kaspersky / corporate DLP) installed in the Windows/Mac store is untrusted and +// Node's default TLS trust may omit the OS certificate store, so a security-software MITM root +// (Kaspersky / corporate DLP) installed in the Windows/Mac store can be untrusted and // every request dies with SELF_SIGNED_CERT_IN_CHAIN (#3773). network.caFile / -// BILI_CA_FILE points at that vendor's PEM; we MERGE it with the built-ins (never -// replace) so all other upstreams keep working. When unset we return undefined and +// BILI_CA_FILE points at that vendor's PEM; we MERGE it with Node's effective trust +// so all other upstreams keep working. When unset we return undefined and // the dispatcher uses Node's default trust byte-for-byte (no connect override). import { readFileSync, statSync } from "node:fs"; @@ -47,7 +46,7 @@ export function upstreamCaIdentity(now = Date.now()): string { return lastIdentity; } -/** Merged CA list (built-ins + NODE_EXTRA_CA_CERTS + network.caFile) for bili's +/** Merged CA list (Node's effective trust + NODE_EXTRA_CA_CERTS + network.caFile) for bili's * own fetch, or undefined when no caFile is configured — in which case the * dispatcher must NOT set connect.ca and Node's default trust applies unchanged. * The merge (not replace) matters: an explicit undici connect.ca REPLACES the @@ -56,7 +55,11 @@ export function upstreamExtraCa(): string[] | undefined { const identity = upstreamCaIdentity(); if (identity === "none") return undefined; if (cacheKey === identity) return cacheValue; - const merged: string[] = [...tls.rootCertificates]; + // Node 22.15+ exposes the effective store, including opted-in system CAs + // and programmatic defaults. rootCertificates contains only bundled roots. + const merged: string[] = typeof tls.getCACertificates === "function" + ? [...tls.getCACertificates("default")] + : [...tls.rootCertificates]; const nodeExtra = process.env["NODE_EXTRA_CA_CERTS"]; // Node treats NODE_EXTRA_CA_CERTS as ONE file path — no list separator on any // platform (verified against Node 22: the whole value is loaded as a single diff --git a/tests/admin-probe.test.ts b/tests/admin-probe.test.ts index da931f9fee..b39838084f 100644 --- a/tests/admin-probe.test.ts +++ b/tests/admin-probe.test.ts @@ -41,6 +41,8 @@ test("selfAddressedLocalPath: self-addressed absolute-form requests only", () => assert.equal(selfAddressedLocalPath(`http://[::ffff:127.0.0.1]:${P}/__bili/health`, P), "/__bili/health", "hex-mapped loopback folds to 127.0.0.1"); assert.equal(selfAddressedLocalPath(`http://0.0.0.0:${P}/__bili/health`, P), "/__bili/health"); assert.equal(selfAddressedLocalPath(`http://[::]:${P}/__acp/x`, P), "/__acp/x", "v6 unspecified in brackets"); + assert.equal(selfAddressedLocalPath(`http://127.0.0.5:${P}/__bili/health`, P), "/__bili/health", "unconfigured loopback alias is still self (#7022 review)"); + assert.equal(selfAddressedLocalPath(`http://127.42.19.8:${P}/__bili/health`, P), "/__bili/health", "deep 127/8 alias is still self (#7022 review)"); // #3245: ANY path qualifies — model-style paths and the /bili/ zero-config // entry are served locally exactly as a direct connection would send them. assert.equal(selfAddressedLocalPath(`http://127.0.0.1:${P}/v1/chat/completions`, P), "/v1/chat/completions"); diff --git a/tests/google-complete-frame.test.ts b/tests/google-complete-frame.test.ts index 18bb8a8d94..76d8a66bc5 100644 --- a/tests/google-complete-frame.test.ts +++ b/tests/google-complete-frame.test.ts @@ -143,14 +143,15 @@ async function close(server: http.Server): Promise { await new Promise(resolve => server.close(() => resolve())); } -async function throughProxy(rounds: Array>>): Promise<{ calls: Array>; wire: string }> { +async function throughProxy(rounds: Array>>, options: { plugin?: boolean; stream?: boolean } = {}): Promise<{ calls: Array>; wire: string }> { const calls: Array> = []; const upstream = http.createServer(async (req, res) => { const buffers: Buffer[] = []; for await (const chunk of req) buffers.push(Buffer.from(chunk)); calls.push(JSON.parse(Buffer.concat(buffers).toString()) as Record); - res.writeHead(200, { "content-type": "text/event-stream" }); - res.end((rounds[calls.length - 1] ?? [candidate([{ text: "Unexpected retry." }], "STOP")]).map(sse).join("")); + const reply = rounds[calls.length - 1] ?? [candidate([{ text: "Unexpected retry." }], "STOP")]; + res.writeHead(200, { "content-type": options.stream === false ? "application/json" : "text/event-stream" }); + res.end(options.stream === false ? JSON.stringify(reply[0]) : reply.map(sse).join("")); }); upstream.listen(0, "127.0.0.1"); await once(upstream, "listening"); @@ -169,8 +170,9 @@ async function throughProxy(rounds: Array>>): Prom try { const upstreamPort = (upstream.address() as import("node:net").AddressInfo).port; const proxyPort = (proxy.address() as import("node:net").AddressInfo).port; - const response = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/gemini-2.5-pro:streamGenerateContent?alt=sse`, { - method: "POST", headers: { "content-type": "application/json", "x-acp-session": `complete-frame-${proxyPort}` }, + const method = options.stream === false ? "generateContent" : "streamGenerateContent?alt=sse"; + const response = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/gemini-2.5-pro:${method}`, { + method: "POST", headers: { "content-type": "application/json", "x-acp-session": `complete-frame-${proxyPort}`, ...(options.plugin ? { "x-bili-plugin": "pi", "x-bili-plugin-conversation": `complete-frame-${proxyPort}` } : {}) }, body: JSON.stringify({ contents: [{ role: "user", parts: [{ text: "Report status then answer." }] }] }), }); const wire = await response.text(); @@ -225,3 +227,38 @@ test("Google HTTP control: single-part replies remain single-part", async () => assert.equal(calls.length, 1); assert.deepEqual(partsOf(framesOf(wire)), [{ text: "Control answer." }]); }); + +for (const mode of ["sse", "array"] as const) { + test(`Google ${mode}: signed ordinary text bypasses prose filtering`, async () => { + const signed = { text: thought, thoughtSignature: signature }; + const events = await collect([candidate([signed]), candidate([], "STOP")], mode); + assert.deepEqual(partsOf(framesOf(forwarded(events))), [signed]); + assert.deepEqual(events.filter(event => event.kind === "text").map(event => event.delta), [thought]); + }); +} + +for (const plugin of [false, true]) { + for (const stream of [false, true]) { + test(`Google HTTP ${plugin ? "plugin" : "proxy"} ${stream ? "SSE" : "JSON"}: signed text stays byte-exact`, async () => { + const signed = { text: thought, thoughtSignature: signature }; + const { calls, wire } = await throughProxy([[candidate([signed], "STOP")]], { plugin, stream }); + assert.equal(calls.length, 1); + assert.deepEqual(partsOf(stream ? framesOf(wire) : [JSON.parse(wire) as Record]), [signed]); + }); + } +} + +test("Google HTTP proxy: signed text boundaries and signatures survive a tool continuation", async () => { + const signedA = { text: " Signed ordinary answer A. ", thoughtSignature: "c2lnLWE=" }; + const signedB = { text: "Signed ordinary answer B.\n", thoughtSignature: "c2lnLWI=" }; + const parts = [{ text: "Unsigned prefix." }, signedA, signedB, { text: "Unsigned suffix." }]; + const { calls, wire } = await throughProxy([ + [candidate(parts), candidate([{ functionCall: { id: "status", name: "acp_status", args: {} }, thoughtSignature: "c3RhdHVzLXNpZw==" }], "STOP")], + [candidate([{ text: "Final answer." }], "STOP")], + ]); + assert.equal(calls.length, 2); + const visible = partsOf(framesOf(wire)); + assert.deepEqual(visible.filter(part => part.thoughtSignature === signedA.thoughtSignature || part.thoughtSignature === signedB.thoughtSignature), [signedA, signedB]); + const replay = (calls[1]!.contents as Array<{ parts: Part[] }>).flatMap(content => content.parts); + assert.deepEqual(replay.filter(part => part.thoughtSignature === signedA.thoughtSignature || part.thoughtSignature === signedB.thoughtSignature), [signedA, signedB]); +}); diff --git a/tests/server-process-listeners.test.ts b/tests/server-process-listeners.test.ts new file mode 100644 index 0000000000..a736ffd7e8 --- /dev/null +++ b/tests/server-process-listeners.test.ts @@ -0,0 +1,144 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { once, type EventEmitter } from "node:events"; +import type { Server } from "node:http"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; + +const events: Array<"uncaughtException" | "unhandledRejection" | NodeJS.Signals> = ["uncaughtException", "unhandledRejection", "SIGTERM", "SIGINT", + ...(process.platform === "win32" ? ["SIGBREAK" as const] : [])]; +const processEvents: EventEmitter = process; +const snapshot = () => new Map(events.map((event) => [event, processEvents.listeners(event)])); + +async function start(): Promise { + const server = await startServer({ + port: 0, host: "127.0.0.1", upstream: "http://127.0.0.1:9", routes: {}, + modelContextLimit: 200_000, kernelConfig: defaultConfig(200_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: [] }, + } as ProxyOptions); + await once(server, "listening"); + return server; +} + +async function close(server: Server): Promise { + if (!server.listening) return; + const closed = once(server, "close"); + server.close(); + server.closeAllConnections(); + await closed; +} + +function assertSameListeners(expected: ReturnType): void { + for (const event of events) assert.deepEqual(processEvents.listeners(event), expected.get(event), event); +} + +// Remove only this test's leaked handlers when checking the unfixed base so +// one failing regression does not contaminate the next independent scenario. +function restoreListeners(before: ReturnType): void { + for (const event of events) { + for (const listener of processEvents.listeners(event)) { + if (!before.get(event)?.includes(listener)) process.removeListener(event, listener as (...args: unknown[]) => void); + } + } +} + +test("closing each server releases its process handlers across repeated lifecycles", async () => { + const before = snapshot(); + const servers: Server[] = []; + try { + for (let i = 0; i < 3; i += 1) { + const server = await start(); + servers.push(server); + for (const event of events) assert.equal(process.listenerCount(event), before.get(event)!.length + 1, event); + await close(server); + assertSameListeners(before); + } + } finally { + await Promise.all(servers.map(close)); + restoreListeners(before); + } +}); + +test("closing one server keeps another live server and foreign process handlers", async () => { + const before = snapshot(); + const foreign = () => {}; + for (const event of events) process.on(event, foreign); + const withForeign = snapshot(); + const servers: Server[] = []; + try { + const first = await start(); + servers.push(first); + const withFirst = snapshot(); + const second = await start(); + servers.push(second); + await close(second); + assertSameListeners(withFirst); + assert.equal(first.listening, true); + await close(first); + assertSameListeners(withForeign); + } finally { + await Promise.all(servers.map(close)); + restoreListeners(before); + } +}); + +test("re-listening restores exactly one handler set and closing removes it again", async () => { + const before = snapshot(); + let server: Server | undefined; + try { + server = await start(); + const serving = snapshot(); + await close(server); + assertSameListeners(before); + const listening = once(server, "listening"); + server.listen(0, "127.0.0.1"); + await listening; + assertSameListeners(serving); + await close(server); + assertSameListeners(before); + } finally { + if (server) await close(server); + restoreListeners(before); + } +}); + +// #7026 review: the "close" event fires when the listener stops accepting, BEFORE +// the session flush in finishDrain. Handlers stripped there left the flush window +// unprotected — a second Ctrl+C would kill the process mid-flush. The handlers +// must survive until the process actually exits. (Placed last: shutdown() latches +// the module-level flag, so the clean-remove tests above must run first.) +// #7026 review: the "close" event fires when the listener stops accepting, +// BEFORE the session flush in finishDrain. Handlers stripped there left the +// flush window unprotected — a second Ctrl+C would kill the process mid-flush. +// The handlers must survive until the process actually exits. (Placed last: +// shutdown() latches the module-level flag, so the clean-remove tests above +// must run first.) +test("graceful shutdown keeps signal handlers until the process exits", async () => { + const before = snapshot(); + const realExit = process.exit; + const exits: Array = []; + process.exit = ((code?: number | string) => { exits.push(code); }) as typeof process.exit; + const server = await start(); + try { + const serving = snapshot(); + process.kill(process.pid, "SIGINT"); + await once(server, "close"); + // Let the shutdown chain reach (the stubbed) process.exit first: the + // test process must not drain out from under the assertions while the + // flush is still running. + await new Promise((resolve) => setTimeout(resolve, 500)); + assertSameListeners(serving); + // Second Ctrl+C during the flush window: must be absorbed by the + // idempotent shutdown(), not delivered to Node's default handler. + process.kill(process.pid, "SIGTERM"); + await new Promise((resolve) => setImmediate(resolve)); + assertSameListeners(serving); + assert.ok(exits.length <= 1); + } finally { + restoreListeners(before); + process.exit = realExit; + } +}); diff --git a/tests/tunnel-guard.test.ts b/tests/tunnel-guard.test.ts index d5806bb37a..c504351e07 100644 --- a/tests/tunnel-guard.test.ts +++ b/tests/tunnel-guard.test.ts @@ -380,6 +380,11 @@ test("integration: tunnel cannot reach the proxy's own management plane (#409 Po assert.match(poc.body, /tunnel_destination_denied/); const pocPut = await get(selfPort, `/bili/http://127.0.0.1:${selfPort}/__bili/config`); assert.equal(pocPut.status, 403); + // Every 127/8 address denotes this machine, including aliases that do + // not appear in networkInterfaces(). Reject before opening a socket. + const alias = await get(selfPort, `/bili/http://127.42.19.8:${selfPort}/v1/models`); + assert.equal(alias.status, 403); + assert.match(alias.body, /"detail":"self"/); // Direct loopback management access still works (marker not present). const direct = await get(selfPort, "/__bili/config", { host: `127.0.0.1:${selfPort}` }); assert.equal(direct.status, 200); diff --git a/tests/tunnel-self-addresses.test.ts b/tests/tunnel-self-addresses.test.ts new file mode 100644 index 0000000000..fc7cf8afb4 --- /dev/null +++ b/tests/tunnel-self-addresses.test.ts @@ -0,0 +1,64 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { checkTunnelDestination } from "../src/tunnel-guard.ts"; + +const localIps = () => new Set(["127.0.0.1", "::1", "192.168.1.5"]); +const context = { selfPort: 8787, clientLoopback: true, allowlist: [], localIps }; + +test("self admission covers the whole IPv4 loopback range", async () => { + for (const host of ["127.0.1.1", "127.42.19.8", "127.255.255.254"]) { + const verdict = await checkTunnelDestination(`http://${host}:8787/v1/models`, context); + assert.equal(verdict.ok, false, host); + if (verdict.ok) assert.fail("self destination was admitted"); + assert.equal(verdict.code, "self"); + } +}); + +test("mapped IPv4 loopback aliases cannot reach the serving port", async () => { + for (const host of ["[::ffff:127.42.19.8]", "[::ffff:7f2a:1308]"]) { + const verdict = await checkTunnelDestination(`http://${host}:8787`, context); + assert.equal(verdict.ok, false, host); + if (verdict.ok) assert.fail("mapped self destination was admitted"); + assert.equal(verdict.code, "self"); + } +}); + +test("resolved loopback aliases receive the same self verdict as literals", async () => { + for (const address of ["127.42.19.8", "::ffff:127.42.19.8", "::ffff:7f2a:1308"]) { + const verdict = await checkTunnelDestination("http://model.test:8787", { + ...context, resolveHost: async () => [address], + }); + assert.equal(verdict.ok, false, address); + if (verdict.ok) assert.fail("resolved self destination was admitted"); + assert.equal(verdict.code, "self"); + } +}); + +test("mapped resolver answers match the machine's ordinary interface addresses", async () => { + for (const address of ["::ffff:192.168.1.5", "::ffff:c0a8:105"]) { + const verdict = await checkTunnelDestination("http://model.test:8787", { + ...context, resolveHost: async () => [address], + }); + assert.equal(verdict.ok, false, address); + if (verdict.ok) assert.fail("own interface was admitted"); + assert.equal(verdict.code, "self"); + } +}); + +test("an allowlist cannot exempt a remote client's loopback self destination", async () => { + const verdict = await checkTunnelDestination("http://127.42.19.8:8787", { + ...context, clientLoopback: false, allowlist: ["127.42.19.8:8787"], + }); + assert.equal(verdict.ok, false); + if (verdict.ok) assert.fail("allowlisted self destination was admitted"); + assert.equal(verdict.code, "self"); +}); + +test("other ports and other machines retain their self-hosted upstream behavior", async () => { + for (const origin of ["http://127.42.19.8:8199", "http://[::ffff:127.42.19.8]:8199", "http://192.168.1.6:8787"]) { + assert.deepEqual(await checkTunnelDestination(origin, context), { ok: true }, origin); + } + assert.deepEqual(await checkTunnelDestination("http://model.test:8199", { + ...context, resolveHost: async () => ["::ffff:c0a8:105"], + }), { ok: true }); +}); diff --git a/tests/upstream-ca-default-trust.test.ts b/tests/upstream-ca-default-trust.test.ts new file mode 100644 index 0000000000..8f853e38ff --- /dev/null +++ b/tests/upstream-ca-default-trust.test.ts @@ -0,0 +1,126 @@ +import assert from "node:assert/strict"; +import { execFileSync } from "node:child_process"; +import { once } from "node:events"; +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import https from "node:https"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import tls from "node:tls"; +import { fileURLToPath } from "node:url"; +import { after, afterEach, before, test } from "node:test"; +import { ensureRootCA, mintHostCert, pemFingerprint, rootCaPath, _resetForTest as resetCa } from "../src/ca.ts"; +import { upstreamExtraCa, _resetUpstreamCaForTest } from "../src/upstream-ca.ts"; + +const supported = typeof tls.getCACertificates === "function" && typeof tls.setDefaultCACertificates === "function"; +const originalDefaults = supported ? tls.getCACertificates("default") : []; +const previousCa = process.env.BILI_CA_FILE; +const previousExtra = process.env.NODE_EXTRA_CA_CERTS; +const directory = mkdtempSync(join(tmpdir(), "bili-default-ca-")); +const additionalFile = join(directory, "additional.pem"); +let trustedRoot: string; +let trustedRootFile: string; +let leaf: { keyPem: string; certPem: string }; + +before(() => { + if (!supported) return; + const previousData = process.env.XDG_DATA_HOME; + process.env.XDG_DATA_HOME = directory; + try { + resetCa(); + ensureRootCA(); + trustedRootFile = rootCaPath(); + trustedRoot = readFileSync(trustedRootFile, "utf8"); + leaf = mintHostCert("localhost"); + writeFileSync(additionalFile, tls.rootCertificates[0]!); + } finally { + if (previousData === undefined) delete process.env.XDG_DATA_HOME; + else process.env.XDG_DATA_HOME = previousData; + resetCa(); + } +}); + +afterEach(() => { + if (supported) tls.setDefaultCACertificates(originalDefaults); + if (previousCa === undefined) delete process.env.BILI_CA_FILE; + else process.env.BILI_CA_FILE = previousCa; + if (previousExtra === undefined) delete process.env.NODE_EXTRA_CA_CERTS; + else process.env.NODE_EXTRA_CA_CERTS = previousExtra; + _resetUpstreamCaForTest(); +}); + +after(() => rmSync(directory, { recursive: true, force: true })); + +function setDefaultTrust(): void { + tls.setDefaultCACertificates([trustedRoot]); + delete process.env.NODE_EXTRA_CA_CERTS; + process.env.BILI_CA_FILE = additionalFile; + _resetUpstreamCaForTest(); +} + +async function requestWithConfiguredTrust(): Promise { + const server = https.createServer({ key: leaf.keyPem, cert: leaf.certPem }, (_request, response) => response.end("trusted default CA")); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + assert.ok(address && typeof address !== "string"); + try { + return await new Promise((resolve, reject) => { + const request = https.get({ hostname: "localhost", port: address.port, ca: upstreamExtraCa(), agent: false, signal: AbortSignal.timeout(3000) }, (response) => { + let body = ""; + response.setEncoding("utf8"); + response.on("data", (chunk: string) => { body += chunk; }); + response.on("end", () => resolve(body)); + response.on("error", reject); + }); + request.on("error", reject); + }); + } finally { + server.closeAllConnections(); + await new Promise((resolve) => server.close(() => resolve())); + } +} + +test("adding network.caFile preserves CA certificates already in Node's effective default store", { skip: !supported }, () => { + setDefaultTrust(); + const fingerprints = new Set(upstreamExtraCa()!.map(pemFingerprint)); + assert.ok(fingerprints.has(pemFingerprint(trustedRoot))); + assert.ok(fingerprints.has(pemFingerprint(tls.rootCertificates[0]!))); +}); + +test("the merged trust accepts a real HTTPS server signed by a default-only CA", { skip: !supported }, async () => { + setDefaultTrust(); + assert.equal(await requestWithConfiguredTrust(), "trusted default CA"); +}); + +test("merging an extra CA does not reintroduce bundled roots excluded from default trust", { skip: !supported }, () => { + setDefaultTrust(); + const fingerprints = new Set(upstreamExtraCa()!.map(pemFingerprint)); + assert.ok(!fingerprints.has(pemFingerprint(tls.rootCertificates[1]!))); +}); + +test("without network.caFile, native default trust remains untouched", { skip: !supported }, async () => { + setDefaultTrust(); + delete process.env.BILI_CA_FILE; + assert.equal(upstreamExtraCa(), undefined); + assert.equal(await requestWithConfiguredTrust(), "trusted default CA"); +}); + +test("a Linux process started with --use-system-ca retains its SSL_CERT_FILE trust", { skip: !supported || process.platform !== "linux" }, () => { + const certificateDirectory = join(directory, "empty-certs"); + mkdirSync(certificateDirectory); + const result = execFileSync(process.execPath, ["--use-system-ca", "--import", "tsx", "--input-type=module", "-e", ` + import assert from "node:assert/strict"; + import { readFileSync } from "node:fs"; + import tls from "node:tls"; + import { pemFingerprint } from "./src/ca.ts"; + import { upstreamExtraCa } from "./src/upstream-ca.ts"; + const expected = pemFingerprint(readFileSync(process.env.SSL_CERT_FILE, "utf8")); + assert.ok(tls.getCACertificates("default").some(pem => pemFingerprint(pem) === expected)); + assert.ok(upstreamExtraCa().some(pem => pemFingerprint(pem) === expected)); + console.log("system CA retained"); + `], { + cwd: fileURLToPath(new URL("..", import.meta.url)), encoding: "utf8", timeout: 10_000, + env: { ...process.env, SSL_CERT_FILE: trustedRootFile, SSL_CERT_DIR: certificateDirectory, BILI_CA_FILE: additionalFile, NODE_EXTRA_CA_CERTS: "" }, + }); + assert.match(result, /system CA retained/); +});