fix(binarycodec): guard BinaryParser::read bounds + limit STObject decode depth - #344
Open
e-desouza wants to merge 1 commit into
Open
fix(binarycodec): guard BinaryParser::read bounds + limit STObject decode depth#344e-desouza wants to merge 1 commit into
e-desouza wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #344 +/- ##
=======================================
Coverage 86.71% 86.72%
=======================================
Files 252 252
Lines 32630 32676 +46
=======================================
+ Hits 28296 28339 +43
- Misses 4334 4337 +3
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR hardens the XRPL binary decoder against malformed or adversarial inputs by preventing panics in BinaryParser::read and bounding recursive decoding of nested STObject/STArray structures.
Changes:
- Added an explicit bounds check in
BinaryParser::read(n)to return an error instead of panicking on out-of-bounds slices. - Introduced a maximum decode depth (
MAX_DECODE_DEPTH) and a newMaxDecodeDepthExceedederror to prevent stack overflows from deeply nested payloads. - Updated decoding helpers to thread a
depthcounter through recursive calls, and added regression tests for both failure modes.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/core/binarycodec/mod.rs |
Updates the decode() entrypoint to initialize recursive decoding with depth = 0. |
src/core/binarycodec/exceptions.rs |
Adds MaxDecodeDepthExceeded to the binary codec exception set for depth-limit enforcement. |
src/core/binarycodec/binary_wrappers.rs |
Implements read() bounds guard, adds decode depth limit enforcement, threads depth through decode functions, and adds regression tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…rsion depth limit OOBIDX-001: BinaryParser::read() sliced self.0[..n] before checking bounds, causing a panic on truncated input. Now checks n > self.0.len() first and returns UnexpectedParserSkipOverflow, consistent with skip_bytes(). RECURSEDES-001: decode_st_object / decode_field_value / decode_st_array were mutually recursive with no depth limit. A crafted payload with > 32 levels of nested STObject/STArray fields would exhaust the stack (uncatchable SIGSEGV). Adds MAX_DECODE_DEPTH = 32 and a new MaxDecodeDepthExceeded error variant; depth is threaded through all three functions and checked at entry to decode_st_object. The public decode() entry point passes depth 0. Two regression tests added: one asserts the exact UnexpectedParserSkipOverflow error on read() with 10 bytes requested from a 2-byte buffer; the other constructs 34 nested FinalFields (0xE7) headers and asserts MaxDecodeDepthExceeded is returned rather than a stack overflow.
e-desouza
force-pushed
the
fix/binary-wrappers-panics
branch
from
July 15, 2026 16:28
778d2ed to
f56a3ed
Compare
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.
Why
BinaryParser::read(n)slicedself.0[..n]before callingskip_bytes(n), which is where the bounds check lived. On a truncated binary XRPL blob,readpanicked instead of returning an error — reachable from any network-sourced binary data viadecode().decode_st_objectanddecode_field_valueform a mutually recursive pair with no depth limit. A crafted binary blob embedding deeply nested STObject headers (~4–16 KB) overflows the stack. Stack overflows abort the process unconditionally — they are not catchable withcatch_unwind.What changed
BinaryParser::read: addedif n > self.0.len()guard returningUnexpectedParserSkipOverflow { max, found }before the slice.const MAX_DECODE_DEPTH: u32 = 32.MaxDecodeDepthExceeded { max: u32 }variant toXRPLBinaryCodecException.decode_st_object,decode_field_value,decode_st_arraynow accept adepth: u32parameter; the publicdecode()entry passesdepth: 0.decode_st_objectreturnsErr(MaxDecodeDepthExceeded)whendepth > MAX_DECODE_DEPTH.How to validate
New tests:
test_binary_parser_read_truncated_returns_error,test_decode_st_object_depth_limit.