From becc421fcaad8eabb0dbe2aecff709a398d383a7 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Mon, 16 Nov 2026 02:28:52 +0000 Subject: [PATCH 1/2] test(tiles): a checkpoint is signed over a tree that forks the published one Writer::checkpoint promises never to sign a checkpoint inconsistent with one it signed, but publish refuses only when the published checkpoint under the origin is for a larger size. A store whose entries were replaced gets a second checkpoint at the published size with another root, or a larger one that does not extend it. The test publishes at 300 entries, puts that checkpoint beside other entries and expects both signatures refused with the checkpoint left alone, while the log grown from the published tree is signed. --- crates/rfc9162-tiles/tests/checkpoint.rs | 38 ++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/crates/rfc9162-tiles/tests/checkpoint.rs b/crates/rfc9162-tiles/tests/checkpoint.rs index c8103ae..0c2fc98 100644 --- a/crates/rfc9162-tiles/tests/checkpoint.rs +++ b/crates/rfc9162-tiles/tests/checkpoint.rs @@ -224,6 +224,44 @@ fn a_log_publishes_a_checkpoint_a_reader_opens() { shorter.checkpoint(&signer, "example.com/log").unwrap(); } +#[test] +fn a_tree_that_does_not_extend_the_published_checkpoint_is_not_signed() { + let signer = Signer::new("example.com/log", signing_key(7)).unwrap(); + let entries = |tag: u8, range: std::ops::Range| { + range.map(move |i| [&[tag][..], &i.to_be_bytes()].concat()) + }; + let store = MemoryStore::new(); + let mut writer = Writer::open(store.clone(), no_keys()).unwrap(); + writer.checkpoint(&signer, "example.com/log").unwrap(); + writer.append_batch(entries(0, 0..300)).unwrap(); + let published = writer + .checkpoint(&signer, "example.com/log") + .unwrap() + .to_text(); + drop(writer); + + // Other entries in the log's place: neither the tree of the published + // size nor a larger one is signed, and the checkpoint stays. + let rewritten = MemoryStore::new(); + let mut other = Writer::open(rewritten.clone(), no_keys()).unwrap(); + other.append_batch(entries(1, 0..300)).unwrap(); + rewritten.put(CHECKPOINT, published.as_bytes()).unwrap(); + let err = other.checkpoint(&signer, "example.com/log").unwrap_err(); + assert!(matches!(err, Error::Corrupt { .. }), "{err}"); + other.append_batch(entries(1, 300..400)).unwrap(); + let err = other.checkpoint(&signer, "example.com/log").unwrap_err(); + assert!(matches!(err, Error::Corrupt { .. }), "{err}"); + assert_eq!( + rewritten.get(CHECKPOINT).unwrap().unwrap(), + published.as_bytes() + ); + + // The log grown from the published tree is signed. + let mut writer = Writer::open(store.clone(), no_keys()).unwrap(); + writer.append_batch(entries(0, 300..400)).unwrap(); + writer.checkpoint(&signer, "example.com/log").unwrap(); +} + #[test] fn a_cosignature_verifies_over_the_cosignature_lines() { let signer = Signer::new("example.com/log", signing_key(5)).unwrap(); From 575e459f1803332e78c5f68687e113260d639c18 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Mon, 16 Nov 2026 02:29:21 +0000 Subject: [PATCH 2/2] fix(tiles): sign a checkpoint only over a tree extending the published one publish now checks the tree against the checkpoint published under the same origin before signing: the empty tree's root at size 0, the log's own root at the log's size, and otherwise a consistency proof read from the tiles through TileHashReader and checked with rfc9162::verify_consistency. A tree that is not an extension is Error::Corrupt naming both heads, and the published checkpoint stays. A larger published size is still OutOfRange. The doc says which trees replace a published checkpoint. --- changelog.d/+checkpoint-consistency.bugfix.md | 2 + crates/rfc9162-tiles/src/log.rs | 58 ++++++++++++++++--- 2 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 changelog.d/+checkpoint-consistency.bugfix.md diff --git a/changelog.d/+checkpoint-consistency.bugfix.md b/changelog.d/+checkpoint-consistency.bugfix.md new file mode 100644 index 0000000..81bae54 --- /dev/null +++ b/changelog.d/+checkpoint-consistency.bugfix.md @@ -0,0 +1,2 @@ +`rfc9162-tiles`: `Writer::checkpoint` signs only a tree that extends the checkpoint already published under its origin: the same root at that size, or a larger tree the tiles prove consistent with it. +A tree of the published size with another root, or a larger one that does not extend it, is `Error::Corrupt`, where it was signed. diff --git a/crates/rfc9162-tiles/src/log.rs b/crates/rfc9162-tiles/src/log.rs index fff03ca..5254019 100644 --- a/crates/rfc9162-tiles/src/log.rs +++ b/crates/rfc9162-tiles/src/log.rs @@ -289,8 +289,11 @@ impl Writer { /// Signs the tree state as a C2SP checkpoint under `origin` and writes /// it at `checkpoint`, holding `.state/publish.lock` meanwhile. A - /// checkpoint already published for a larger size is never replaced, so - /// the signer never signs a checkpoint inconsistent with one it signed. + /// checkpoint already published under `origin` is replaced only by one + /// for a tree that extends it: the same root at its size, or a larger + /// tree the tiles prove consistent with it. A larger published size is + /// [`Error::OutOfRange`], any other tree [`Error::Corrupt`], so the + /// signer never signs a checkpoint inconsistent with one it signed. /// Returns the note. pub fn checkpoint(&self, signer: &Signer, origin: &str) -> Result { let body = format!("{}\n", std::process::id()); @@ -309,12 +312,26 @@ impl Writer { reason: "not UTF-8".to_string(), })?; let published = Checkpoint::parse(&Note::parse(&text)?.text)?; - if published.origin == origin && published.size > self.size() { - return Err(Error::OutOfRange(format!( - "a checkpoint for size {} is published, the log is at {}", - published.size, - self.size() - ))); + if published.origin == origin { + if published.size > self.size() { + return Err(Error::OutOfRange(format!( + "a checkpoint for size {} is published, the log is at {}", + published.size, + self.size() + ))); + } + if !self.extends(published.size, &published.root)? { + return Err(Error::Corrupt { + path: CHECKPOINT.to_string(), + reason: format!( + "the published tree of size {} and root {} is not a prefix of the log's tree of size {} and root {}", + published.size, + rfc9162::hex(&published.root), + self.size(), + rfc9162::hex(&self.root()) + ), + }); + } } } let note = signer.sign(&Checkpoint::new(origin, self.size(), self.root())); @@ -322,6 +339,31 @@ impl Writer { Ok(note) } + /// Whether the tree of `size` leaves with `root`, at most the log's size, + /// is a prefix of the log's tree: the empty tree's root, the log's own + /// root at its size, or a consistency proof from the tiles that checks. + fn extends(&self, size: u64, root: &Hash) -> Result { + if size == 0 { + return Ok(*root == rfc9162::root(&[])); + } + if size == self.size() { + return Ok(*root == self.root()); + } + let reader = TileHashReader::new(&self.store, self.size(), self.root()); + let proof = reader + .consistency_proof(size)? + .ok_or_else(|| Error::OutOfRange(format!("no consistency proof from size {size}")))?; + let width = + |n: u64| usize::try_from(n).map_err(|_| Error::OutOfRange(format!("tree size {n}"))); + Ok(rfc9162::verify_consistency( + width(size)?, + &rfc9162::hex(root), + width(self.size())?, + &rfc9162::hex(&self.root()), + &proof, + )) + } + fn remove_partials(&self, full_path: &str) -> Result<()> { for path in self.store.list(&format!("{full_path}.p"))? { self.store.delete(&path)?;