Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion js/flate/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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/);
});
43 changes: 20 additions & 23 deletions js/flate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}. */
Expand Down Expand Up @@ -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);
};
Expand All @@ -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 = [];
}
}
}
Loading