diff options
| author | tv <tv@krebsco.de> | 2026-11-18 18:49:50 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-18 20:29:41 +0100 |
| commit | c64fcc05d8c53bee99ab039bd709039afb730cdd (patch) | |
| tree | 45baad78d7b92de42283be2954fc3f9b4dd6129d /src/federation/held.rs | |
| parent | 598366b03679eac9d04f32d9194f3ba4ba9c3da8 (diff) | |
federation: a node stops at a version it doesn't read
A version event writing a proposal passed on the version a node reads, which doesn't
read as a version there (of a later federation, using what a later continuum takes, or
none at all), the node can't judge: `Fold::stops`. The store stops at it: it takes
nothing and refuses nothing from then on, holding back whatever comes, that event first
(`Offered::Stopped`), and keeps none of it across a restart, where it stops there again.
One beside the version the node reads is refused of its own, as any other.
Its operator has it drop that event (`POST /admin/events/drop`), or one it builds on:
it is refused as dropped, or as built on what was, any version event writing that
version with it, kept in the store file; the node judges what it held back and goes on
under the version before. Allowed again, it stops there once more.
The node logs the stop and its end; `GET /events` names the event, why, and how many it
holds back (`stopped`); a put answers `stopped`, written into the document all the same;
a node voting by itself casts nothing while stopped. The test fold's eras tagged `new`
stop a store.
Row 39 is tested (`federation_attack_39`); of rows 9, 10 and 48, a node stops at the
version event.
Diffstat (limited to 'src/federation/held.rs')
| -rw-r--r-- | src/federation/held.rs | 195 |
1 files changed, 190 insertions, 5 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs index 200323e..5155573 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -135,6 +135,8 @@ struct Inner { none: Option<Id>, /// The pinned checkpoint not found in the document, logged once. unfound: bool, + /// The event the store stopped at, logged once (`Fold::stops`). + stopped: Option<Id>, /// The refused changes looked at, of those screening keeps (`look`). looked: HashSet<ChangeHash>, /// When, by its own clock, the node took each event its store took after its @@ -331,7 +333,7 @@ impl Holding { refused += 1; info!(event = %id, why = ?why, "The store refuses an event the document holds"); } - Offered::Held | Offered::Waiting => {} + Offered::Held | Offered::Waiting | Offered::Stopped => {} } } if taken > 0 || refused > 0 { @@ -341,11 +343,23 @@ impl Holding { } /// After the store changed: what the node keeps of its own, written to its file where - /// it changed, and the members, as the store reads now. + /// it changed, and the members, as the store reads now; whether it stopped, logged. fn settle(&self, inner: &mut Inner) { let Some(store) = &inner.store else { return; }; + let stopped = store.stopped().map(|(id, why, _)| (*id, why)); + if stopped.map(|(id, _)| id) != inner.stopped { + match stopped { + Some((id, why)) => error!( + event = %id, + why = %why, + "The store stopped at an event this continuum can't judge: it takes none until it runs one that does, or its operator has it drop that one" + ), + None => warn!("The store takes events again"), + } + inner.stopped = stopped.map(|(id, _)| id); + } let now = Instant::now(); let checkpoint = Id::of(store.checkpoint()); if inner.at_checkpoint.map(|(at, _)| at) != Some(checkpoint) { @@ -385,9 +399,10 @@ impl Holding { Some(offered) } - /// Drops `ids`, events the store took after its checkpoint, with everything built on - /// them, as its operator has the node do: it refuses them from then on, and keeps so - /// (`docs/store.md`, A node's own). What it dropped, in the order taken. + /// Drops `ids`, events the store took after its checkpoint or the one it stopped at, + /// with everything built on them, as its operator has the node do: it refuses them from + /// then on, and keeps so (`docs/store.md`, A node's own). What it dropped, in the order + /// taken, then the one it stopped at. pub(crate) fn drop_taken(&self, ids: &[Id]) -> Option<Result<Vec<Id>, Refused>> { let mut inner = self.inner.lock_or_recover(); let dropped = inner.store.as_mut()?.drop_taken(ids); @@ -657,6 +672,7 @@ impl Holding { /// What it signs now, voting by itself by its operator's rules (`vote::decide`), or by /// hand (`hand`, `vote::by_hand`): recorded, the record written, and offered to the /// store; the events, to put into the document. By hand, why not where it isn't. + /// Nothing while its store is stopped. fn sign(&self, hand: Option<(Id, bool)>) -> Result<Vec<Vec<u8>>, String> { let (Some(voter), Some(party)) = (&self.voter, &self.party) else { return Err("this node doesn't vote for its party".to_owned()); @@ -668,6 +684,15 @@ impl Holding { let (Some(store), Some(record)) = (&inner.store, &inner.record) else { return Err("this node holds no store".to_owned()); }; + // What it reads is behind what passed: it casts nothing. + if let Some((id, ..)) = store.stopped() { + return match hand { + None => Ok(Vec::new()), + Some(_) => Err(format!( + "its store stopped at {id}: it votes once it takes events again" + )), + }; + } let now = Instant::now(); let held = |id: &Id| inner.taken_at.get(id).map(|at| now.duration_since(*at)); let seen = Seen { @@ -1676,6 +1701,166 @@ mod tests { assert_eq!(current(&restarted), Some(one.hash())); } + /// Row 39 (`docs/federation.md`): a version of a federation this continuum doesn't + /// know passed, and a version event writes it: the node stops there, taking nothing + /// and refusing nothing of what comes after, and writes none of it to its file, before + /// or after a restart. Its operator has it drop that event: it takes again, under the + /// version before, and keeps so; allowed again, it stops there once more. A root of a + /// federation it doesn't know, it holds no store of. + #[test] + fn federation_attack_39() { + let (tv, lass, makefu) = ( + Party::new("tv", 1), + Party::new("lass", 2), + Party::new("makefu", 3), + ); + let first = Spec::first(&[&tv, &lass, &makefu], &[]); + let root = first.root(); + let on = first.hash(); + let later = |json: &[u8]| { + String::from_utf8(json.to_vec()) + .unwrap() + .replace(r#""federation":1"#, r#""federation":2"#) + }; + let unknown = + crate::federation::jcs::read(later(&first.after(on).json().bytes()).as_bytes()) + .unwrap(); + let hash = Id::of(&unknown.bytes()); + let proposal = tv.propose(&[Id::of(&root)], &unknown); + let votes = [&tv, &lass].map(|party| party.vote(&[Id::of(&proposal)], on, hash, true)); + let written = harness::version_event(&[Id::of(&votes[0]), Id::of(&votes[1])], hash); + let stopped_at = Id::of(&written); + let beside = tv.write(&[Id::of(&votes[0])], "home/tv/a", harness::str("1")); + let after = tv.write(&[stopped_at], "home/tv/b", harness::str("1")); + let mut doc = events::started(Federation::NAME, &root, &[]).unwrap(); + for event in [&proposal].into_iter().chain(&votes).chain([&written]) { + put(&mut doc, event); + } + let stopped = |holding: &Holding| { + holding.with(|store| store.stopped().map(|(id, _, held)| (*id, held))) + }; + let current = |holding: &Holding| holding.with(|store| store.state().current().hash); + let taken = |holding: &Holding, event: &[u8]| { + holding.with(|store| store.taken().contains(&Id::of(event))) + }; + let dir = crate::files::scratch_dir("held-attack-39"); + let path = dir.join("store.json"); + let files = Arc::new(Files::default()); + let holding = Holding::open(Some(path.clone()), Arc::clone(&files), None, None).unwrap(); + read(&holding, &doc, 0); + assert_eq!(stopped(&holding), Some(Some((stopped_at, 1)))); + put(&mut doc, &beside); + put(&mut doc, &after); + read(&holding, &doc, 0); + assert_eq!(stopped(&holding), Some(Some((stopped_at, 3)))); + assert_eq!( + holding.offer(tv.write(&[Id::of(&root)], "home/tv/c", harness::str("1"))), + Some(Offered::Stopped) + ); + assert_eq!(holding.passed(), Vec::<Vec<u8>>::new()); + assert_eq!(holding.with(|store| store.refused().count()), Some(0)); + assert!(!path.exists()); + // Restarted: it stops there again. + let restarted = Holding::open(Some(path.clone()), Arc::clone(&files), None, None).unwrap(); + read(&restarted, &doc, 0); + assert_eq!( + stopped(&restarted).flatten().map(|(id, _)| id), + Some(stopped_at) + ); + // Dropped: it takes again, under the version before, and refuses what is built on + // that event; it writes no version event naming that version. + assert_eq!( + restarted.drop_taken(&[stopped_at]), + Some(Ok(vec![stopped_at])) + ); + assert_eq!(stopped(&restarted), Some(None)); + assert_eq!(current(&restarted), Some(on)); + assert_eq!(taken(&restarted, &beside), Some(true)); + assert_eq!( + restarted.with(|store| store.refusal(&Id::of(&after)).cloned()), + Some(Some(Refusal::BuiltOn(stopped_at))) + ); + assert_eq!(restarted.passed(), Vec::<Vec<u8>>::new()); + // Kept so across a restart. + let again = Holding::open(Some(path.clone()), Arc::clone(&files), None, None).unwrap(); + read(&again, &doc, 0); + assert_eq!(stopped(&again), Some(None)); + assert_eq!(current(&again), Some(on)); + assert_eq!(taken(&again, &beside), Some(true)); + // Allowed again: it stops there once more. + assert!(again.allow(&[stopped_at])); + read(&again, &doc, 0); + assert_eq!( + stopped(&again).flatten().map(|(id, _)| id), + Some(stopped_at) + ); + + // A root of a federation it doesn't know. + let unknown_root = later(&root).into_bytes(); + assert_ne!(unknown_root, root); + let doc = events::started(Federation::NAME, &unknown_root, &[]).unwrap(); + let holding = Holding::default(); + read(&holding, &doc, 0); + assert!(holding.with(|_| ()).is_none()); + } + + /// A node voting by itself whose store stopped casts nothing, by itself or by hand: + /// what it reads is behind what passed. + #[test] + fn a_node_whose_store_stopped_votes_nothing() { + let (tv, lass, makefu) = ( + Party::new("tv", 1), + Party::new("lass", 2), + Party::new("makefu", 3), + ); + let first = Spec::first(&[&tv, &lass, &makefu], &[]); + let root = first.root(); + let on = first.hash(); + let mut none = first.after(on); + none.rules + .push(("_continuum/x".to_owned(), harness::str("tv"))); + let next = first.after(on); + let proposal = tv.propose(&[Id::of(&root)], &none.json()); + let other = lass.propose(&[Id::of(&root)], &next.json()); + let votes = + [&tv, &lass].map(|party| party.vote(&[Id::of(&proposal)], on, none.hash(), true)); + let written = harness::version_event(&[Id::of(&votes[0]), Id::of(&votes[1])], none.hash()); + let mut doc = events::started(Federation::NAME, &root, &[]).unwrap(); + for event in [&proposal, &other] + .into_iter() + .chain(&votes) + .chain([&written]) + { + put(&mut doc, event); + } + let dir = crate::files::scratch_dir("held-stopped-votes"); + let record = dir.join("makefu.json"); + let version = Version::read(&first.json()).unwrap(); + let mut begun = Record::begin(Id::of(&root), "makefu", &version); + begun.began = Id::of(b"before the federation").to_string(); + begun.save(&record).unwrap(); + let holding = Holding::open( + None, + Arc::new(Files::default()), + None, + Some("makefu".into()), + ) + .unwrap() + .voting(Some(Voter { + key: makefu.key.clone(), + record, + rules: Rules::default(), + })); + read(&holding, &doc, 0); + assert!(holding.with(|store| store.stopped().is_some()).unwrap()); + assert_eq!(holding.act(), Vec::<Vec<u8>>::new()); + let why = holding.vote_by_hand(next.hash(), true).unwrap_err(); + assert!(why.contains("stopped"), "{why}"); + // Dropped: it votes again. + holding.drop_taken(&[Id::of(&written)]).unwrap().unwrap(); + assert!(holding.vote_by_hand(next.hash(), true).is_ok()); + } + // The store's attack rows (`docs/store.md`, Attacks) read from a document. /// tv's federation, its root, and a document started from it. |
