diff options
| -rw-r--r-- | docs/access.md | 6 | ||||
| -rw-r--r-- | src/access/rules.rs | 78 | ||||
| -rw-r--r-- | src/document/store.rs | 21 |
3 files changed, 104 insertions, 1 deletions
diff --git a/docs/access.md b/docs/access.md index 89e74d7..b2cdb70 100644 --- a/docs/access.md +++ b/docs/access.md @@ -470,7 +470,11 @@ that isn't strict, a member may write as any user). So a swarm is best strict fr start (`--bootstrap-policy`, A new swarm, above); one already running turns strict through a remake (Turning a swarm strict, below), which keeps only what an operator looked over. Writing the strict policy into it directly trusts it as it is: look it -over first (`continuum-signed verify` on each entry). A value there that isn't an envelope +over first (`continuum-signed verify` on each entry). From then on nothing is added to +the time before: a change written under a policy that isn't strict is refused, received +or held, unless it is in the history of the change that wrote the strict policy, so a +member can't plant at that time what nobody could follow; a node that took one beside +that change drops it once a peer tells it so (Changes judged on every node, below). 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. diff --git a/src/access/rules.rs b/src/access/rules.rs index 738cdf8..0690696 100644 --- a/src/access/rules.rs +++ b/src/access/rules.rs @@ -159,6 +159,11 @@ pub(crate) fn judge_reading( .policy .get_or_insert_with(|| super::policy::of(&at)) .clone(); + if !policy.strict() + && let Some(why) = before_strict(doc, change) + { + return Err(Refusal::Rule(why)); + } if !policy.enforced() { return Ok(()); } @@ -345,6 +350,31 @@ pub(crate) fn judge_reading( Ok(()) } +/// Why `change`, written under a policy that isn't strict, is refused where `doc`'s is: +/// it isn't in the history of the change that wrote the policy `doc` reads. Nothing is +/// added to the time before a swarm turned strict, so nothing planted there can stand +/// where every value is its user's to sign; what a node held of it beside that change +/// it drops, once told (`swarm::refusals`). A swarm strict from its genesis change +/// (`--bootstrap-policy`) has no such time. None where `doc`'s policy isn't strict: +/// there, nothing changes. +fn before_strict(doc: &Document, change: &Change<'_>) -> Option<String> { + if !super::policy::of(doc).strict() { + return None; + } + let policy = store::written_by(doc, &super::policy::path())?; + let before = change.hash == policy + || (crate::document::changes::holds(doc, &change.hash) + && !doc + .get_changes(&[policy]) + .iter() + .any(|later| later.hash() == change.hash)); + (!before).then(|| { + format!( + "written before the swarm turned strict, beside the change that turned it ({policy})" + ) + }) +} + /// `hash`, which `doc` (of `epoch`) holds, judged by its rules now, as received: why /// they refuse it, if they do; None if `doc` doesn't hold it. A change this node just /// made, before it leaves (`AppState::mutate_doc_for`), or one a peer says it refused @@ -1131,6 +1161,54 @@ mod tests { assert!(judged(&doc, "stranger", Some("mallory"), &["notes/x"]).is_ok()); } + /// Once a swarm turned strict, a change written before it, beside the change that + /// turned it, is refused, received or held: nothing is added to the time before. What + /// is in that change's history stands. A swarm that isn't strict judges as before. + #[test] + fn nothing_added_before_a_strict_policy() { + let mut doc = Document::new(); + write(&mut doc, "_continuum/swarm", json!("s")); + write(&mut doc, "_continuum/members/genesis", json!({"since": 1})); + let before = doc.get_heads(); + write(&mut doc, "notes/kept", json!(1)); + // Beside it: what turns the swarm strict, and a change the node holds already. + let mut beside = doc.fork_at(&before).unwrap(); + write(&mut beside, "notes/beside", json!(2)); + let planted = beside.get_heads()[0]; + let kept = doc.get_heads()[0]; + // A policy that isn't strict: the change beside stands. + let mut open = doc.clone(); + write(&mut open, "_continuum/access/policy", json!({"version": 1})); + open.merge(&mut beside.clone()).unwrap(); + assert_eq!(judge_held(&open, Epoch::GENESIS, &planted), Some(Ok(()))); + write( + &mut doc, + "_continuum/access/policy", + json!({"version": 1, "values": "signed"}), + ); + doc.merge(&mut beside).unwrap(); + assert!(matches!( + judge_held(&doc, Epoch::GENESIS, &planted), + Some(Err(_)) + )); + assert_eq!(judge_held(&doc, Epoch::GENESIS, &kept), Some(Ok(()))); + // Received, from the time before: refused too. + let touched = paths(&["notes/late"]); + assert!(refused(judge( + &doc, + Epoch::GENESIS, + &Change { + hash: ChangeHash([9; 32]), + deps: &before, + seen: &before, + committer: Some("genesis"), + user: None, + paths: &touched, + ops: &[], + }, + ))); + } + /// A map made on the way to what is written, where there was none: judged by what is /// written within it; one made where there is one, or holding nothing written, as /// any write there. diff --git a/src/document/store.rs b/src/document/store.rs index 8b8894c..9afb5d3 100644 --- a/src/document/store.rs +++ b/src/document/store.rs @@ -258,6 +258,27 @@ fn signs_values(doc: &impl ReadDoc) -> bool { }) } +/// The change that wrote the value a read of `path` shows (`candidates`), if `doc` holds +/// one there. +pub(crate) fn written_by(doc: &Automerge, path: &[String]) -> Option<automerge::ChangeHash> { + let (leaf, parents) = path.split_last()?; + let Some(Resolved::Maps(maps)) = resolve_through(doc, parents).ok()? else { + return None; + }; + let (_, id) = candidates(doc, &maps, leaf.as_str()) + .ok()? + .into_iter() + .next()?; + let ObjId::Id(counter, actor, _) = id else { + return None; + }; + doc.get_changes(&[]).into_iter().find_map(|change| { + let start = change.start_op().get(); + (change.actor_id() == &actor && (start..start + change.len() as u64).contains(&counter)) + .then(|| change.hash()) + }) +} + /// What `prop` of `parent` holds as a read shows it, of the values written there at /// once (`candidates`): the value, and its operation's ID. pub(crate) fn shown( |
