From 8cdde74fcdcf31414da0d22222d2cc338df20fa1 Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 29 Nov 2026 15:06:04 +0000 Subject: wire: what another kind of document says, in a SYNC's body and a STATUS's stamp From version 53 on, beside Automerge's (SyncBody::Kind, HeadsSummary::Kind): a SYNC's body is one more of its shapes, another kind's bytes; a STATUS's stamp is another kind's bytes after a byte no Automerge stamp begins with. Nothing version 53 said changes, and version 52 says neither (WireError::OtherKind). Two stamps of another kind are the same where their bytes are. A node holding an Automerge document passes over a body of another kind: a peer holding one isn't let near it. --- src/transport/wire.rs | 104 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) (limited to 'src/transport/wire.rs') diff --git a/src/transport/wire.rs b/src/transport/wire.rs index 79532fb..54efd0f 100644 --- a/src/transport/wire.rs +++ b/src/transport/wire.rs @@ -89,6 +89,9 @@ pub(crate) enum WireError { #[error("wire format version {0} (we speak version {VERSION})")] Version(u8), + #[error("a document of another kind than Automerge's, which wire version {0} doesn't say")] + OtherKind(u8), + #[error("unknown message kind {0}")] UnknownKind(u8), @@ -139,6 +142,9 @@ pub(crate) fn encode_in(message: &ParsedMessage, version: u8) -> Result, if !(OLDEST..=VERSION).contains(&version) && !proof { return Err(WireError::Version(version)); } + if version < KINDS && of_another_kind(message) { + return Err(WireError::OtherKind(version)); + } let mut payload = vec![version, tag(message.kind())]; // From `KINDS` on, what is a document's kind's goes as bytes (`kinds`); before, as // the messages' own types have it, which are Automerge's. @@ -173,6 +179,20 @@ pub(crate) fn encode_in(message: &ParsedMessage, version: u8) -> Result, Ok(payload) } +/// Whether `message` carries what a document of another kind than Automerge's says, +/// which only the versions from `KINDS` on carry. +fn of_another_kind(message: &ParsedMessage) -> bool { + match message { + ParsedMessage::Sync(envelope) => { + matches!(envelope.body, crate::transport::SyncBody::Kind(_)) + } + ParsedMessage::Status(status) | ParsedMessage::StatusReply(status) => { + matches!(status.heads, crate::transport::HeadsSummary::Kind(_)) + } + _ => false, + } +} + /// The payload of `message`, in the newest wire version. #[cfg(test)] pub(crate) fn encode(message: &ParsedMessage) -> Result, WireError> { @@ -256,6 +276,10 @@ mod kinds { round_trips: Option>, } + /// What the stamp of a document of another kind than Automerge's begins with: an + /// Automerge stamp is a MessagePack array, which begins otherwise. + const OTHER_STAMP: u8 = 0; + /// Where an Automerge document stands, as its bytes have it. struct StampOut<'a>(&'a HeadsSummary); @@ -338,7 +362,11 @@ mod kinds { )? } ParsedMessage::Status(status) | ParsedMessage::StatusReply(status) => { - let stamp = rmp_serde::to_vec(&StampOut(&status.heads))?; + let stamp = match &status.heads { + // Another kind's, after a byte that begins no Automerge stamp. + HeadsSummary::Kind(bytes) => [&[OTHER_STAMP], bytes.as_slice()].concat(), + heads => rmp_serde::to_vec(&StampOut(heads))?, + }; rmp_serde::encode::write( payload, &StatusOut { @@ -437,7 +465,10 @@ mod kinds { /// The STATUS, or the STATUS_REPLY, in `body`. pub(super) fn status(body: &[u8]) -> Result { let status: StatusIn = rmp_serde::from_slice(body)?; - let StampIn(heads) = rmp_serde::from_slice(&status.stamp)?; + let heads = match status.stamp.split_first() { + Some((&OTHER_STAMP, bytes)) => HeadsSummary::Kind(bytes.to_vec()), + _ => rmp_serde::from_slice::(&status.stamp)?.0, + }; Ok(Status { heads, epoch: status.epoch, @@ -784,6 +815,11 @@ pub(crate) mod codec { heads.iter().map(|head| Bytes::new(&head.0)).collect(), Bytes::new(&[]), ), + HeadsSummary::Kind(_) => { + return Err(serde::ser::Error::custom( + "where a document of another kind stands, which heads don't say", + )); + } HeadsSummary::Digest { digest, sample } => ( sample.iter().map(|head| Bytes::new(&head.0)).collect(), Bytes::new(digest), @@ -1497,6 +1533,70 @@ mod tests { /// The exact bytes of the messages that carry what is a document's kind's, from /// `KINDS` on (`kinds`). If this fails after a change to a message type or a codec, /// the change is a new wire format: bump `VERSION` and take the new bytes. + /// What a document of another kind than Automerge's says goes where Automerge's + /// does, from the version that carries a kind's bytes on; the version before + /// doesn't say it. + #[test] + fn another_kinds_bytes_go_in_sync_and_status() { + let epoch = Epoch { + generation: 0, + id: [0; 32], + }; + let status = ParsedMessage::Status(Status { + heads: HeadsSummary::Kind(vec![0xaa; 4]), + epoch, + epoch_seq: 0, + ready: true, + syncs_with_you: true, + leaving: false, + announcing: false, + reaches: None, + round_trips: None, + }); + let sync = ParsedMessage::Sync(crate::transport::SyncEnvelope { + epoch: crate::transport::EpochTag::of(epoch), + epoch_seq: 0, + body: crate::transport::SyncBody::Kind(vec![5, 6]), + ready: true, + writer: None, + delivered: false, + sent: crate::clock::UnixMillis(8), + signatures: serde_bytes::ByteBuf::new(), + }); + let status_bytes = encode_in(&status, KINDS).unwrap(); + let sync_bytes = encode_in(&sync, KINDS).unwrap(); + assert_eq!( + hex(&status_bytes), + "350399c40500aaaaaaaa9200c420000000000000000000000000000000000000000000000000000000000000000000c3c3c2c2c0c0" + ); + assert_eq!( + hex(&sync_bytes), + "3502979200c408000000000000000000c40c9281a44b696e64c4020506c2c3c008c400" + ); + let ParsedMessage::Status(read) = decode(&status_bytes).unwrap() else { + panic!("no status"); + }; + assert!(matches!(read.heads, HeadsSummary::Kind(bytes) if bytes == vec![0xaa; 4])); + let ParsedMessage::Sync(read) = decode(&sync_bytes).unwrap() else { + panic!("no sync"); + }; + assert!( + matches!(read.body, crate::transport::SyncBody::Kind(bytes) if bytes == vec![5, 6]) + ); + assert!(!read.delivered); + // Two stamps of another kind are the same where their bytes are. + assert_eq!(HeadsSummary::Kind(vec![1]), HeadsSummary::Kind(vec![1])); + assert_ne!(HeadsSummary::Kind(vec![1]), HeadsSummary::Kind(vec![2])); + assert!(matches!( + encode_in(&status, 52), + Err(WireError::OtherKind(52)) + )); + assert!(matches!( + encode_in(&sync, 52), + Err(WireError::OtherKind(52)) + )); + } + #[test] fn golden_bytes_of_kinds() { let epoch = Epoch { -- cgit v1.3.1 From c73199a2114916097703b846841413f61d79c5f9 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 30 Nov 2026 14:26:30 +0000 Subject: wire: a handshake of another kind doesn't go in a version that says none A HELLO in version 52 says no kind, and is read as of an Automerge document: a node holding another kind would be refused for it. Such a handshake is not written in a version before 53 (WireError::OtherKind), and waits for the peer to say what it speaks. --- src/transport/wire.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/transport/wire.rs') diff --git a/src/transport/wire.rs b/src/transport/wire.rs index 54efd0f..dda052b 100644 --- a/src/transport/wire.rs +++ b/src/transport/wire.rs @@ -183,6 +183,10 @@ pub(crate) fn encode_in(message: &ParsedMessage, version: u8) -> Result, /// which only the versions from `KINDS` on carry. fn of_another_kind(message: &ParsedMessage) -> bool { match message { + // A handshake in a version that says no kind is taken for an Automerge + // document's: one of another kind waits for a version that says it. + ParsedMessage::Hello(hello) => hello.kind != crate::document::KIND, + ParsedMessage::HelloReply(reply) => reply.kind != crate::document::KIND, ParsedMessage::Sync(envelope) => { matches!(envelope.body, crate::transport::SyncBody::Kind(_)) } @@ -1238,7 +1242,16 @@ mod tests { other => panic!("{other:?}"), }; assert_eq!(read(&hello("another"), KINDS), "another"); - assert_eq!(read(&hello("another"), KINDS - 1), crate::document::KIND); + // The version before says no kind, and one read in it is Automerge's: a + // handshake of another kind doesn't go in it. + assert_eq!( + read(&hello(crate::document::KIND), KINDS - 1), + crate::document::KIND + ); + assert!(matches!( + encode_in(&hello("another"), KINDS - 1), + Err(WireError::OtherKind(_)) + )); assert_eq!( read(&hello(crate::document::KIND), VERSION), crate::document::KIND -- cgit v1.3.1