Skip to content

fix(replication): refuse a peer-chosen tree type of another family when serving chunks - #989

Merged
QuantumExplorer merged 2 commits into
developfrom
fix/fetch-chunk-tree-type-family
Sep 23, 2026
Merged

QuantumExplorer merged 2 commits into
developfrom
fix/fetch-chunk-tree-type-family

Conversation

@QuantumExplorer

@QuantumExplorer QuantumExplorer commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Issue being fixed or feature implemented

GroveDb::fetch_chunk is the entry point that untrusted state-sync peers call. It decodes a TreeType from a byte of the peer's global chunk id (it accepts 0..=16) and opens the addressed subtree with that type as-is. It never checks the type against the aggregate family of the nodes on disk. Hashing nodes under a type from another family breaks the invariant that TreeNode::hash_for_link enforces with a deliberate panic!, so one request from any peer crashes the serving handler. Two routes reach it:

  • Plain chunk route. Name ProvableCount, ProvableCountSum, ProvableSum or ProvableCountProvableSum (bytes 5, 6, 11, 12) for a Normal, Sum or Count subtree deep enough to have Hash boundaries in its root chunk. Chunk generation (to_hash_node_for_tree_type) then hits the panic ("got NoAggregateData").
  • Indexed header route. Name an indexed type (bytes 13, 14, 15) with a header request [0xFE, 1, 0, 0, 0]. serve_indexed_header_page calls merk.root_hash() on any non-provable subtree with a root key and hits the same panic.

This was reproduced before the fix: 7 panics under both the latest version and GROVE_V3. There is no state or consensus effect, but the panic is deterministic and a peer can repeat it at will. No released Platform build serves snapshots yet. On the unmerged feat/state-sync-v15 branch, drive-abci's panic hook would shut the node down. The restore side already has the matching check (Restorer::write_chunk); the serving side lacked one.

What was done?

This change is on the serving side only. hash_for_link is untouched: it is consensus-critical and its panic is an intentional invariant.

  • New replication::ensure_served_merk_family compares the root node's node_type() with merk.tree_type.inner_node_type(). On a mismatch it returns Error::CorruptedData, with the named type, the subtree prefix and the stored family, before anything is hashed. An empty Merk passes because it has nothing to hash.
  • It runs after every Merk the source opens for a peer:
    • the generic chunk path in fetch_chunk;
    • the indexed primary in serve_indexed_header_page;
    • each indexed secondary. The secondary check is defensive: a secondary's type and prefix both come from the axis tag, so a mismatch would need corrupt storage.
  • The root node decides for the whole subtree. Every node of a genuine Merk shares one family, and restore enforces the same rule node by node. An honest target always names the type its restored element carries, so the check never refuses an honest request.
  • There is no GroveVersion gate, because serving is not consensus. fetch_chunk runs only check_grovedb_v0.
  • One dishonest request shape is now refused earlier than before: a header request that drops the secondary root key for a populated secondary. With no root key, open_transactional_merk_by_prefix opens through the secondary's persisted base root with a forced NormalTree. The source used to hash the provable secondary under that type. This did not panic (NormalTree hashing falls through to the plain hash), but it produced a hash the target would reject. The family check now refuses it. fetch_chunk_rejects_malformed_indexed_header_requests now passes the real secondary root key in its "primary root key opens nothing" case, so that case still exercises the primary, and it pins this refusal as a separate case.

How Has This Been Tested?

  • grovedb/tests/state_sync_serving_tree_type.rs is a black-box test that uses only the public API, like a remote peer. It is the audit reproduction: bytes 5, 6, 11 and 12 on the plain route and 13, 14 and 15 on the header route, all against a 100-item NormalTree. Every request is now refused with the family error, and the node keeps serving the honest request. It runs under the latest version and GROVE_V3.
  • grovedb/src/tests/replication_serving_tree_type_tests.rs:
    • The full matrix builds a grove with one populated subtree of every kind: Normal, Sum, BigSum, Count, CountSum, PC, PCS, PS, PCPS, empty, PCIT, PSIT, PCPSIT (with their secondaries), CommitmentTree, MMR, BulkAppend, Dense and PrivateDocumentStore. It records every global chunk id an honest sync sends, then replays each target with:

      • every tree-type byte 0..=16 plus the unknown bytes 17, 128 and 255;
      • the root key kept or dropped;
      • every local-id shape: the honest ones, root chunk, deeper traversal, indexed header request, and non-Merk page cursors in range, past the end and at extreme values.

      The matrix asserts that no request panics, that honest requests are still served, that a Merk subtree named with a type from another family is refused with the family error, and that unknown bytes are refused. It runs under the latest version and GROVE_V3.

    • The grove root and an empty grove get every tree-type byte, with the root key kept or dropped.

  • replication_fuzz_tests.rs adds a proptest (fetch_chunk_survives_peer_chosen_tree_types_and_ids) that drives fetch_chunk on the same fixture. Each case uses a random target, a peer-chosen type byte, a root key that may be dropped, and mutated local ids or a mutated whole request. It asserts there is never a panic and that a mismatched family is never served.

