observation.rs (11072B)
1 use super::super::crypto::{DeliveryPayload, DeliveryScope, WorkspaceCrypto}; 2 use super::{ 3 codec::{self, WorkRef}, 4 graph::Graph, 5 }; 6 use radroots_domain::EnvelopeEventId; 7 use radroots_storage::command::{DeliveryAttempt, DeliveryObservation, OperationError}; 8 use radroots_transport::sink::DeliveryTargetReceipt; 9 use serde::{Deserialize, Serialize}; 10 use sha2::{Digest, Sha256}; 11 use sqlx::{Row, SqliteConnection}; 12 use zeroize::Zeroizing; 13 14 #[cfg(test)] 15 #[path = "observation_tests.rs"] 16 mod tests; 17 18 pub(super) const TABLE_V10: &str = "CREATE TABLE chat_delivery_observations (workspace BLOB NOT NULL, operation_id BLOB NOT NULL, recipient BLOB NOT NULL, request_key BLOB NOT NULL, target_key BLOB NOT NULL CHECK(length(target_key)=32), kind INTEGER NOT NULL CHECK(kind BETWEEN 1 AND 6), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(workspace,operation_id,recipient,request_key,target_key,kind), FOREIGN KEY(workspace,operation_id,recipient,request_key) REFERENCES chat_delivery_attempts(workspace,operation_id,recipient,request_key)) STRICT, WITHOUT ROWID"; 19 pub(super) const TABLE: &str = "CREATE TABLE artifact_delivery_observations (workspace BLOB NOT NULL, operation_id BLOB NOT NULL, artifact_key BLOB NOT NULL, request_key BLOB NOT NULL, target_key BLOB NOT NULL CHECK(length(target_key)=32), kind INTEGER NOT NULL CHECK(kind BETWEEN 1 AND 6), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(workspace,operation_id,artifact_key,request_key,target_key,kind), FOREIGN KEY(workspace,operation_id,artifact_key,request_key) REFERENCES artifact_delivery_attempts(workspace,operation_id,artifact_key,request_key)) STRICT, WITHOUT ROWID"; 20 const MAX_OBSERVATIONS: usize = 2 * 16 * 12 * 6; 21 22 #[derive(Serialize, Deserialize)] 23 #[serde(deny_unknown_fields)] 24 struct Wire { 25 version: u8, 26 work: WorkRef, 27 request: String, 28 event: EnvelopeEventId, 29 envelope_digest: [u8; 32], 30 target: DeliveryTargetReceipt, 31 kind: u8, 32 observed_at: Option<core::num::NonZeroU64>, 33 } 34 fn bad<E>(_: E) -> OperationError { 35 OperationError::RecoveryRequired 36 } 37 fn target_key(value: &DeliveryObservation) -> [u8; 32] { 38 Sha256::digest( 39 value 40 .target_receipt() 41 .target() 42 .fingerprint() 43 .as_str() 44 .as_bytes(), 45 ) 46 .into() 47 } 48 fn context(scope: DeliveryScope, target: [u8; 32], kind: u8) -> DeliveryScope { 49 let mut hash = Sha256::new(); 50 hash.update(b"radroots.delivery.observation.key.v1\0"); 51 hash.update(scope.request); 52 hash.update(target); 53 hash.update([kind]); 54 DeliveryScope { 55 request: hash.finalize().into(), 56 ..scope 57 } 58 } 59 pub(super) struct Fact { 60 scope: DeliveryScope, 61 target: [u8; 32], 62 kind: u8, 63 digest: [u8; 32], 64 } 65 pub(in crate::workspace) struct Observations { 66 pub reports: Vec<DeliveryObservation>, 67 facts: Vec<Fact>, 68 } 69 pub(super) struct Protected { 70 pub report: DeliveryObservation, 71 scope: DeliveryScope, 72 target: [u8; 32], 73 protected: Vec<u8>, 74 } 75 pub(super) async fn protect( 76 report: DeliveryObservation, 77 crypto: &WorkspaceCrypto, 78 ) -> Result<Protected, OperationError> { 79 let wire = Wire { 80 version: if report.work().recipient().is_some() { 81 1 82 } else { 83 2 84 }, 85 work: WorkRef::new(report.work()), 86 request: report.request_id().as_str().into(), 87 event: report.event_id(), 88 envelope_digest: *report.envelope_digest(), 89 target: report.target_receipt().clone(), 90 kind: report.kind().code(), 91 observed_at: report.observed_at_unix_ms(), 92 }; 93 let plain = Zeroizing::new(serde_json::to_vec(&wire).map_err(bad)?); 94 if plain.len() > 16 * 1024 { 95 return Err(OperationError::InvalidEvidence); 96 } 97 let scope = codec::scope( 98 report.work(), 99 codec::request_key(report.request_id().as_str()), 100 ); 101 let target = target_key(&report); 102 let protected = crypto 103 .seal_delivery( 104 context(scope, target, report.kind().code()), 105 DeliveryPayload::Observation, 106 0, 107 &plain, 108 ) 109 .await?; 110 Ok(Protected { 111 report, 112 scope, 113 target, 114 protected, 115 }) 116 } 117 impl Protected { 118 pub(super) async fn insert( 119 &self, 120 connection: &mut SqliteConnection, 121 ) -> Result<(), OperationError> { 122 let s = self.scope; 123 sqlx::query(super::sql(connection,"INSERT INTO chat_delivery_observations(workspace,operation_id,recipient,request_key,target_key,kind,protected) VALUES(?,?,?,?,?,?,?)").await?) 124 .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.target.key().as_slice()).bind(s.request.as_slice()).bind(self.target.as_slice()).bind(self.report.kind().code() as i64).bind(&self.protected).execute(connection).await.map_err(|_|OperationError::StorageFailure)?; 125 Ok(()) 126 } 127 } 128 impl Observations { 129 pub(in crate::workspace) async fn read( 130 connection: &mut SqliteConnection, 131 graph: &Graph, 132 selected: &super::super::AttachmentInput, 133 operation: radroots_domain::OperationId, 134 crypto: &WorkspaceCrypto, 135 ) -> Result<Self, OperationError> { 136 // Bound keys before fetching or opening any protected observation body. 137 let rows=sqlx::query(super::sql(connection,"SELECT CASE WHEN length(recipient)=32 OR (length(recipient)=1 AND hex(recipient)='00') THEN recipient END,CASE WHEN length(request_key)=32 THEN request_key END,CASE WHEN length(target_key)=32 THEN target_key END,kind FROM chat_delivery_observations WHERE workspace=? AND operation_id=? ORDER BY recipient,request_key,target_key,kind LIMIT 2305").await?) 138 .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).fetch_all(&mut *connection).await.map_err(bad)?; 139 if rows.len() > MAX_OBSERVATIONS { 140 return Err(OperationError::RecoveryRequired); 141 } 142 let mut result = Self { 143 reports: Vec::with_capacity(rows.len()), 144 facts: Vec::with_capacity(rows.len()), 145 }; 146 for row in rows { 147 let key = |index| -> Result<[u8; 32], OperationError> { 148 row.try_get::<Option<Vec<u8>>, _>(index) 149 .map_err(bad)? 150 .ok_or(OperationError::RecoveryRequired)? 151 .try_into() 152 .map_err(bad) 153 }; 154 let artifact = row 155 .try_get::<Option<Vec<u8>>, _>(0) 156 .map_err(bad)? 157 .ok_or(OperationError::RecoveryRequired)?; 158 let target_scope = 159 radroots_storage::command::SigningTarget::from_key(&artifact).map_err(bad)?; 160 let request = key(1)?; 161 let target = key(2)?; 162 let kind: i64 = row.try_get(3).map_err(bad)?; 163 if !(1..=6).contains(&kind) { 164 return Err(OperationError::RecoveryRequired); 165 } 166 let attempt = graph 167 .attempts 168 .iter() 169 .find(|attempt| { 170 attempt.work().target() == target_scope 171 && codec::request_key(attempt.intent().request_id().as_str()) == request 172 }) 173 .ok_or(OperationError::RecoveryRequired)?; 174 let scope = codec::scope(attempt.work(), request); 175 let protected = read_body(connection, scope, target, kind as u8).await?; 176 let plain = crypto 177 .open_delivery( 178 context(scope, target, kind as u8), 179 DeliveryPayload::Observation, 180 0, 181 &protected, 182 ) 183 .await?; 184 let wire: Wire = plain.expose_secret(|raw| { 185 if !super::json::bounded(raw, 32) { 186 return Err(OperationError::RecoveryRequired); 187 } 188 serde_json::from_slice(raw).map_err(bad) 189 })?; 190 let report = decode(wire, attempt, target, kind as u8)?; 191 result.reports.push(report); 192 result.facts.push(Fact { 193 scope, 194 target, 195 kind: kind as u8, 196 digest: Sha256::digest(&protected).into(), 197 }); 198 } 199 Ok(result) 200 } 201 pub(in crate::workspace) async fn unchanged( 202 &self, 203 connection: &mut SqliteConnection, 204 selected: &super::super::AttachmentInput, 205 operation: radroots_domain::OperationId, 206 ) -> Result<(), OperationError> { 207 let count:i64=sqlx::query_scalar(super::sql(connection,"SELECT count(*) FROM (SELECT 1 FROM chat_delivery_observations WHERE workspace=? AND operation_id=? LIMIT 2305)").await?) 208 .bind(selected.workspace_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).fetch_one(&mut *connection).await.map_err(bad)?; 209 if count != self.facts.len() as i64 { 210 return Err(OperationError::RecoveryRequired); 211 } 212 for fact in &self.facts { 213 let protected = read_body(connection, fact.scope, fact.target, fact.kind).await?; 214 if <[u8; 32]>::from(Sha256::digest(&protected)) != fact.digest { 215 return Err(OperationError::RecoveryRequired); 216 } 217 } 218 Ok(()) 219 } 220 } 221 fn decode( 222 wire: Wire, 223 attempt: &DeliveryAttempt, 224 target: [u8; 32], 225 kind: u8, 226 ) -> Result<DeliveryObservation, OperationError> { 227 if wire.version 228 != if attempt.work().recipient().is_some() { 229 1 230 } else { 231 2 232 } 233 || wire.work != WorkRef::new(attempt.work()) 234 || wire.request != attempt.intent().request_id().as_str() 235 || wire.event != attempt.intent().event_id() 236 || wire.envelope_digest != *attempt.envelope_digest() 237 || wire.kind != kind 238 { 239 return Err(OperationError::RecoveryRequired); 240 } 241 let report = DeliveryObservation::from_store_report(attempt, &wire.target, wire.observed_at) 242 .map_err(bad)?; 243 if report.kind().code() != kind || target_key(&report) != target { 244 return Err(OperationError::RecoveryRequired); 245 } 246 Ok(report) 247 } 248 async fn read_body( 249 connection: &mut SqliteConnection, 250 s: DeliveryScope, 251 target: [u8; 32], 252 kind: u8, 253 ) -> Result<Vec<u8>, OperationError> { 254 sqlx::query_scalar::<_,Option<Vec<u8>>>(super::sql(connection,"SELECT CASE WHEN length(protected)>0 AND length(protected)<=262144 THEN protected END FROM chat_delivery_observations WHERE workspace=? AND operation_id=? AND recipient=? AND request_key=? AND target_key=? AND kind=?").await?) 255 .bind(s.workspace.as_bytes().as_slice()).bind(s.operation.as_bytes().as_slice()).bind(s.target.key().as_slice()).bind(s.request.as_slice()).bind(target.as_slice()).bind(kind as i64).fetch_optional(connection).await.map_err(bad)?.flatten().ok_or(OperationError::RecoveryRequired) 256 }