Skip to content
This repository was archived by the owner on Oct 23, 2026. It is now read-only.
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
19 changes: 19 additions & 0 deletions src/lib/bspatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ const TRDIFF10_MAGIC = "TRDIFF10";
/** Header size in bytes (magic + 3 × i64) */
const HEADER_SIZE = 32;

/**
* Upper bound on the declared output size (`newSize`) a patch may claim.
*
* The header's `newSize` is attacker-controlled and used to preallocate the
* output buffer BEFORE the patch content is verified, so an unbounded value
* would let a malicious or corrupt patch force an out-of-memory allocation.
* 2 GiB is far above any realistic binary (sentry-cli binaries are ~30 MB);
* legitimate patched outputs never approach it.
*/
export const MAX_OUTPUT_SIZE = 2_147_483_648; // 2 GiB

/** Parsed TRDIFF10 header fields */
export type PatchHeader = {
/** Compressed size of the control block (bytes) */
Expand Down Expand Up @@ -114,6 +125,14 @@ export function parsePatchHeader(patch: Uint8Array): PatchHeader {
throw new Error("Corrupt patch: negative length in header");
}

// Bound the attacker-controlled newSize before it is used to preallocate
// the output buffer (applyReaderToMemory) — see MAX_OUTPUT_SIZE.
if (newSize > MAX_OUTPUT_SIZE) {
throw new Error(
`Corrupt patch: declared output size ${newSize} exceeds maximum ${MAX_OUTPUT_SIZE} bytes`
);
}

const totalCompressed = HEADER_SIZE + controlLen + diffLen;
if (totalCompressed > patch.byteLength) {
throw new Error(
Expand Down
25 changes: 25 additions & 0 deletions test/lib/bspatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
applyPatch,
applyPatchChainInMemory,
applyPatchToMemory,
MAX_OUTPUT_SIZE,
parsePatchHeader,
} from "../../src/lib/bspatch.js";

Expand Down Expand Up @@ -149,6 +150,30 @@ describe("parsePatchHeader: fixtures", () => {
buf[15] = 0x80; // Set sign bit → negative controlLen
expect(() => parsePatchHeader(buf)).toThrow("negative");
});

test("rejects an attacker-controlled newSize above MAX_OUTPUT_SIZE", () => {
// The header's newSize is used to preallocate the output buffer before the
// patch content is verified. An unbounded value lets a malicious/corrupt
// patch force an OOM via `new Uint8Array(newSize)` in applyReaderToMemory.
// Regression test: a header claiming newSize > MAX_OUTPUT_SIZE must be
// rejected here, before any allocation.
const buf = new Uint8Array(64);
buf.set(new TextEncoder().encode("TRDIFF10"));
writeI64LE(buf, 8, 0); // controlLen = 0
writeI64LE(buf, 16, 0); // diffLen = 0
writeI64LE(buf, 24, MAX_OUTPUT_SIZE + 1); // newSize just over the cap
expect(() => parsePatchHeader(buf)).toThrow("exceeds maximum");
});

test("accepts a newSize at MAX_OUTPUT_SIZE (boundary)", () => {
const buf = new Uint8Array(64);
buf.set(new TextEncoder().encode("TRDIFF10"));
writeI64LE(buf, 8, 0);
writeI64LE(buf, 16, 0);
writeI64LE(buf, 24, MAX_OUTPUT_SIZE); // exactly the cap — allowed
const header = parsePatchHeader(buf);
expect(header.newSize).toBe(MAX_OUTPUT_SIZE);
});
});

describe("applyPatch", () => {
Expand Down
Loading