diff options
Diffstat (limited to 'src/access')
| -rw-r--r-- | src/access/policy.rs | 63 |
1 files changed, 32 insertions, 31 deletions
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. |
