Skip to content

Do not read CBOR 32-bit lengths >= 2^31 as indefinite length - #912

Open
Dhanu-dynamic wants to merge 1 commit into
FasterXML:2.21from
Dhanu-dynamic:2.21/cbor-32bit-length-sign
Open

Dhanu-dynamic wants to merge 1 commit into
FasterXML:2.21from
Dhanu-dynamic:2.21/cbor-32bit-length-sign

Conversation

@Dhanu-dynamic

Copy link
Copy Markdown
Contributor

CBORParser._decodeExplicitLength() returns the 4-byte length form (additional info 26) straight from _decode32Bits(), so a definite length of 2^31 or more wraps to a negative int. Every read-path caller takes a negative length to mean indefinite length, so the item is read as an indefinite-length one, up to a 0xFF break. 9A FF FF FF FF 01 02 FF (an array of 4294967295 items) reads as [1, 2], 7A FF FF FF FF 63 61 62 63 FF as "abc", and the same happens for maps, byte strings and text or binary property names, through nextToken(), nextFieldName(), nextTextValue(), getText() and getBinaryValue(). A decoder following RFC 8949 expects 4294967295 items or bytes there and fails, so Jackson accepts a malformed document and returns a structure the bytes do not encode.

The 8-byte form (additional info 27) already rejects anything above Integer.MAX_VALUE with "Illegal length", and for text and binary values _skipIncomplete() / _skipChunked() already reject these 32-bit lengths through _skipBytes(); only the read path let them through. Integer values have decoded the 4-byte form as unsigned since #30. The check goes in _decodeExplicitLength() so it covers every caller. Jackson's own generator never writes such lengths, since no Java array or String can be that long.

Explicit32BitLengthTest covers arrays, maps, text, binary and property names: 5 of its 6 tests fail without the change, and the sixth checks that valid 4-byte lengths still read. ./mvnw verify passes on JDK 21. Based on 2.21; 3.x has the same _decodeExplicitLength(), so it should merge forward, with the test's package changed to tools.jackson. I can add the VERSION-2.x entry once this has a number.

_decodeExplicitLength() returned the 4-byte length form as a signed int,
so a definite length of 2^31 or more came back negative, which callers
take to mean indefinite length: a definite-length array, map, text, byte
string or property name was then read up to a 0xFF break instead. Reject
such lengths like the 8-byte form already does.
@cowtowncoder cowtowncoder changed the title (cbor) Do not read 32-bit lengths >= 2^31 as indefinite length Do not read CBOR 32-bit lengths >= 2^31 as indefinite length Nov 22, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants