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