aboutsummaryrefslogtreecommitdiffstats
path: root/src/access/policy.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-15 23:34:11 +0000
committertv <tv@krebsco.de>2026-11-15 23:34:11 +0000
commit3827195e784999c772357e547d3d4efc9bb8d627 (patch)
tree0f0806ee290bfb779b5dcf15c9936f44da3b75d2 /src/access/policy.rs
parentcc998c1d45939d170bcf8a6512817405ed2b44a3 (diff)
policy: read only as continuum writes it
A swarm turns strict only at its start or through a strict remake, and a change writing a strict policy where the policy isn't strict is refused, found by its operations: `"signed"` put at the policy's `values`, or an envelope asking for it put at its place. But the policy was read in plain JSON, where an Automerge text reads as a string, and writing a text is no such operation: `{"$text": "signed"}` at `values`, through the typed API, turned a swarm strict that nothing refused, with a time before it nothing closes. The policy is read in typed JSON now (`store::read_plain_as`): what continuum writes there reads as it is, plain JSON or an envelope, and a text, a counter or bytes as itself, tagged; a setting that isn't a string makes a policy no node knows (`policy::read`), as a version it doesn't know does. So no text turns a swarm strict, and the operations `rules::turns_strict` asks of are the only ways to. A policy holding none of these reads as before.
Diffstat (limited to 'src/access/policy.rs')
-rw-r--r--src/access/policy.rs64
1 files changed, 54 insertions, 10 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.