Skip to content

fix(merk): validate ChunkOp bytes before trusting them - #975

Merged
QuantumExplorer merged 1 commit into
developfrom
claude/chunkop-decoder-bugs-4e2857
Sep 17, 2026
Merged

QuantumExplorer merged 1 commit into
developfrom
claude/chunkop-decoder-bugs-4e2857

Conversation

@QuantumExplorer

Copy link
Copy Markdown
Member

Issue being fixed or feature implemented

Two hygiene findings in the ChunkOp byte decoder (merk/src/proofs/chunk/chunk_op.rs):

As #904 notes, production state sync does not use this decoder (it goes through decode_vec_ops and process_chunk; Restorer::process_multi_chunk takes already-materialized values). The only direct ChunkOp::decode callers are tests. So this is decoder hygiene, not a demonstrated remote vulnerability.

What was done?

  • Traversal bytes are decoded with an explicit 0/1 match; anything else returns ed::Error::UnexpectedByte(byte).
  • A chunk id's declared length is checked against MAX_CHUNK_ID_LEN before the buffer is allocated. The bound is derived, not invented: Merk stores node heights as u8, so max_traversal_instruction_len(u8::MAX) = 254 is the longest instruction that can address a node in any tree. (max_traversal_instruction_len became a const fn for this.)
  • The op count only sizes the initial reservation up to MAX_PREALLOCATED_OPS (1024). The reader is a generic Read, so remaining input is unknown and there is no principled cap on the op count itself; past the cap the vec grows as ops actually decode, so the allocation stays proportional to the input consumed. Valid chunks with more ops than the cap still decode.

The encoder is untouched. Chunk producers already refuse over-long chunk ids (#883), so nothing legitimate is emitted that the decoder now rejects.

No GroveVersion gate: the decoder is not on a consensus or production sync path, and the rejected inputs are ones no honest encoder produces.

How Has This Been Tested?

cargo test -p grovedb-merk --lib chunk (79 pass), cargo clippy -p grovedb-merk -- -D warnings, cargo fmt --check.

New/changed tests in chunk_op.rs:

  • test_chunk_op_decoding_rejects_non_binary_chunk_id_values — replaces the old test that pinned the canonicalizing behavior; bytes 2/3/128/255 now error.
  • test_chunk_op_decoding_bounds_chunk_id_length_before_allocating — usize::MAX and MAX_CHUNK_ID_LEN + 1 are refused with a typed InvalidData error.
  • test_chunk_op_decoding_does_not_reserve_declared_op_count — a declared usize::MAX op count returns an error.
  • test_chunk_op_longest_chunk_id_round_trips, test_chunk_op_more_ops_than_preallocated_round_trip — bounded valid encodings still round-trip.

Confirmed against the old decoder logic: the three rejection tests fail (two with a capacity overflow panic, one with the canonicalized decode), and pass with the fix.

Breaking Changes

None for honest encodings. ChunkOp::decode now rejects non-binary traversal bytes and chunk ids longer than 254 steps, which it previously accepted.

Closes #704
Closes #904

🤖 Generated with Claude Code

`ChunkOp::decode` mapped every traversal byte other than 1 to `false`, so
one chunk id had many encodings, unlike `vec_bytes_as_traversal_instruction`
which rejects non-binary bytes (#704). It also sized its buffers from the
declared lengths before checking them, so a hostile length panicked on
capacity overflow or reserved memory the input never backed (#904).

- reject traversal bytes other than 0 and 1
- refuse a chunk id longer than any tree is deep (heights are `u8`) before
  allocating its buffer
- cap the up-front op reservation; past it the vec grows as ops decode, so
  the allocation tracks the input actually consumed

Production state sync does not go through this decoder
(`decode_vec_ops` / `process_chunk`), so this is decoder hygiene with no
consensus impact and needs no version gate.

Closes #704
Closes #904

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@coderabbitai

coderabbitai Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 28 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 90ede621-1ce3-4dfb-85a4-15295dc01853

📥 Commits

Reviewing files that changed from the base of the PR and between edc346c and 01d0f00.

📒 Files selected for processing (2)
  • merk/src/proofs/chunk/chunk_op.rs
  • merk/src/proofs/chunk/util.rs

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.

@thepastaclaw

thepastaclaw commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

🕓 Queued for automated review — 2nd in line, estimated start in ~40 min (commit 01d0f00)
Estimated review time once started: ~1.4 h (two-phase automated review; median of recent runs).

  • Request priority review — click to move this review to the front of the queue.

@QuantumExplorer
QuantumExplorer merged commit d5a54b7 into develop Sep 17, 2026
10 checks passed
@QuantumExplorer
QuantumExplorer deleted the claude/chunkop-decoder-bugs-4e2857 branch September 17, 2026 04:40
@codecov

codecov Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.95%. Comparing base (edc346c) to head (01d0f00).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop     #975   +/-   ##
========================================
  Coverage    92.95%   92.95%           
========================================
  Files          331      331           
  Lines       104450   104500   +50     
========================================
+ Hits         97088    97138   +50     
  Misses        7362     7362           
Components Coverage Δ
grovedb-core 91.22% <ø> (ø)
merk 93.98% <100.00%> (+0.01%) ⬆️
storage 91.94% <ø> (ø)
commitment-tree 95.62% <ø> (ø)
mmr 95.43% <ø> (ø)
bulk-append-tree 92.78% <ø> (ø)
element 97.18% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

None yet

Projects

None yet

2 participants