From 2308e36596af8a968fc8b438188485afb254fa27 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 6 Sep 2026 22:40:30 +0200 Subject: [PATCH 1/2] fix(proofs): refuse raw reference rows in the V1 layer verifier (#862) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A `KVValueHash` / `KVValueHashFeatureType` proof node hashes only `(key, value_hash)`, and the merk-level V1 guard refuses only item elements on those forms. An attacker could therefore take an honest `KVRefValueHash(key, target, H(ref))` node, recompute the combined value hash it commits to, and re-present the row as `KVValueHash(key, , combined)`: the Merk root still reconstructed and `verify_layer_proof_v1` consumed reference metadata nothing had authenticated. Reproduced at runtime on develop: * the forged `Reference` (attacker-chosen target path) was returned as the proven element for the key, via both `verify_query` and `verify_query_raw`; the `KVValueHashFeatureType` shape under a ProvableCountTree and a forged `ReferenceWithSumItem` behaved alike; * with a subquery at that key and the lower layer dropped, every branch of the terminal arm fell through without error and the proof verified with a populated subtree's rows missing. Fix: `verify_layer_proof_v1` rejects any raw `Reference` / `ReferenceWithSumItem` row (wrapped or not) right after deserializing it, before the element type can steer a result or a descent. An honest V1 prover rewrites every consumed reference row into a `KVRefValueHash*` node carrying the dereferenced target, so no released proof is affected and the check is not version-gated, matching the existing count-offset rejection. The rejection deliberately does not live in the merk-level guard: a reference row past the query limit legitimately stays a bare `KVValueHash` in released V1 proofs (the prover's post-pass only rewrites rows within the limit), the merk verifier walks every node, and the GroveDB row loop stops before reading it — refusing references there rejected an honest limit-4 proof in `test_mixed_level_proofs`. Recorded in docs/audit-non-issues.md and the proof-system book chapter. Tests: `unbound_reference_row_tests` — honest round trip, forged row on KVValueHash (both entry points), forged row on KVValueHashFeatureType, forged ReferenceWithSumItem, forged reference hiding a populated subtree. Co-Authored-By: Claude Fable 5.1 --- docs/audit-non-issues.md | 7 +- docs/book/src/proof-system.md | 36 ++ grovedb/src/operations/proof/verify.rs | 32 ++ grovedb/src/tests/mod.rs | 1 + .../src/tests/unbound_reference_row_tests.rs | 489 ++++++++++++++++++ merk/src/proofs/query/verify.rs | 16 + 6 files changed, 579 insertions(+), 2 deletions(-) create mode 100644 grovedb/src/tests/unbound_reference_row_tests.rs diff --git a/docs/audit-non-issues.md b/docs/audit-non-issues.md index ff4574afe..33b5a5b73 100644 --- a/docs/audit-non-issues.md +++ b/docs/audit-non-issues.md @@ -76,10 +76,13 @@ observing an append error. | Proof encoding truncates keys ≥ 256 bytes | Unreachable: every public insert path (direct + batch) enforces the 255-byte key limit (PR #506), so oversized keys cannot reach proof encoding. The `debug_assert!` is sufficient. Raw-Merk hardening exists separately (#728). | | `feature_type` forgery in `KVValueHashFeatureType` proof nodes | The decoded `_feature_type` is discarded by the verifier; the canonical type/sum/count lives in the hash-verified `Element` bytes. Forged values never reach callers. Documented in `verify.rs` comments and `proof_exploit_tests.rs`. | | `saturating_sub` on negative `SumItem` values corrupts `sum_limit` | Correct as written: `sum_limit` tracks the remaining **net sum** budget; +7 and −4 must consume 3, not 11. Absolute-value math would be the bug. | +| The merk-level V1 guard (`execute_proof`, proof version 1) rejects items on `KVValueHash` / `KVValueHashFeatureType` nodes but lets **reference** elements through, so reference bytes on those nodes are unbound at the Merk level | True at the Merk level and deliberate. A reference row past the query limit legitimately stays a bare `KVValueHash` in released V1 proofs (the GroveDB post-pass only rewrites rows within the limit into `KVRefValueHash*`), so refusing references there would reject honest proofs. The only consumer of those rows, `verify_layer_proof_v1`, rejects any raw `Reference` / `ReferenceWithSumItem` row it consumes — before the element type is used for a result or a descent decision — and its limit accounting stops it before the beyond-limit node is ever read. The exploitable half of this claim (forged reference row returned as a result; forged reference hiding a populated subtree by skipping its descent) was real and is fixed via [#862](https://github.com/dashpay/grovedb/issues/862); see `docs/book/src/proof-system.md` "Which node forms bind their value bytes" and `grovedb/src/tests/unbound_reference_row_tests.rs`. | -**Becomes real if:** a new write path bypasses the 255-byte key check, or a +**Becomes real if:** a new write path bypasses the 255-byte key check, a verifier starts trusting a decoded field instead of the hash-verified -element bytes. +element bytes, or a V1 code path other than `verify_layer_proof_v1` starts +reading the value bytes of a `KVValueHash*` row (the merk-level guard does +not cover reference bytes there). ## Proof-envelope trailing bytes: strict rejection is canonical, do not re-gate leniency diff --git a/docs/book/src/proof-system.md b/docs/book/src/proof-system.md index 619bdb0f7..c7e5b0caf 100644 --- a/docs/book/src/proof-system.md +++ b/docs/book/src/proof-system.md @@ -381,6 +381,42 @@ impl Tree { } ``` +### Which node forms bind their value bytes + +A proof node's value bytes reach the caller as the proven element, so the +verifier has to know, for every node form, *what* binds those bytes to the +root hash. Two forms — `KVValueHash` and `KVValueHashFeatureType` — hash +only `(key, value_hash)`: the bytes they carry are not covered by the Merk +chain at all, and a prover can pair any bytes with a genuine `value_hash`. +The V1 verifier therefore admits only elements whose bytes are bound +somewhere else onto them: + +| Element family on a `KVValueHash*` node | Verdict | What binds the bytes | +|---|---|---| +| Tree (any Merk or non-Merk tree) | accepted | The GroveDB layer recomputes `combine_hash(H(value), child)` from the lower layer, from `NULL_HASH` for an empty tree, or from the `child_hash` carried by `KVValueHashFeatureTypeWithChildHash`. | +| Item (`Item`, `SumItem`, `ItemWithSumItem`) | rejected by the Merk verifier at proof version 1 | Nothing — items commit the plain `H(value)` and must ride on `KV` / `KVCount` / `KVSum` / `KVCountSum`, where the verifier recomputes the hash from the bytes. | +| Reference (`Reference`, `ReferenceWithSumItem`, wrapped or not) | passes the Merk verifier; **rejected by `verify_layer_proof_v1` the moment it is consumed as a row** | Nothing on the node — references commit `combine_hash(H(reference), H(target))` and every row the GroveDB verifier consumes is served as `KVRefValueHash{,Count,Sum,CountSum}`, which hashes the **target** bytes and carries only `H(reference)`. Inside a sum-budget window a reference row rides on `KVValueHashFeatureTypeWithChildHash`, whose own `combine_hash(H(value), child_hash)` check binds it. | + +The reference rule lives in the GroveDB layer rather than the Merk +verifier on purpose. A reference row that lies *past* the query limit +legitimately stays a bare `KVValueHash` in released V1 proofs: the GroveDB +post-pass only rewrites rows within the limit, and the GroveDB verifier +stops consuming rows the moment its limit accounting is exhausted, so that +node is never read. Refusing references at the Merk level would therefore +reject honest proofs; refusing them at the row consumer covers every row +that can influence a result or a descent. + +Without that rule an attacker could take an honest +`KVRefValueHash(key, target, H(reference))` node, recompute the combined +value hash it commits to, and re-present the row as +`KVValueHash(key, , combined)`. The Merk root still +reconstructs, and the GroveDB layer would either return the forged +reference — an attacker-chosen target path — as the proven element, or, +when the query descends at that key, silently skip the descent (a reference +has no lower layer) and verify a proof that hides a populated subtree +(issue #862). `verify_layer_proof_v1` now refuses any raw reference row +before the row's element type is consulted for either purpose. + ## Absence Proofs GroveDB can prove that a key does **not** exist. This uses boundary nodes — diff --git a/grovedb/src/operations/proof/verify.rs b/grovedb/src/operations/proof/verify.rs index 3e8e5f58b..e7c806b7e 100644 --- a/grovedb/src/operations/proof/verify.rs +++ b/grovedb/src/operations/proof/verify.rs @@ -1635,6 +1635,38 @@ impl GroveDb { let element = Element::deserialize(value_bytes, grove_version)?.into_underlying(); + // A raw reference can never be an honest V1 row. The + // prover rewrites every reference the verifier will + // consume into a `KVRefValueHash*` node carrying the + // dereferenced TARGET bytes, so the bytes surfaced + // here are the target's — never the reference's own. + // A `KVValueHash` / `KVValueHashFeatureType` node + // hashes only `(key, value_hash)`, and the merk-level + // V1 guard refuses only *items* on those forms (it + // cannot refuse references: a reference row past the + // query limit legitimately stays bare there, and this + // loop stops before reading it). So a forged proof can + // pair genuine `value_hash` with arbitrary reference + // bytes and still reconstruct the root. This is the + // check that binds the row: refuse before the element + // type can be returned as unauthenticated reference + // metadata, or make "a reference has no lower layer" + // silently skip a descent the query asked for + // (issue #862). + if element.is_reference() { + return Err(Error::InvalidProof( + query.clone(), + format!( + "V1 proof surfaces a raw {} at key {}: the prover serves \ + references as KVRefValueHash nodes carrying the dereferenced \ + target, so an unresolved reference row is unbound and \ + malformed", + element.type_str(), + hex::encode(key), + ), + )); + } + verified_keys.insert(key.clone()); // Axis-ordered read: resolved from the QUERY first — diff --git a/grovedb/src/tests/mod.rs b/grovedb/src/tests/mod.rs index 038e94793..edcb67162 100644 --- a/grovedb/src/tests/mod.rs +++ b/grovedb/src/tests/mod.rs @@ -117,6 +117,7 @@ mod test_compaction_sizes; mod test_provable_count_fresh; mod tree_hashes_tests; mod trunk_proof_tests; +mod unbound_reference_row_tests; mod v1_cidx_descent_tests; mod v1_proof_tests; mod verify_grovedb_indexed_tests; diff --git a/grovedb/src/tests/unbound_reference_row_tests.rs b/grovedb/src/tests/unbound_reference_row_tests.rs new file mode 100644 index 000000000..59de4002e --- /dev/null +++ b/grovedb/src/tests/unbound_reference_row_tests.rs @@ -0,0 +1,489 @@ +//! Runtime validation for audit finding P14 (issue #862): a V1 proof +//! must never let raw reference-family element bytes reach the caller or +//! steer a descent decision, because no accepted proof node form binds +//! them. +//! +//! An honest V1 prover never emits a raw reference. Every reference row +//! is rewritten by the GroveDB post-pass into a `KVRefValueHash*` node +//! that carries the dereferenced TARGET bytes (hashed into the node) and +//! only the *hash* of the reference element. But a `KVValueHash` / +//! `KVValueHashFeatureType` node hashes `(key, value_hash)` alone, and +//! the merk-level V1 guard added by PR #553 rejects only *item* elements +//! there. So an attacker can take the honest `KVRefValueHash(key, target, +//! H(ref))` node, recompute the combined value hash it commits to, and +//! re-present the row as `KVValueHash(key, , +//! combined)`: the Merk root still reconstructs, the guard passes (the +//! bytes are a reference, not an item), and the GroveDB layer sees a +//! `Reference` element whose path / type it never authenticated. +//! +//! Two exploit shapes are exercised here, each one an actual attack +//! against the live `develop` verifier before the fix: +//! +//! 1. **Forged result row** — the verifier returns the forged reference +//! (attacker-chosen target path) as the proven element for the key. +//! 2. **Silent descent skip** — a populated subtree the query descends +//! into is re-presented as a reference; every branch of the terminal +//! arm falls through without error, the lower layer is dropped, and +//! the proof verifies with the subtree's rows missing. + +#[cfg(test)] +mod tests { + use std::collections::LinkedList; + + use grovedb_merk::{ + proofs::{encode_into, Decoder, Node, Op}, + tree::{combine_hash, value_hash, TreeFeatureType}, + }; + use grovedb_version::version::GroveVersion; + + use crate::{ + operations::proof::{GroveDBProof, GroveDBProofV1, LayerProof, ProofBytes}, + reference_path::ReferencePathType, + tests::{make_test_grovedb, TempGroveDb, ANOTHER_TEST_LEAF, TEST_LEAF}, + Element, Error, GroveDb, PathQuery, Query, SizedQuery, + }; + + const TARGET_KEY: &[u8] = b"target"; + const DECOY_KEY: &[u8] = b"decoy"; + const REF_KEY: &[u8] = b"ref"; + const COUNT_TREE_KEY: &[u8] = b"pct"; + const SUB_KEY: &[u8] = b"sub"; + + // ================================================================= + // Envelope helpers + // ================================================================= + + fn proof_cfg() -> impl bincode::config::Config { + bincode::config::standard() + .with_big_endian() + .with_no_limit() + } + + fn decode_v1_root(proof: &[u8]) -> LayerProof { + let (decoded, consumed) = bincode::decode_from_slice::(proof, proof_cfg()) + .expect("decode proof"); + assert_eq!(consumed, proof.len(), "proof must decode canonically"); + match decoded { + GroveDBProof::V1(GroveDBProofV1 { root_layer }) => root_layer, + GroveDBProof::V0(_) => panic!("expected a V1 proof envelope"), + } + } + + fn encode_v1_root(root_layer: LayerProof) -> Vec { + bincode::encode_to_vec(GroveDBProof::V1(GroveDBProofV1 { root_layer }), proof_cfg()) + .expect("encode tampered envelope") + } + + fn merk_bytes(layer: &LayerProof) -> Vec { + match &layer.merk_proof { + ProofBytes::Merk(b) => b.clone(), + _ => panic!("layer under forge must carry ProofBytes::Merk"), + } + } + + /// Re-present the honest `KVRefValueHash{,Count}` node for + /// `target_key` as a `KVValueHash` / `KVValueHashFeatureType` node + /// carrying `forged_value` and the *same* committed value hash the + /// honest node reconstructs — `combine_hash(H(reference), H(target))`. + /// The Merk chain is unchanged by construction. + fn forge_reference_row(layer: &mut LayerProof, target_key: &[u8], forged_value: Vec) { + let original = merk_bytes(layer); + let mut ops: LinkedList = LinkedList::new(); + let mut replaced = false; + let mut seen = Vec::new(); + for op in Decoder::new(&original) { + let op = op.expect("decode proof op"); + let mut rewrite = |node: Node| -> Node { + match node { + Node::KVRefValueHash(k, referenced, ref_hash) => { + seen.push(hex::encode(&k)); + if k.as_slice() == target_key { + replaced = true; + let combined = + combine_hash(&ref_hash, &value_hash(&referenced).unwrap()).unwrap(); + Node::KVValueHash(k, forged_value.clone(), combined) + } else { + Node::KVRefValueHash(k, referenced, ref_hash) + } + } + Node::KVRefValueHashCount(k, referenced, ref_hash, count) => { + seen.push(hex::encode(&k)); + if k.as_slice() == target_key { + replaced = true; + let combined = + combine_hash(&ref_hash, &value_hash(&referenced).unwrap()).unwrap(); + Node::KVValueHashFeatureType( + k, + forged_value.clone(), + combined, + TreeFeatureType::ProvableCountedMerkNode(count), + ) + } else { + Node::KVRefValueHashCount(k, referenced, ref_hash, count) + } + } + other => other, + } + }; + let new_op = match op { + Op::Push(node) => Op::Push(rewrite(node)), + Op::PushInverted(node) => Op::PushInverted(rewrite(node)), + other => other, + }; + ops.push_back(new_op); + } + assert!( + replaced, + "forge: key {} not found as a KVRefValueHash{{,Count}} node (saw {:?})", + hex::encode(target_key), + seen + ); + let mut out = Vec::with_capacity(original.len() + forged_value.len()); + encode_into(ops.iter(), &mut out); + layer.merk_proof = ProofBytes::Merk(out); + } + + /// Swap only the value bytes of the `KVValueHash*` node for + /// `target_key`, keeping the committed value hash. Used to + /// re-present a populated subtree as a reference. + fn forge_value_bytes(layer: &mut LayerProof, target_key: &[u8], forged_value: Vec) { + let original = merk_bytes(layer); + let mut ops: LinkedList = LinkedList::new(); + let mut replaced = false; + let mut seen = Vec::new(); + for op in Decoder::new(&original) { + let op = op.expect("decode proof op"); + let mut rewrite = |node: Node| -> Node { + match node { + Node::KVValueHash(k, v, vh) => { + seen.push(hex::encode(&k)); + if k.as_slice() == target_key { + replaced = true; + Node::KVValueHash(k, forged_value.clone(), vh) + } else { + Node::KVValueHash(k, v, vh) + } + } + Node::KVValueHashFeatureType(k, v, vh, ft) => { + seen.push(hex::encode(&k)); + if k.as_slice() == target_key { + replaced = true; + Node::KVValueHashFeatureType(k, forged_value.clone(), vh, ft) + } else { + Node::KVValueHashFeatureType(k, v, vh, ft) + } + } + other => other, + } + }; + let new_op = match op { + Op::Push(node) => Op::Push(rewrite(node)), + Op::PushInverted(node) => Op::PushInverted(rewrite(node)), + other => other, + }; + ops.push_back(new_op); + } + assert!( + replaced, + "forge: key {} not found as a KVValueHash{{FeatureType}} node (saw {:?})", + hex::encode(target_key), + seen + ); + let mut out = Vec::with_capacity(original.len() + forged_value.len()); + encode_into(ops.iter(), &mut out); + layer.merk_proof = ProofBytes::Merk(out); + } + + fn assert_rejected_as_unbound_reference(result: Result) { + match result { + Err(Error::InvalidProof(_, msg)) => assert!( + msg.to_ascii_lowercase().contains("reference"), + "rejection must name the unbound reference row; got {msg}" + ), + other => panic!("forged reference row must be rejected as InvalidProof; got {other:?}"), + } + } + + // ================================================================= + // Fixtures + // ================================================================= + + /// `[TEST_LEAF]/target` and `[TEST_LEAF]/decoy` are items; + /// `[ANOTHER_TEST_LEAF]/ref` is a reference to `target`. + fn reference_fixture(grove_version: &GroveVersion) -> TempGroveDb { + let db = make_test_grovedb(grove_version); + db.insert( + &[TEST_LEAF], + TARGET_KEY, + Element::new_item(b"the real target".to_vec()), + None, + None, + grove_version, + ) + .unwrap() + .expect("insert target"); + db.insert( + &[TEST_LEAF], + DECOY_KEY, + Element::new_item(b"an unrelated element".to_vec()), + None, + None, + grove_version, + ) + .unwrap() + .expect("insert decoy"); + db.insert( + &[ANOTHER_TEST_LEAF], + REF_KEY, + Element::new_reference(ReferencePathType::AbsolutePathReference(vec![ + TEST_LEAF.to_vec(), + TARGET_KEY.to_vec(), + ])), + None, + None, + grove_version, + ) + .unwrap() + .expect("insert reference"); + db + } + + fn forged_reference_bytes(grove_version: &GroveVersion) -> Vec { + Element::new_reference(ReferencePathType::AbsolutePathReference(vec![ + TEST_LEAF.to_vec(), + DECOY_KEY.to_vec(), + ])) + .serialize(grove_version) + .expect("serialize forged reference") + } + + fn key_query(path: Vec>, key: &[u8]) -> PathQuery { + let mut q = Query::new(); + q.insert_key(key.to_vec()); + PathQuery::new(path, SizedQuery::new(q, None, None)) + } + + // ================================================================= + // 1. Forged result row through a bare KVValueHash node + // ================================================================= + + #[test] + fn honest_reference_proof_returns_the_dereferenced_target() { + let grove_version = GroveVersion::latest(); + let db = reference_fixture(grove_version); + let path_query = key_query(vec![ANOTHER_TEST_LEAF.to_vec()], REF_KEY); + let proof = db + .prove_query(&path_query, None, grove_version) + .unwrap() + .expect("prove"); + let (root, rows) = GroveDb::verify_query(&proof, &path_query, grove_version) + .expect("honest proof verifies"); + assert_eq!(root, db.root_hash(None, grove_version).unwrap().unwrap()); + assert_eq!( + rows, + vec![( + vec![ANOTHER_TEST_LEAF.to_vec()], + REF_KEY.to_vec(), + Some(Element::new_item(b"the real target".to_vec())) + )] + ); + } + + #[test] + fn forged_reference_row_in_kv_value_hash_node_is_rejected() { + let grove_version = GroveVersion::latest(); + let db = reference_fixture(grove_version); + let path_query = key_query(vec![ANOTHER_TEST_LEAF.to_vec()], REF_KEY); + let honest = db + .prove_query(&path_query, None, grove_version) + .unwrap() + .expect("prove"); + + let mut root_layer = decode_v1_root(&honest); + let layer = root_layer + .lower_layers + .get_mut(ANOTHER_TEST_LEAF) + .expect("layer for the reference's tree"); + forge_reference_row(layer, REF_KEY, forged_reference_bytes(grove_version)); + let tampered = encode_v1_root(root_layer); + + assert_rejected_as_unbound_reference(GroveDb::verify_query( + &tampered, + &path_query, + grove_version, + )); + assert_rejected_as_unbound_reference(GroveDb::verify_query_raw( + &tampered, + &path_query, + grove_version, + )); + } + + /// Same forgery when the reference is wrapped in a count-carrying + /// parent: the honest `KVRefValueHashCount` becomes a + /// `KVValueHashFeatureType` carrying the honest count. + #[test] + fn forged_reference_row_in_feature_type_node_is_rejected() { + let grove_version = GroveVersion::latest(); + let db = reference_fixture(grove_version); + db.insert( + &[TEST_LEAF], + COUNT_TREE_KEY, + Element::empty_provable_count_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("insert count tree"); + db.insert( + &[TEST_LEAF, COUNT_TREE_KEY], + REF_KEY, + Element::new_reference(ReferencePathType::AbsolutePathReference(vec![ + TEST_LEAF.to_vec(), + TARGET_KEY.to_vec(), + ])), + None, + None, + grove_version, + ) + .unwrap() + .expect("insert reference under count tree"); + + let path_query = key_query(vec![TEST_LEAF.to_vec(), COUNT_TREE_KEY.to_vec()], REF_KEY); + let honest = db + .prove_query(&path_query, None, grove_version) + .unwrap() + .expect("prove"); + let (_, rows) = GroveDb::verify_query(&honest, &path_query, grove_version) + .expect("honest proof verifies"); + assert_eq!( + rows[0].2, + Some(Element::new_item(b"the real target".to_vec())) + ); + + let mut root_layer = decode_v1_root(&honest); + let layer = root_layer + .lower_layers + .get_mut(TEST_LEAF) + .and_then(|l| l.lower_layers.get_mut(COUNT_TREE_KEY)) + .expect("layer for the count tree"); + forge_reference_row(layer, REF_KEY, forged_reference_bytes(grove_version)); + let tampered = encode_v1_root(root_layer); + + assert_rejected_as_unbound_reference(GroveDb::verify_query( + &tampered, + &path_query, + grove_version, + )); + } + + /// The forged bytes may also be a `ReferenceWithSumItem`; the + /// family, not just the plain variant, is unbound. + #[test] + fn forged_reference_with_sum_item_row_is_rejected() { + let grove_version = GroveVersion::latest(); + let db = reference_fixture(grove_version); + let path_query = key_query(vec![ANOTHER_TEST_LEAF.to_vec()], REF_KEY); + let honest = db + .prove_query(&path_query, None, grove_version) + .unwrap() + .expect("prove"); + + let forged = Element::new_reference_with_sum_item( + ReferencePathType::AbsolutePathReference(vec![TEST_LEAF.to_vec(), DECOY_KEY.to_vec()]), + 7, + ) + .serialize(grove_version) + .expect("serialize forged reference with sum item"); + + let mut root_layer = decode_v1_root(&honest); + let layer = root_layer + .lower_layers + .get_mut(ANOTHER_TEST_LEAF) + .expect("layer for the reference's tree"); + forge_reference_row(layer, REF_KEY, forged); + let tampered = encode_v1_root(root_layer); + + assert_rejected_as_unbound_reference(GroveDb::verify_query( + &tampered, + &path_query, + grove_version, + )); + } + + // ================================================================= + // 2. Silent descent skip: a populated subtree re-presented as a + // reference, lower layer dropped. + // ================================================================= + + fn subtree_fixture(grove_version: &GroveVersion) -> TempGroveDb { + let db = make_test_grovedb(grove_version); + db.insert( + &[TEST_LEAF], + SUB_KEY, + Element::empty_tree(), + None, + None, + grove_version, + ) + .unwrap() + .expect("insert subtree"); + for (k, v) in [(b"a", b"1"), (b"b", b"2"), (b"c", b"3")] { + db.insert( + &[TEST_LEAF, SUB_KEY], + k.as_slice(), + Element::new_item(v.to_vec()), + None, + None, + grove_version, + ) + .unwrap() + .expect("insert subtree row"); + } + db + } + + fn subtree_query() -> PathQuery { + let mut inner = Query::new(); + inner.insert_all(); + let mut q = Query::new(); + q.insert_key(SUB_KEY.to_vec()); + q.set_subquery(inner); + PathQuery::new(vec![TEST_LEAF.to_vec()], SizedQuery::new(q, None, None)) + } + + #[test] + fn forged_reference_cannot_hide_a_populated_subtree() { + let grove_version = GroveVersion::latest(); + let db = subtree_fixture(grove_version); + let path_query = subtree_query(); + let honest = db + .prove_query(&path_query, None, grove_version) + .unwrap() + .expect("prove"); + let (_, rows) = GroveDb::verify_query(&honest, &path_query, grove_version) + .expect("honest proof verifies"); + assert_eq!(rows.len(), 3, "the honest proof carries every subtree row"); + + // Re-present the subtree element as a reference and drop the + // lower layer the query would have descended into. + let mut root_layer = decode_v1_root(&honest); + let layer = root_layer + .lower_layers + .get_mut(TEST_LEAF) + .expect("layer for TEST_LEAF"); + forge_value_bytes(layer, SUB_KEY, forged_reference_bytes(grove_version)); + assert!( + layer.lower_layers.remove(SUB_KEY).is_some(), + "honest proof descends into the subtree" + ); + let tampered = encode_v1_root(root_layer); + + assert_rejected_as_unbound_reference(GroveDb::verify_query( + &tampered, + &path_query, + grove_version, + )); + } +} diff --git a/merk/src/proofs/query/verify.rs b/merk/src/proofs/query/verify.rs index eafbfca5a..3c20d47c9 100644 --- a/merk/src/proofs/query/verify.rs +++ b/merk/src/proofs/query/verify.rs @@ -378,6 +378,18 @@ impl QueryProofVerify for Query { // attacker substitutes a KV node with KVValueHash to inject // a fake value while keeping the original hash. // Skipped for V0 backwards compatibility. + // + // Reference elements deliberately PASS here even though + // this node hashes only (key, value_hash) and so binds + // none of their bytes. A reference row that lies past + // the query limit legitimately stays a bare KVValueHash + // in released V1 proofs — the GroveDB post-pass only + // rewrites rows within the limit into KVRefValueHash* — + // so refusing references at this level would reject + // honest proofs. The binding contract for reference + // bytes is enforced by the only consumer of these rows: + // `verify_layer_proof_v1` rejects any raw reference row + // it consumes (issue #862). if proof_version >= 1 { let element_type = ElementType::from_serialized_value(value).map_err(|e| { @@ -427,6 +439,10 @@ impl QueryProofVerify for Query { println!("Processing KVValueHashFeatureType node"); } // Same check as KVValueHash — reject item elements. + // References pass here for the same reason as on + // KVValueHash (beyond-limit reference rows stay bare in + // released proofs); `verify_layer_proof_v1` refuses any + // raw reference row it consumes. // Skipped for V0 backwards compatibility. if proof_version >= 1 { let element_type = From fd70370c3058fb46bc38781c5a339284492cfd11 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 6 Sep 2026 23:33:57 +0200 Subject: [PATCH 2/2] test: accept the earlier raw-reference rejection in the #869 subquery forgery test Since #862 a raw reference row is refused as soon as it is deserialized, ahead of the arm chain's last-resort "neither descended into nor bound" rejection that #880 added. The forgery is rejected either way; the assertion now accepts both messages. Co-Authored-By: Claude Fable 5.1 --- grovedb/src/tests/unbound_empty_tree_tests.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/grovedb/src/tests/unbound_empty_tree_tests.rs b/grovedb/src/tests/unbound_empty_tree_tests.rs index 9abb88a84..405fe6ef1 100644 --- a/grovedb/src/tests/unbound_empty_tree_tests.rs +++ b/grovedb/src/tests/unbound_empty_tree_tests.rs @@ -532,8 +532,13 @@ fn unbound_non_tree_classification_under_a_subquery_is_rejected() { match outcome { Err(e) => { let message = format!("{e}"); + // Since #862 a raw reference row is refused as soon as + // it is deserialized — before the arm chain can classify + // it — so the specific rejection fires ahead of the + // last-resort one. Either way the forgery is rejected. assert!( - message.contains("neither descended into nor bound"), + message.contains("raw reference") + || message.contains("neither descended into nor bound"), "include_empty={include_empty}: got: {message}" ); }