diff options
| author | tv <tv@krebsco.de> | 2026-11-15 18:52:49 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-15 18:52:49 +0000 |
| commit | fbe98b00c9e98ebad02fe18103ca02bdfe1ed9e5 (patch) | |
| tree | eb899837386d4fffa59a027e2bacdb69f9380bfc /src | |
| parent | c8f7165bae7344c0fb0d5265b7736de8ed26d91c (diff) | |
store: values ordered by seq as the policy a read shows says
Whether values are read by `seq` went by whether any value at the
policy's place asks for signed values, not by what the policy reads as.
Where the place holds two, they part: a signed policy that isn't strict
read before a plain one that is, or two maps, the newer not strict. Such
a swarm judged as not strict and read by `seq`.
Now the policy's own place is read by `seq` where any value there asks
for signed values, so an older policy put back beside a strict one
can't undo it; every other place is read by `seq` only where the policy
that read shows is strict (`policy::read`, which `policy::of` reads
with). A swarm whose policy doesn't read as strict reads every place as
Automerge has it.
Diffstat (limited to 'src')
| -rw-r--r-- | src/access/policy.rs | 14 | ||||
| -rw-r--r-- | src/document/store.rs | 97 |
2 files changed, 86 insertions, 25 deletions
diff --git a/src/access/policy.rs b/src/access/policy.rs index 9f18eb5..97bea27 100644 --- a/src/access/policy.rs +++ b/src/access/policy.rs @@ -88,11 +88,15 @@ pub(crate) fn first( /// The policy `doc` holds. pub(crate) fn of(doc: &impl ReadDoc) -> Policy { - let value = match store::read_plain(doc, &path()) { - Ok(Some(value)) => value, - Ok(None) => return Policy::Open, - Err(error) => return Policy::Unknown(format!("it doesn't read: {error}")), - }; + match store::read_plain(doc, &path()) { + Ok(Some(value)) => read(&value), + Ok(None) => Policy::Open, + Err(error) => Policy::Unknown(format!("it doesn't read: {error}")), + } +} + +/// The policy `value`, as the policy's place holds it, says. +pub(crate) fn read(value: &JsonValue) -> Policy { let version = value.get("version").and_then(JsonValue::as_u64); if version != Some(VERSION) { return Policy::Unknown(format!( diff --git a/src/document/store.rs b/src/document/store.rs index 759d188..1f26053 100644 --- a/src/document/store.rs +++ b/src/document/store.rs @@ -225,8 +225,11 @@ fn signed_order(value: &Value<'_>) -> Option<(u64, [u8; 32])> { } /// Whether `doc`'s policy asks for signed values (`access::policy`: `"values": -/// "signed"`), as any value at its place says, read as written: below every reading -/// that goes by it (`candidates`), the policy's own included. +/// "signed"`), as a read of it shows it: below every reading that goes by it +/// (`candidates`). Its own place is read by `seq` wherever any value there asks for +/// signed values, so an older policy put back beside a strict one can't undo it; every +/// other place, as the policy that read shows says. Read here as written, not through +/// `candidates`, which asks this. fn signs_values(doc: &impl ReadDoc) -> bool { let mut maps = vec![automerge::ROOT]; for key in [super::meta::ROOT, super::meta::ACCESS] { @@ -236,26 +239,46 @@ fn signs_values(doc: &impl ReadDoc) -> bool { .filter_map(|(value, id)| matches!(value, Value::Object(ObjType::Map)).then_some(id)) .collect(); } - let asks = |value: &JsonValue| value.get("values") == Some(&JsonValue::from("signed")); - maps.iter() - .flat_map(|map| doc.get_all(map, "policy").unwrap_or_default()) - .any(|(value, id)| match value { - Value::Object(ObjType::Map) => doc - .get_all(&id, "values") - .unwrap_or_default() - .iter() - .any(|(value, _)| { - matches!(value, Value::Scalar(scalar) - if matches!(scalar.as_ref(), ScalarValue::Str(text) if text == "signed")) - }), - Value::Scalar(scalar) => match scalar.as_ref() { - ScalarValue::Str(text) => { - super::signed::read(text).is_some_and(|payload| asks(&payload.value)) + let policy = |value: &Value<'_>, id: &ObjId| -> Option<JsonValue> { + match value { + Value::Object(ObjType::Map) => { + let mut fields = serde_json::Map::new(); + for key in doc.keys(id) { + if let Ok(Some((Value::Scalar(scalar), _))) = doc.get(id, key.as_str()) + && let Some(field) = typed::scalar_to_json(&scalar, JsonForm::default()) + { + fields.insert(key, field); + } } - _ => false, + Some(JsonValue::Object(fields)) + } + Value::Scalar(scalar) => match scalar.as_ref() { + ScalarValue::Str(text) => super::signed::read(text).map(|payload| payload.value), + _ => None, }, - _ => false, - }) + _ => None, + } + }; + let asks = |json: Option<JsonValue>| { + json.is_some_and(|json| json.get("values") == Some(&JsonValue::from("signed"))) + }; + let mut all: Vec<(Value<'static>, ObjId)> = maps + .iter() + .flat_map(|map| doc.get_all(map, "policy").unwrap_or_default()) + .map(|(value, id)| (value.into_owned(), id)) + .collect(); + if !all.iter().any(|(value, id)| asks(policy(value, id))) { + return false; + } + // As `candidates` orders them where the policy asks for signed values. + all.sort_by(|a, b| op_order(&b.1).cmp(&op_order(&a.1))); + all.sort_by_key(|(value, _)| match signed_order(value) { + Some((seq, digest)) => (false, std::cmp::Reverse(seq), digest), + None => (true, std::cmp::Reverse(0), [0; 32]), + }); + all.first() + .and_then(|(value, id)| policy(value, id)) + .is_some_and(|json| crate::access::policy::read(&json).strict()) } /// The changes that wrote what `path` holds as `view` (of `doc`) shows it: each value @@ -1853,6 +1876,40 @@ mod tests { ) } + /// Values are read by `seq` as the policy a read shows says: its own place by `seq` + /// where any value there asks for signed values, so a signed policy that doesn't, read + /// before a plain one that does, leaves every other place read as Automerge has it. + #[test] + fn values_go_by_the_policy_as_read() { + let doc = |actor: &[u8]| crate::document::Document::new().with_actor(ActorId::from(actor)); + let (mut ours, mut theirs) = (doc(b"a"), doc(b"b")); + try_change(&mut ours, |doc| { + write_path( + doc, + &path("_continuum/access/policy"), + &json!({"version": 1, "values": "signed"}), + )?; + write_path(doc, &path("a"), &json!(envelope_of(&json!("higher"), 1))) + }) + .unwrap(); + try_change(&mut theirs, |doc| { + write_path( + doc, + &path("_continuum/access/policy"), + &json!(envelope_of(&json!({"version": 1}), 0)), + )?; + for round in 0..20 { + write_path(doc, &path("padding"), &json!(round))?; + } + write_path(doc, &path("a"), &json!(envelope_of(&json!("newer"), 0))) + }) + .unwrap(); + ours.merge(&mut theirs).unwrap(); + assert!(!crate::access::policy::of(&ours).strict()); + assert!(!signs_values(&ours)); + assert_eq!(read_plain(&ours, &path("a")).unwrap(), Some(json!("newer"))); + } + /// Of signed values written at once at one place, where the policy asks for signed /// values, the highest `seq` is read, whichever was written last, and before /// anything unsigned; a write replaces them all. Without such a policy, the newest, |
