diff --git a/js/flate/src/index.test.ts b/js/flate/src/index.test.ts index b4f5d11ea94..9dedeed444f 100644 --- a/js/flate/src/index.test.ts +++ b/js/flate/src/index.test.ts @@ -1,5 +1,6 @@ -import { expect, test } from "bun:test"; +import { expect, spyOn, test } from "bun:test"; import { Deflate, Inflate } from "fflate"; +import { Inflate as PakoInflate } from "pako"; import { DEFAULT_MAX_FRAME_SIZE, Decoder, Encoder } from "./index.ts"; const enc = new TextEncoder(); @@ -149,3 +150,61 @@ test("pako round-trips a stream that fflate's flush corrupts", () => { // fflate's streaming flush does not round-trip the same stream our pako codec handles. expect(fflateRoundTrips(frames)).toBe(false); }); + +test("inflation stops at the frame cap plus one scratch chunk", () => { + const cap = 1024; + const payload = new Uint8Array(1024 * 1024); + const slice = new Encoder().frame(payload); + const push = PakoInflate.prototype.push; + let output = 0; + let scratch = 0; + const pushing = spyOn(PakoInflate.prototype, "push").mockImplementation(function (this: PakoInflate, ...args) { + const onData = this.onData; + this.onData = (chunk) => { + output += chunk.length; + scratch = Math.max(scratch, chunk.length); + onData.call(this, chunk); + }; + try { + return push.apply(this, args); + } finally { + this.onData = onData; + } + }); + try { + const decoder = new Decoder({ maxFrameSize: cap }); + expect(() => decoder.frame(slice)).toThrow(/exceeded 1024/); + expect(output).toBeGreaterThan(cap); + expect(output).toBeLessThanOrEqual(cap + scratch); + expect(output).toBeLessThan(payload.length); + expect(() => decoder.frame(slice)).toThrow(/exceeded 1024/); + expect(() => decoder.frame(new Uint8Array())).toThrow(/exceeded 1024/); + } finally { + pushing.mockRestore(); + } +}); + +test("retained frames own tight buffers across later pushes and scratch boundaries", () => { + const encoder = new Encoder(); + const decoder = new Decoder(); + const frames = [1, 7, 65_535, 65_536, 65_537, 0].map((size, n) => new Uint8Array(size).fill(n)); + // Even an empty slice can be a view into a larger incoming allocation. + frames[5] = new Uint8Array(65_536).subarray(0, 0); + const retained = frames.map((frame) => decoder.frame(encoder.frame(frame))); + decoder.frame(encoder.frame(new Uint8Array(128 * 1024).fill(0xff))); + for (const [n, frame] of retained.entries()) { + expect(frame).toEqual(frames[n]); + expect(frame.buffer.byteLength).toBe(frame.byteLength); + } +}); + +test("each frame has an independent cap and accepts its exact limit", () => { + const cap = 65_536; + const encoder = new Encoder(); + const decoder = new Decoder({ maxFrameSize: cap }); + for (const value of [1, 2]) { + const payload = new Uint8Array(cap).fill(value); + expect(decoder.frame(encoder.frame(payload))).toEqual(payload); + } + expect(() => decoder.frame(encoder.frame(new Uint8Array(cap + 1)))).toThrow(/exceeded 65536/); +}); diff --git a/js/flate/src/index.ts b/js/flate/src/index.ts index 9aa91ed9a64..5b68d4beb06 100644 --- a/js/flate/src/index.ts +++ b/js/flate/src/index.ts @@ -35,19 +35,6 @@ export const DEFAULT_MAX_FRAME_SIZE = 64 * 1024 * 1024; // The trailing bytes of a DEFLATE sync flush, stripped on the wire and re-appended to decode. const SYNC_FLUSH_TAIL = new Uint8Array([0x00, 0x00, 0xff, 0xff]); -// Concatenate chunks into one tight buffer (a single chunk passes through untouched). Safe only for -// output that is consumed before the next push, since a single chunk is pako's own reused buffer. -function concat(chunks: Uint8Array[], total: number): Uint8Array { - if (chunks.length === 1) return chunks[0]; - const out = new Uint8Array(total); - let offset = 0; - for (const chunk of chunks) { - out.set(chunk, offset); - offset += chunk.length; - } - return out; -} - /** Options for an {@link Encoder}. */ export interface EncoderOptions { /** DEFLATE level, `0..=9` (higher is smaller and slower). Defaults to {@link DEFAULT_LEVEL}. */ @@ -137,10 +124,10 @@ export class Decoder { const bytes = chunk as Uint8Array; this.#total += bytes.length; // Bound the inflated output as it is produced; a tiny slice can expand enormously. Stop - // retaining past the cap, then reject once the push returns. + // inside push rather than inflating the rest of a hostile slice. if (this.#total > this.#maxFrameSize) { this.#tooLarge = true; - return; + throw new Error(`decompressed frame exceeded ${this.#maxFrameSize} bytes`); } this.#chunks.push(bytes); }; @@ -151,19 +138,29 @@ export class Decoder { * the input is malformed or inflates past the per-frame size cap. */ frame(slice: Uint8Array): Uint8Array { - if (slice.length === 0) return slice; + if (this.#tooLarge) throw new Error(`decompressed frame exceeded ${this.#maxFrameSize} bytes`); + if (slice.length === 0) return new Uint8Array(); this.#chunks = []; this.#total = 0; - this.#tooLarge = false; // Feed the slice then the re-appended sync-flush marker as two pushes, so no combined buffer is // allocated. The marker delimits the frame and flushes its last bytes out of the inflate buffer. - this.#inflate.push(slice, false); - this.#inflate.push(SYNC_FLUSH_TAIL, pako.Z_SYNC_FLUSH); - if (this.#inflate.err) throw new Error(`decompression failed: ${this.#inflate.msg}`); - if (this.#tooLarge) throw new Error(`decompressed frame exceeded ${this.#maxFrameSize} bytes`); - - return concat(this.#chunks, this.#total); + try { + this.#inflate.push(slice, false); + this.#inflate.push(SYNC_FLUSH_TAIL, pako.Z_SYNC_FLUSH); + if (this.#inflate.err) throw new Error(`decompression failed: ${this.#inflate.msg}`); + + // Retained frames must own tight buffers rather than pinning pako's scratch storage. + const out = new Uint8Array(this.#total); + let offset = 0; + for (const chunk of this.#chunks) { + out.set(chunk, offset); + offset += chunk.length; + } + return out; + } finally { + this.#chunks = []; + } } }