diff --git a/pkgs/id/crates/id-worlds/src/world/replication.rs b/pkgs/id/crates/id-worlds/src/world/replication.rs index 3cb58178f9..eb2ad614d2 100644 --- a/pkgs/id/crates/id-worlds/src/world/replication.rs +++ b/pkgs/id/crates/id-worlds/src/world/replication.rs @@ -89,7 +89,10 @@ use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey}; use serde::{Deserialize, Serialize}; -use super::{JOURNAL_VERSION, JournalEntry, MAX_CHECKPOINT_PART_BYTES, MAX_JOURNAL_VERSION}; +use super::{ + JOURNAL_VERSION, JOURNAL_VERSION_PORTALS, JournalEntry, MAX_CHECKPOINT_PART_BYTES, + MAX_JOURNAL_VERSION, journal_version_required_by, +}; /// Domain separator bound into every signature and hash-chain step, so a /// replication signature or digest can never be reinterpreted by another @@ -619,6 +622,50 @@ fn validate_payload_journal_header(envelope: &ReplicationEnvelope) -> Result<(), Ok(()) } +/// Check a snapshot/tail payload's entries against the frame's journal +/// header version (#7970). Chaining only proves the payload is a sequence +/// of decodable entries; restore additionally refuses any entry whose +/// [`journal_version_required_by`] exceeds the journal header version — +/// e.g. a portal-grant entry (requires version 10) under a version-1 +/// header. A signed payload carrying such an entry would advance the +/// verified cursor over it, the durable receiver would commit it, and the +/// replica would then refuse its own journal at the next restore, so +/// [`ReplicationTracker::accept`] refuses it here, before any state +/// changes. A real writer can never produce this frame (the writer bumps +/// the header before appending such an entry); this closes the +/// writer-bug / forged-stream hole at admission, not application. +/// +/// # Errors +/// +/// [`ReplicationError::EntryRequiresNewerJournal`] on the first entry +/// whose required version exceeds `envelope.header_version`; +/// [`ReplicationError::MalformedPayload`] if a line does not decode (the +/// same refusal [`chain_payload`] would give). +fn validate_payload_entry_versions(envelope: &ReplicationEnvelope) -> Result<(), ReplicationError> { + if !matches!( + envelope.kind, + ReplicationKind::Snapshot | ReplicationKind::Tail + ) || envelope.payload.is_empty() + { + return Ok(()); + } + if !envelope.payload.ends_with(b"\n") { + return Err(ReplicationError::MalformedPayload); + } + for line in envelope.payload.split_inclusive(|byte| *byte == b'\n') { + let entry = serde_json::from_slice::(line) + .map_err(|_| ReplicationError::MalformedPayload)?; + let required = journal_version_required_by(&entry); + if required > envelope.header_version { + return Err(ReplicationError::EntryRequiresNewerJournal { + required, + header: envelope.header_version, + }); + } + } + Ok(()) +} + /// Check every rule [`ReplicationTracker::accept`] enforces that is /// decidable from the envelope alone (#6469): envelope version, journal /// header version range, payload bound and digest equality, the payload @@ -663,6 +710,7 @@ fn validate_envelope_local(envelope: &ReplicationEnvelope) -> Result<(), Replica return Err(ReplicationError::CursorFork); } validate_payload_journal_header(envelope)?; + validate_payload_entry_versions(envelope)?; let chained = chain_payload(envelope.cursor_before, &envelope.payload)?; if chained != envelope.cursor_after { return Err(ReplicationError::EntryChainMismatch); @@ -670,6 +718,7 @@ fn validate_envelope_local(envelope: &ReplicationEnvelope) -> Result<(), Replica } ReplicationKind::Tail => { validate_payload_journal_header(envelope)?; + validate_payload_entry_versions(envelope)?; let chained = chain_payload(envelope.cursor_before, &envelope.payload)?; if chained != envelope.cursor_after { return Err(ReplicationError::EntryChainMismatch); @@ -869,6 +918,21 @@ pub enum ReplicationError { /// cursor must never advance over it (#4486). #[error("replication payload journal header does not match the frame")] InvalidJournalHeader, + /// A payload entry requires a newer journal version than the frame's + /// header version admits: restore refuses any entry whose + /// [`journal_version_required_by`] exceeds the journal header version, + /// so a verified cursor must never advance over such an entry — the + /// replica would commit it, then refuse its own journal at the next + /// restore (#7970, the admission-side twin of the #4486 header check). + #[error( + "replication payload entry requires journal version {required}, above the frame header version {header}" + )] + EntryRequiresNewerJournal { + /// The journal version the offending entry requires. + required: u32, + /// The frame's journal header version. + header: u32, + }, /// A terminal frame names a cursor the batch has not reached — the /// batch is truncated or the terminal belongs to another batch. #[error("terminal frame does not match the reached cursor")] @@ -1025,6 +1089,7 @@ impl ReplicationTracker { return Err(ReplicationError::CursorFork); } validate_payload_journal_header(envelope)?; + validate_payload_entry_versions(envelope)?; let chained = chain_payload(envelope.cursor_before, &envelope.payload)?; if chained != envelope.cursor_after { return Err(ReplicationError::EntryChainMismatch); @@ -1046,6 +1111,7 @@ impl ReplicationTracker { return Err(ReplicationError::CursorFork); } validate_payload_journal_header(envelope)?; + validate_payload_entry_versions(envelope)?; let chained = chain_payload(envelope.cursor_before, &envelope.payload)?; if chained != envelope.cursor_after { return Err(ReplicationError::EntryChainMismatch); @@ -2198,6 +2264,96 @@ mod tests { assert_eq!(receiver.cursor(), reached); } + /// A payload mixing the version-1 entries with one entry that + /// requires journal version 10 (a portal grant). + fn over_version_payload() -> Vec { + let mut payload = entry_payload(); + payload.extend_from_slice(&journal_entry_line(&JournalEntry::PortalGrantSet { + peer: "world-portal-peer".to_string(), + granted: true, + })); + payload + } + + #[test] + fn snapshot_and_tail_payloads_must_not_carry_over_version_entries() { + // Regression tests for #7970: accept chained payloads whose + // entries restore refuses (required version above the frame's + // header version), so the verified cursor advanced over an entry + // the replica would brick on at its next restore. + let key = signing_key(1); + let base = chain_base_cursor(WORLD, &source_of(&key), 1); + let mut receiver = tracker(&key); + + // Snapshot: an in-range header (version 1) carrying an entry + // that requires version 10 is refused at admission, with the + // verified cursor untouched. + let frame = signed_snapshot_bypass(&key, 1, [0; 32], over_version_payload()); + assert_eq!( + receiver.accept(&frame), + Err(ReplicationError::EntryRequiresNewerJournal { + required: JOURNAL_VERSION_PORTALS, + header: 1, + }) + ); + assert_eq!(receiver.cursor(), base); + + // The production signer refuses the same envelope locally + // (#6469): a frame every receiver would refuse is never signed. + let envelope = ReplicationEnvelope::snapshot( + WORLD, + source_of(&key), + 1, + NONCE, + [0; 32], + over_version_payload(), + ) + .unwrap(); + assert!(SignedReplicationEnvelope::sign(envelope, &key).is_none()); + + // Tail: after a valid version-1 snapshot, a tail carrying the + // over-version entry is refused and the cursor does not move. + let snapshot = signed_snapshot(&key, 1, [0; 32], entry_payload()); + receiver.accept(&snapshot).unwrap(); + let reached = receiver.cursor(); + let mut tail_payload = Vec::new(); + tail_payload.extend_from_slice(&journal_entry_line(&JournalEntry::PortalGrantSet { + peer: "world-portal-peer".to_string(), + granted: true, + })); + let tail = ReplicationEnvelope::tail( + WORLD, + source_of(&key), + 1, + NONCE, + reached, + frame_digest(&snapshot), + tail_payload, + ) + .unwrap(); + let frame = sign_bypass(tail, &key); + assert_eq!( + receiver.accept(&frame), + Err(ReplicationError::EntryRequiresNewerJournal { + required: JOURNAL_VERSION_PORTALS, + header: 1, + }) + ); + assert_eq!(receiver.cursor(), reached); + + // Control: the same entry under a header version that admits it + // (a version-10 snapshot) is accepted end to end. + let mut receiver10 = tracker(&key); + let mut payload10 = entry_payload_with_version(JOURNAL_VERSION_PORTALS); + payload10.extend_from_slice(&journal_entry_line(&JournalEntry::PortalGrantSet { + peer: "world-portal-peer".to_string(), + granted: true, + })); + let snapshot10 = signed_snapshot(&key, JOURNAL_VERSION_PORTALS, [0; 32], payload10); + receiver10.accept(&snapshot10).unwrap(); + assert_eq!(receiver10.header_version(), JOURNAL_VERSION_PORTALS); + } + #[test] fn trailing_bytes_and_undecodable_entries_are_refused() { // Regression tests for the Codex review findings on PR #4447.