From 3152af43504bcfd6285d41f6b6ff67631d01fd0e Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 15 Nov 2026 16:31:37 +0000 Subject: signed values: one reading of an envelope, everywhere Three parts read an envelope three ways: the store's order took any string with the prefix whose meta held a `seq`, its reads any whose value decoded, the checks only one with four parts and a meta of exactly its fields. A string that the first two took and the checks refused was read first, read as the registry's or the policy's value, and treated as nothing by the check of the next write there. `document::signed` reads an envelope the one way: four parts, the value JSON, the meta exactly its six fields (one more, or one twice, and it isn't one), no newline in either, the signature decoding. The store's order, its plain reads, the checks, the API and the users API all go by it. What isn't one in every respect is a plain string, wherever it is read. --- src/document/store.rs | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) (limited to 'src/document/store.rs') diff --git a/src/document/store.rs b/src/document/store.rs index aa79e47..06dcce4 100644 --- a/src/document/store.rs +++ b/src/document/store.rs @@ -210,13 +210,9 @@ enum Resolved { Other(Value<'static>, ObjId), } -/// What every signed value starts with (`access::envelope`). -pub(crate) const ENVELOPE: &str = "cv2."; - /// Where `value` stands among the values written at once at one place, if it is a -/// signed value (`access::envelope`), unchecked: its `seq`, and its digest. +/// signed value (`document::signed`), unchecked: its `seq`, and its digest. fn signed_order(value: &Value<'_>) -> Option<(u64, [u8; 32])> { - use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; use sha2::{Digest, Sha256}; let Value::Scalar(scalar) = value else { return None; @@ -224,12 +220,8 @@ fn signed_order(value: &Value<'_>) -> Option<(u64, [u8; 32])> { let ScalarValue::Str(text) = scalar.as_ref() else { return None; }; - let meta = text.strip_prefix(ENVELOPE)?.split('.').nth(1)?; - let meta: JsonValue = serde_json::from_slice(&URL_SAFE_NO_PAD.decode(meta).ok()?).ok()?; - Some(( - meta.get("seq")?.as_u64()?, - Sha256::digest(text.as_bytes()).into(), - )) + let payload = super::signed::read(text)?; + Some((payload.seq, Sha256::digest(text.as_bytes()).into())) } /// All concurrent values of `key` across a merged set of parent maps, the one that @@ -502,21 +494,14 @@ pub(crate) fn read_plain( path: &[String], ) -> Result, StoreError> { Ok(match read_path(doc, path)? { - Some(JsonValue::String(text)) if text.starts_with(ENVELOPE) => { - enveloped(&text).filter(|value| !value.is_null()) - } + Some(JsonValue::String(text)) => match super::signed::read(&text) { + Some(payload) => Some(payload.value).filter(|value| !value.is_null()), + None => Some(JsonValue::String(text)), + }, other => other, }) } -/// The value an envelope (`cv2...`) holds; None if it isn't -/// one. -fn enveloped(text: &str) -> Option { - use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; - let value = text.split('.').nth(1)?; - serde_json::from_slice(&URL_SAFE_NO_PAD.decode(value).ok()?).ok() -} - /// Reads a path; the empty path is the whole store. pub(crate) fn read_path( doc: &impl ReadDoc, @@ -1743,7 +1728,8 @@ mod tests { let meta = json!({"swarm": "s", "path": ["a"], "user": "alice", "at": 1, "seq": seq, "prev": ""}); format!( - "{ENVELOPE}{}.{}.c2ln", + "{}{}.{}.c2ln", + crate::document::signed::START, URL_SAFE_NO_PAD.encode(value.to_string()), URL_SAFE_NO_PAD.encode(meta.to_string()) ) @@ -1788,7 +1774,7 @@ mod tests { } /// An envelope reads as the value it holds, a signed `null` as nothing, anything - /// else as it is. + /// else (a string shaped like one but not one in every respect too) as it is. #[test] fn plain_reads_open_envelopes() { let sealed = |value: JsonValue| envelope_of(&value, 0); @@ -1796,7 +1782,9 @@ mod tests { try_change(&mut doc, |doc| { write_path(doc, &path("a"), &json!(sealed(json!({"mode": "fast"}))))?; write_path(doc, &path("gone"), &json!(sealed(JsonValue::Null)))?; - write_path(doc, &path("plain"), &json!("cv2-ish")) + write_path(doc, &path("plain"), &json!("cv2-ish"))?; + // Shaped like one, short of its meta's fields: a plain string. + write_path(doc, &path("short"), &json!("cv2.MQ.eyJzZXEiOjF9.c2ln")) }) .unwrap(); assert_eq!( @@ -1808,6 +1796,10 @@ mod tests { read_plain(&doc, &path("plain")).unwrap(), Some(json!("cv2-ish")) ); + assert_eq!( + read_plain(&doc, &path("short")).unwrap(), + Some(json!("cv2.MQ.eyJzZXEiOjF9.c2ln")) + ); assert_eq!(read_plain(&doc, &path("none")).unwrap(), None); } -- cgit v1.3.1