diff --git a/README.md b/README.md index 85e3999..d72fd11 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This repository is private. The implementation is under review and has not been - Runner protocol: [`proto/polymorfa/signal/v1/signal_service.proto`](proto/polymorfa/signal/v1/signal_service.proto), version `1`, subprotocol `polymorfa.signal.v1`, originating at `b6d5f5f`. - Application API: [`docs/app-api.openapi.json`](docs/app-api.openapi.json). JSON field names use snake_case. Event and message timestamps use Unix seconds; lease expiry uses Unix milliseconds internally. -- Signal libraries: exact `polymorfa/whatsapp-rust` revision `4b3b13cacd40a27573d0246386db553ef887539b`, the unmerged [recordstore/Signal operations producer PR](https://github.com/polymorfa/whatsapp-rust/pull/5). All five normal/dev crate families use this same source. A successful local build does not make that producer a released dependency. +- Signal libraries: core from `oxidezap/whatsapp-rust` at `2671430db6161c01c507587d0e3c90dcf153aa85`, with `wacore-recordstore` and `wacore-signal-ops` owned by this workspace. Status-session storage and identity finalization live in the local operations adapter. Local checks do not establish a released service or native-client acceptance. - Rust MSRV: `1.95`. The checked-in toolchain selects `1.95.0`. The server connection uses a project credential in `Authorization`, never in a URL or browser. The local API uses a separate application bearer secret. It rejects every authenticated request with an `Origin` header, including `Origin: null`, and supplies no CORS permission. Browser/WASM remote transport is unavailable until Polymorfa provides a distinct, short-lived client grant backed by live authorization. Never put the project credential or application secret in a browser. @@ -162,3 +162,26 @@ cargo build --release --locked Without the PostgreSQL variable, local tests explicitly report that PG conformance was not exercised. CI requires it. Tests exercise real SQLite and PostgreSQL store conformance; per-slot authentication; a fake Polymorfa WebSocket with Hello/Welcome/claims; actual two-device Signal encryption/decryption; signed webhook failure/ACK and SSE delivery; encrypted SendStanza; consent-off history; expiry, stale writes and crash replay; and the five-per-second ceiling. The transport peer is a fixture, not a real runner or WhatsApp account. Release still requires the pinned library producer to merge, backend/SDK/CLI/docs consumers to integrate on explicit revisions, real-account pairing/send/receive/media/history/group/retry acceptance, and the private AGPL/legal review plus owner publication approval. Repository visibility, package/image publication and production rollout controls remain unchanged by this implementation. The service adds no staff control authority; backend-owned eligibility and counts-only diagnostics stay in the Polymorfa control plane. + +## Receiving status publications + +`RunnerRequest.status_receive` is a distinct operation. Implement it before +accepting status publications: the nested `Receive` preserves origin, sender, +alternate identity and provider time; `status_kind` identifies personal, group +or channel scope. Apply `status_session` only to pairwise `pkmsg`/`msg` session +storage, keeping identity keys and prekeys shared. Sender keys retain their +existing group/sender scope. Keep phone-number to linked-identity migration, +identity replacement and session deletion consistent across both session stores. + +An older service must return `CODE_UNSUPPORTED` without decrypting, delivering +or changing a chat session. Do not fall back to `receive`. The response remains +`ServiceResponse.receive`; plaintext stays in the customer-owned service. +The receiver delivers `status.received` with `status_scope` and the existing shared +content fields. Group publications retain their full protobuf while text and +media use the wrapped publication content. Replies and mention notifications +remain messages. Local synthetic tests do not establish live rollout eligibility. + +Legacy group publication wrappers also return their classification for delivery +receipts, including after application acknowledgment. Delivered markers retain +only that classification, not plaintext. Empty markers written by older versions +have unknown classification; no sender or publication scope is guessed from them. diff --git a/crates/signal-ops/src/error.rs b/crates/signal-ops/src/error.rs index 5621210..9c67591 100644 --- a/crates/signal-ops/src/error.rs +++ b/crates/signal-ops/src/error.rs @@ -49,10 +49,22 @@ impl OpsError { pub fn is_fence_lost(&self) -> bool { match self { OpsError::Store(e) => state_is_refused(e), - OpsError::Signal(SignalProtocolError::BackendError(_, source)) => source - .downcast_ref::() - .is_some_and(state_is_refused), + OpsError::Signal(error) => signal_state_is_refused(error), _ => false, } } } + +fn signal_state_is_refused(error: &SignalProtocolError) -> bool { + let mut source: Option<&(dyn std::error::Error + 'static)> = Some(error); + while let Some(current) = source { + if current + .downcast_ref::() + .is_some_and(state_is_refused) + { + return true; + } + source = current.source(); + } + false +} diff --git a/crates/signal-ops/src/inbound.rs b/crates/signal-ops/src/inbound.rs index 4580c22..2410093 100644 --- a/crates/signal-ops/src/inbound.rs +++ b/crates/signal-ops/src/inbound.rs @@ -60,6 +60,8 @@ pub enum Received { /// Delivered before; the client only needs to send its receipt again. AlreadyDelivered { receipt_key: String, + /// Classification retained in the delivered marker, without plaintext. + group_status_wrapper: Option, }, } @@ -83,7 +85,7 @@ pub struct ReceivedMessage { pub app_state_key_share: Option, } -fn receipt_key(request: &ReceiveRequest<'_>) -> String { +fn receipt_key(request: &ReceiveRequest<'_>, status_session: bool) -> String { let mut hasher = Sha256::new(); for part in [ request.kind.as_str().as_bytes(), @@ -93,6 +95,9 @@ fn receipt_key(request: &ReceiveRequest<'_>) -> String { hasher.update(part); hasher.update([0]); } + if status_session { + hasher.update(b"status-session\0"); + } hasher.update(request.ciphertext); hex::encode(hasher.finalize()) } @@ -152,14 +157,18 @@ impl BufferEntry { }) } - fn delivered(inserted_ms: u64) -> Self { + fn delivered(inserted_ms: u64, group_status_wrapper: Option) -> Self { Self { state: STATE_DELIVERED, padding_version: 0, identity_changed: false, sender_key_rejected: false, inserted_ms, - padded_plaintext: Vec::new(), + // A delivered marker never carries message content. Older readers + // ignore its trailing metadata; empty legacy markers stay unknown. + padded_plaintext: group_status_wrapper + .map(|flag| vec![u8::from(flag)]) + .unwrap_or_default(), } } } @@ -214,7 +223,31 @@ impl SignalOps { /// Decrypt, buffer and classify one received payload. Sender-key /// distributions inside it are stored in the same batch as the decrypt. pub async fn receive(&self, request: ReceiveRequest<'_>) -> Result { - let receipt_key = receipt_key(&request); + self.receive_scoped(request, false).await + } + + /// Receive a status using a separate pairwise session when selected by + /// the caller's served configuration. Identity/prekeys and sender keys + /// remain shared. This does not enable status publishing. + pub async fn receive_status( + &self, + request: ReceiveRequest<'_>, + status_session: bool, + ) -> Result { + if status_session && request.kind == ReceiveKind::SenderKey { + return Err(OpsError::InvalidInput( + "sender keys cannot use a pairwise status session".into(), + )); + } + self.receive_scoped(request, status_session).await + } + + async fn receive_scoped( + &self, + request: ReceiveRequest<'_>, + status_session: bool, + ) -> Result { + let receipt_key = receipt_key(&request, status_session); // Pairwise payloads can carry a sender-key distribution for this chat, // so they also lock the sender's chain. let chain = chain_key(request.chat, request.sender.as_str()); @@ -250,7 +283,14 @@ impl SignalOps { { let entry = BufferEntry::decode(bytes)?; if entry.state == STATE_DELIVERED { - return Ok(Received::AlreadyDelivered { receipt_key }); + return Ok(Received::AlreadyDelivered { + receipt_key, + group_status_wrapper: match entry.padded_plaintext.as_slice() { + [0] => Some(false), + [1] => Some(true), + _ => None, + }, + }); } let content = classify(&entry.padded_plaintext, entry.padding_version, from_me)?; return Ok(Received::Message(Box::new(ReceivedMessage { @@ -272,12 +312,13 @@ impl SignalOps { EncKind::Message }; let decrypted = self - .decrypt_into( + .decrypt_into_scoped( &staged, request.sender, kind, request.ciphertext, request.device_identity, + status_session, ) .await?; (decrypted.plaintext, decrypted.identity_changed) @@ -304,7 +345,7 @@ impl SignalOps { .put_aux( Namespace::DecryptBuffer, &receipt_key, - &BufferEntry::delivered(inserted_ms).encode(), + &BufferEntry::delivered(inserted_ms, None).encode(), ) .await?; staged.commit().await?; @@ -384,7 +425,13 @@ impl SignalOps { .put_aux( Namespace::DecryptBuffer, receipt_key, - &BufferEntry::delivered(entry.inserted_ms).encode(), + &BufferEntry::delivered( + entry.inserted_ms, + classify(&entry.padded_plaintext, entry.padding_version, true) + .ok() + .map(|content| content.message.group_status_message_v2.is_set()), + ) + .encode(), ) .await?; } diff --git a/crates/signal-ops/src/ops.rs b/crates/signal-ops/src/ops.rs index 252f2e1..e4e158e 100644 --- a/crates/signal-ops/src/ops.rs +++ b/crates/signal-ops/src/ops.rs @@ -423,6 +423,9 @@ impl SignalOps { pub(crate) async fn stores_over(&self, store: Arc) -> Stores { Stores { store, + status_session: false, + finalization_error: Arc::new(std::sync::Mutex::new(None)), + pending_identities: Arc::new(std::sync::Mutex::new(std::collections::HashMap::new())), keys: self.authority.keys.read().await.clone(), sender_key_locks: self.authority.sender_key_locks.clone(), } @@ -606,6 +609,10 @@ impl SignalOps { let mut found = Vec::new(); for ns in [Namespace::Session, Namespace::Identity] { for address in self.store.scan_peer_addresses(ns, user).await? { + let address = address + .strip_suffix("_status") + .unwrap_or(&address) + .to_owned(); if let Some(device) = device_of(&address, user, server) { found.push((address, device)); } @@ -644,7 +651,11 @@ impl SignalOps { let batch: Vec> = addresses.iter().map(|a| Arc::from(a.as_str())).collect(); let staged = self.store.staged(); - staged.delete_sessions_batch(&batch).await?; + let sessions: Vec> = addresses + .iter() + .flat_map(|a| [Arc::from(a.as_str()), Arc::from(format!("{a}_status"))]) + .collect(); + staged.delete_sessions_batch(&sessions).await?; staged.delete_identities_batch(&batch).await?; staged.commit().await?; Ok(()) @@ -718,12 +729,16 @@ impl SignalOps { let mut decided = std::collections::HashSet::new(); for (from, to) in &moves { let first = !decided.contains(to); - if let Some(session) = staged.get_session(from).await? { - if first { - staged.put_session(to, &session).await?; + for suffix in ["", "_status"] { + let from_key = format!("{from}{suffix}"); + let to_key = format!("{to}{suffix}"); + if let Some(session) = staged.get_session(&from_key).await? { + if first { + staged.put_session(&to_key, &session).await?; + } + staged.delete_session(&from_key).await?; + moved.insert(to.clone()); } - staged.delete_session(from).await?; - moved.insert(to.clone()); } if let Some(identity) = staged.load_identity(from).await? { if first && staged.load_identity(to).await?.is_none() { @@ -850,6 +865,19 @@ impl SignalOps { kind: EncKind, ciphertext: &[u8], device_identity: Option<&[u8]>, + ) -> Result { + self.decrypt_into_scoped(staged, sender, kind, ciphertext, device_identity, false) + .await + } + + pub(crate) async fn decrypt_into_scoped( + &self, + staged: &Arc, + sender: &ProtocolAddress, + kind: EncKind, + ciphertext: &[u8], + device_identity: Option<&[u8]>, + status_session: bool, ) -> Result { if kind == EncKind::PreKey { let identity = *PreKeySignalMessage::try_from(ciphertext)? @@ -862,6 +890,7 @@ impl SignalOps { .map_err(OpsError::BundleRefused)?; } let mut sessions = self.stores_over(staged.clone()).await; + sessions.status_session = status_session; let mut identities = sessions.clone(); // libsignal reports a replaced identity for a prekey message but not // for an ordinary `msg` that commits a session with another identity, diff --git a/crates/signal-ops/src/outbound.rs b/crates/signal-ops/src/outbound.rs index 64aa796..2c67fc0 100644 --- a/crates/signal-ops/src/outbound.rs +++ b/crates/signal-ops/src/outbound.rs @@ -516,6 +516,7 @@ impl SignalOps { .await .map_err(send_error)?; + sessions.ensure_finalized()?; let encoded = waproto::codec::message_to_vec(message); staged .put_aux( @@ -873,6 +874,7 @@ impl SignalOps { ) .await?; } + sessions.ensure_finalized()?; let encoded = waproto::codec::message_to_vec(message); staged .put_aux( @@ -1143,6 +1145,7 @@ impl SignalOps { ) .await .map_err(send_error)?; + sessions.ensure_finalized()?; staged.commit().await?; wacore_binary::marshal(&node) .map_err(|e| OpsError::Send(format!("stanza did not marshal: {e}"))) diff --git a/crates/signal-ops/src/stores.rs b/crates/signal-ops/src/stores.rs index 06d2c32..3cfe863 100644 --- a/crates/signal-ops/src/stores.rs +++ b/crates/signal-ops/src/stores.rs @@ -43,8 +43,57 @@ where move |e| SignalProtocolError::BackendError(context, e.into()) } +// Keep sibling ratchets only when they belong to the accepted identity. A +// delayed archived message can temporarily change the shared identity row. +pub(crate) async fn invalidate_sibling_identity( + store: &S, + key: &str, + accepted: &IdentityKey, +) -> SignalResult<()> { + if let Some(bytes) = store + .get_session(key) + .await + .map_err(backend("load sibling session"))? + { + let record = SessionRecord::deserialize_for_store(&bytes, incarnation())?; + if let Some(state) = record.session_state() { + let identity = state.remote_identity_key().map_err(|_| { + SignalProtocolError::InvalidArgument("invalid sibling session identity".into()) + })?; + if identity.as_ref() == Some(accepted) { + return Ok(()); + } + } + store + .delete_session(key) + .await + .map_err(backend("delete sibling session"))?; + } + Ok(()) +} + +struct SharedFinalizationError(Arc); +impl std::fmt::Display for SharedFinalizationError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("session finalization failed") + } +} +impl std::fmt::Debug for SharedFinalizationError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(self, f) + } +} +impl std::error::Error for SharedFinalizationError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + Some(self.0.as_ref()) + } +} + pub(crate) struct Stores { pub store: Arc, + pub status_session: bool, + pub finalization_error: Arc>>>, + pub pending_identities: Arc>>, pub keys: Arc, pub sender_key_locks: Arc, } @@ -53,12 +102,83 @@ impl Clone for Stores { fn clone(&self) -> Self { Self { store: self.store.clone(), + status_session: self.status_session, + finalization_error: self.finalization_error.clone(), + pending_identities: self.pending_identities.clone(), keys: self.keys.clone(), sender_key_locks: self.sender_key_locks.clone(), } } } +impl Stores { + fn session_key(&self, address: &ProtocolAddress) -> String { + if self.status_session { + format!("{}_status", address.as_str()) + } else { + address.as_str().to_owned() + } + } + fn begin_identity_write(&self, address: &ProtocolAddress) { + let mut pending = self + .pending_identities + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + *pending.entry(self.session_key(address)).or_default() += 1; + } + fn complete_identity_write(&self, address: &ProtocolAddress) { + let mut pending = self + .pending_identities + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let key = self.session_key(address); + if let Some(count) = pending.get_mut(&key) { + *count -= 1; + if *count == 0 { + pending.remove(&key); + } + } + } + + fn failed(&self, error: SignalProtocolError) -> SignalProtocolError { + let error = Arc::new(error); + let mut first = self + .finalization_error + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + if first.is_none() { + *first = Some(error.clone()); + } + SignalProtocolError::BackendError( + "session finalization", + Box::new(SharedFinalizationError(error)), + ) + } + pub fn ensure_finalized(&self) -> Result<(), crate::error::OpsError> { + if let Some(error) = self + .finalization_error + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .clone() + { + // Keep the original source chain, including a lost lease fence. + return Err(wacore::store::error::StoreError::Database(Box::new( + SharedFinalizationError(error), + )) + .into()); + } + if !self + .pending_identities + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .is_empty() + { + return Err(crate::error::interrupted_operation()); + } + Ok(()) + } +} + /// Lazily created per-key async mutexes. Entries are never removed; a node /// serves a bounded set of chats per scope. #[derive(Default)] @@ -85,7 +205,11 @@ impl SessionStore for Stores { async fn load_session(&self, address: &ProtocolAddress) -> SignalResult> { match self .store - .get_session(address.as_str()) + .get_session(&if self.status_session { + format!("{}_status", address.as_str()) + } else { + address.as_str().to_owned() + }) .await .map_err(backend("load_session"))? { @@ -99,7 +223,11 @@ impl SessionStore for Stores { async fn has_session(&self, address: &ProtocolAddress) -> SignalResult { self.store - .has_session(address.as_str()) + .has_session(&if self.status_session { + format!("{}_status", address.as_str()) + } else { + address.as_str().to_owned() + }) .await .map_err(backend("has_session")) } @@ -109,12 +237,49 @@ impl SessionStore for Stores { address: &ProtocolAddress, record: SessionRecord, ) -> SignalResult<()> { + // Only a newly accepted ratchet can replace the sibling identity. + // Existing/archived ratchet updates must not discard a newer sibling. + // This store boundary is shared by explicit bundles and core fan-out. + let sibling = if self.status_session { + address.as_str().to_owned() + } else { + format!("{}_status", address.as_str()) + }; + let finalized = async { + if let Some(state) = record.session_state() { + let base = PublicKey::from_djb_public_key_bytes(state.alice_base_key()?)?; + let known = match self.load_session(address).await? { + Some(previous) => { + previous.has_session_state(state.session_version()?, &base)? + } + None => false, + }; + if !known && let Some(identity) = state.remote_identity_key()? { + invalidate_sibling_identity(self.store.as_ref(), &sibling, &identity).await?; + } + } + Ok::<(), SignalProtocolError>(()) + } + .await; + if let Err(error) = finalized { + return Err(self.failed(error)); + } let mut bytes = Vec::new(); record.serialize_into_for_store(&mut bytes, incarnation()); self.store - .put_session(address.as_str(), &bytes) + .put_session( + &if self.status_session { + format!("{}_status", address.as_str()) + } else { + address.as_str().to_owned() + }, + &bytes, + ) .await .map_err(backend("store_session")) + .map_err(|error| self.failed(error))?; + self.complete_identity_write(address); + Ok(()) } } @@ -134,12 +299,19 @@ impl IdentityKeyStore for Stores { address: &ProtocolAddress, identity: &IdentityKey, ) -> SignalResult { - let existing = self.get_identity(address).await?; + // Core fan-out can drop an error or a lost task. Keep the cohort + // uncommittable until the corresponding session write completes. + self.begin_identity_write(address); + let existing = self + .get_identity(address) + .await + .map_err(|error| self.failed(error))?; let key: [u8; 32] = *identity.public_key().public_key_bytes(); self.store .put_identity(address.as_str(), key) .await - .map_err(backend("save_identity"))?; + .map_err(backend("save_identity")) + .map_err(|error| self.failed(error))?; Ok(match existing { Some(previous) if &previous != identity => IdentityChange::ReplacedExisting, _ => IdentityChange::NewOrUnchanged, @@ -284,3 +456,109 @@ impl SenderKeyStore for Stores { self.sender_key_locks.get(name.cache_key()) } } + +#[cfg(test)] +mod tests { + use super::*; + use wacore::libsignal::protocol::KeyPair; + use wacore_recordstore::{ + AesGcmSealer, Fence, LeaseStore, MemoryRecordStore, Namespace, RecordSignalStore, + RecordStore, WriteOp, + }; + + #[derive(Default)] + struct PausedIdentityRecords(MemoryRecordStore); + #[async_trait] + impl RecordStore for PausedIdentityRecords { + async fn get( + &self, + scope: &str, + ns: Namespace, + key: &str, + ) -> wacore::store::error::Result>> { + if ns == Namespace::Identity { + return std::future::pending().await; + } + self.0.get(scope, ns, key).await + } + async fn scan_keys( + &self, + scope: &str, + ns: Namespace, + prefix: &str, + limit: Option, + ) -> wacore::store::error::Result> { + self.0.scan_keys(scope, ns, prefix, limit).await + } + async fn write(&self, fence: &Fence, ops: &[WriteOp]) -> wacore::store::error::Result<()> { + self.0.write(fence, ops).await + } + } + + #[test] + fn a_cancelled_identity_callback_keeps_the_batch_uncommittable() { + futures::executor::block_on(async { + let records = Arc::new(PausedIdentityRecords::default()); + let lease = records + .0 + .acquire("authored-cancelled-identity", "test", 0, 60_000) + .await + .unwrap() + .unwrap(); + let store = Arc::new(RecordSignalStore::new( + records, + Arc::new(AesGcmSealer::new(&[7; 32])), + lease.fence, + )); + let ops = crate::SignalOps::create(store).await.unwrap(); + let stores = ops.stores_over(Arc::new(ops.store().staged())).await; + let mut identities = stores.clone(); + let address = ProtocolAddress::new("111@s.whatsapp.net", 1u32.into()); + let identity = IdentityKey::new( + KeyPair::generate(&mut rand::make_rng::()).public_key, + ); + { + let future = identities.save_identity(&address, &identity); + futures::pin_mut!(future); + assert!(futures::poll!(future.as_mut()).is_pending()); + } + assert!( + stores.ensure_finalized().unwrap_err().is_fence_lost(), + "a lost callback must require a new fenced instance" + ); + }); + } + + #[test] + fn service_classification_keeps_the_actual_finalization_fence_cause() { + let lost = wacore_recordstore::FenceLost { + scope: "authored-finalization-source".into(), + generation: 7, + } + .into_store_error(); + let source = SignalProtocolError::BackendError("authored store", Box::new(lost)); + let error = crate::OpsError::Signal(SignalProtocolError::BackendError( + "session finalization", + Box::new(SharedFinalizationError(Arc::new(source))), + )); + assert!( + error.is_fence_lost(), + "service lost the fence refusal inside its finalization wrapper" + ); + } + + #[test] + fn service_classification_does_not_turn_finalization_io_into_fence_loss() { + let source = SignalProtocolError::BackendError( + "authored store", + Box::new(wacore::store::error::StoreError::Io(std::io::Error::other( + "authored ordinary I/O", + ))), + ); + let error = crate::OpsError::Signal(SignalProtocolError::BackendError( + "session finalization", + Box::new(SharedFinalizationError(Arc::new(source))), + )); + assert!(!error.is_fence_lost()); + } +} diff --git a/crates/signal-ops/tests/receive.rs b/crates/signal-ops/tests/receive.rs index 22e0587..f6ece18 100644 --- a/crates/signal-ops/tests/receive.rs +++ b/crates/signal-ops/tests/receive.rs @@ -7,6 +7,9 @@ use futures::executor::block_on; use std::sync::Arc; use wacore::libsignal::protocol::{DeviceId, KeyPair, ProtocolAddress}; use wacore::messages::MessageUtils; +use wacore::store::traits::SignalStore; +use wacore::types::jid::JidExt; +use wacore_binary::Jid; use wacore_recordstore::{ AesGcmSealer, Fence, LeaseStore, MemoryRecordStore, Namespace, RecordSignalStore, RecordStore, WriteOp, @@ -29,6 +32,7 @@ struct Device { records: Arc, scope: &'static str, ops: SignalOps, + store: Arc, } fn store_for(records: &Arc, scope: &str, holder: &str, now: u64) -> Arc { @@ -44,11 +48,13 @@ fn store_for(records: &Arc, scope: &str, holder: &str, now: u fn device(scope: &'static str) -> Device { let records = Arc::new(MemoryRecordStore::new()); - let ops = block_on(SignalOps::create(store_for(&records, scope, "node-a", 0))).unwrap(); + let store = store_for(&records, scope, "node-a", 0); + let ops = block_on(SignalOps::create(store.clone())).unwrap(); Device { records, scope, ops, + store, } } @@ -706,3 +712,347 @@ fn a_message_that_replaces_the_stored_identity_reports_it() { "a msg that replaces the stored identity must say so" ); } + +thread_local! { + static STATUS_ACCOUNTS: std::cell::RefCell> = std::cell::RefCell::default(); +} +fn status_vouch( + receiver: &SignalOps, + peer: &SignalOps, + address: &ProtocolAddress, +) -> Vec { + let account = STATUS_ACCOUNTS.with(|accounts| accounts.borrow().get(address.name()).cloned()); + let (account, identity) = if let Some(account) = account { + let identity = block_on(common::pair_under(peer, &account)); + (account, identity) + } else { + let paired = block_on(common::pair(peer)); + STATUS_ACCOUNTS.with(|accounts| { + accounts + .borrow_mut() + .insert(address.name().to_owned(), paired.account_keys.clone()); + }); + (paired.account_keys, paired.device_identity) + }; + let jid: Jid = address.name().parse().unwrap(); + block_on(receiver.trust_account_identity(&jid, *account.public_key.public_key_bytes())) + .unwrap(); + identity +} +fn status_connect( + from: &Device, + to: &Device, + from_addr: &ProtocolAddress, + to_addr: &ProtocolAddress, +) { + let from_identity = status_vouch(&to.ops, &from.ops, from_addr); + SENDER_IDENTITIES.with(|map| { + map.borrow_mut().insert( + from_addr.name().to_owned(), + Box::leak(from_identity.into_boxed_slice()), + ); + }); + let to_identity = status_vouch(&from.ops, &to.ops, to_addr); + let identity = block_on(to.ops.public_identity()); + let prekey = block_on(to.ops.generate_prekeys(&InlineRuntime, 1)) + .unwrap() + .remove(0); + block_on(from.ops.establish_session( + to_addr, + &RemoteBundle { + registration_id: identity.registration_id, + device_id: 1, + identity_key: identity.identity_key, + signed_prekey_id: identity.signed_prekey.id, + signed_prekey: identity.signed_prekey.public_key, + signed_prekey_signature: identity.signed_prekey.signature, + prekey: Some((prekey.id, prekey.public_key)), + device_identity: Some(to_identity), + }, + )) + .unwrap(); +} +fn status_identity(address: &ProtocolAddress) -> Option<&'static [u8]> { + SENDER_IDENTITIES.with(|map| map.borrow().get(address.name()).copied()) +} + +#[test] +fn status_ratchets_alternate_without_changing_chat_state_and_follow_identity_migration() { + let alice = device("alice-status"); + let bob = device("bob-status"); + let alice_addr = "111:1@s.whatsapp.net" + .parse::() + .unwrap() + .to_protocol_address(); + let bob_addr = "222:1@s.whatsapp.net" + .parse::() + .unwrap() + .to_protocol_address(); + let mut sender_sessions = Vec::new(); + for status in [false, true] { + status_connect(&alice, &bob, &alice_addr, &bob_addr); + let body = if status { "status first" } else { "chat first" }; + let sent = block_on(alice.ops.encrypt(&bob_addr, &padded(&text(body)))).unwrap(); + let req = request( + if status { + "status@broadcast" + } else { + "111@s.whatsapp.net" + }, + &alice_addr, + kind(&sent), + &sent.ciphertext, + false, + ); + let received = message(block_on(bob.ops.receive_status(req, status)).unwrap()); + assert_eq!(received.content.message.conversation.as_deref(), Some(body)); + let repeated = message(block_on(bob.ops.receive_status(req, status)).unwrap()); + assert!(repeated.redelivered); + block_on(bob.ops.mark_delivered(&received.receipt_key)).unwrap(); + assert!(matches!( + block_on(bob.ops.receive_status(req, status)).unwrap(), + Received::AlreadyDelivered { .. } + )); + sender_sessions.push( + block_on(alice.store.get_session(bob_addr.as_str())) + .unwrap() + .unwrap(), + ); + } + for i in 0..4 { + let status = i % 2 == 1; + block_on( + alice + .store + .put_session(bob_addr.as_str(), &sender_sessions[i % 2]), + ) + .unwrap(); + let body = format!("authored alternating {i}"); + let sent = block_on(alice.ops.encrypt(&bob_addr, &padded(&text(&body)))).unwrap(); + sender_sessions[i % 2] = block_on(alice.store.get_session(bob_addr.as_str())) + .unwrap() + .unwrap(); + let req = request( + if status { + "status@broadcast" + } else { + "111@s.whatsapp.net" + }, + &alice_addr, + kind(&sent), + &sent.ciphertext, + false, + ); + let received = message(block_on(bob.ops.receive_status(req, status)).unwrap()); + assert_eq!( + received.content.message.conversation.as_deref(), + Some(body.as_str()) + ); + assert!(!received.identity_changed); + } + let pn: Jid = "111@s.whatsapp.net".parse().unwrap(); + let lid: Jid = "333@lid".parse().unwrap(); + assert_eq!(block_on(bob.ops.migrate_pn_to_lid(&pn, &lid)).unwrap(), 1); + let migrated: Jid = "333:1@lid".parse().unwrap(); + let address = migrated.to_protocol_address(); + let keys = block_on( + bob.records + .scan_keys(bob.scope, Namespace::Session, "", None), + ) + .unwrap(); + assert_eq!(keys.len(), 2); + assert!(keys.contains(&address.as_str().to_owned())); + assert!(keys.contains(&format!("{}_status", address.as_str()))); + block_on(bob.ops.forget_user(&lid)).unwrap(); + assert!( + block_on( + bob.records + .scan_keys(bob.scope, Namespace::Session, "", None) + ) + .unwrap() + .is_empty() + ); + assert!( + block_on( + bob.records + .scan_keys(bob.scope, Namespace::Identity, "", None) + ) + .unwrap() + .is_empty() + ); +} + +#[test] +fn delivered_group_publications_keep_only_classification_for_legacy_receipts() { + let alice = device("alice-legacy-group-status"); + let bob = device("bob-legacy-group-status"); + let (alice_addr, bob_addr) = (address("111"), address("222")); + status_connect(&alice, &bob, &alice_addr, &bob_addr); + let mut publication = wa::Message::default(); + let mut wrapper = wa::message::FutureProofMessage::default(); + wrapper.message = text("authored publication").into(); + publication.group_status_message_v2 = wrapper.into(); + let sent = block_on(alice.ops.encrypt(&bob_addr, &padded(&publication))).unwrap(); + let request = request(GROUP, &alice_addr, kind(&sent), &sent.ciphertext, false); + let first = message(block_on(bob.ops.receive(request)).unwrap()); + assert!(first.content.message.group_status_message_v2.is_set()); + block_on(bob.ops.mark_delivered(&first.receipt_key)).unwrap(); + assert!(matches!( + block_on(bob.ops.receive(request)).unwrap(), + Received::AlreadyDelivered { + group_status_wrapper: Some(true), + .. + } + )); + let mut legacy_marker = block_on( + bob.store + .load_aux(Namespace::DecryptBuffer, &first.receipt_key), + ) + .unwrap() + .unwrap(); + assert_eq!(legacy_marker.pop(), Some(1)); + block_on( + bob.store + .put_aux(Namespace::DecryptBuffer, &first.receipt_key, &legacy_marker), + ) + .unwrap(); + assert!(matches!( + block_on(bob.ops.receive(request)).unwrap(), + Received::AlreadyDelivered { + group_status_wrapper: None, + .. + } + )); +} + +#[test] +fn outbound_identity_replacement_invalidates_the_old_status_session() { + let old_peer = device("old-peer"); + let new_peer = device("new-peer"); + let receiver = device("identity-receiver"); + let peer_addr = address("111"); + let receiver_addr = address("222"); + status_connect(&old_peer, &receiver, &peer_addr, &receiver_addr); + let sent = block_on( + old_peer + .ops + .encrypt(&receiver_addr, &padded(&text("authored old status"))), + ) + .unwrap(); + let req = request( + "status@broadcast", + &peer_addr, + kind(&sent), + &sent.ciphertext, + false, + ); + message(block_on(receiver.ops.receive_status(req, true)).unwrap()); + let status_key = format!("{}_status", peer_addr.as_str()); + let old_status = block_on(receiver.store.get_session(&status_key)) + .unwrap() + .unwrap(); + status_connect(&receiver, &old_peer, &receiver_addr, &peer_addr); + assert_eq!( + block_on(receiver.store.get_session(&status_key)).unwrap(), + Some(old_status) + ); + status_connect(&receiver, &new_peer, &receiver_addr, &peer_addr); + assert!( + block_on(receiver.store.get_session(&status_key)) + .unwrap() + .is_none() + ); + let expected = padded(&text("authored new chat")); + let sent = block_on(receiver.ops.encrypt(&peer_addr, &expected)).unwrap(); + assert_eq!( + block_on(new_peer.ops.decrypt( + &receiver_addr, + sent.kind, + &sent.ciphertext, + status_identity(&receiver_addr) + )) + .unwrap() + .plaintext, + expected + ); +} + +#[test] +fn delayed_archived_prekey_does_not_discard_a_sibling_with_the_accepted_identity() { + let old_peer = device("delayed-old-peer"); + let new_peer = device("delayed-new-peer"); + let receiver = device("delayed-receiver"); + let peer_addr = address("111"); + let receiver_addr = address("222"); + status_connect(&old_peer, &receiver, &peer_addr, &receiver_addr); + let first = block_on( + old_peer + .ops + .encrypt(&receiver_addr, &padded(&text("authored first chat"))), + ) + .unwrap(); + block_on(receiver.ops.decrypt( + &peer_addr, + first.kind, + &first.ciphertext, + status_identity(&peer_addr), + )) + .unwrap(); + let delayed = block_on( + old_peer + .ops + .encrypt(&receiver_addr, &padded(&text("authored delayed chat"))), + ) + .unwrap(); + let old_device_identity = status_identity(&peer_addr); + // The new bundle archives the old chat ratchet, while the status peer uses B. + status_connect(&receiver, &new_peer, &receiver_addr, &peer_addr); + status_connect(&new_peer, &receiver, &peer_addr, &receiver_addr); + let status = block_on( + new_peer + .ops + .encrypt(&receiver_addr, &padded(&text("authored new status"))), + ) + .unwrap(); + message( + block_on(receiver.ops.receive_status( + request( + "status@broadcast", + &peer_addr, + kind(&status), + &status.ciphertext, + false, + ), + true, + )) + .unwrap(), + ); + let key = format!("{}_status", peer_addr.as_str()); + let retained = block_on(receiver.store.get_session(&key)).unwrap().unwrap(); + block_on(receiver.ops.decrypt( + &peer_addr, + delayed.kind, + &delayed.ciphertext, + old_device_identity, + )) + .unwrap(); + assert_eq!( + block_on(receiver.store.get_session(&key)).unwrap(), + Some(retained.clone()) + ); + block_on( + receiver + .ops + .encrypt(&peer_addr, &padded(&text("authored archived reply"))), + ) + .unwrap(); + assert_eq!( + block_on(receiver.store.get_session(&key)).unwrap(), + Some(retained.clone()) + ); + status_connect(&receiver, &new_peer, &receiver_addr, &peer_addr); + assert_eq!( + block_on(receiver.store.get_session(&key)).unwrap(), + Some(retained) + ); +} diff --git a/crates/signal-ops/tests/send.rs b/crates/signal-ops/tests/send.rs index 8588df2..2b560e4 100644 --- a/crates/signal-ops/tests/send.rs +++ b/crates/signal-ops/tests/send.rs @@ -123,7 +123,7 @@ impl SendContextResolver for FakeClient { } } -async fn bundle_for(ops: &SignalOps) -> PreKeyBundle { +async fn bundle_for(ops: &SignalOps) -> PreKeyBundle { let identity = ops.public_identity().await; let prekey = ops .generate_prekeys(&InlineRuntime, 1) @@ -2176,3 +2176,360 @@ async fn acknowledgments_for_one_addressing_chain_do_not_skip_the_other() { next.distribution_targets ); } + +#[derive(Default)] +struct BundleFaultRecords { + inner: MemoryRecordStore, + fail_status_read: std::sync::atomic::AtomicBool, + fail_identity_read: std::sync::atomic::AtomicBool, + fence_identity_failure: std::sync::atomic::AtomicBool, +} + +#[async_trait] +impl wacore_recordstore::RecordStore for BundleFaultRecords { + async fn get( + &self, + scope: &str, + ns: wacore_recordstore::Namespace, + key: &str, + ) -> wacore::store::error::Result>> { + if ns == wacore_recordstore::Namespace::Session + && key.ends_with("_status") + && self + .fail_status_read + .swap(false, std::sync::atomic::Ordering::SeqCst) + { + return Err(wacore::store::error::StoreError::Io(std::io::Error::other( + "authored sibling read failure", + ))); + } + if ns == wacore_recordstore::Namespace::Identity + && self + .fail_identity_read + .swap(false, std::sync::atomic::Ordering::SeqCst) + { + if self + .fence_identity_failure + .load(std::sync::atomic::Ordering::SeqCst) + { + return Err(wacore_recordstore::FenceLost { + scope: Arc::from(scope), + generation: 1, + } + .into_store_error()); + } + return Err(wacore::store::error::StoreError::Io(std::io::Error::other( + "authored identity read failure", + ))); + } + self.inner.get(scope, ns, key).await + } + async fn scan_keys( + &self, + scope: &str, + ns: wacore_recordstore::Namespace, + prefix: &str, + limit: Option, + ) -> wacore::store::error::Result> { + self.inner.scan_keys(scope, ns, prefix, limit).await + } + async fn write( + &self, + fence: &wacore_recordstore::Fence, + ops: &[wacore_recordstore::WriteOp], + ) -> wacore::store::error::Result<()> { + self.inner.write(fence, ops).await + } +} + +async fn cold_status_identity_case(route: &str, fail_sibling: bool) { + cold_status_identity_case_at(route, fail_sibling, false, false).await; +} +async fn cold_status_identity_case_at( + route: &str, + fail_sibling: bool, + fail_identity: bool, + fenced: bool, +) { + use wacore::store::traits::SignalStore; + use wacore::types::message::AddressingMode; + use wacore_signal_ops::{RetryRequest, RetryRoute}; + let own: Jid = "111:1@s.whatsapp.net".parse().unwrap(); + let peer: Jid = "222:1@s.whatsapp.net".parse().unwrap(); + let records = Arc::new(BundleFaultRecords::default()); + let lease = records + .inner + .acquire(route, "authored-node", 0, 60_000) + .await + .unwrap() + .unwrap(); + let store = Arc::new(RecordSignalStore::new( + records.clone(), + Arc::new(AesGcmSealer::new(&[5; 32])), + lease.fence, + )); + let receiver = SignalOps::create(store.clone()).await.unwrap(); + let own_pair = common::pair(&receiver).await; + receiver + .set_own_jids(&own.to_string(), Some("900111:1@lid")) + .await + .unwrap(); + let old_peer = device("authored-old-peer", &peer.to_string()).await; + let new_peer = device("authored-new-peer", &peer.to_string()).await; + let peer_pair = common::pair(&old_peer).await; + let new_identity = common::pair_under(&new_peer, &peer_pair.account_keys).await; + old_peer + .trust_account_identity(&own.to_non_ad(), own_pair.account) + .await + .unwrap(); + receiver + .trust_account_identity(&peer.to_non_ad(), peer_pair.account) + .await + .unwrap(); + let status_client = FakeClient { + device_identities: HashMap::from([(own.clone(), own_pair.device_identity.clone())]), + devices: vec![own.clone()], + bundles: HashMap::from([(own.clone(), bundle_for(&receiver).await)]), + group: None, + }; + let status = old_peer + .send_direct( + &TokioRuntime, + &status_client, + &own.to_non_ad(), + &text("authored old status"), + "OLD1", + ) + .await + .unwrap(); + let delayed = old_peer + .send_direct( + &TokioRuntime, + &status_client, + &own.to_non_ad(), + &text("authored delayed old status"), + "OLD2", + ) + .await + .unwrap(); + let (kind, version, ciphertext) = enc_for(&status.stanza, &own); + let address = peer.to_protocol_address(); + receiver + .receive_status( + ReceiveRequest { + chat: "status@broadcast", + sender: &address, + kind: ReceiveKind::try_from(kind.as_str()).unwrap(), + ciphertext: &ciphertext, + padding_version: version, + is_from_me: false, + device_identity: Some(&peer_pair.device_identity), + }, + true, + ) + .await + .unwrap(); + let status_key = format!("{}_status", address.as_str()); + assert!(store.get_session(&status_key).await.unwrap().is_some()); + if route == "retry" { + let initial = FakeClient { + device_identities: HashMap::from([(peer.clone(), peer_pair.device_identity.clone())]), + devices: vec![peer.clone()], + bundles: HashMap::from([(peer.clone(), bundle_for(&old_peer).await)]), + group: None, + }; + receiver + .send_direct( + &TokioRuntime, + &initial, + &peer.to_non_ad(), + &text("authored original send"), + "RETRY", + ) + .await + .unwrap(); + store.delete_session(address.as_str()).await.unwrap(); + } + let healthy_jid: Jid = "222:2@s.whatsapp.net".parse().unwrap(); + let healthy = device("authored-healthy-device", &healthy_jid.to_string()).await; + let healthy_identity = common::pair_under(&healthy, &peer_pair.account_keys).await; + let before_chat = store.get_session(address.as_str()).await.unwrap(); + let before_status = store.get_session(&status_key).await.unwrap(); + let before_identity = store.load_identity(address.as_str()).await.unwrap(); + records + .fail_status_read + .store(fail_sibling, std::sync::atomic::Ordering::SeqCst); + records + .fence_identity_failure + .store(fenced, std::sync::atomic::Ordering::SeqCst); + records + .fail_identity_read + .store(fail_identity, std::sync::atomic::Ordering::SeqCst); + let client = FakeClient { + device_identities: HashMap::from([ + (peer.clone(), new_identity), + (healthy_jid.clone(), healthy_identity), + ]), + devices: vec![peer.clone(), healthy_jid.clone()], + bundles: HashMap::from([ + (peer.clone(), bundle_for(&new_peer).await), + (healthy_jid.clone(), bundle_for(&healthy).await), + ]), + group: Some(Arc::new(GroupRoutingInfo::new( + vec![own.to_non_ad(), peer.to_non_ad()], + AddressingMode::Pn, + ))), + }; + let result = match route { + "direct" => receiver + .send_direct( + &TokioRuntime, + &client, + &peer.to_non_ad(), + &text("authored new chat"), + "NEW", + ) + .await + .map(|_| ()), + "group" => receiver + .send_group( + &TokioRuntime, + &client, + &"120363000000001@g.us".parse().unwrap(), + &text("authored new group"), + "GROUP", + ) + .await + .map(|_| ()), + _ => receiver + .resend( + &client, + RetryRequest { + chat: &peer.to_non_ad(), + message_id: "RETRY", + requester: &peer, + encryption_jid: &peer, + route: RetryRoute::Direct { recipient: None }, + retry_count: 2, + bundle: None, + }, + ) + .await + .map(|_| ()), + }; + if fail_sibling || fail_identity { + if fenced { + assert!( + result.as_ref().unwrap_err().is_fence_lost(), + "lost fence was hidden by the cohort error" + ); + } + assert!( + !records + .fail_identity_read + .load(std::sync::atomic::Ordering::SeqCst) + ); + assert!( + result.is_err(), + "{route} published a cohort after sibling invalidation failed" + ); + assert!( + !records + .fail_status_read + .load(std::sync::atomic::Ordering::SeqCst) + ); + assert_eq!( + store.get_session(address.as_str()).await.unwrap(), + before_chat + ); + assert_eq!(store.get_session(&status_key).await.unwrap(), before_status); + assert_eq!( + store.load_identity(address.as_str()).await.unwrap(), + before_identity + ); + assert!( + store + .get_session(healthy_jid.to_protocol_address().as_str()) + .await + .unwrap() + .is_none() + ); + return; + } + result.unwrap(); + assert!( + store.get_session(&status_key).await.unwrap().is_none(), + "old status survived {route}" + ); + assert_eq!( + store.load_identity(address.as_str()).await.unwrap(), + Some(new_peer.public_identity().await.identity_key) + ); + let (kind, version, ciphertext) = enc_for(&delayed.stanza, &own); + assert!( + receiver + .receive_status( + ReceiveRequest { + chat: "status@broadcast", + sender: &address, + kind: ReceiveKind::try_from(kind.as_str()).unwrap(), + ciphertext: &ciphertext, + padding_version: version, + is_from_me: false, + device_identity: Some(&peer_pair.device_identity), + }, + true + ) + .await + .is_err(), + "delayed old status accepted after {route}" + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn cold_direct_invalidates_an_old_status_identity() { + cold_status_identity_case("direct", false).await; +} + +#[tokio::test(flavor = "multi_thread")] +async fn cold_group_invalidates_an_old_status_identity() { + cold_status_identity_case("group", false).await; +} + +#[tokio::test(flavor = "multi_thread")] +async fn cold_retry_invalidates_an_old_status_identity() { + cold_status_identity_case("retry", false).await; +} + +#[tokio::test(flavor = "multi_thread")] +async fn failed_status_invalidation_aborts_the_direct_batch() { + cold_status_identity_case("direct", true).await; +} + +#[tokio::test(flavor = "multi_thread")] +async fn failed_status_invalidation_aborts_the_group_batch() { + cold_status_identity_case("group", true).await; +} + +#[tokio::test(flavor = "multi_thread")] +async fn failed_status_invalidation_aborts_the_retry_batch() { + cold_status_identity_case("retry", true).await; +} + +#[tokio::test(flavor = "multi_thread")] +async fn failed_identity_read_aborts_the_direct_batch() { + cold_status_identity_case_at("direct", false, true, false).await; +} +#[tokio::test(flavor = "multi_thread")] +async fn failed_identity_read_aborts_the_group_batch() { + cold_status_identity_case_at("group", false, true, false).await; +} + +#[tokio::test(flavor = "multi_thread")] +async fn failed_identity_read_keeps_the_direct_fence_refusal() { + cold_status_identity_case_at("direct", false, true, true).await; +} +#[tokio::test(flavor = "multi_thread")] +async fn failed_identity_read_keeps_the_group_fence_refusal() { + cold_status_identity_case_at("group", false, true, true).await; +} diff --git a/docs/app-api.openapi.json b/docs/app-api.openapi.json index 7e3187a..789435f 100644 --- a/docs/app-api.openapi.json +++ b/docs/app-api.openapi.json @@ -374,6 +374,7 @@ "type": "string", "enum": [ "message", + "status.received", "history.sync" ] }, @@ -416,6 +417,15 @@ }, "history": { "$ref": "#/components/schemas/HistoryDescriptor" + }, + "status_scope": { + "type": "string", + "enum": [ + "personal", + "group", + "channel" + ], + "description": "Status publication audience. Absent for ordinary messages and history." } } }, @@ -462,16 +472,30 @@ "UnconfirmedSend": { "type": "object", "additionalProperties": false, - "required": ["error"], + "required": [ + "error" + ], "properties": { "error": { "type": "object", "additionalProperties": false, - "required": ["code", "message", "message_id"], + "required": [ + "code", + "message", + "message_id" + ], "properties": { - "code": { "type": "string", "const": "send_unconfirmed" }, - "message": { "type": "string" }, - "message_id": { "type": "string", "minLength": 1 } + "code": { + "type": "string", + "const": "send_unconfirmed" + }, + "message": { + "type": "string" + }, + "message_id": { + "type": "string", + "minLength": 1 + } } } } diff --git a/docs/integration-status.md b/docs/integration-status.md index 7051485..c1fef1d 100644 --- a/docs/integration-status.md +++ b/docs/integration-status.md @@ -1,6 +1,6 @@ # Producer integration status -This service implements the accepted server-mode contract. It does not establish deployed zero-knowledge availability or browser remote transport support. Source revisions remain explicit: runner protocol originates at signal-node `b6d5f5f` and adds durable public upload requests 20/21 and response 18 without changing protocol version 1; all Signal library families are pinned to `4b3b13cacd40a27573d0246386db553ef887539b` in unmerged whatsapp-rust PR #5. The unchanged service OpenAPI artifact is the application/native-client contract. +This service implements the accepted server-mode contract. It does not establish deployed zero-knowledge availability or browser remote transport support. Source revisions remain explicit: runner protocol originates at signal-node `b6d5f5f` and adds durable public upload requests 20/21 and response 18 without changing protocol version 1; the Signal core is pinned to `oxidezap/whatsapp-rust` revision `2671430db6161c01c507587d0e3c90dcf153aa85`, while recordstore and Signal operations are local workspace crates. The unchanged service OpenAPI artifact is the application/native-client contract. ## Mandatory six-part assessment @@ -17,7 +17,7 @@ This service implements the accepted server-mode contract. It does not establish | Layer | Service slice status / dependency | | --- | --- | -| 1. Libraries | All normal/dev families repinned to exact `4b3b13c`; PR #5 unmerged. Durable atomic upload batches, exact replay ACK and fresh-only legacy marks consumed without a private-format parser. No library rewrite or publication here. | +| 1. Libraries | Core uses exact upstream `2671430db6161c01c507587d0e3c90dcf153aa85`; normal/dev operations and recordstore use the same local workspace source. Durable atomic upload batches, exact replay ACK and fresh-only legacy marks consumed without a private-format parser. No library rewrite or publication here. | | 1b. Signal service | Additive durable upload dispatch and core-backed local uploaded count; real framed SQLite/PostgreSQL recovery and rollback tests. HTTP routes, consent, key custody and runtime policy unchanged. | | 2. Runners / Bartender | HyperMeow explicit batch interface and backend PR #593 wire/vendor integration remain pending. Unknown operations fail Unsupported without fresh-generation fallback. Fake WS is not a live runner; Bartender unchanged. | | 3. Temporal | Parent/backend owner assesses workflows and transport recovery; this process creates no Temporal workflow or control-plane command. | @@ -41,3 +41,5 @@ Durable-upload tests additionally prepare 812, reopen the service/store and requ The existing `[app_state].share_keys` opt-in remains false by default and requires own-device evidence when enabled. That exception permits contact-name, label and mute/archive/pin metadata decryption; message plaintext custody does not promise metadata opacity. QuickLink admission, short-lived tunnel/grant authority, Temporal, standard-custody behavior, billing/metering, logging and hosted retention are unchanged. No public release record or feature-flag change is warranted for an unmerged private protocol producer; parent owns coordinated release documentation and availability gates. Real WhatsApp pairing, media/history download, group resend, retry receipts and server redelivery have not been exercised. The participant-hash resend addresses every current device rather than WA Web's exclusion list; provider acceptance remains unresolved. Arbitrary clock skew, key rotation and same-slot rollback protection are not implemented. Browser remote relay is blocked pending distinct short-lived client grant authority. AGPL/legal review and owner approval are required before making this repository or artifacts public. + +The status candidate composes the service owner's local-crate migration, pinned-account/device verification, sender-key holder rotation and session-scoped application event IDs. A newly accepted pairwise ratchet finalizes the sibling chat/status identity through the local store adapter; existing and archived ratchet updates preserve the sibling. Failed local finalization aborts a staged direct/group cohort before commit. Protocol 1 retains additive status operations, duplicate publication classification and ACK privacy. Candidate native, retained-state, PostgreSQL and release acceptance require their own exact-head receipts; earlier receipts do not cover this composition. diff --git a/proto/polymorfa/signal/v1/signal_service.proto b/proto/polymorfa/signal/v1/signal_service.proto index 07d545a..bf111bd 100644 --- a/proto/polymorfa/signal/v1/signal_service.proto +++ b/proto/polymorfa/signal/v1/signal_service.proto @@ -99,6 +99,8 @@ message RunnerRequest { Resend resend = 17; ForgetIdentity forget_identity = 18; MigratePnToLid migrate_pn_to_lid = 19; + // A distinct operation prevents old services from using the chat ratchet. + StatusReceive status_receive = 22; PreparePreKeyUpload prepare_pre_key_upload = 20; AcknowledgePreKeyUpload acknowledge_pre_key_upload = 21; } @@ -218,7 +220,22 @@ message Receive { bytes device_identity = 10; } +// StatusReceive must be explicitly implemented. An unsupported service returns +// CODE_UNSUPPORTED without decrypting, delivering or mutating a chat session. +// The response uses ServiceResponse.receive and retains the no-plaintext contract. +message StatusReceive { + Receive payload = 1; + // "personal", "group" or "channel". This is publication provenance, + // never inferred from quoted content or a status mention notification. + string status_kind = 2; + // Use the sender's separate pairwise status session for pkmsg/msg only. + // Identity keys and prekeys remain shared. skmsg keeps its sender-key scope. + bool status_session = 3; +} + message ReceiveResult { + // Decrypted publication context for receipts. Empty when unknown. + string status_kind = 5; bool already_delivered = 1; bool redelivered = 2; bool sender_key_rejected = 3; diff --git a/src/delivery.rs b/src/delivery.rs index 7fa6648..b4c4a1f 100644 --- a/src/delivery.rs +++ b/src/delivery.rs @@ -255,6 +255,7 @@ mod tests { Event { id: id.into(), kind: "message", + status_scope: None, session_id: "s".into(), chat: "c".into(), sender: "x".into(), diff --git a/src/message.rs b/src/message.rs index 409d828..0613d51 100644 --- a/src/message.rs +++ b/src/message.rs @@ -21,10 +21,13 @@ pub struct Event { /// Stable across redeliveries of the same ciphertext; use it to /// deduplicate and to acknowledge. pub id: String, - /// `message` or `history.sync`. + /// `message`, `status.received` or `history.sync`. #[serde(rename = "type")] pub kind: &'static str, pub session_id: String, + /// Publication audience; replies and mentions remain ordinary messages. + #[serde(skip_serializing_if = "Option::is_none")] + pub status_scope: Option, pub chat: String, pub sender: String, #[serde(skip_serializing_if = "Option::is_none")] diff --git a/src/serve.rs b/src/serve.rs index f48e065..be4ef86 100644 --- a/src/serve.rs +++ b/src/serve.rs @@ -69,7 +69,30 @@ pub async fn handle( let fail = |e: OpsError| node.ops_error(session, &e); match op { Op::GetIdentity(_) => unreachable!("handled above"), - Op::Receive(receive) => self::receive(node, session, &ops, receive).await, + Op::Receive(receive) => self::receive(node, session, &ops, receive, None, false).await, + Op::StatusReceive(request) => { + let receive = request + .payload + .ok_or_else(|| invalid("status payload is required"))?; + let valid = match request.status_kind.as_str() { + "personal" => receive.chat == "status@broadcast", + "group" => receive.chat.ends_with("@g.us"), + "channel" => receive.chat.ends_with("@newsletter"), + _ => false, + }; + if !valid { + return Err(invalid("status scope does not match publication origin")); + } + self::receive( + node, + session, + &ops, + receive, + Some(&request.status_kind), + request.status_session, + ) + .await + } Op::GeneratePreKeys(request) => { let keys = ops .generate_prekeys(&TokioRuntime, request.count) @@ -243,6 +266,8 @@ async fn receive( session: &Arc, ops: &crate::node::Ops, receive: proto::Receive, + status_kind: Option<&str>, + status_session: bool, ) -> Result { let sender = jid(&receive.sender, "sender")?; let address = sender.to_protocol_address(); @@ -254,31 +279,37 @@ async fn receive( }; let padding_version = u8::try_from(receive.padding_version) .map_err(|_| invalid("padding_version out of range"))?; - let received = ops - .receive(ReceiveRequest { - chat: &receive.chat, - sender: &address, - kind, - ciphertext: &receive.ciphertext, - padding_version, - is_from_me: receive.is_from_me, - device_identity: (!receive.device_identity.is_empty()) - .then_some(receive.device_identity.as_slice()), - }) - .await - .map_err(|e| { - let error = receive_error(node, session, &e); - tracing::info!( - session = %session.id, - message_id = %receive.message_id, - kind = %receive.kind, - code = error.code_enum().as_str_name(), - "receive failed" - ); - error - })?; + let request = ReceiveRequest { + chat: &receive.chat, + sender: &address, + kind, + ciphertext: &receive.ciphertext, + padding_version, + is_from_me: receive.is_from_me, + device_identity: (!receive.device_identity.is_empty()) + .then_some(receive.device_identity.as_slice()), + }; + let result = if status_kind.is_some() { + ops.receive_status(request, status_session).await + } else { + ops.receive(request).await + }; + let received = result.map_err(|e| { + let error = receive_error(node, session, &e); + tracing::info!( + session = %session.id, + message_id = %receive.message_id, + kind = %receive.kind, + code = error.code_enum().as_str_name(), + "receive failed" + ); + error + })?; let message = match received { - Received::AlreadyDelivered { receipt_key } => { + Received::AlreadyDelivered { + receipt_key, + group_status_wrapper, + } => { let app_state_key_share = if node.cfg.share_app_state_keys && receive.is_from_me { let cutoff = crate::node::now_ms() .saturating_sub(node.cfg.decrypt_buffer_retention.as_millis() as u64); @@ -296,6 +327,13 @@ async fn receive( )); } return respond(Out::Receive(ReceiveResult { + status_kind: publication_kind( + &receive, + status_kind, + group_status_wrapper == Some(true), + ) + .unwrap_or("") + .to_owned(), already_delivered: true, app_state_key_share, ..Default::default() @@ -304,6 +342,12 @@ async fn receive( Received::Message(message) => message, }; + let status_kind = publication_kind( + &receive, + status_kind, + message.content.message.group_status_message_v2.is_set(), + ); + // Explicit operator consent in addition to the pinned core own-device gate. let app_state_key_share = if node.cfg.share_app_state_keys && receive.is_from_me { message @@ -314,7 +358,7 @@ async fn receive( } else { Vec::new() }; - if let Some(event) = event_for(node, session, &receive, &message) { + if let Some(event) = event_for(node, session, &receive, &message, status_kind) { let kind = event.kind; if let Err(e) = node.delivery.deliver(event).await { tracing::info!( @@ -345,6 +389,7 @@ async fn receive( .await .map_err(|e| node.ops_error(session, &e))?; respond(Out::Receive(ReceiveResult { + status_kind: status_kind.unwrap_or("").to_owned(), already_delivered: false, redelivered: message.redelivered, sender_key_rejected: message.sender_key_rejected, @@ -370,21 +415,38 @@ pub fn event_id(session_id: &str, receipt_key: &str) -> String { /// The application event for a received message, or `None` when the /// message is protocol-only (sender keys, app-state keys, peer data /// operations) or is history and history delivery is off. +fn publication_kind<'a>( + receive: &proto::Receive, + explicit: Option<&'a str>, + group_wrapper: bool, +) -> Option<&'a str> { + explicit + .or_else(|| (receive.chat.ends_with("@g.us") && group_wrapper).then_some("group")) + .or_else(|| (receive.chat == "status@broadcast").then_some("personal")) +} + fn event_for( node: &Node, session: &ActiveSession, receive: &proto::Receive, received: &wacore_signal_ops::ReceivedMessage, + status_kind: Option<&str>, ) -> Option { let content = &received.content; if content.is_skdm_only { return None; } + let status_kind = publication_kind( + receive, + status_kind, + content.message.group_status_message_v2.is_set(), + ); let id = event_id(&session.id, &received.receipt_key); let base = |kind: &'static str| Event { id: id.clone(), kind, session_id: session.id.clone(), + status_scope: status_kind.map(str::to_owned), chat: receive.chat.clone(), sender: receive.sender.clone(), sender_alt: Some(receive.sender_alt.clone()).filter(|s| !s.is_empty()), @@ -448,9 +510,18 @@ fn event_for( } else { &content.message }; + let display_message = if status_kind == Some("group") { + wa_message + .group_status_message_v2 + .as_option() + .and_then(|wrapped| wrapped.message.as_option()) + .unwrap_or(wa_message) + } else { + wa_message + }; Some(Event { - text: message::text_of(wa_message), - media: message::media_of(wa_message), + text: message::text_of(display_message), + media: message::media_of(display_message), message_proto: base64_of(&message::encode(wa_message)), history: content .protocol_message @@ -458,7 +529,11 @@ fn event_for( .and_then(|p| p.history_sync_notification.as_ref()) .filter(|_| node.cfg.deliver_history && receive.is_from_me) .map(message::history_descriptor), - ..base("message") + ..base(if status_kind.is_some() { + "status.received" + } else { + "message" + }) }) } diff --git a/tests/service_boundary.rs b/tests/service_boundary.rs index 61d3e66..b15c1d8 100644 --- a/tests/service_boundary.rs +++ b/tests/service_boundary.rs @@ -1384,3 +1384,272 @@ async fn a_node_without_a_live_link_takes_no_leases() { assert!(node.session(SESSION).is_none()); node.shutdown().await; } + +#[tokio::test] +async fn encrypted_status_uses_scoped_session_and_separate_publication_event() { + let hub = Hub::start().await; + let records = Arc::new(MemoryRecordStore::new()); + let running = start(&hub, "node-status", Backend::Memory(records.clone()), false).await; + let generation = running.node.session(SESSION).unwrap().generation; + let mut receive = encrypted( + &hub, + "node-status", + &running, + { + let mut message = wa::Message::default(); + message.conversation = Some("application-private-text".into()); + message + }, + false, + ) + .await; + receive.chat = "status@broadcast".into(); + let invalid_scope = hub + .command( + "node-status", + generation, + Op::StatusReceive(proto::StatusReceive { + payload: Some(receive.clone()), + status_kind: "group".into(), + status_session: true, + }), + ) + .await; + assert!( + matches!(invalid_scope.body, Some(Body::Error(proto::Error {code, ..})) if code == proto::Code::InvalidArgument as i32) + ); + assert!( + records + .scan_keys(SESSION, Namespace::Session, "", None) + .await + .unwrap() + .is_empty() + ); + let client = reqwest::Client::new(); + let url = format!("http://{}", running.http_addr); + let mut stream = client + .get(format!("{url}/v1/events")) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .bytes_stream(); + let command = hub.command( + "node-status", + generation, + Op::StatusReceive(proto::StatusReceive { + payload: Some(receive.clone()), + status_kind: "personal".into(), + status_session: true, + }), + ); + let app = async { + let event = next_event(&mut stream).await; + assert_eq!(event["text"], "application-private-text"); + assert_eq!(event["type"], "status.received"); + assert_eq!(event["status_scope"], "personal"); + assert!(!event["redelivered"].as_bool().unwrap()); + assert_status_records_sealed(&records).await; + assert_eq!( + client + .post(format!( + "{url}/v1/events/{}/ack", + event["id"].as_str().unwrap() + )) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .status(), + 204 + ); + event + }; + let (response, event) = tokio::join!(command, app); + assert!(matches!( + response.body, + Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(Out::Receive(proto::ReceiveResult { + already_delivered: false, + .. + })) + })) + )); + let again = hub + .command( + "node-status", + generation, + Op::StatusReceive(proto::StatusReceive { + payload: Some(receive), + status_kind: "personal".into(), + status_session: true, + }), + ) + .await; + assert!(matches!( + again.body, + Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(Out::Receive(proto::ReceiveResult { + already_delivered: true, + .. + })) + })) + )); + assert_status_records_sealed(&records).await; + assert_eq!( + client + .post(format!( + "{url}/v1/events/{}/ack", + event["id"].as_str().unwrap() + )) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .status(), + 404 + ); + drop(stream); + running.shutdown().await; +} + +#[tokio::test] +async fn legacy_group_publication_classification_survives_duplicate_delivery() { + let hub = Hub::start().await; + let records = Arc::new(MemoryRecordStore::new()); + let running = start( + &hub, + "node-legacy-group-status", + Backend::Memory(records.clone()), + false, + ) + .await; + let generation = running.node.session(SESSION).unwrap().generation; + let mut receive = encrypted( + &hub, + "node-legacy-group-status", + &running, + { + let mut message = wa::Message::default(); + message.conversation = Some("application-private-text".into()); + let mut wrapper = wa::message::FutureProofMessage::default(); + wrapper.message = message.into(); + let mut publication = wa::Message::default(); + publication.group_status_message_v2 = wrapper.into(); + publication + }, + false, + ) + .await; + receive.chat = "120363000000001@g.us".into(); + let invalid_scope = hub + .command( + "node-legacy-group-status", + generation, + Op::StatusReceive(proto::StatusReceive { + payload: Some(receive.clone()), + status_kind: "personal".into(), + status_session: true, + }), + ) + .await; + assert!( + matches!(invalid_scope.body, Some(Body::Error(proto::Error {code, ..})) if code == proto::Code::InvalidArgument as i32) + ); + assert!( + records + .scan_keys(SESSION, Namespace::Session, "", None) + .await + .unwrap() + .is_empty() + ); + let client = reqwest::Client::new(); + let url = format!("http://{}", running.http_addr); + let mut stream = client + .get(format!("{url}/v1/events")) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .bytes_stream(); + let command = hub.command( + "node-legacy-group-status", + generation, + Op::Receive(receive.clone()), + ); + let app = async { + let event = next_event(&mut stream).await; + assert_eq!(event["text"], "application-private-text"); + assert_eq!(event["type"], "status.received"); + assert_eq!(event["status_scope"], "group"); + assert!(!event["redelivered"].as_bool().unwrap()); + assert_status_records_sealed(&records).await; + assert_eq!( + client + .post(format!( + "{url}/v1/events/{}/ack", + event["id"].as_str().unwrap() + )) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .status(), + 204 + ); + event + }; + let (response, event) = tokio::join!(command, app); + assert!(matches!( + response.body, + Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(Out::Receive(proto::ReceiveResult { + already_delivered: false, + status_kind: ref classification, + .. + })) + })) if classification == "group" + )); + let again = hub + .command("node-legacy-group-status", generation, Op::Receive(receive)) + .await; + assert!(matches!( + again.body, + Some(Body::ServiceResponse(proto::ServiceResponse { + result: Some(Out::Receive(proto::ReceiveResult { + already_delivered: true, + status_kind: ref classification, + .. + })) + })) if classification == "group" + )); + assert_status_records_sealed(&records).await; + assert_eq!( + client + .post(format!( + "{url}/v1/events/{}/ack", + event["id"].as_str().unwrap() + )) + .bearer_auth(APP_SECRET) + .send() + .await + .unwrap() + .status(), + 404 + ); + drop(stream); + running.shutdown().await; +} + +async fn assert_status_records_sealed(records: &MemoryRecordStore) { + for ns in Namespace::ALL { + for key in records.scan_keys(SESSION, ns, "", None).await.unwrap() { + let value = records.get(SESSION, ns, &key).await.unwrap().unwrap(); + assert!( + !value + .windows(b"application-private-text".len()) + .any(|part| part == b"application-private-text") + ); + } + } +}