diff options
| author | tv <tv@krebsco.de> | 2026-11-19 01:48:57 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-19 03:18:49 +0100 |
| commit | 03744437a5ffc36fbb5e973a0ddfd6b91945b212 (patch) | |
| tree | 227eb42b4e7cac546b857cfacb79ab890e8bb137 /src/federation | |
| parent | f24063e90d02ef76d87401d6e74cf8c9f489ff7d (diff) | |
federation: votes on the events proposing a checkpoint above its heads
Anyone could propose a checkpoint's version again, by an event beside its heads. A node
voting by itself, and a party's client, built a vote on every event proposing what it
voted on, so every vote was beside the heads, and no version event written on them was
taken: the checkpoint could never move the store. And the voters' rule refused it once
that event was held for a while, as the heads left it out.
They vote on the events proposing it that are above the heads it names
(`State::proposing`), which `GET /events/counts` lists as `proposals`; where there are
none, `none` says so, the node casts nothing and the client says why. The voters' rule
leaves proposals and votes aside, as they lapse with the move.
Row 70 is tested (`federation_attack_70`).
Diffstat (limited to 'src/federation')
| -rw-r--r-- | src/federation/fold.rs | 27 | ||||
| -rw-r--r-- | src/federation/held.rs | 112 | ||||
| -rw-r--r-- | src/federation/party.rs | 10 | ||||
| -rw-r--r-- | src/federation/vote.rs | 14 |
4 files changed, 147 insertions, 16 deletions
diff --git a/src/federation/fold.rs b/src/federation/fold.rs index 2e3fc56..7986a13 100644 --- a/src/federation/fold.rs +++ b/src/federation/fold.rs @@ -354,6 +354,33 @@ impl State { self.proposed.get(proposal) } + /// Of the events proposing the proposal of hash `proposal` on `on`, those a vote on it + /// may build on: where its version names a checkpoint, those above its heads, so that + /// a version event written on the votes is above them too. One proposing it again, + /// beside them, a vote doesn't build on. + pub(crate) fn proposing(&self, on: &Id, proposal: &Id) -> Vec<Id> { + let proposals = self + .ballots(on) + .remove(proposal) + .map(|ballot| ballot.proposals) + .unwrap_or_default(); + let heads = self + .proposed + .get(proposal) + .and_then(|json| Version::read(json).ok()) + .and_then(|version| version.checkpoint); + match heads { + None => proposals, + Some(heads) => { + let above = self.above(&heads.into_iter().collect()); + proposals + .into_iter() + .filter(|id| above.contains(id)) + .collect() + } + } + } + /// The version event writing the proposal of hash `proposal` on the current version, /// where it passed here: on the `true` votes of the first parties by name that pass it, /// each party's of the lowest ID, so that nodes holding those votes write the same. diff --git a/src/federation/held.rs b/src/federation/held.rs index 66f20e9..93e2505 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -728,7 +728,6 @@ impl Holding { return Ok(Vec::new()); } let current = store.state().current().clone(); - let ballots = store.state().ballots(¤t.hash); let heads = store.heads(); let line = crate::access::sshsig::PublicKey(voter.key.verifying_key().to_bytes()).line(); let mut record = record.clone(); @@ -740,21 +739,24 @@ impl Holding { yes, nays, } => { - // On the events proposing it: above the heads it names for a - // checkpoint wherever they are, so a version event may be written on - // it, which the node's heads, holding what is beside them, may not be. - let proposing = ballots - .get(&proposal) - .map_or_else(Vec::new, |ballot| ballot.proposals.clone()); + // On the events proposing it, those above the heads it names for a + // checkpoint, so a version event may be written on it, which the + // node's heads, holding what is beside them, may not be. + let proposing = store.state().proposing(¤t.hash, &proposal); + if proposing.is_empty() { + if hand.is_some() { + return Err(format!( + "no event proposing {proposal} is above the heads it names: \ + no version event is written on a vote on it" + )); + } + continue; + } let cast = event::signed( &voter.key, party, &line, - if proposing.is_empty() { - &heads - } else { - &proposing - }, + &proposing, event::vote(current.hash, proposal, yes), ); record.cast(¤t, &cast, nays); @@ -1949,6 +1951,92 @@ mod tests { assert!(holding.vote_by_hand(next.hash(), true).is_ok()); } + /// Row 70 (`docs/federation.md`): a checkpoint's version proposed again, by an event + /// beside its heads. A node backs it all the same, as a proposal lapses with the move, + /// and votes on the event proposing it above the heads alone: a version event written + /// on such votes moves it; one on votes built on both events is beside the heads, and + /// refused. + #[test] + fn federation_attack_70() { + 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 head = tv.write(&[Id::of(&root)], "home/tv/a", harness::str("1")); + let mut next = first.after(on); + next.checkpoint = Some(vec![Id::of(&head)]); + let above = tv.propose(&[Id::of(&head)], &next.json()); + let beside = lass.propose(&[Id::of(&root)], &next.json()); + let mut doc = events::started(Federation::NAME, &root, &[]).unwrap(); + for event in [&head, &above, &beside] { + put(&mut doc, event); + } + let dir = crate::files::scratch_dir("held-attack-70"); + 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, + // Everything held for a while: the proposal beside the heads too. + rules: Rules::default(), + })); + read(&holding, &doc, 0); + assert_eq!( + holding.with(|store| store.state().proposing(&on, &next.hash())), + Some(vec![Id::of(&above)]) + ); + let cast = holding.vote_by_hand(next.hash(), true).unwrap(); + assert!(matches!( + Event::read_taken(&cast), + Some(Event::Signed(Signed { deps, .. })) if deps == vec![Id::of(&above)] + )); + let both = [Id::of(&above), Id::of(&beside)]; + let beside_votes = [&tv, &lass].map(|party| party.vote(&both, on, next.hash(), true)); + for vote in &beside_votes { + assert!(matches!( + holding.offer(vote.clone()), + Some(Offered::Taken(_)) + )); + } + let refused = harness::version_event( + &[Id::of(&beside_votes[0]), Id::of(&beside_votes[1])], + next.hash(), + ); + assert!(matches!( + holding.offer(refused), + Some(Offered::Refused(Refusal::Judged(_))) + )); + let tv_yes = tv.vote(&[Id::of(&above)], on, next.hash(), true); + assert!(matches!( + holding.offer(tv_yes.clone()), + Some(Offered::Taken(_)) + )); + let written = harness::version_event(&[Id::of(&cast), Id::of(&tv_yes)], next.hash()); + assert!(matches!(holding.offer(written), Some(Offered::Taken(_)))); + assert_eq!( + holding.with(|store| store.state().current().hash), + Some(next.hash()) + ); + assert_ne!( + holding.with(|store| Id::of(store.checkpoint())), + Some(Id::of(&root)) + ); + } + // The store's attack rows (`docs/store.md`, Attacks) read from a document. /// tv's federation, its root, and a document started from it. diff --git a/src/federation/party.rs b/src/federation/party.rs index 04837c3..162f8bd 100644 --- a/src/federation/party.rs +++ b/src/federation/party.rs @@ -696,6 +696,9 @@ async fn vote( }; let proposals = ids(&proposal, "proposals"); let Some(proposing) = proposals.first().copied() else { + if let Some(why) = ballots[proposal.to_string()]["none"].as_str() { + bail!("{proposal}: {why}"); + } bail!("no proposal of hash {proposal} on the current version, as the node reads it"); }; let proposed = match Event::read(&node.event(&proposing).await?) { @@ -731,8 +734,8 @@ async fn vote( let kept = record .may_vote(¤t, proposal, &proposed, yes, &nays) .map_err(|why| anyhow!(why))?; - // On the events proposing it: above the heads it names for a checkpoint wherever they - // are, so a version event may be written on it. + // On the events proposing it the node shows, those above the heads it names for a + // checkpoint, so a version event may be written on it. let cast = signer.sign(&proposals, event::vote(current.hash, proposal, yes))?; record.cast(¤t, &cast, kept); record.save(path).map_err(|why| anyhow!(why))?; @@ -799,6 +802,9 @@ async fn proposals(node: &Node) -> Result<()> { parties(&proposal["no"]) ); let Some(id) = proposal["proposals"][0].as_str() else { + if let Some(why) = proposal["none"].as_str() { + println!(" backed by no client: {why}"); + } continue; }; let id = Id::from_hex(id).ok_or_else(|| anyhow!("the node names no event ID: {id}"))?; diff --git a/src/federation/vote.rs b/src/federation/vote.rs index d4968be..1b4de16 100644 --- a/src/federation/vote.rs +++ b/src/federation/vote.rs @@ -144,7 +144,7 @@ fn proposed(seen: &Seen<'_>, current: &Version) -> Vec<Proposed> { /// Whether the node backs `version`, proposed `proposed_held` ago, by the voters' rule: one /// naming a checkpoint only where it changes nothing of `current` but its number, what it /// changes and its checkpoint, and its heads leave out nothing the node held for a while -/// before it was proposed. +/// before it was proposed, proposals and votes aside, which lapse with the move. pub(crate) fn backs_checkpoint( seen: &Seen<'_>, rules: &Rules, @@ -161,11 +161,21 @@ pub(crate) fn backs_checkpoint( ); } let before = proposed_held + rules.held_for; + // Proposals and votes lapse with the move: they count no more on the version after. + let lapsing = |id: &Id| { + matches!( + seen.store.get(id).and_then(Event::read_taken), + Some(Event::Signed(Signed { + does: Does::Proposal { .. } | Does::Vote { .. }, + .. + })) + ) + }; let held: Vec<Id> = seen .store .taken() .iter() - .filter(|id| (seen.held)(id).is_some_and(|held| held >= before)) + .filter(|id| (seen.held)(id).is_some_and(|held| held >= before) && !lapsing(id)) .copied() .collect(); let left_out = seen.store.left_out(heads, &held); |
