From d4d8cee8d17b19b9a31b93ab80026bf386ca49f8 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 20:01:33 -0600 Subject: [PATCH] fix(id): refuse duplicate keys at read_records receipt (#6634) read_records parsed replicated record values with plain serde_json::from_slice and measured the value cap on the parsed value, so a duplicate object key collapsed last-wins before the cap was measured: an over-cap value hidden behind a duplicate passed receipt, while parse_records (publish, #6473) refuses the same shape. Route the parse through id_core::json_unique:: parse_json_without_duplicate_keys, mirroring parse_records, and add write_raw regression tests: a duplicate hiding an oversized value and a small nested duplicate are both refused naming the record key. --- pkgs/id/src/world_records.rs | 38 ++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/pkgs/id/src/world_records.rs b/pkgs/id/src/world_records.rs index 783674b854..cc1a88b0e2 100644 --- a/pkgs/id/src/world_records.rs +++ b/pkgs/id/src/world_records.rs @@ -266,8 +266,13 @@ pub async fn read_records(doc: &Doc, blobs: &Store) -> Result { .get_bytes(entry.content_hash()) .await .with_context(|| format!("content of record {key:?} is not available yet"))?; - let value: serde_json::Value = - serde_json::from_slice(&bytes).with_context(|| format!("record {key:?}"))?; + // A duplicate key is a parse error (#6520, #6634): the cap below + // must see the value as written, never a last-wins collapse of it — + // a duplicate could hide an over-cap value behind a small second + // entry. The publish path (parse_records) refuses the same shape. + let text = std::str::from_utf8(&bytes).with_context(|| format!("record {key:?}"))?; + let value = id_core::json_unique::parse_json_without_duplicate_keys(text) + .with_context(|| format!("record {key:?}"))?; let size = serde_json::to_vec(&value).map_or(usize::MAX, |v| v.len()); ensure!( size <= MAX_RECORD_VALUE_BYTES, @@ -657,6 +662,35 @@ mod tests { host.shutdown().await; } + #[tokio::test] + async fn read_records_refuses_a_duplicate_key_hiding_an_oversized_value() { + let host = Node::spawn().await; + let store = RecordsStore::open(&host.docs, None).await.unwrap(); + // As written this value is over the cap; collapsed last-wins it is + // {"k":"small"} and would pass — the #6634 receipt bypass. + let raw = format!( + "{{\"k\":\"{}\",\"k\":\"small\"}}", + "x".repeat(MAX_RECORD_VALUE_BYTES + 100) + ); + assert!(raw.len() > MAX_RECORD_VALUE_BYTES); + write_raw(&store, "dup", raw.into_bytes()).await; + let err = read_err(&store, &host.blobs).await; + assert!(err.contains("dup"), "{err}"); + host.shutdown().await; + } + + #[tokio::test] + async fn read_records_refuses_a_small_nested_duplicate_key() { + let host = Node::spawn().await; + let store = RecordsStore::open(&host.docs, None).await.unwrap(); + // Well under every cap, raw and collapsed: only the duplicate-key + // refusal can reject this value. + write_raw(&store, "nested", br#"{"a":{"b":1,"b":2}}"#.to_vec()).await; + let err = read_err(&store, &host.blobs).await; + assert!(err.contains("nested"), "{err}"); + host.shutdown().await; + } + #[tokio::test] async fn read_records_refuses_an_over_long_key() { let host = Node::spawn().await;