With the check disabled, all four new src/tests tests fail.

Local runs:

  • cargo fmt --check passes.
  • cargo clippy -p grovedb --all-targets -- -D warnings is clean.
  • The new tests pass: replication_serving_tree_type_tests, the new fuzz property, and --test state_sync_serving_tree_type.
  • A full cargo test -p grovedb run passes: 3494 lib tests plus every integration binary and the doctests, 0 failures. An earlier run showed fetch_chunk_rejects_malformed_indexed_header_requests failing; that test was adjusted as described above.

Breaking Changes

None. The change is serving-side only and not version-gated. Honest requests are unaffected; only requests that name a tree type from another family than the addressed Merk's nodes are now refused with Error::CorruptedData.

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

🤖 Generated with Claude Code

PR Hygiene · bf04e8e

  • Bots — coderabbitai not yet · thepastaclaw not yet — /skip-bots proceeds without the ones not yet reported
  • Self-review — post /self-reviewed once the bots are done
  • Within your 5 open PRs
  • Build green
  • Approvals — you own every area touched; none needed

When every box is checked the PR Hygiene check passes and this can merge.

…en serving chunks

`GroveDb::fetch_chunk` opened the subtree a state-sync peer addressed
with the tree type from the peer's global chunk id, as-is. Naming a
Provable* type for a plain, sum or count subtree (plain chunk route), or
an indexed type with a header request (indexed header route), hashed
nodes under a type of another aggregate family and reached the
fail-closed panic in `TreeNode::hash_for_link`: any peer could crash a
serving node at will.

Every Merk the source opens for a peer (the generic chunk path, the
indexed primary and each indexed secondary) now passes
`ensure_served_merk_family`, which compares the root node's family with
the opened type and returns `Error::CorruptedData` on a mismatch before
anything is hashed. `hash_for_link` is unchanged. Serving is not
consensus and an honest target always names the type its restored
element carries, so the check is not version-gated.

Tests: a black-box reproduction in grovedb/tests, a matrix that replays
every honest global chunk id of a grove holding every subtree kind with
every tree-type byte, root key kept or dropped and every local-id shape
(latest and GROVE_V3), and a fetch_chunk proptest in the replication
fuzz tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 59 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: 259e800d-3c7e-4748-a7b7-cca18305ec7b

📥 Commits

Reviewing files that changed from the base of the PR and between b7f8716 and bf04e8e.

📒 Files selected for processing (8)
  • CHANGELOG.md
  • grovedb/src/replication.rs
  • grovedb/src/replication/indexed_sync.rs
  • grovedb/src/tests/mod.rs
  • grovedb/src/tests/replication_fuzz_tests.rs
  • grovedb/src/tests/replication_serving_tree_type_tests.rs
  • grovedb/src/tests/replication_session_tests.rs
  • grovedb/tests/state_sync_serving_tree_type.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.

@github-actions github-actions Bot added the waiting-bots Waiting for the review bots to report on this head label Sep 23, 2026
@thepastaclaw

thepastaclaw commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

🕓 Queued for automated review — 9th in line, estimated start in ~2.4 h (commit bf04e8e)
Estimated review time once started: ~35 min (two-phase automated review; median of recent runs).

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

@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.98%. Comparing base (d5a54b7) to head (bf04e8e).
⚠️ Report is 10 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #989      +/-   ##
===========================================
+ Coverage    92.95%   92.98%   +0.02%     
===========================================
  Files          331      331              
  Lines       104500   104518      +18     
===========================================
+ Hits         97138    97183      +45     
+ Misses        7362     7335      -27     
Components Coverage Δ
grovedb-core 91.27% <100.00%> (+0.04%) ⬆️
merk 93.99% <ø> (+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.

@QuantumExplorer
QuantumExplorer merged commit 2d92ac4 into develop Sep 23, 2026
14 of 15 checks passed
@QuantumExplorer
QuantumExplorer deleted the fix/fetch-chunk-tree-type-family branch September 23, 2026 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-bots Waiting for the review bots to report on this head

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants