From 7f51a2ce3770633f5a473f7a8e5c58e28f7315da Mon Sep 17 00:00:00 2001 From: "MK (fengmk2)" Date: Fri, 13 Nov 2026 13:25:24 +0800 Subject: [PATCH] fix(multipart): stream part bodies past rejected boundary prefixes A boundary prefix followed by padding and a non-CRLF byte is body data, but the part body generator searched the whole buffer for the next prefix before it yielded anything. A body that repeats such prefixes was held in memory up to the 72 MiB store limit, with a full buffer copy on every 64 KiB read, and the metadata and blob size limits never ran. Yield through the first byte of each rejected prefix before searching again. No delimiter can start earlier, so the yielded bytes are the same; only the chunking changes. Co-Authored-By: Claude Opus 5.5 --- src/multipart.ts | 9 ++++++--- test/protocol.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/multipart.ts b/src/multipart.ts index cacbdd6..0f60283 100644 --- a/src/multipart.ts +++ b/src/multipart.ts @@ -76,8 +76,8 @@ export async function* multipart( let consumed = false; async function* body(): AsyncGenerator { while (true) { - let index = input.buffer.indexOf(delimiter); - while (index >= 0) { + const index = input.buffer.indexOf(delimiter); + if (index >= 0) { const match = await delimiterEnd(input, index + delimiter.length, headerLimit); if (match) { if (index) yield input.take(index); @@ -86,7 +86,10 @@ export async function* multipart( consumed = true; return; } - index = input.buffer.indexOf(delimiter, index + 1); + // The rejected prefix is body data and no delimiter starts before it. Yield through its + // first byte now, so a run of padded prefixes can't hold the body in the buffer. + yield input.take(index + 1); + continue; } const safe = input.buffer.length - delimiter.length - 2; if (safe > 0) yield input.take(safe); diff --git a/test/protocol.test.ts b/test/protocol.test.ts index b434388..e4d8cd6 100644 --- a/test/protocol.test.ts +++ b/test/protocol.test.ts @@ -102,6 +102,46 @@ test('multipart accepts every split point, either part order, and boundary-like } }); +test('multipart streams a part whose bytes repeat padded boundary prefixes', async () => { + // Each prefix is followed by padding up to the header limit and then a non-CRLF byte, so it + // stays body data. The parser must still yield as it reads instead of buffering the whole body. + const prefix = '\r\n--b' + ' '.repeat(8186) + 'X'; + const value = prefix.repeat(128); + const data = Buffer.from( + 'preamble\r\n--b\r\nContent-Disposition: form-data; name="metadata"\r\nContent-Type: application/cbor\r\n\r\n' + + value + + '\r\n--b--\r\n', + ); + let offset = 0; + const stream = new ReadableStream({ + pull(controller) { + if (offset >= data.length) return controller.close(); + controller.enqueue(data.subarray(offset, offset + 65536)); + offset += 65536; + }, + }); + const deadline = new Deadline(5000); + const input = new Input(stream, data.length, deadline); + let largest = 0; + const fill = input.fill.bind(input); + input.fill = async () => { + const more = await fill(); + largest = Math.max(largest, input.buffer.length); + return more; + }; + try { + const chunks: Uint8Array[] = []; + for await (const part of multipart(input, 'b', 8192)) + for await (const chunk of part.body) chunks.push(chunk); + assert.equal(Buffer.concat(chunks).toString(), value); + assert.ok(chunks.length > 1, `expected several chunks, received ${chunks.length}`); + assert.ok(largest < 128 * 1024, `buffer reached ${largest} bytes`); + } finally { + deadline.dispose(); + input.close(); + } +}); + test('a cancelled response cancels the value being copied and the ones after it', async () => { let copying = false, later = false;