aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--docs/access.md13
-rw-r--r--src/access/policy.rs64
-rw-r--r--src/document/store.rs11
3 files changed, 72 insertions, 16 deletions
diff --git a/docs/access.md b/docs/access.md
index 942621c..a85a3e5 100644
--- a/docs/access.md
+++ b/docs/access.md
@@ -469,12 +469,15 @@ 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. 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
+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
+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 its chain anew: any envelope its
-user once signed for that path replacing nothing (`prev` empty) can be put there again.
-Without the policy, signed values may be written all the same, and plain ones too.
+swarm from its content (A new swarm, above). A value there that isn't an envelope starts
+its chain anew: any envelope its user once signed for that path replacing nothing
+(`prev` empty) can be put there again. Without the policy, signed values may be written
+all the same, and plain ones too.
Keep two users who may manage users, or two keys each: under a strict policy, a
registry whose only manager lost its keys changes no more (the protocol writers do
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)),