fix(decoder): limit the nesting depth of the decoded data - #143
Merged
Conversation
The decoder processed nested data items recursively without any bound. A payload made only of nested arrays (one byte per level) was enough to build an object graph deep enough to crash the process when it was released, after decode() had returned successfully. Nested lists, maps, tags and indefinite length containers now count towards a maximum nesting depth. Data nested deeper than that limit is rejected with an InvalidArgumentException. The limit defaults to Decoder::DEFAULT_MAX_DEPTH (1000 levels) and can be changed with the third argument of Decoder::create(). Reported by Ivan Tse (https://github.com/ivantsepp).
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.
Problem
CBOR\Decoderprocessed nested data items recursively without any bound. A payload made only of nested arrays costs one byte per level (0x81), so ~44 KB of input is enough to build an object graph deep enough to crash the process — a segmentation fault, not a catchable error.The crash does not happen during parsing:
decode()returns successfully, and the process dies later, when the graph is released, because the destructor chain is recursive on the C stack. The threshold follows the stack size (measured: ~1 600 levels with a 256 KB stack, ~4 000 with 512 KB, ~40 000 with the default 8 MB), so a worker with a reduced stack crashes on a much smaller payload.Nested arrays are not the only vector: nested maps, tag chains (
0xcbrepeated) and indefinite length containers are all one byte per level too.Fix
Nesting depth is now tracked while processing and checked on every nested item — lists, maps, tags and indefinite length containers alike. Data nested deeper than the limit is rejected with an
InvalidArgumentExceptioninstead of building the graph:The limit defaults to
Decoder::DEFAULT_MAX_DEPTH(1000 levels) and can be lowered — recommended when the data comes from an untrusted source:Backward compatible: the depth is a new optional third argument of
Decoder::__construct()/Decoder::create(),DecoderInterfaceis unchanged, and 1000 levels is far beyond what COSE, CWT or WebAuthn data ever reaches.Tests
tests/MaxDepthTest.phpcovers the limit being reached, exceeded, and configured, each of the four nesting vectors, and the rejection of a limit lower than 1.Full suite: 497 tests, 1103 assertions, ECS and parallel-lint clean. PHPStan reports 4 errors on
NegativeIntegerObject/UnsignedIntegerObject, all pre-existing on3.3.xand unrelated to this change;Decoder.phpis clean.Reported by Ivan Tse.