From 52e40764cf4441dfb507efeef5f32644eae94993 Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 13 Nov 2026 03:25:50 +0000 Subject: screen: refuse what Automerge's transactions never write Automerge 0.12.0 takes some changes that name nothing wrong, and then can't give them back: a deletion that replaces nothing, a value put at a list's head without being inserted, a mark ended where no mark began right before it. Giving the change back panics, the save doesn't load, and a sync to a peer that lacks it panics: one member stops every node it reaches. Those are refused with the other checks (`changes::Written`). --- docs/access.md | 6 ++-- src/document/changes.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/docs/access.md b/docs/access.md index dd87d3c..976197e 100644 --- a/docs/access.md +++ b/docs/access.md @@ -314,8 +314,10 @@ wouldn't have written: bytes that aren't Automerge's encoding of the change; a f operation not numbered one after the highest of its dependencies; a change that isn't its actor's first and doesn't build on the one before; an operation naming one that wasn't made in the change's own history, or that isn't what it is named as (the -object written into, the element of that list, a value replaced at that place). -Automerge applies some of those, and then panics, saves a document that doesn't +object written into, the element of that list, a value replaced at that place); and +anything Automerge's own transactions never write (a deletion replacing nothing, a +value put at a list's head without being inserted, a mark ended but right after its +begin). Automerge applies some of those, and then panics, saves a document that doesn't load, or holds another change than the one it was given. All of it is asked of the change and its history alone, so two nodes that hold a change's history come to the same, whatever else they hold: a change of the same batch refused after it passed diff --git a/src/document/changes.rs b/src/document/changes.rs index 11d0228..5569995 100644 --- a/src/document/changes.rs +++ b/src/document/changes.rs @@ -390,6 +390,10 @@ impl Written { /// that kind, the element of that list, the operation replaced one at that /// place. (One naming an operation elsewhere marks an unrelated value gone, and /// the document then gives back another change under this one's hash.) + /// * Nothing Automerge's transactions never write: a deletion that replaces + /// nothing; a value put at a list's head without being inserted; a mark ended + /// but by the operation after the one that began it. (Automerge takes those, and + /// then can't give the change back: its save doesn't load.) /// /// A change that passes is among those the next are checked against only once /// it is [`Written::taken`]. @@ -399,7 +403,7 @@ impl Written { change: &Change, expanded: &ExpandedChange, ) -> Option { - use automerge::legacy::{ElementId, Key, ObjectId, OpId}; + use automerge::legacy::{ElementId, Key, ObjectId, OpId, OpType}; self.checked = None; let hash = change.hash(); let again = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { @@ -475,6 +479,30 @@ impl Written { .flatten() .find(|made| written.reaches(doc, &mut history, made.change, *counter)) }; + // What Automerge's transactions never write. + match &op.action { + OpType::Delete if op.pred.is_empty() => { + return Some("it deletes, replacing nothing".to_owned()); + } + OpType::Put(_) | OpType::Make(_) + if !op.insert && matches!(op.key, Key::Seq(ElementId::Head)) => + { + return Some( + "it puts a value at a list's head without inserting it".to_owned(), + ); + } + OpType::MarkEnd(_) + if !(index > 0 + && matches!( + expanded.operations[index - 1].action, + OpType::MarkBegin(..) + ) + && expanded.operations[index - 1].obj == op.obj) => + { + return Some("it ends a mark but right after the one that began it".to_owned()); + } + _ => {} + } // The object written into: one, of the kind the key is for. let in_list = matches!(op.key, Key::Seq(_)); let fits = |kind: automerge::ObjType| { @@ -1208,6 +1236,52 @@ mod tests { ); } + /// What Automerge's transactions never write: a deletion replacing nothing, a value + /// put at a list's head without inserting, a mark ended but right after its begin. + #[test] + fn what_transactions_never_write_is_refused() { + use automerge::legacy::{ElementId, Key, OpType}; + let mut theirs = Document::new(); + let list = change(&mut theirs, |doc| { + doc.put(ROOT, "k", 1).unwrap(); + let list = doc + .put_object(ROOT, "list", automerge::ObjType::List) + .unwrap(); + doc.insert(&list, 0, "a").unwrap(); + list + }); + let mut ours = Document::new(); + ours.apply_changes(theirs.get_changes(&[])).unwrap(); + change(&mut theirs, |doc| doc.delete(ROOT, "k").unwrap()); + let delete = theirs.get_last_local_change().unwrap(); + change(&mut theirs, |doc| doc.insert(&list, 0, "b").unwrap()); + let insert = theirs.get_last_local_change().unwrap(); + for honest in [&delete, &insert] { + assert_eq!( + Written::default().check(&ours, honest, &honest.decode()), + None + ); + } + let nothing_replaced = crafted(&delete, |change| { + change.operations[0].pred = Vec::new().into(); + }); + let at_head = crafted(&insert, |change| { + change.operations[0].insert = false; + change.operations[0].key = Key::Seq(ElementId::Head); + }); + // In the list, where the insert was: a mark's end nothing began. + let mark_end = crafted(&insert, |change| { + change.operations[0].action = OpType::MarkEnd(false); + }); + for crafted in [nothing_replaced, at_head, mark_end] { + assert!( + Written::default() + .check(&ours, &crafted, &crafted.decode()) + .is_some() + ); + } + } + /// A fork of a change the document holds, checked before an honest change of the /// same batch and refused after (as forged): it doesn't stand in for the change the /// document holds, and the honest change passes. -- cgit v1.3.1