Skip to content

fix: reject a decoded array length that exceeds the stream - #68

Merged
blikblum merged 1 commit into
foliojs:masterfrom
spokodev:fix/array-length-exceeds-stream
Sep 3, 2026
Merged

fix: reject a decoded array length that exceeds the stream#68
blikblum merged 1 commit into
foliojs:masterfrom
spokodev:fix/array-length-exceeds-stream

Conversation

@spokodev

@spokodev spokodev commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

An Array in count mode reads its length from a preceding field and then loops that many times, pushing a decoded element each iteration, with no check that the count fits the stream:

} else {
  for (let i = 0, end = length; i < end; i++) {
    res.push(this.type.decode(stream, ctx));
  }
}

length comes from the input, so a crafted buffer that declares a huge count followed by little payload forces both the loop and the result array to grow far past the input — exhausting memory from a handful of bytes. restructure is the decode engine under fontkit, so a schema with a count-prefixed array of a NUL-terminated string (the default new String(), whose element is zero-width at EOF and so never fast-fails) is a reachable, idiomatic surface.

Repro

const array = new r.Array(new r.String(), r.uint32);
array.fromBuffer(new Uint8Array([0x00, 0x01, 0x86, 0xa0, 0x00])); // count=100000, one byte of payload

Heap grows linearly with the declared count; at uint32 max it is unbounded. (Numeric-element arrays happen to self-limit because a DataView read throws past EOF, so the reachable shape is a subarray/pointer/string element.)

Fix

Every element consumes at least one byte, so a count larger than the bytes remaining in the stream is unsatisfiable — reject it before looping. The eof and lengthInBytes modes are untouched, and the guard never fires on valid data.

Test

Adds a case: a uint32 count of 100000 with one payload byte must throw. It fails on master ("Missing expected exception" — the decoder runs the full loop) and passes with the fix. Full suite: 243 passing.

An array in count mode reads its length from a preceding field and then
loops that many times, pushing a decoded element each iteration, with no
check that the count fits the stream. A crafted buffer that declares a
huge count (e.g. a uint32 length followed by little payload) forces the
loop and the result array to grow far past the input, exhausting memory
from a handful of bytes. restructure is the decode engine under fontkit,
so a schema with a count-prefixed array of a NUL-terminated string (the
default String) is a reachable, idiomatic surface.

Each element consumes at least one byte, so a count larger than the
bytes remaining in the stream is unsatisfiable; reject it before looping.
The EOF/lengthInBytes-driven modes are unchanged.
@blikblum
blikblum merged commit 4c77b1a into foliojs:master Sep 3, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants