Repository navigation
fix(merk,grovedb): refuse backward-reference rows in count-offset proofs - #988
Conversation
The count-offset verifier's `classify_self` refused only Item / SumItem / ItemWithSumItem on a KVValueHashFeatureType node. That node hashes the proof-carried value hash, not the value bytes, so a proof supplier could re-emit an honest KVCount(k, item, c) as KVValueHashFeatureType(k, forged, H(item), c) without changing the root and have a forged ItemWithBackwardsReferences, SumItemWithBackwardsReferences or ItemWithSumItemWithBackwardsReferences returned as the row under the genuine root. The verifier rebuilds the tree with execute_with_options, so the refusal execute_proof applies to these types never ran, and the GroveDB post-filter did not check for them. - classify_self now allowlists what an honest prover puts on that node: trees and references. Everything else, including future element types, is refused. - run_count_offset_layer_dispatch additionally requires a row not resolved through a reference to be a simple item or a tree. Both run only on the V1 count-offset path and reject only shapes an honest prover never emits, so no GroveVersion gate is needed. Regression tests cover all three forged types on PCT, PCS and PCPS hosts under GROVE_V3 and latest, through every public verify entry point. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (8)
Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthroughCount-offset proof verification now rejects forged backward-reference item values on Merk feature-type nodes and unresolved GroveDB count rows. New tests cover these checks across query directions, host types, and public verification entry points. Documentation describes the verifier paths and the allowlists. ChangesCount-offset proof verification
Priority: ⬆️ High Estimated code review effort: 3 (Moderate) | ~25 minutes Change: Bug fix · Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to This change tightens count-offset proof verification so that forged backward-reference rows are rejected instead of verifying under the genuine root hash. The new checks accept only the node shapes that honest provers already emit, so valid proofs are not expected to be affected. No merge-blocking risk remains. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 71.43% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 28 functions across 6 files. (2 skipped: 2 unsupported.)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
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. Comment |
|
🕓 Queued for automated review — 8th in line, estimated start in ~1.9 h (commit 559c242)
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #988 +/- ##
========================================
Coverage 92.95% 92.95%
========================================
Files 331 331
Lines 104500 104516 +16
========================================
+ Hits 97138 97154 +16
Misses 7362 7362
🚀 New features to boost your workflow:
|
Summary
This fixes a proof-soundness bug in the count-offset verifier. A malicious proof supplier could make an offset-paginated query over a
ProvableCountTree,ProvableCountSumTreeorProvableCountProvableSumTreereturn attacker-chosen item bytes or sum values together with the genuine root hash. The bug was found and reproduced by the 2026-09-23 audit and had not been filed as an issue.The forgery
An honest count-offset proof returns an in-range item as
KVCount(k, item, c)(orKVCountSumon a PCPS host), and the verifier recomputesH(item)from the bytes.KVValueHashFeatureTypeinstead hashes(key, value_hash, aggregates)withvalue_hashtaken from the proof, so its value bytes are not bound. The attacker rewrites the honest node as:The node hash does not change, because
kv_hash(k, v) == kv_digest_to_kv_hash(k, H(v)), so the root does not change either.classify_self(merk/src/proofs/query/count_offset/verify.rs) was the only element-type check on that node, and it refused onlyItem/SumItem/ItemWithSumItem(has_simple_value_hash). The regular merk verifierexecute_proofalso refuses the backward-references items there (added in #843). The count-offset verifier rebuilds its tree withexecute_with_options, though, so that refusal never ran on this path. The GroveDB post-filter inrun_count_offset_layer_dispatchchecked only NonCounted, non-empty trees, raw references and the empty-tree hash. So a forged row of any of these types was returned throughverify_query,verify_query_with_options,verify_query_rawandverify_path_queryunder the genuine root:ItemWithBackwardsReferences: arbitrary item bytesSumItemWithBackwardsReferences: an arbitrary sumItemWithSumItemWithBackwardsReferences: bothProvable* count trees refuse all three at insert, so an honest proof never carries one on a directly read row. Every such row is a forgery.
Scope: offset > 0 queries on PCT / PCS / PCPS hosts with V1 envelopes, under GROVE_V3 (live) and V4, in grovedb >= 6.0.0. No current Dash Platform caller verifies offset queries on these trees, because DAPI refuses document offsets outside the V4 ranked path.
Fix
classify_self): theKVValueHashFeatureTypeguard is now an allowlist of what an honest prover puts on that node: trees (ElementType::is_tree, which covers every Merk, non-Merk and indexed tree) and references (is_reference). Only these have a combine-hash value hash the verifier cannot recompute. Everything else is refused, including element types added later. This guard is the only one for the indexed-axis callers ofverify_count_offset_on_range_proof, which have no GroveDB post-filter.run_count_offset_layer_dispatch), defense in depth: a returned row that did not come through a reference must be a simple item or a tree. Non-empty trees and the empty-tree hash are already checked there. Rows resolved through a reference are exempt, because their value is the target's bytes, bound by theKVRefValueHash*combine. A reference in a count tree may legitimately resolve to anItemWithBackwardsReferenceselsewhere;end_to_end_offset_dereferences_bidirectional_referencespins that honest shape, and it still passes.run_count_offset_layer_dispatchis called only fromverify_layer_proof_v1. V0 envelopes with an offset are rejected earlier byapply_count_offset_envelope_gate, so neither change touches V0 verification.No GroveVersion gate. This is client-side verifier strictness: it rejects only proof shapes an honest prover never emits. The prover and proof bytes are unchanged, and no V0 function is touched.
Tests
New module
grovedb/src/tests/count_offset_backward_ref_forgery_tests.rs, built from the audit's reproductions:forged_backward_reference_rows_are_rejected_on_every_host_and_entry_point: 3 forged variants × PCT / PCS / PCPS × GROVE_V3 / latest × ascending / descending × all four public verify entry points. Each case first checks that the honest proof verifies to the real root. It then checks that the rewrite leaves the host root unchanged, so the refusal is not a side effect of a hash mismatch, and that the refusal comes from the merk-level allowlist.grovedb_allowlist_rejects_unresolved_backward_reference_rows: the GroveDB-level check fires by itself, ahead of the parent chain check.forged_plain_item_row_stays_rejected: the pre-existingItemcontrol.count_hosts_refuse_backward_reference_items_at_insert: pins the premise that every Provable* count host refuses all three types at insert.Plus
rejects_backward_reference_items_on_kv_value_hash_feature_typeinmerk/src/proofs/query/count_offset/tests.rs, for direct merk callers.Checked against the unfixed code:
Ok(<genuine root>), the same result the audit reproduced.cargo test -p grovedb-merkpasses (775 unit tests and 9 doctests), as do the new grovedb module and the existing count-offset suites. The fullcargo test -p grovedbrun was still in progress when this PR was opened; I'll follow up here with the result.cargo fmtis clean, andcargo clippy -p grovedb -p grovedb-merk --all-targets -- -D warningsis clean.Follow-up (not in this PR):
execute_proof'sKVValueHashFeatureTypeguard is still a denylist (has_simple_value_hashplus the three backward-references items). Today that matches this allowlist exactly, because every element type is an item, a reference or a tree, but it would not refuse a new item type by default.🤖 Generated with Claude Code
PR Hygiene ·
559c242/skip-botsproceeds without the ones not yet reported/self-reviewedonce the bots are doneWhen every box is checked the
PR Hygienecheck passes and this can merge.Summary by CodeRabbit