diff --git a/reference/architecture.md b/reference/architecture.md index 69a1bf0b24..fe4d115c98 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -41,6 +41,7 @@ billion-context/ │ ├── conflict-watch.ts # Per-session compression-conflict ledger (#1206) │ ├── upstream-proxy.ts # undici ProxyAgent routing (explicit-direct vs no-preference, SOCKS reject) │ ├── fetch-util.ts # Capped body reads, timeout/replay helpers (knob-resolved) +│ ├── request-body-budget.ts # 接收、解压、发送字节预算与大请求名额 │ ├── fetch-transport.ts # AsyncLocalStorage fetch-transport override (withFetchTransport) │ ├── content-encoding.ts # gzip/deflate decode + decompression-bomb guard (413) │ ├── wire-body.ts # Trailing-user-turn append on assembled wire bodies diff --git a/reference/request-body-budget.md b/reference/request-body-budget.md new file mode 100644 index 0000000000..27044216a5 --- /dev/null +++ b/reference/request-body-budget.md @@ -0,0 +1,19 @@ +# Request body byte budgets / 请求体字节预算 + +HTTP model requests have three separate byte boundaries: + +| Stage | Limit | Failure | +| --- | --- | --- | +| Raw HTTP reception | 100 MiB | 413, `request_too_large`, `stage: receive` when the socket permits a response | +| Decompression of recognized model protocols | 200 MiB per decoding stage | 413, `request_too_large`, `stage: decode` | +| Rebuilt upstream body | 100 MiB of UTF-8 / Buffer bytes | 413, `request_too_large`, `stage: forward` before initial send | + +Only two requests whose decoded body crosses 100 MiB may be active in one proxy process. A third receives 503 `request_body_busy`, `stage: decode`, `Retry-After: 1`, without being forwarded. Its slot is released on completion or cancellation. Ordinary requests do not use these slots. The limit bounds the **additional large decoded payloads**, not total process RSS: buffers, JSON parsing, session state and ordinary concurrent requests also consume memory. Streaming codecs stop on cancellation and enforce the cap while decoding; synchronous zstd can only observe cancellation at its synchronous checkpoints. + +This permits a gzip / zstd / Brotli-compressed history slightly above 100 MiB to reach existing explicit reductions. It does not enable `compress.stripImages` or image compression. If the rebuilt body remains over 100 MiB, it is refused; retries cannot bypass the send guard. Already-started SSE responses deliver the failure through the existing error channel instead of changing committed headers. Budget diagnostics report sizes/stages, without request text or pixels. + +Configure the existing `compress.stripImages` / `compress.stripImagesKeepRecent` policy only when historical-image removal is appropriate. The recent setting counts **messages**, not individual images. Existing image-archive / retrieval behavior, including its failure limitations, is unchanged; this is not a lossless-image lifecycle repair. Passthrough-marked or unrecognized compressed requests keep their original bytes and the raw 100 MiB limit. Uncompressed HTTP bodies above 100 MiB and WebSocket limits are unchanged. Byte limits do not replace model token-window checks or establish an upstream's accepted maximum. + +HTTP 请求分成三道字节边界:原始接收 100 MiB;已识别模型协议每层解压最多 200 MiB;重建后发送正文最多 100 MiB。只有两个超过旧解压上限的请求能同时占用名额,第三个在解压阶段返回可重试的 503,完成或取消后释放。该名额限制额外超大正文,不是整个进程的 RSS 上限。 + +此修复让略超旧限制的压缩历史进入已有、显式开启的减量流程,不会自动删除图片或开启缩图。减量后仍过大时,在正式上游发送前拒绝;流已经开始时使用现有错误事件。历史图片保留策略仍由 `stripImages` 与 `stripImagesKeepRecent` 决定,后者计数单位是消息。原图归档失败等既有生命周期问题另行处理,不能把本修复视为无损恢复保证。未压缩大正文、WebSocket、透传行为和模型 token 窗口保持原有边界。 diff --git a/src/content-encoding.ts b/src/content-encoding.ts index cdf0b7b413..f1a39eab42 100644 --- a/src/content-encoding.ts +++ b/src/content-encoding.ts @@ -1,4 +1,5 @@ import { createBrotliDecompress, createGunzip, createInflate, createInflateRaw } from "node:zlib"; +import { DecodedRequestBusyError } from "./request-body-budget.js"; import type { Duplex } from "node:stream"; import { Decompress as ZstdDecompress } from "fzstd"; @@ -19,48 +20,63 @@ export class DecompressedTooLargeError extends Error { * instead reports overflow opaquely (ERR_BUFFER_TOO_LARGE for gzip/br; an * ambiguous Z_DATA_ERROR for deflate that cannot be told apart from a corrupt * stream), so counting the stream ourselves is the only reliable signal. */ -function streamDecode(factory: () => Duplex, input: Buffer, max: number): Promise { +interface DecodeOptions { + signal?: AbortSignal; + onBytes?: (bytes: number) => void; +} + +function streamDecode(factory: () => Duplex, input: Buffer, max: number, options?: DecodeOptions): Promise { return new Promise((resolve, reject) => { const chunks: Buffer[] = []; let size = 0; const decoder = factory(); + const abort = () => decoder.destroy(options?.signal?.reason ?? new Error("request decoding aborted")); + decoder.once("close", () => options?.signal?.removeEventListener("abort", abort)); decoder.on("data", (chunk: Buffer) => { + if (decoder.destroyed) return; size += chunk.length; - if (size > max) { - decoder.destroy(); - reject(new DecompressedTooLargeError(max)); - return; + try { + if (size > max) throw new DecompressedTooLargeError(max); + options?.onBytes?.(size); + chunks.push(chunk); + } catch (err) { + decoder.destroy(err instanceof Error ? err : new Error(String(err))); } - chunks.push(chunk); }); decoder.on("end", () => resolve(Buffer.concat(chunks, size))); decoder.on("error", (err: Error) => reject(err)); - decoder.end(input); + if (options?.signal?.aborted) abort(); + else { + options?.signal?.addEventListener("abort", abort, { once: true }); + decoder.end(input); + } }); } -async function decodeZstd(input: Buffer, max: number): Promise { +async function decodeZstd(input: Buffer, max: number, options?: DecodeOptions): Promise { const chunks: Buffer[] = []; let size = 0; const decoder = new ZstdDecompress((chunk) => { size += chunk.byteLength; if (size > max) throw new DecompressedTooLargeError(max); + options?.signal?.throwIfAborted(); + options?.onBytes?.(size); chunks.push(Buffer.from(chunk)); }); decoder.push(input, true); return Buffer.concat(chunks, size); } -async function decodeOne(coding: string, input: Buffer, max: number): Promise { - if (coding === "gzip" || coding === "x-gzip") return streamDecode(createGunzip, input, max); - if (coding === "br") return streamDecode(createBrotliDecompress, input, max); - if (coding === "zstd") return decodeZstd(input, max); +async function decodeOne(coding: string, input: Buffer, max: number, options?: DecodeOptions): Promise { + if (coding === "gzip" || coding === "x-gzip") return streamDecode(createGunzip, input, max, options); + if (coding === "br") return streamDecode(createBrotliDecompress, input, max, options); + if (coding === "zstd") return decodeZstd(input, max, options); if (coding === "deflate") { try { - return await streamDecode(createInflate, input, max); + return await streamDecode(createInflate, input, max, options); } catch (err) { - if (err instanceof DecompressedTooLargeError) throw err; - return streamDecode(createInflateRaw, input, max); + if (err instanceof DecompressedTooLargeError || err instanceof DecodedRequestBusyError || options?.signal?.aborted) throw err; + return streamDecode(createInflateRaw, input, max, options); } } throw new Error(`unsupported request content-encoding: ${coding}`); @@ -70,7 +86,9 @@ export async function decodeRequestBody( contentEncoding: string | undefined, input: Buffer, maxOutputBytes: number, + options?: DecodeOptions, ): Promise<{ body: Buffer; decoded: boolean }> { + options?.signal?.throwIfAborted(); const codings = (contentEncoding ?? "") .split(",") .map((coding) => coding.trim().toLowerCase()) @@ -78,7 +96,7 @@ export async function decodeRequestBody( if (codings.length === 0) return { body: input, decoded: false }; let body = input; for (const coding of codings.reverse()) { - body = await decodeOne(coding, body, maxOutputBytes); + body = await decodeOne(coding, body, maxOutputBytes, options); if (body.byteLength > maxOutputBytes) throw new DecompressedTooLargeError(maxOutputBytes); } return { body, decoded: true }; diff --git a/src/fetch-util.ts b/src/fetch-util.ts index b3e8ec7d16..a2e8ec9d79 100644 --- a/src/fetch-util.ts +++ b/src/fetch-util.ts @@ -11,14 +11,15 @@ import { /** HTTP robustness helpers for the proxy. - readBody is capped: an unbounded request body is a memory-exhaustion - vector when the proxy listens publicly. 100 MB is generous for LLM - payloads (which can carry large tool results / file contents) while - still rejecting pathological sizes. + vector when the proxy listens publicly. Wire intake and rebuilt sends + stay capped at 100 MiB; recognized encoded requests have a separate, + bounded decode allowance before existing reductions run. - fetchWithTimeout wraps upstream requests with an AbortController so a stuck upstream cannot hold a client connection open forever. LLM streams can legitimately run for minutes, so the default is long. */ -export const MAX_REQUEST_BYTES = 100 * 1024 * 1024; +export { MAX_REQUEST_BYTES } from "./request-body-budget.js"; +import { checkOutboundBody } from "./request-body-budget.js"; export const UPSTREAM_TIMEOUT_MS = 12 * 60 * 1000; const liveUpstreamTimers = new Set>(); @@ -127,6 +128,7 @@ export async function fetchWithTimeout( timeoutMs?: number, externalSignal?: AbortSignal, ): Promise<{ response: Response; clearTimer: () => void; stopIdleTimer: () => void }> { + checkOutboundBody(opts.body); const effective = timeoutMs ?? upstreamTimeoutMs(); const controller = new AbortController(); let cleared = false; diff --git a/src/request-body-budget.ts b/src/request-body-budget.ts new file mode 100644 index 0000000000..acad1dcb46 --- /dev/null +++ b/src/request-body-budget.ts @@ -0,0 +1,39 @@ +export const MAX_REQUEST_BYTES = 100 * 1024 * 1024; +export const MAX_DECODED_REQUEST_BYTES = 2 * MAX_REQUEST_BYTES; +const MAX_LARGE_DECODED_REQUESTS = 2; +let largeDecodedRequests = 0; + +export class DecodedRequestBusyError extends Error { + constructor() { + super("large decoded request budget is busy; retry after an active request finishes"); + this.name = "DecodedRequestBusyError"; + } +} + +/** 只限制超过旧上限的解压请求;普通请求不占超大正文名额。 */ +export class DecodedRequestAdmission { + private admitted = false; + observe(bytes: number): void { + if (bytes <= MAX_REQUEST_BYTES || this.admitted) return; + if (largeDecodedRequests >= MAX_LARGE_DECODED_REQUESTS) throw new DecodedRequestBusyError(); + largeDecodedRequests++; + this.admitted = true; + } + release(): void { + if (!this.admitted) return; + largeDecodedRequests--; + this.admitted = false; + } +} + +export class OutboundBodyTooLargeError extends Error { + constructor(public readonly bytes: number) { + super(`rebuilt request exceeds ${MAX_REQUEST_BYTES} bytes (${bytes} bytes); reduce historical images or content before retrying`); + this.name = "OutboundBodyTooLargeError"; + } +} + +export function checkOutboundBody(body: unknown): void { + const bytes = typeof body === "string" ? Buffer.byteLength(body) : Buffer.isBuffer(body) ? body.byteLength : 0; + if (bytes > MAX_REQUEST_BYTES) throw new OutboundBodyTooLargeError(bytes); +} diff --git a/src/server/handle.ts b/src/server/handle.ts index e2fe46b99d..034b4aee59 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -11,7 +11,7 @@ import { FALLBACK_EFFECTIVE_WINDOW_FLOOR, findRoute, findRouteKey, lookupContext import { resolveProxyDecision } from "../upstream-proxy.js"; import { contextFromRegistry, peekRegistryContext, peekRegistryOutputLimit, peekRegistryPriceProfile } from "../registry.js"; import { codexAlignedWindow } from "../codex-models.js"; -import { MAX_REQUEST_BYTES } from "../fetch-util.js"; +import { DecodedRequestAdmission, DecodedRequestBusyError, MAX_DECODED_REQUEST_BYTES } from "../request-body-budget.js"; import { hostIdForLog, maskHeadersForLog, maskUrlForLog, maskUrlsInText } from "../log-mask.js"; import { buildIncomingImageIndex, foldAnchoredCutoff, pruneRetrieveImgExports } from "../image-restore.js"; import { durableMessageGuards } from "../durable-message-guards.js"; @@ -76,6 +76,33 @@ export async function handle( proxyWatchers: Set, initialWatcherPid: number | null, adminCtx: Parameters[2], +): Promise { + const admission = new DecodedRequestAdmission(); + const decodingAbort = new AbortController(); + const close = () => { if (!res.writableEnded) decodingAbort.abort(); }; + res.on("close", close); + try { + await handleRequest(req, res, opts, core, config, log, instanceId, instanceStartedAt, proxyWatchers, initialWatcherPid, adminCtx, admission, decodingAbort.signal); + } finally { + res.removeListener("close", close); + admission.release(); + } +} + +async function handleRequest( + req: http.IncomingMessage, + res: http.ServerResponse, + opts: ProxyOptions, + core: CompressionCore, + config: Config, + log: (level: string, msg: string) => void, + instanceId: string, + instanceStartedAt: number, + proxyWatchers: Set, + initialWatcherPid: number | null, + adminCtx: Parameters[2], + admission: DecodedRequestAdmission, + decodingSignal: AbortSignal, ): Promise { // #1440 P2 cut 1: the /__bili/* + /__acp/* management surface (security gates + // endpoint dispatch) moved to src/server/admin.ts behind the loopback + @@ -174,11 +201,11 @@ export async function handle( // (e.g. GET /models) must forward raw bytes without content-encoding decode. if (!passthroughMark && protocol !== null && bodyBuffer.length > 0) { try { - const decoded = await decodeRequestBody(headerValue(req, "content-encoding"), bodyBuffer, MAX_REQUEST_BYTES); + const decoded = await decodeRequestBody(headerValue(req, "content-encoding"), bodyBuffer, MAX_DECODED_REQUEST_BYTES, { signal: decodingSignal, onBytes: (bytes) => admission.observe(bytes) }); bodyBuffer = decoded.body; if (decoded.decoded) delete req.headers["content-encoding"]; } catch (decErr) { - if (decErr instanceof DecompressedTooLargeError) throw decErr; + if (decErr instanceof DecompressedTooLargeError || decErr instanceof DecodedRequestBusyError || decodingSignal.aborted) throw decErr; // #619: bili can't decode this content-encoding -> don't 400. Drop // protocol so the request falls to the verbatim passthrough below, // relaying the ORIGINAL still-encoded bytes (the reassignment never @@ -189,10 +216,17 @@ export async function handle( } } } catch (err) { + if (decodingSignal.aborted || res.destroyed) return; + if (err instanceof DecodedRequestBusyError) { + log("warn", "503: large decoded request budget is busy"); + res.writeHead(503, { "content-type": "application/json", "retry-after": "1" }); + res.end(JSON.stringify({ error: { type: "request_body_busy", stage: "decode", message: err.message } })); + return; + } if (err instanceof BodyTooLargeError || err instanceof DecompressedTooLargeError) { log("warn", `413: request body exceeds ${err.limit} bytes`); res.writeHead(413, { "content-type": "application/json" }); - res.end(JSON.stringify({ error: { type: "request_too_large", message: err.message } })); + res.end(JSON.stringify({ error: { type: "request_too_large", stage: err instanceof BodyTooLargeError ? "receive" : "decode", message: err.message } })); return; } log("warn", `failed to prepare inbound request (${String(err)}) - 400`); diff --git a/src/server/relay.ts b/src/server/relay.ts index 701338545c..f7400c99f3 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -1,3 +1,4 @@ +import { checkOutboundBody, OutboundBodyTooLargeError } from "../request-body-budget.js"; // #1440 P2 cut 4: upstream relay zone (forward) extracted verbatim from src/server.ts — pure move, zero behavior change. import http from "node:http"; @@ -267,6 +268,7 @@ export async function forward( log("debug", `[${prepared.session.id}] [chain] outbound stamping failed (${String(err)}); forwarding unstamped`); } } + if (res.destroyed || res.writableEnded) return; // #552: wire transform shared by ALL re-send paths (compress-retry loops // below) so re-sent bodies carry the same rewrite as the initial forward — // otherwise a developer-role 400 would hit mid-stream on the first retry. @@ -392,6 +394,18 @@ export async function forward( if (typeof wireBody === "string") wireBody = scrubLoneSurrogatesOnWire(wireBody); applyResign(headers, wireBody); } + try { + checkOutboundBody(wireBody); + } catch (err) { + if (!(err instanceof OutboundBodyTooLargeError)) throw err; + log("warn", `413: ${err.message}`); + if (res.headersSent) emitStreamError(res, prepared?.protocol ?? "openai", err.message, undefined, opts.streamErrorShape); + else { + res.writeHead(413, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { type: "request_too_large", stage: "forward", message: err.message } })); + } + return; + } const init: Omit & { dispatcher?: object } = { method: req.method ?? "GET", headers, diff --git a/tests/image-request-budget.test.ts b/tests/image-request-budget.test.ts new file mode 100644 index 0000000000..20735351eb --- /dev/null +++ b/tests/image-request-budget.test.ts @@ -0,0 +1,223 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { gzipSync } from "node:zlib"; +import { createCore, defaultConfig } from "acp-kernel"; +import { resolveUpstream, startServer } from "../src/server.ts"; +import { forward } from "../src/server/relay.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { DecodedRequestAdmission, MAX_REQUEST_BYTES } from "../src/request-body-budget.ts"; + +const isolation = mkdtempSync(join(tmpdir(), "bili-request-budget-")); +process.env.XDG_STATE_HOME = join(isolation, "state"); +process.env.XDG_DATA_HOME = join(isolation, "data"); +process.env.XDG_CACHE_HOME = join(isolation, "cache"); +process.env.XDG_CONFIG_HOME = join(isolation, "config"); +test.after(() => rmSync(isolation, { recursive: true, force: true })); + +async function fixture(run: (base: string, opts: ProxyOptions, calls: Buffer[]) => Promise): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const calls: Buffer[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + calls.push(Buffer.concat(chunks)); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "resp_test", status: "completed", output: [], usage: { input_tokens: 10, output_tokens: 1 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { + [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-5": { context: 400_000 } } }, + }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true, stripImages: true, stripImagesKeepRecent: 1 }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + passthroughSource: null, + autoUpdate: false, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: true, + releaseNotesCheck: true, + compat: { roles: {} }, + streamErrorShape: "protocol", + mitm: { enabled: false, domains: [] }, + }; + + const proxy = await startServer(opts); + if (!proxy.listening) await once(proxy, "listening"); + const base = `http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/http://127.0.0.1:${upstreamPort}`; + try { await run(base, opts, calls); } + finally { + proxy.closeAllConnections(); upstream.closeAllConnections(); + await Promise.all([new Promise(resolve => proxy.close(() => resolve())), new Promise(resolve => upstream.close(() => resolve()))]); + } +} + +const recentImage = { type: "input_image", image_url: "data:image/png;base64,QUJD", detail: "high" }; +const recent = { type: "message", role: "user", content: [{ type: "input_text", text: "Continue" }, recentImage] }; + +function largeBody(tool: boolean, extraBytes = 1024): Buffer { + const image = { ...recentImage, image_url: "data:image/png;base64," + "QUJD".repeat((MAX_REQUEST_BYTES + extraBytes) / 4) }; + const old = tool + ? [{ type: "custom_tool_call", call_id: "call_img", name: "screenshot", input: "{}" }, { type: "custom_tool_call_output", call_id: "call_img", output: [{ type: "input_text", text: "old screenshot" }, image] }] + : [{ type: "message", role: "user", content: [{ type: "input_text", text: "old screenshot" }, image] }]; + const raw = Buffer.from(JSON.stringify({ model: "gpt-5", stream: false, input: [...old, recent] })); + assert.ok(raw.length > MAX_REQUEST_BYTES); + return gzipSync(raw); +} + +async function send(base: string, body: Buffer, session: string, headers: Record = {}): Promise { + return fetch(`${base}/responses`, { method: "POST", headers: { "content-type": "application/json", "content-encoding": "gzip", "session-id": session, ...headers }, body: new Uint8Array(body) }); +} + +test("gzip history above 100 MiB reaches explicit image reduction and preserves recent images, including tool screenshots", async () => { + await fixture(async (base, _opts, calls) => { + for (const tool of [false, true]) { + const r = await send(base, largeBody(tool), `large-strip-${tool}`); + assert.equal(r.status, 200, await r.text()); + const body = JSON.parse(calls.at(-1)!.toString("utf8")) as { input: unknown[] }; + const text = JSON.stringify(body.input); + assert.equal(text.split('"input_image"').length - 1, 1); + assert.ok(text.includes("old screenshot")); + assert.ok(text.includes(recentImage.image_url)); + assert.ok(calls.at(-1)!.length < MAX_REQUEST_BYTES); + } + assert.equal(calls.length, 2); + }); +}); + +test("disabled stripping never silently drops images: rebuilt over-budget requests stop with a forward-stage 413", async () => { + await fixture(async (base, opts, calls) => { + opts.compress.stripImages = false; + const r = await send(base, largeBody(false), "large-no-strip"); + assert.equal(r.status, 413); + const error = await r.json() as { error: { stage: string; type: string } }; + assert.equal(error.error.stage, "forward"); + assert.equal(error.error.type, "request_too_large"); + assert.equal(calls.length, 0); + }); +}); + +test("third large decode gets retryable admission error, then succeeds after a slot is released", async () => { + await fixture(async (base, _opts, calls) => { + const a = new DecodedRequestAdmission(), b = new DecodedRequestAdmission(); + const compressed = largeBody(false); + a.observe(MAX_REQUEST_BYTES + 1); b.observe(MAX_REQUEST_BYTES + 1); + try { + const r = await send(base, compressed, "busy-strip"); + assert.equal(r.status, 503); + assert.equal(r.headers.get("retry-after"), "1"); + assert.equal((await r.json() as { error: { stage: string } }).error.stage, "decode"); + assert.equal(calls.length, 0); + const small = await send(base, gzipSync(Buffer.from(JSON.stringify({ model: "gpt-5", input: [recent] }))), "small-during-busy"); + assert.equal(small.status, 200, await small.text()); + assert.equal(calls.length, 1, "normal traffic must not use the large-body slots"); + a.release(); + const ok = await send(base, compressed, "busy-strip-retry"); + assert.equal(ok.status, 200, await ok.text()); + assert.equal(calls.length, 2); + } finally { a.release(); b.release(); } + }); +}); + + +test("decompression bombs and malformed oversized JSON cannot escape through raw forwarding", async () => { + await fixture(async (base, _opts, calls) => { + for (const [bytes, stage] of [[2 * MAX_REQUEST_BYTES + 1, "decode"], [MAX_REQUEST_BYTES + 1, "forward"]] as const) { + const r = await send(base, gzipSync(Buffer.alloc(bytes, 65)), `oversize-${stage}`); + assert.equal(r.status, 413); + assert.equal((await r.json() as { error: { stage: string } }).error.stage, stage); + assert.equal(calls.length, 0); + } + const ok = await send(base, gzipSync(Buffer.from(JSON.stringify({ model: "gpt-5", input: [recent] }))), "after-size-error"); + assert.equal(ok.status, 200, await ok.text()); + assert.equal(calls.length, 1); + }); +}); + +test("disconnect during a large decode cancels before forwarding and releases the large-request slot", async () => { + await fixture(async (base, _opts, calls) => { + const compressed = largeBody(false, 12 * 1024 * 1024); + const observe = DecodedRequestAdmission.prototype.observe; + const release = DecodedRequestAdmission.prototype.release; + let observed: DecodedRequestAdmission | undefined; + let cancelled = false; + let client: http.ClientRequest; + let done: () => void = () => {}; + const finished = new Promise(resolve => { done = resolve; }); + DecodedRequestAdmission.prototype.observe = function(bytes) { + observe.call(this, bytes); + if (bytes > MAX_REQUEST_BYTES && !cancelled) { + observed = this; + cancelled = true; + client.destroy(); + } + }; + DecodedRequestAdmission.prototype.release = function() { + release.call(this); + if (this === observed) done(); + }; + const timeout = setTimeout(done, 30_000); + try { + client = http.request(`${base}/responses`, { method: "POST", headers: { "content-encoding": "gzip", "session-id": "abort-large", "content-type": "application/json" } }); + client.on("error", () => {}); + client.end(compressed); + await finished; + assert.ok(cancelled, "the decode must actually cross the large-request threshold"); + assert.equal(calls.length, 0); + const a = new DecodedRequestAdmission(), b = new DecodedRequestAdmission(); + try { a.observe(MAX_REQUEST_BYTES + 1); b.observe(MAX_REQUEST_BYTES + 1); } + finally { a.release(); b.release(); } + } finally { + clearTimeout(timeout); + DecodedRequestAdmission.prototype.observe = observe; + DecodedRequestAdmission.prototype.release = release; + } + }); +}); + + +test("an oversized rebuilt body reports an in-band error after SSE headers are committed, without contacting upstream", async () => { + await fixture(async (base, opts, calls) => { + const core = createCore(); + const server = http.createServer((req, res) => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.flushHeaders(); + void forward(req, res, opts, Buffer.alloc(MAX_REQUEST_BYTES + 1), null, core, defaultConfig(400_000), () => {}, resolveUpstream(opts, req.url ?? "", req), "late-budget").catch(err => res.destroy(err)); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + try { + const port = (server.address() as { port: number }).port; + const r = await fetch(`http://127.0.0.1:${port}${new URL(base).pathname}/responses`, { method: "POST" }); + assert.equal(r.status, 200, "committed headers cannot be changed to 413"); + const text = await r.text(); + assert.match(text, /rebuilt request exceeds 104857600 bytes/); + assert.match(text, /error/); + assert.equal(calls.length, 0); + } finally { + server.closeAllConnections(); + await new Promise(resolve => server.close(() => resolve())); + } + }); +}); diff --git a/tests/request-body-budget.test.ts b/tests/request-body-budget.test.ts new file mode 100644 index 0000000000..78f115faab --- /dev/null +++ b/tests/request-body-budget.test.ts @@ -0,0 +1,46 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { gzipSync, deflateRawSync, brotliCompressSync } from "node:zlib"; +import { decodeRequestBody, DecompressedTooLargeError } from "../src/content-encoding.ts"; +import { checkOutboundBody, DecodedRequestAdmission, DecodedRequestBusyError, MAX_REQUEST_BYTES, MAX_DECODED_REQUEST_BYTES, OutboundBodyTooLargeError } from "../src/request-body-budget.ts"; +import { fetchWithTimeout, _liveUpstreamTimersForTest } from "../src/fetch-util.ts"; + +test("large decoded admission keeps normal requests free, limits two large requests, and releases idempotently", () => { + const a = new DecodedRequestAdmission(), b = new DecodedRequestAdmission(), c = new DecodedRequestAdmission(); + try { + c.observe(MAX_REQUEST_BYTES); + a.observe(MAX_REQUEST_BYTES + 1); + a.observe(MAX_DECODED_REQUEST_BYTES); + b.observe(MAX_REQUEST_BYTES + 1); + assert.throws(() => c.observe(MAX_REQUEST_BYTES + 1), DecodedRequestBusyError); + a.release(); a.release(); + c.observe(MAX_REQUEST_BYTES + 1); + assert.throws(() => a.observe(MAX_REQUEST_BYTES + 1), DecodedRequestBusyError); + } finally { a.release(); b.release(); c.release(); } +}); + +test("every stacked decode retains its hard cap and resource failures do not fall back to raw deflate", async () => { + const raw = Buffer.alloc(8192, 65); + for (const [encoding, input] of [["gzip", gzipSync(raw)], ["gzip, br", brotliCompressSync(gzipSync(raw))], ["deflate", deflateRawSync(raw)]] as const) { + await assert.rejects(decodeRequestBody(encoding, input, 4096), DecompressedTooLargeError); + await assert.rejects(decodeRequestBody(encoding, input, 16_384, { onBytes: () => { throw new DecodedRequestBusyError(); } }), DecodedRequestBusyError); + } +}); + +test("decoding stops on client cancellation, including a pre-aborted signal", async () => { + const ac = new AbortController(); + const input = gzipSync(Buffer.alloc(100_000, 65)); + let chunks = 0; + await assert.rejects(decodeRequestBody("gzip", input, 200_000, { signal: ac.signal, onBytes: () => { chunks++; ac.abort(); } }), { name: "AbortError" }); + assert.equal(chunks, 1); + await assert.rejects(decodeRequestBody("gzip", input, 200_000, { signal: ac.signal }), { name: "AbortError" }); +}); + +test("outbound budget counts UTF-8 bytes and refuses before fetch or upstream watchdog allocation", async () => { + const unicode = "界".repeat(Math.floor(MAX_REQUEST_BYTES / 3) + 1); + assert.throws(() => checkOutboundBody(unicode), OutboundBodyTooLargeError); + const timers = _liveUpstreamTimersForTest(); + await assert.rejects(fetchWithTimeout("http://127.0.0.1:1", { method: "POST", body: unicode }), OutboundBodyTooLargeError); + assert.equal(_liveUpstreamTimersForTest(), timers); + checkOutboundBody(Buffer.alloc(1)); +});