fix: reject a decoded array length that exceeds the stream - #68
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An
Arrayin 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:lengthcomes 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.restructureis the decode engine underfontkit, so a schema with a count-prefixed array of a NUL-terminated string (the defaultnew String(), whose element is zero-width at EOF and so never fast-fails) is a reachable, idiomatic surface.Repro
Heap grows linearly with the declared count; at
uint32max it is unbounded. (Numeric-element arrays happen to self-limit because aDataViewread throws past EOF, so the reachable shape is asubarray/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
eofandlengthInBytesmodes are untouched, and the guard never fires on valid data.Test
Adds a case: a
uint32count of 100000 with one payload byte must throw. It fails onmaster("Missing expected exception" — the decoder runs the full loop) and passes with the fix. Full suite: 243 passing.