aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/held.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation/held.rs')
-rw-r--r--src/federation/held.rs83
1 files changed, 75 insertions, 8 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs
index c29dd0a..200323e 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -50,7 +50,8 @@ const LOOK: Duration = Duration::from_secs(5);
const PUT_BYTES: usize = 1024 * 1024;
/// What a node keeps of its own of its store across restarts, as its file holds it: the
-/// events it refused by what it took before, and those its operator had it drop, by ID.
+/// events it refused by what it took before, and those its operator had it drop, by ID;
+/// and of the dropped version events, the version each writes.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct Kept {
@@ -58,24 +59,38 @@ struct Kept {
refused: BTreeSet<String>,
#[serde(default)]
dropped: BTreeSet<String>,
+ #[serde(default)]
+ writes: BTreeMap<String, String>,
}
+/// What the store refused of its own, what it dropped, and of the dropped what each writes.
+type KeptIds = (Vec<Id>, Vec<Id>, Vec<(Id, Id)>);
+
impl Kept {
- fn of(own: &[Id], dropped: &[Id]) -> Self {
+ fn of(own: &[Id], dropped: &[Id], writes: &[(Id, Id)]) -> Self {
Kept {
refused: own.iter().map(Id::to_string).collect(),
dropped: dropped.iter().map(Id::to_string).collect(),
+ writes: writes
+ .iter()
+ .map(|(id, writes)| (id.to_string(), writes.to_string()))
+ .collect(),
}
}
- /// The IDs it holds: what was refused, and what dropped.
- fn ids(&self) -> Option<(Vec<Id>, Vec<Id>)> {
+ /// The IDs it holds: what was refused, what dropped, and what of that writes what.
+ fn ids(&self) -> Option<KeptIds> {
let ids = |set: &BTreeSet<String>| {
set.iter()
.map(|id| Id::from_hex(id))
.collect::<Option<Vec<Id>>>()
};
- Some((ids(&self.refused)?, ids(&self.dropped)?))
+ let writes = self
+ .writes
+ .iter()
+ .map(|(id, writes)| Some((Id::from_hex(id)?, Id::from_hex(writes)?)))
+ .collect::<Option<Vec<(Id, Id)>>>()?;
+ Some((ids(&self.refused)?, ids(&self.dropped)?, writes))
}
}
@@ -248,7 +263,7 @@ impl Holding {
return;
};
let id = Id::of(&root);
- let (own, dropped) = inner.kept.ids().unwrap_or_default();
+ let (own, dropped, writes) = inner.kept.ids().unwrap_or_default();
// What a checkpoint keeps follows it in the document: a fork's root's too.
let kept = |checkpoint: &[u8]| -> Option<Vec<Vec<u8>>> {
Federation::parts(checkpoint)
@@ -296,7 +311,7 @@ impl Holding {
match started {
Ok(store) => {
info!(store = %id, refused = own.len(), dropped = dropped.len(), "Reading the store from the document");
- inner.store = Some(store.refusing(&own, &dropped));
+ inner.store = Some(store.refusing(&own, &dropped, &writes));
}
Err(why) => {
if inner.none.replace(id) != Some(id) {
@@ -342,7 +357,7 @@ impl Holding {
inner.taken_at.entry(*id).or_insert(now);
}
let (own, dropped) = store.own();
- let kept = Kept::of(&own, &dropped);
+ let kept = Kept::of(&own, &dropped, &store.dropped_writes());
if kept != inner.kept {
for id in kept.refused.difference(&inner.kept.refused) {
warn!(event = %id, "Refused an event its history takes, by what this node took before: kept so");
@@ -1609,6 +1624,58 @@ mod tests {
);
}
+ /// Row 51 (`docs/federation.md`): a node whose operator had it drop a version is sent
+ /// it again, written anew by another node, and holds the votes that passed it: it takes
+ /// no event writing that version and writes it no more, before or after a restart,
+ /// until its operator allows it again.
+ #[test]
+ fn federation_attack_51() {
+ 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 one = first.after(on);
+ let proposal = tv.propose(&[Id::of(&root)], &one.json());
+ let votes = [&tv, &lass, &makefu]
+ .map(|party| party.vote(&[Id::of(&proposal)], on, one.hash(), true));
+ let written = harness::version_event(&[Id::of(&votes[0]), Id::of(&votes[1])], one.hash());
+ let anew = harness::version_event(&[Id::of(&votes[1]), Id::of(&votes[2])], one.hash());
+ let current = |holding: &Holding| holding.with(|store| store.state().current().hash);
+ let started = || {
+ let mut doc = events::started(Federation::NAME, &root, &[]).unwrap();
+ for event in [&proposal].into_iter().chain(&votes).chain([&written]) {
+ put(&mut doc, event);
+ }
+ doc
+ };
+ let dir = crate::files::scratch_dir("held-attack-51");
+ 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, &started(), 0);
+ assert_eq!(current(&holding), Some(one.hash()));
+ holding.drop_taken(&[Id::of(&written)]).unwrap().unwrap();
+ assert_eq!(current(&holding), Some(on));
+ // Sent again, in another document, beside the same version written anew.
+ let mut again = started();
+ assert_eq!(put_by(&mut again, "node-b", &anew), Ok(()));
+ read(&holding, &again, 1);
+ assert_eq!(current(&holding), Some(on));
+ assert_eq!(holding.passed(), Vec::<Vec<u8>>::new());
+ let restarted = Holding::open(Some(path), files, None, None).unwrap();
+ read(&restarted, &again, 0);
+ assert_eq!(current(&restarted), Some(on));
+ assert_eq!(restarted.passed(), Vec::<Vec<u8>>::new());
+ // Allowed again: taken.
+ assert!(restarted.allow(&[Id::of(&written)]));
+ read(&restarted, &again, 0);
+ assert_eq!(current(&restarted), Some(one.hash()));
+ }
+
// The store's attack rows (`docs/store.md`, Attacks) read from a document.
/// tv's federation, its root, and a document started from it.