Fix InputLen() guard bypass in streaming XDR decoders#5905
Merged
tamirms merged 4 commits intostellar:mainfrom Feb 19, 2026
Merged
Fix InputLen() guard bypass in streaming XDR decoders#5905tamirms merged 4 commits intostellar:mainfrom
tamirms merged 4 commits intostellar:mainfrom
Conversation
The XDR InputLen() guard is bypassed when the decoder wraps a streaming reader (bufio.Reader, zstd reader) that does not implement Len(). This allows attacker-controlled length fields to trigger unbounded memory allocations. Adopt the buffer-first pattern: read/decompress into []byte, then decode from bytes.NewReader which implements Len(), activating all 95 generated InputLen() guards. - Change ReadFrameLength to accept io.Reader instead of *xdr.Decoder - Use ReadFrameLength in Stream.ReadOne (adds missing magic bit validation) - Buffer frames in bufferedLedgerMetaReader before decoding with SafeUnmarshal - Buffer decompressed data in compressxdr.XDRDecoder before decoding - Add max frame size guard (256 MB) for captive core pipe reader Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a critical security vulnerability where XDR InputLen() guards were bypassed when decoding from streaming readers (bufio.Reader, zstd reader) that don't implement Len(). The fix adopts a buffer-first pattern: data is read/decompressed into []byte first, then decoded from bytes.NewReader which implements Len(), activating all 95 generated InputLen() guards.
Changes:
- Refactored ReadFrameLength to accept io.Reader instead of *xdr.Decoder and validate the magic bit (0x80000000)
- Implemented buffer-first pattern in bufferedLedgerMetaReader and compressxdr.XDRDecoder to ensure InputLen() guards are active
- Added max frame size guard (256 MB) for captive core pipe reader to prevent unbounded allocations
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| xdr/main.go | Refactored ReadFrameLength to accept io.Reader and validate magic bit; migrated to standard library errors |
| xdr/xdrstream.go | Updated Stream.ReadOne to use new ReadFrameLength signature; migrated to standard library errors |
| xdr/xdrstream_test.go | Updated tests to include magic bit (0x80000000) in frame headers |
| support/compressxdr/compress_xdr.go | Implemented buffer-first pattern: read all decompressed data into memory before XDR decoding |
| ingest/ledgerbackend/buffered_meta_pipe_reader.go | Implemented buffer-first pattern with reusable frame buffer and added 256 MB max frame size validation |
| ingest/ledgerbackend/buffered_meta_pipe_reader_test.go | Added comprehensive tests for frame reading, size validation, and multiple frames |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Define xdrFrameLastFragment and xdrFrameLengthMask constants with a comment referencing RFC 5531 section 11 (XDR record marking). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
leighmcculloch
approved these changes
Feb 18, 2026
Member
leighmcculloch
left a comment
There was a problem hiding this comment.
Couple questions but otherwise lgtm.
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.
PR Checklist
PR Structure
otherwise).
services/friendbot, orallordocif the changes are broad or impact manypackages.
Thoroughness
.mdfiles, etc... affected by this change). Take a look in the
docsfolder for a given service,like this one.
Release planning
CHANGELOG.mdwithin the component folder structure. For example, if I changed horizon, then I updated (services/horizon/CHANGELOG.md. I add a new line item describing the change and reference to this PR. If I don't update a CHANGELOG, I acknowledge this PR's change may not be mentioned in future release notes.semver, or if it's mainly a patch change. The PR is targeted at the next
release branch if it's not a patch change.
What
Every xdr type generated by xdrgen has a DecodeFrom() function which contains a guards like:
These fire before every variable-length allocation (slices, strings, opaque data), preventing attacker-controlled length fields from triggering unbounded memory allocations.
InputLen() delegates to the decoder's internal lenLeft interface:
When creating a decoder, go-xdr checks if the reader implements Len():
bytes.NewReader implements Len(), so guards are active. Streaming readers like bufio.Reader and zstd readers do not, so l is nil and all guards are silently skipped.
Problem
Two production decode paths create decoders wrapping streaming readers:
In both cases, InputLen() returns (0, false) which means the allocation checks are bypassed.
These two cases are fixed by using bytes.NewReader.
Known limitations
[N/A]