diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/access/policy.rs | 64 | ||||
| -rw-r--r-- | src/document/store.rs | 11 |
2 files changed, 64 insertions, 11 deletions
diff --git a/src/access/policy.rs b/src/access/policy.rs index 97bea27..54e70d0 100644 --- a/src/access/policy.rs +++ b/src/access/policy.rs @@ -86,9 +86,16 @@ pub(crate) fn first( } } -/// The policy `doc` holds. +/// 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. pub(crate) fn of(doc: &impl ReadDoc) -> Policy { - match store::read_plain(doc, &path()) { + 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}")), @@ -106,22 +113,30 @@ pub(crate) fn read(value: &JsonValue) -> Policy { .map_or("none".to_owned(), JsonValue::to_string) )); } - 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:?}")), + // 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, }; - match value.get("signatures").and_then(JsonValue::as_str) { + match field("signatures") { // Signed values: signed changes too. - None | Some("optional") => Policy::Enforced { + Ok(None | Some("optional")) => Policy::Enforced { required: strict, strict, }, - Some("required") => Policy::Enforced { + Ok(Some("required")) => Policy::Enforced { required: true, strict, }, - Some(other) => Policy::Unknown(format!("signatures {other:?}")), + Ok(Some(other)) => Policy::Unknown(format!("signatures {other:?}")), + Err(unknown) => unknown, } } @@ -178,6 +193,35 @@ 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. + let typed = |policy: JsonValue| { + use crate::document::typed::{TimestampFormat, Val}; + let value = Val::from_typed_json(&policy, TimestampFormat::default()).unwrap(); + let mut doc = Document::new(); + change(&mut doc, |doc| { + store::write_val_path( + doc, + &path(), + &value, + store::Writing::sticky(TimestampFormat::default()), + ) + .unwrap(); + }); + of(&doc) + }; + assert!(matches!( + typed(json!({"version": 1, "values": {"$text": "signed"}})), + Policy::Unknown(_) + )); + assert!(matches!( + 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. diff --git a/src/document/store.rs b/src/document/store.rs index 77d5c81..8da6270 100644 --- a/src/document/store.rs +++ b/src/document/store.rs @@ -590,7 +590,16 @@ pub(crate) fn read_plain( doc: &impl ReadDoc, path: &[String], ) -> Result<Option<JsonValue>, StoreError> { - Ok(match read_path(doc, path)? { + read_plain_as(doc, path, JsonForm::default()) +} + +/// [`read_plain`], in JSON of `form`. +pub(crate) fn read_plain_as( + doc: &impl ReadDoc, + path: &[String], + form: JsonForm, +) -> Result<Option<JsonValue>, StoreError> { + Ok(match read_path_as(doc, path, form)? { Some(JsonValue::String(text)) => match super::signed::read(&text) { Some(payload) => Some(payload.value).filter(|value| !value.is_null()), None => Some(JsonValue::String(text)), |
