diff options
| author | tv <tv@krebsco.de> | 2026-11-15 16:47:46 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-15 16:47:46 +0000 |
| commit | f751f411e954001c124afa1e3591d10094df15d7 (patch) | |
| tree | ff680cf67f0198c8e97d645ff8dea21a55877499 /src/access/rules.rs | |
| parent | af120b87c42ca56f69bbe6f8d27c023919742430 (diff) | |
rules: nothing added to the time before a swarm turned strict
A swarm turned strict by writing the policy into it kept the time
before open: a change whose history lacks the policy's change is
judged by the policy then, none or one that isn't strict, needs no
signature there, and is taken whenever it comes, from any member. With
values read by `seq`, an envelope planted at that time with the
highest `seq` there is can never be followed: a ban, a user, a
declaration, for good.
Once a document's policy is strict, a change written under one that
isn't is refused, received or held, unless it is in the history of the
change that wrote the policy the document reads (`store::written_by`).
A node that holds one beside that change, taken before it came, is
told so by any peer that refused it (REFUSALS), judges it again, now
refuses it too, and drops it with what builds on it, as for any change
it took unjudged. A swarm strict from its genesis change has no such
time; one whose policy isn't strict judges as before.
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. |
