Skip to content

Bound preallocation by available input to prevent OOM (#119) - #266

Merged
c-rack merged 1 commit into
c-rack:masterfrom
dngr2:bound-preallocation-by-available-input
Aug 17, 2026
Merged

Bound preallocation by available input to prevent OOM (#119)#266
c-rack merged 1 commit into
c-rack:masterfrom
dngr2:bound-preallocation-by-available-input

Conversation

@dngr2

@dngr2 dngr2 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #119.

A crafted CBOR message can declare a huge array/map/string length, and the decoder preallocated a collection or buffer of that size before reading any data. So a 5-byte message like 9A 7F FF FF FF — an array claiming 2,147,483,647 elements — triggered an OutOfMemoryError.

getPreallocationSize now also caps the size at InputStream.available(), so preallocation can never exceed what the message can actually provide. Legitimate data is unaffected (the bytes are present, so available() ≥ the length); a forged length allocates nothing and fails with the normal end-of-stream CborException. This applies by default, without needing setMaxPreallocationSize.

Updated the existing forged-length test (which previously expected the OutOfMemoryError) and added array/map cases.

A decoder preallocated a collection or buffer of the declared length before
reading any data, so a small crafted message with a huge declared length --
e.g. an array header claiming 2^31-1 elements -- triggered an OutOfMemoryError.
Cap preallocation at InputStream.available() so it can never exceed what the
message can actually provide.
@c-rack
c-rack merged commit f054284 into c-rack:master Aug 17, 2026
@c-rack

c-rack commented Aug 17, 2026

Copy link
Copy Markdown
Owner

 Thanks @dngr2 !
 
 ❤️

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.

The CBOR library is sensitive to a DOS attack

2 participants