diff options
| author | tv <tv@krebsco.de> | 2026-11-16 02:10:47 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-16 02:10:47 +0000 |
| commit | 0afc17ab0c6e0e34ce63c33db736e4fe59305c12 (patch) | |
| tree | c125c4b128201346d089bd5aab48238f7d24dff9 | |
| parent | b606449e51b8233d214f14500e0e47cd42af5744 (diff) | |
policy: strict only where its values is a string
The policy was read in typed JSON, so that an Automerge text spelling
"signed" at its `values`, which no operation `rules::turns_strict` asks
of writes, turns no swarm strict. But that read every policy anew: a
setting that wasn't a string, or a text at `signatures`, made a policy
no node knows in swarms that aren't strict, which read it as before.
The policy is read in plain JSON again, as it was, and only its
strictness asks more (`policy::of`): it reads strict only where its
`values` is a string in typed JSON, an envelope read as its payload.
A policy that would read strict only through a text, at `values` or
holding the policy, is one no node knows. Every policy that doesn't read
strict reads as it did.
| -rw-r--r-- | docs/access.md | 6 | ||||
| -rw-r--r-- | src/access/policy.rs | 63 |
2 files changed, 35 insertions, 34 deletions
diff --git a/docs/access.md b/docs/access.md index a85a3e5..dcd67f8 100644 --- a/docs/access.md +++ b/docs/access.md @@ -469,9 +469,9 @@ changes nothing (`409`). A swarm turns strict only at its start (`--bootstrap-po new swarm, above) or through a remake (Turning a swarm strict, below), which keeps only what an operator looked over: each is its epoch's first change, so no change of a strict epoch is from before its policy. A change writing a strict policy where the policy isn't -strict is refused; and a policy reads only as continuum writes it, plain JSON or an -envelope: a text, or a setting that isn't a string, makes it one no node knows, never -what its characters spell. A strict swarm stays strict: its policy is written again only +strict is refused; and a policy reads strict only where its `values` is a string, as +continuum writes it: one that would read strict only through an Automerge text is one no +node knows, never what its characters spell. A strict swarm stays strict: its policy is written again only as strict, never relaxed nor deleted, nor moved to a policy its nodes don't know (a node that doesn't know a policy reads it as none it can judge by); to leave it, start a new swarm from its content (A new swarm, above). A value there that isn't an envelope starts diff --git a/src/access/policy.rs b/src/access/policy.rs index 23e445d..9d6d198 100644 --- a/src/access/policy.rs +++ b/src/access/policy.rs @@ -90,20 +90,34 @@ pub(crate) fn as_written(value: &JsonValue) -> Result<Policy, String> { Ok(policy) } -/// The policy `doc` holds, read in typed JSON: what continuum writes there reads as it -/// is, plain JSON or an envelope, and anything else (a text, a counter, bytes) as itself, -/// tagged, so that a policy holding one where it says what it is reads as one nobody -/// knows, never as what its characters spell. +/// The policy `doc` holds. Strict only where its `values` is a string, as continuum +/// writes it: an Automerge text there, or one holding the policy, reads as a string in +/// plain JSON, so a policy that would read strict only through a text is one nobody +/// knows, never what its characters spell. pub(crate) fn of(doc: &impl ReadDoc) -> Policy { + match store::read_plain(doc, &path()) { + Ok(Some(value)) => match read(&value) { + policy if policy.strict() && !values_a_string(doc) => { + Policy::Unknown("values not a string".to_owned()) + } + policy => policy, + }, + Ok(None) => Policy::Open, + Err(error) => Policy::Unknown(format!("it doesn't read: {error}")), + } +} + +/// Whether the policy `doc` holds, read in typed JSON (where a text is tagged, and an +/// envelope reads as its payload), has a string for its `values`. +fn values_a_string(doc: &impl ReadDoc) -> bool { let typed = crate::document::typed::JsonForm { typed: true, ..Default::default() }; - match store::read_plain_as(doc, &path(), typed) { - Ok(Some(value)) => read(&value), - Ok(None) => Policy::Open, - Err(error) => Policy::Unknown(format!("it doesn't read: {error}")), - } + matches!( + store::read_plain_as(doc, &path(), typed), + Ok(Some(value)) if value.get("values").is_some_and(JsonValue::is_string) + ) } /// The policy `value`, as the policy's place holds it, says. @@ -117,30 +131,22 @@ pub(crate) fn read(value: &JsonValue) -> Policy { .map_or("none".to_owned(), JsonValue::to_string) )); } - // Each a string where it is given at all. - let field = |name: &str| match value.get(name) { - None => Ok(None), - Some(JsonValue::String(text)) => Ok(Some(text.as_str())), - Some(other) => Err(Policy::Unknown(format!("{name} {other}"))), - }; - let strict = match field("values") { - Ok(None | Some("plain")) => false, - Ok(Some("signed")) => true, - Ok(Some(other)) => return Policy::Unknown(format!("values {other:?}")), - Err(unknown) => return unknown, + let strict = match value.get("values").and_then(JsonValue::as_str) { + None | Some("plain") => false, + Some("signed") => true, + Some(other) => return Policy::Unknown(format!("values {other:?}")), }; - match field("signatures") { + match value.get("signatures").and_then(JsonValue::as_str) { // Signed values: signed changes too. - Ok(None | Some("optional")) => Policy::Enforced { + None | Some("optional") => Policy::Enforced { required: strict, strict, }, - Ok(Some("required")) => Policy::Enforced { + Some("required") => Policy::Enforced { required: true, strict, }, - Ok(Some(other)) => Policy::Unknown(format!("signatures {other:?}")), - Err(unknown) => unknown, + Some(other) => Policy::Unknown(format!("signatures {other:?}")), } } @@ -197,8 +203,7 @@ mod tests { Policy::Unknown(_) )); assert!(matches!(with(json!("on")), Policy::Unknown(_))); - // A text where the policy says what it is, whatever its characters spell, and a - // field that isn't a string: one nobody knows. + // Strict only through a text, whatever its characters spell: one nobody knows. let typed = |policy: JsonValue| { use crate::document::typed::{TimestampFormat, Val}; let value = Val::from_typed_json(&policy, TimestampFormat::default()).unwrap(); @@ -222,10 +227,6 @@ mod tests { typed(json!({"$text": "{\"version\": 1, \"values\": \"signed\"}"})), Policy::Unknown(_) )); - assert!(matches!( - with(json!({"version": 1, "values": 5})), - Policy::Unknown(_) - )); } /// A new swarm's policy: one this node knows; strict, only with keys to sign. |
