Skip to content

fix: fail on truncated bodies instead of NUL-padding them - #3195

Merged
predic8 merged 1 commit into
masterfrom
fix/truncated-body-zero-padding
Sep 4, 2026
Merged

fix: fail on truncated bodies instead of NUL-padding them#3195
predic8 merged 1 commit into
masterfrom
fix/truncated-body-zero-padding

Conversation

@predic8

@predic8 predic8 commented Sep 2, 2026

Copy link
Copy Markdown
Member

ByteUtil.readByteArray() returned its pre-allocated buffer when the stream ended before the requested number of bytes had been read, so the zero padding was indistinguishable from real content.

On the Content-Length path (Body.readLocal) this produced no error at all: the body reported isRead() == true with its content padded to the announced length, and the truncated message was forwarded. The chunked reader handed the padded chunk to the observers and to the consumer, then failed one chunk later with the misleading "Empty chunk-size field"; ChunkedBody.writeStreamed() had already written it to the peer. Frame.read() padded HTTP/2 frame payloads the same way, although Frame.readByte() already threw EOFException for the frame header.

readByteArray() now throws EOFException on a short read. As it extends IOException, every caller's existing handling turns it into a ReadingBodyException without a signature change. Reading until the end of the stream (length < 0) is unaffected.

Note this changes a path that previously appeared to work: a truncated Content-Length body is now a hard body-read failure rather than silently padded.

Closes #3193

Summary by CodeRabbit

  • Bug Fixes
    • Incomplete fixed-length HTTP request and response bodies are now rejected instead of being treated as complete.
    • Truncated chunked transfers now fail safely without delivering partial data to consumers.
    • Complete bodies continue to be processed successfully, including data received across multiple short reads.
  • Tests
    • Added coverage for truncated, complete, and fragmented body-reading scenarios.

ByteUtil.readByteArray() returned its pre-allocated buffer when the stream
ended before the requested number of bytes had been read, so the zero padding
was indistinguishable from real content.

On the Content-Length path (Body.readLocal) this produced no error at all: the
body reported isRead() == true with its content padded to the announced length,
and the truncated message was forwarded. The chunked reader handed the padded
chunk to the observers and to the consumer, then failed one chunk later with
the misleading "Empty chunk-size field"; ChunkedBody.writeStreamed() had already
written it to the peer. Frame.read() padded HTTP/2 frame payloads the same way,
although Frame.readByte() already threw EOFException for the frame header.

readByteArray() now throws EOFException on a short read. As it extends
IOException, every caller's existing handling turns it into a
ReadingBodyException without a signature change. Reading until the end of the
stream (length < 0) is unaffected.

Note this changes a path that previously appeared to work: a truncated
Content-Length body is now a hard body-read failure rather than silently padded.

Closes #3193

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@predic8 predic8 added the bug label Sep 2, 2026
@coderabbitai

coderabbitai Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: 678a289d-9769-461f-97f2-1c6b1ace6a1d

📥 Commits

Reviewing files that changed from the base of the PR and between cfe7c76 and 7f1ae0d.

📒 Files selected for processing (4)
  • core/src/main/java/com/predic8/membrane/core/util/ByteUtil.java
  • core/src/test/java/com/predic8/membrane/core/http/BodyTest.java
  • core/src/test/java/com/predic8/membrane/core/http/ChunkedBodyTest.java
  • core/src/test/java/com/predic8/membrane/core/util/ByteUtilTest.java

Included review availability: Your plan provides up to 4 included reviews per hour; 2 remain after this review.


📝 Walkthrough

Walkthrough

readByteArray now enforces fixed-length reads and throws EOFException on premature EOF. Tests cover short reads, complete bodies, truncated fixed-length bodies, and truncated chunked bodies without partial delivery.

Changes

Body read integrity

Layer / File(s) Summary
Exact-length read contract
core/src/main/java/com/predic8/membrane/core/util/ByteUtil.java, core/src/test/java/com/predic8/membrane/core/util/ByteUtilTest.java
readByteArray now accumulates short reads and throws EOFException when fixed-length input ends early. Tests cover both behaviors.
HTTP body truncation handling
core/src/test/java/com/predic8/membrane/core/http/BodyTest.java, core/src/test/java/com/predic8/membrane/core/http/ChunkedBodyTest.java
Tests verify that complete bodies remain successful and truncated bodies fail without marking them read or delivering partial data.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 7f1ae

The change rejects truncated HTTP and HTTP/2 content instead of forwarding zero-padded data, with existing error handling preserved. No actionable merge-blocking risk remains beyond normal checks and review.

Poem

A rabbit reads each byte in line
No padded tails shall cross the sign
Short streams now raise EOF with care
Whole chunks travel everywhere
The burrow cheers for exact reads fine

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main fix: truncated bodies now fail instead of receiving NUL padding.
Linked Issues check ✅ Passed The changes satisfy issue #3193. Fixed-length reads now fail with EOFException on premature EOF, existing IOException handling remains compatible, negative-length reads remain unchanged, and regressio…
Out of Scope Changes check ✅ Passed All changes are directly related to issue #3193. The production change fixes truncated reads, and the added tests validate the affected fixed-length, chunked, and utility behavior. No unrelated code c…
Full details: Linked Issues check

Explanation

The changes satisfy issue #3193. Fixed-length reads now fail with EOFException on premature EOF, existing IOException handling remains compatible, negative-length reads remain unchanged, and regression tests cover Content-Length and chunked-body behavior. The shared ByteUtil fix also applies to HTTP/2 payload reads.

Full details: Out of Scope Changes check

Explanation

All changes are directly related to issue #3193. The production change fixes truncated reads, and the added tests validate the affected fixed-length, chunked, and utility behavior. No unrelated code changes are present.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/truncated-body-zero-padding

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@predic8
predic8 merged commit 80a90cc into master Sep 4, 2026
4 of 6 checks passed
@predic8
predic8 deleted the fix/truncated-body-zero-padding branch September 4, 2026 13:25
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.

Truncated message bodies are silently NUL-padded instead of failing (ByteUtil.readByteArray)

2 participants