| 1 | use crate::protocol::*; |
| 2 | use std::io::{self, Read, Write}; |
| 3 | |
| 4 | pub const MAX_BODY: usize = 1_048_576; |
| 5 | pub const MAX_FRAMES: u64 = 1_000_000; |
| 6 | const LEGACY_VECTOR_ITEMS: usize = 32_768; |
| 7 | const MAX_SELECTION_IDS: usize = 200_000; |
| 8 | fn invalid() -> io::Error { |
| 9 | io::Error::new(io::ErrorKind::InvalidData, "invalid worker frame") |
| 10 | } |
| 11 | struct Encoder { |
| 12 | bytes: Vec<u8>, |
| 13 | selection_limit: usize, |
| 14 | } |
| 15 | impl Encoder { |
| 16 | fn put(&mut self, b: &[u8]) -> io::Result<()> { |
| 17 | if self |
| 18 | .bytes |
| 19 | .len() |
| 20 | .checked_add(b.len()) |
| 21 | .filter(|n| *n <= MAX_BODY) |
| 22 | .is_none() |
| 23 | { |
| 24 | return Err(invalid()); |
| 25 | } |
| 26 | self.bytes.extend_from_slice(b); |
| 27 | Ok(()) |
| 28 | } |
| 29 | } |
| 30 | struct Decoder<'a> { |
| 31 | bytes: &'a [u8], |
| 32 | at: usize, |
| 33 | selection_limit: usize, |
| 34 | } |
| 35 | impl<'a> Decoder<'a> { |
| 36 | fn take(&mut self, n: usize) -> io::Result<&'a [u8]> { |
| 37 | let end = self.at.checked_add(n).ok_or_else(invalid)?; |
| 38 | let b = self.bytes.get(self.at..end).ok_or_else(invalid)?; |
| 39 | self.at = end; |
| 40 | Ok(b) |
| 41 | } |
| 42 | } |
| 43 | trait Wire: Sized { |
| 44 | fn enc(&self, e: &mut Encoder) -> io::Result<()>; |
| 45 | fn dec(d: &mut Decoder<'_>) -> io::Result<Self>; |
| 46 | } |
| 47 | macro_rules! integer { ($($t:ty),*) => { $(impl Wire for $t { |
| 48 | fn enc(&self,e:&mut Encoder)->io::Result<()> { e.put(&self.to_le_bytes()) } |
| 49 | fn dec(d:&mut Decoder<'_>)->io::Result<Self> { Ok(Self::from_le_bytes(d.take(size_of::<Self>())?.try_into().map_err(|_|invalid())?)) } |
| 50 | })* }; } |
| 51 | integer!(u8, u16, u32, u64); |
| 52 | impl<const N: usize> Wire for [u8; N] { |
| 53 | fn enc(&self, e: &mut Encoder) -> io::Result<()> { |
| 54 | e.put(self) |
| 55 | } |
| 56 | fn dec(d: &mut Decoder<'_>) -> io::Result<Self> { |
| 57 | d.take(N)?.try_into().map_err(|_| invalid()) |
| 58 | } |
| 59 | } |
| 60 | impl Wire for bool { |
| 61 | fn enc(&self, e: &mut Encoder) -> io::Result<()> { |
| 62 | u8::from(*self).enc(e) |
| 63 | } |
| 64 | fn dec(d: &mut Decoder<'_>) -> io::Result<Self> { |
| 65 | match u8::dec(d)? { |
| 66 | 0 => Ok(false), |
| 67 | 1 => Ok(true), |
| 68 | _ => Err(invalid()), |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | impl<T: Wire> Wire for Option<T> { |
| 73 | fn enc(&self, e: &mut Encoder) -> io::Result<()> { |
| 74 | self.is_some().enc(e)?; |
| 75 | if let Some(v) = self { |
| 76 | v.enc(e)?; |
| 77 | } |
| 78 | Ok(()) |
| 79 | } |
| 80 | fn dec(d: &mut Decoder<'_>) -> io::Result<Self> { |
| 81 | if bool::dec(d)? { |
| 82 | Ok(Some(T::dec(d)?)) |
| 83 | } else { |
| 84 | Ok(None) |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | fn encode_vec<T: Wire>(values: &[T], limit: usize, e: &mut Encoder) -> io::Result<()> { |
| 89 | if values.len() > limit { |
| 90 | return Err(invalid()); |
| 91 | } |
| 92 | (values.len() as u32).enc(e)?; |
| 93 | for value in values { |
| 94 | value.enc(e)?; |
| 95 | } |
| 96 | Ok(()) |
| 97 | } |
| 98 | fn decode_vec<T: Wire>(limit: usize, d: &mut Decoder<'_>) -> io::Result<Vec<T>> { |
| 99 | let n = u32::dec(d)? as usize; |
| 100 | if n > limit || n > d.bytes.len() - d.at { |
| 101 | return Err(invalid()); |
| 102 | } |
| 103 | let mut values = Vec::new(); |
| 104 | values.try_reserve(n).map_err(|_| invalid())?; |
| 105 | for _ in 0..n { |
| 106 | values.push(T::dec(d)?); |
| 107 | } |
| 108 | Ok(values) |
| 109 | } |
| 110 | impl<T: Wire> Wire for Vec<T> { |
| 111 | fn enc(&self, e: &mut Encoder) -> io::Result<()> { |
| 112 | encode_vec(self, LEGACY_VECTOR_ITEMS, e) |
| 113 | } |
| 114 | fn dec(d: &mut Decoder<'_>) -> io::Result<Self> { |
| 115 | decode_vec(LEGACY_VECTOR_ITEMS, d) |
| 116 | } |
| 117 | } |
| 118 | impl Wire for String { |
| 119 | fn enc(&self, e: &mut Encoder) -> io::Result<()> { |
| 120 | if self.len() > 32768 { |
| 121 | return Err(invalid()); |
| 122 | } |
| 123 | (self.len() as u32).enc(e)?; |
| 124 | e.put(self.as_bytes()) |
| 125 | } |
| 126 | fn dec(d: &mut Decoder<'_>) -> io::Result<Self> { |
| 127 | let n = u32::dec(d)? as usize; |
| 128 | if n > 32768 { |
| 129 | return Err(invalid()); |
| 130 | } |
| 131 | String::from_utf8(d.take(n)?.to_vec()).map_err(|_| invalid()) |
| 132 | } |
| 133 | } |
| 134 | macro_rules! enumeration { ($t:ident {$($v:ident=$n:expr),*$(,)?}) => { impl Wire for $t { |
| 135 | fn enc(&self,e:&mut Encoder)->io::Result<()> { (match self {$(Self::$v=>$n,)*} as u8).enc(e) } |
| 136 | fn dec(d:&mut Decoder<'_>)->io::Result<Self> { match u8::dec(d)? {$($n=>Ok(Self::$v),)*_=>Err(invalid())} } |
| 137 | } }; } |
| 138 | enumeration!(Format{Zip=0,Tar=1,Gzip=2,TarGzip=3,SevenZip=4}); |
| 139 | enumeration!(Method{Store=0,Deflate=1}); |
| 140 | enumeration!(SevenZipMethod{Lzma2=0}); |
| 141 | enumeration!(Encryption{None=0,Aes256=1}); |
| 142 | enumeration!(ActionKind{List=0,Extract=1,CreateZip=2,Preview=3,CreateSevenZip=4}); |
| 143 | enumeration!(PreviewFormat{Utf8Text=0,Rgba8=1}); |
| 144 | enumeration!(EntryKind{File=0,Directory=1,Link=2,Other=3}); |
| 145 | enumeration!(Stage{Scan=0,Read=1,Decode=2,Write=3,Verify=4,Commit=5}); |
| 146 | enumeration!(Disposition{Created=0,PartialRetained=1,Removed=2,CommitUncertain=3}); |
| 147 | enumeration!(Outcome{Completed=0,Failed=1,Cancelled=2}); |
| 148 | enumeration!(ErrorCode{None=0,Integrity=1,UnsupportedFormat=2,UnsupportedFeature=3,WrongPassword=4,UnsafePath=5,Conflict=6,Limit=7,Io=8,Protocol=9,Internal=10,SourceChanged=11,InvalidRequest=12,DiskFull=13,MissingVolume=14}); |
| 149 | enumeration!(VolumeProblemReason{FirstVolumeRequired=0,MissingVolume=1,IncompleteSet=2}); |
| 150 | enumeration!(Publication{None=0,Partial=1,Complete=2,Uncertain=3}); |
| 151 | enumeration!(Durability{NotApplicable=0,Synced=1,Unconfirmed=2,Failed=3}); |
| 152 | enumeration!(Cleanup{Complete=0,Failed=1,Unconfirmed=2}); |
| 153 | enumeration!(CancelStatus{Requested=0,AlreadyRequested=1,TooLate=2,Finished=3}); |
| 154 | enumeration!(PasswordPurpose{Decrypt=0,Encrypt=1}); |
| 155 | enumeration!(ConflictChoice{Overwrite=0,Skip=1,Rename=2}); |
| 156 | macro_rules! structure {($t:ident {$($f:ident),*})=>{impl Wire for $t { |
| 157 | fn enc(&self,e:&mut Encoder)->io::Result<()> {$(self.$f.enc(e)?;)*Ok(())} |
| 158 | fn dec(d:&mut Decoder<'_>)->io::Result<Self> {Ok(Self {$($f:Wire::dec(d)?,)*})} |
| 159 | }};} |
| 160 | structure!(Limits { |
| 161 | input, |
| 162 | output, |
| 163 | entries |
| 164 | }); |
| 165 | impl Wire for ListId { |
| 166 | fn enc(&self, e: &mut Encoder) -> io::Result<()> { |
| 167 | if !self.valid() { |
| 168 | return Err(invalid()); |
| 169 | } |
| 170 | self.catalog_version.enc(e)?; |
| 171 | self.format.enc(e)?; |
| 172 | self.source_object.enc(e)?; |
| 173 | self.archive_bytes.enc(e)?; |
| 174 | self.archive_sha256.enc(e) |
| 175 | } |
| 176 | fn dec(d: &mut Decoder<'_>) -> io::Result<Self> { |
| 177 | let id = Self { |
| 178 | catalog_version: Wire::dec(d)?, |
| 179 | format: Wire::dec(d)?, |
| 180 | source_object: Wire::dec(d)?, |
| 181 | archive_bytes: Wire::dec(d)?, |
| 182 | archive_sha256: Wire::dec(d)?, |
| 183 | }; |
| 184 | if !id.valid() { |
| 185 | return Err(invalid()); |
| 186 | } |
| 187 | Ok(id) |
| 188 | } |
| 189 | } |
| 190 | impl Wire for PreviewInfo { |
| 191 | fn enc(&self, e: &mut Encoder) -> io::Result<()> { |
| 192 | if !self.valid() { |
| 193 | return Err(invalid()); |
| 194 | } |
| 195 | self.entry_id.enc(e)?; |
| 196 | self.format.enc(e)?; |
| 197 | self.total_bytes.enc(e)?; |
| 198 | self.width.enc(e)?; |
| 199 | self.height.enc(e) |
| 200 | } |
| 201 | fn dec(d: &mut Decoder<'_>) -> io::Result<Self> { |
| 202 | let info = Self { |
| 203 | entry_id: Wire::dec(d)?, |
| 204 | format: Wire::dec(d)?, |
| 205 | total_bytes: Wire::dec(d)?, |
| 206 | width: Wire::dec(d)?, |
| 207 | height: Wire::dec(d)?, |
| 208 | }; |
| 209 | if !info.valid() { |
| 210 | return Err(invalid()); |
| 211 | } |
| 212 | Ok(info) |
| 213 | } |
| 214 | } |
| 215 | structure!(Source { path, archive_name }); |
| 216 | structure!(Start { action, limits }); |
| 217 | structure!(GuardedStart { |
| 218 | start, |
| 219 | ceiling, |
| 220 | interactive |
| 221 | }); |
| 222 | structure!(ResourceScope { |
| 223 | output, |
| 224 | entries, |
| 225 | ratio |
| 226 | }); |
| 227 | structure!(ResourceUsage { |
| 228 | estimated_bytes, |
| 229 | actual_bytes, |
| 230 | estimated_entries, |
| 231 | actual_entries, |
| 232 | compressed_bytes, |
| 233 | ratio_output_bytes, |
| 234 | available_bytes |
| 235 | }); |
| 236 | structure!(Entry { |
| 237 | entry_id, |
| 238 | name, |
| 239 | kind, |
| 240 | size, |
| 241 | compressed_size, |
| 242 | encrypted |
| 243 | }); |
| 244 | structure!(Terminal { |
| 245 | outcome, |
| 246 | error, |
| 247 | publication, |
| 248 | created_count, |
| 249 | retained_partial_count, |
| 250 | durability, |
| 251 | cleanup, |
| 252 | detail |
| 253 | }); |
| 254 | impl Wire for NativePath { |
| 255 | fn enc(&self, e: &mut Encoder) -> io::Result<()> { |
| 256 | match self { |
| 257 | Self::Unix(v) => { |
| 258 | 1u8.enc(e)?; |
| 259 | v.enc(e) |
| 260 | } |
| 261 | Self::Windows(v) => { |
| 262 | 2u8.enc(e)?; |
| 263 | v.enc(e) |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | fn dec(d: &mut Decoder<'_>) -> io::Result<Self> { |
| 268 | match u8::dec(d)? { |
| 269 | 1 => Ok(Self::Unix(Wire::dec(d)?)), |
| 270 | 2 => Ok(Self::Windows(Wire::dec(d)?)), |
| 271 | _ => Err(invalid()), |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | impl Wire for SourceObject { |
| 276 | fn enc(&self, e: &mut Encoder) -> io::Result<()> { |
| 277 | match self { |
| 278 | Self::Unix { device, inode } => { |
| 279 | 1u8.enc(e)?; |
| 280 | device.enc(e)?; |
| 281 | inode.enc(e) |
| 282 | } |
| 283 | Self::Windows { |
| 284 | volume_serial, |
| 285 | file_id, |
| 286 | } => { |
| 287 | 2u8.enc(e)?; |
| 288 | volume_serial.enc(e)?; |
| 289 | file_id.enc(e) |
| 290 | } |
| 291 | Self::SplitSevenZip { manifest_sha256 } => { |
| 292 | 3u8.enc(e)?; |
| 293 | manifest_sha256.enc(e) |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | fn dec(d: &mut Decoder<'_>) -> io::Result<Self> { |
| 298 | match u8::dec(d)? { |
| 299 | 1 => Ok(Self::Unix { |
| 300 | device: Wire::dec(d)?, |
| 301 | inode: Wire::dec(d)?, |
| 302 | }), |
| 303 | 2 => Ok(Self::Windows { |
| 304 | volume_serial: Wire::dec(d)?, |
| 305 | file_id: Wire::dec(d)?, |
| 306 | }), |
| 307 | 3 => Ok(Self::SplitSevenZip { |
| 308 | manifest_sha256: Wire::dec(d)?, |
| 309 | }), |
| 310 | _ => Err(invalid()), |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | pub(crate) fn encode_source_object(value: &SourceObject) -> io::Result<Vec<u8>> { |
| 315 | encode_component(value) |
| 316 | } |
| 317 | pub(crate) fn encode_native_path(value: &NativePath) -> io::Result<Vec<u8>> { |
| 318 | encode_component(value) |
| 319 | } |
| 320 | fn encode_component(value: &impl Wire) -> io::Result<Vec<u8>> { |
| 321 | let mut encoder = Encoder { |
| 322 | bytes: Vec::new(), |
| 323 | selection_limit: LEGACY_VECTOR_ITEMS, |
| 324 | }; |
| 325 | value.enc(&mut encoder)?; |
| 326 | Ok(encoder.bytes) |
| 327 | } |
| 328 | impl Wire for Selection { |
| 329 | fn enc(&self, e: &mut Encoder) -> io::Result<()> { |
| 330 | match self { |
| 331 | Self::All => 0u8.enc(e), |
| 332 | Self::EntryIds(ids) => { |
| 333 | 1u8.enc(e)?; |
| 334 | encode_vec(ids, e.selection_limit, e) |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | fn dec(d: &mut Decoder<'_>) -> io::Result<Self> { |
| 339 | match u8::dec(d)? { |
| 340 | 0 => Ok(Self::All), |
| 341 | 1 => Ok(Self::EntryIds(decode_vec(d.selection_limit, d)?)), |
| 342 | _ => Err(invalid()), |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | impl Wire for Action { |
| 347 | fn enc(&self, e: &mut Encoder) -> io::Result<()> { |
| 348 | match self { |
| 349 | Self::List { archive } => { |
| 350 | 0u8.enc(e)?; |
| 351 | archive.enc(e) |
| 352 | } |
| 353 | Self::Extract { |
| 354 | archive, |
| 355 | destination, |
| 356 | selection, |
| 357 | list_id, |
| 358 | gzip_output_name, |
| 359 | } => { |
| 360 | 1u8.enc(e)?; |
| 361 | archive.enc(e)?; |
| 362 | destination.enc(e)?; |
| 363 | selection.enc(e)?; |
| 364 | list_id.enc(e)?; |
| 365 | gzip_output_name.enc(e) |
| 366 | } |
| 367 | Self::CreateZip { |
| 368 | sources, |
| 369 | output_file, |
| 370 | method, |
| 371 | encryption, |
| 372 | } => { |
| 373 | 2u8.enc(e)?; |
| 374 | sources.enc(e)?; |
| 375 | output_file.enc(e)?; |
| 376 | method.enc(e)?; |
| 377 | encryption.enc(e) |
| 378 | } |
| 379 | Self::Preview { |
| 380 | archive, |
| 381 | entry_id, |
| 382 | list_id, |
| 383 | } => { |
| 384 | 3u8.enc(e)?; |
| 385 | archive.enc(e)?; |
| 386 | entry_id.enc(e)?; |
| 387 | list_id.enc(e) |
| 388 | } |
| 389 | Self::CreateSevenZip { |
| 390 | sources, |
| 391 | output_file, |
| 392 | method, |
| 393 | solid, |
| 394 | encryption, |
| 395 | } => { |
| 396 | 4u8.enc(e)?; |
| 397 | sources.enc(e)?; |
| 398 | output_file.enc(e)?; |
| 399 | method.enc(e)?; |
| 400 | solid.enc(e)?; |
| 401 | encryption.enc(e) |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | fn dec(d: &mut Decoder<'_>) -> io::Result<Self> { |
| 406 | match u8::dec(d)? { |
| 407 | 0 => Ok(Self::List { |
| 408 | archive: Wire::dec(d)?, |
| 409 | }), |
| 410 | 1 => Ok(Self::Extract { |
| 411 | archive: Wire::dec(d)?, |
| 412 | destination: Wire::dec(d)?, |
| 413 | selection: Wire::dec(d)?, |
| 414 | list_id: Wire::dec(d)?, |
| 415 | gzip_output_name: Wire::dec(d)?, |
| 416 | }), |
| 417 | 2 => Ok(Self::CreateZip { |
| 418 | sources: Wire::dec(d)?, |
| 419 | output_file: Wire::dec(d)?, |
| 420 | method: Wire::dec(d)?, |
| 421 | encryption: Wire::dec(d)?, |
| 422 | }), |
| 423 | 3 => Ok(Self::Preview { |
| 424 | archive: Wire::dec(d)?, |
| 425 | entry_id: Wire::dec(d)?, |
| 426 | list_id: Wire::dec(d)?, |
| 427 | }), |
| 428 | 4 => Ok(Self::CreateSevenZip { |
| 429 | sources: Wire::dec(d)?, |
| 430 | output_file: Wire::dec(d)?, |
| 431 | method: Wire::dec(d)?, |
| 432 | solid: Wire::dec(d)?, |
| 433 | encryption: Wire::dec(d)?, |
| 434 | }), |
| 435 | _ => Err(invalid()), |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | fn valid_preview_chunk(offset: u64, length: usize) -> bool { |
| 440 | (1..=MAX_PREVIEW_CHUNK_BYTES).contains(&length) |
| 441 | && offset |
| 442 | .checked_add(length as u64) |
| 443 | .is_some_and(|end| end <= MAX_PREVIEW_RGBA_BYTES) |
| 444 | } |
| 445 | impl Message { |
| 446 | fn encode(&self) -> io::Result<(u16, Vec<u8>)> { |
| 447 | let mut e = Encoder { |
| 448 | bytes: Vec::new(), |
| 449 | selection_limit: LEGACY_VECTOR_ITEMS, |
| 450 | }; |
| 451 | let kind = match self { |
| 452 | Self::Start(v) => { |
| 453 | if matches!(v.action, Action::Preview { .. }) { |
| 454 | return Err(invalid()); |
| 455 | } |
| 456 | v.enc(&mut e)?; |
| 457 | 1 |
| 458 | } |
| 459 | Self::Password { |
| 460 | challenge_id, |
| 461 | secret, |
| 462 | } => { |
| 463 | if secret.0.len() > 1024 || std::str::from_utf8(&secret.0).is_err() { |
| 464 | return Err(invalid()); |
| 465 | } |
| 466 | challenge_id.enc(&mut e)?; |
| 467 | secret.0.enc(&mut e)?; |
| 468 | 2 |
| 469 | } |
| 470 | Self::Cancel => 3, |
| 471 | Self::StartInteractive(v) => { |
| 472 | if !matches!(&v.action, Action::Extract { .. }) { |
| 473 | return Err(invalid()); |
| 474 | } |
| 475 | v.enc(&mut e)?; |
| 476 | 4 |
| 477 | } |
| 478 | Self::ConflictReply { |
| 479 | challenge_id, |
| 480 | choice, |
| 481 | apply_to_remaining, |
| 482 | } => { |
| 483 | challenge_id.enc(&mut e)?; |
| 484 | choice.enc(&mut e)?; |
| 485 | apply_to_remaining.enc(&mut e)?; |
| 486 | 5 |
| 487 | } |
| 488 | Self::StartGuarded(v) => { |
| 489 | if !v.valid() { |
| 490 | return Err(invalid()); |
| 491 | } |
| 492 | e.selection_limit = LEGACY_VECTOR_ITEMS.max(v.ceiling.entries as usize); |
| 493 | v.enc(&mut e)?; |
| 494 | 6 |
| 495 | } |
| 496 | Self::ResourceReply { |
| 497 | challenge_id, |
| 498 | scope, |
| 499 | allow, |
| 500 | } => { |
| 501 | challenge_id.enc(&mut e)?; |
| 502 | scope.enc(&mut e)?; |
| 503 | allow.enc(&mut e)?; |
| 504 | 7 |
| 505 | } |
| 506 | Self::ResourceRequired { |
| 507 | challenge_id, |
| 508 | action, |
| 509 | scope, |
| 510 | usage, |
| 511 | soft_limits, |
| 512 | ceiling, |
| 513 | } => { |
| 514 | challenge_id.enc(&mut e)?; |
| 515 | action.enc(&mut e)?; |
| 516 | scope.enc(&mut e)?; |
| 517 | usage.enc(&mut e)?; |
| 518 | soft_limits.enc(&mut e)?; |
| 519 | ceiling.enc(&mut e)?; |
| 520 | 110 |
| 521 | } |
| 522 | Self::Accepted { action, encryption } => { |
| 523 | action.enc(&mut e)?; |
| 524 | encryption.enc(&mut e)?; |
| 525 | 100 |
| 526 | } |
| 527 | Self::Entry(v) => { |
| 528 | v.enc(&mut e)?; |
| 529 | 101 |
| 530 | } |
| 531 | Self::ListEnd { |
| 532 | entry_count, |
| 533 | list_id, |
| 534 | } => { |
| 535 | entry_count.enc(&mut e)?; |
| 536 | list_id.enc(&mut e)?; |
| 537 | 102 |
| 538 | } |
| 539 | Self::PasswordRequired { |
| 540 | challenge_id, |
| 541 | attempt, |
| 542 | purpose, |
| 543 | } => { |
| 544 | challenge_id.enc(&mut e)?; |
| 545 | attempt.enc(&mut e)?; |
| 546 | purpose.enc(&mut e)?; |
| 547 | 103 |
| 548 | } |
| 549 | Self::Progress { |
| 550 | stage, |
| 551 | processed_bytes, |
| 552 | total_bytes, |
| 553 | completed_entries, |
| 554 | } => { |
| 555 | stage.enc(&mut e)?; |
| 556 | processed_bytes.enc(&mut e)?; |
| 557 | total_bytes.enc(&mut e)?; |
| 558 | completed_entries.enc(&mut e)?; |
| 559 | 104 |
| 560 | } |
| 561 | Self::Output { |
| 562 | entry_id, |
| 563 | relative_name, |
| 564 | disposition, |
| 565 | } => { |
| 566 | entry_id.enc(&mut e)?; |
| 567 | relative_name.enc(&mut e)?; |
| 568 | disposition.enc(&mut e)?; |
| 569 | 105 |
| 570 | } |
| 571 | Self::PreviewBegin(info) => { |
| 572 | info.enc(&mut e)?; |
| 573 | 111 |
| 574 | } |
| 575 | Self::PreviewChunk { |
| 576 | entry_id, |
| 577 | offset, |
| 578 | data, |
| 579 | } => { |
| 580 | if !valid_preview_chunk(*offset, data.len()) { |
| 581 | return Err(invalid()); |
| 582 | } |
| 583 | entry_id.enc(&mut e)?; |
| 584 | offset.enc(&mut e)?; |
| 585 | (data.len() as u32).enc(&mut e)?; |
| 586 | e.put(data)?; |
| 587 | 112 |
| 588 | } |
| 589 | Self::PreviewEnd { |
| 590 | entry_id, |
| 591 | total_bytes, |
| 592 | sha256, |
| 593 | } => { |
| 594 | if *total_bytes > MAX_PREVIEW_RGBA_BYTES { |
| 595 | return Err(invalid()); |
| 596 | } |
| 597 | entry_id.enc(&mut e)?; |
| 598 | total_bytes.enc(&mut e)?; |
| 599 | sha256.enc(&mut e)?; |
| 600 | 113 |
| 601 | } |
| 602 | Self::VolumeProblem { reason, volume } => { |
| 603 | if !volume.is_basename() { |
| 604 | return Err(invalid()); |
| 605 | } |
| 606 | reason.enc(&mut e)?; |
| 607 | volume.enc(&mut e)?; |
| 608 | 114 |
| 609 | } |
| 610 | Self::Terminal(v) => { |
| 611 | if v.detail.len() > 4096 { |
| 612 | return Err(invalid()); |
| 613 | } |
| 614 | v.enc(&mut e)?; |
| 615 | 106 |
| 616 | } |
| 617 | Self::CancelAck { |
| 618 | request_sequence, |
| 619 | status, |
| 620 | } => { |
| 621 | request_sequence.enc(&mut e)?; |
| 622 | status.enc(&mut e)?; |
| 623 | 107 |
| 624 | } |
| 625 | Self::ConflictRequired { |
| 626 | challenge_id, |
| 627 | entry_id, |
| 628 | relative_name, |
| 629 | incoming_size, |
| 630 | existing_size, |
| 631 | } => { |
| 632 | challenge_id.enc(&mut e)?; |
| 633 | entry_id.enc(&mut e)?; |
| 634 | relative_name.enc(&mut e)?; |
| 635 | incoming_size.enc(&mut e)?; |
| 636 | existing_size.enc(&mut e)?; |
| 637 | 108 |
| 638 | } |
| 639 | Self::ConflictResolved { |
| 640 | entry_id, |
| 641 | relative_name, |
| 642 | choice, |
| 643 | } => { |
| 644 | entry_id.enc(&mut e)?; |
| 645 | relative_name.enc(&mut e)?; |
| 646 | choice.enc(&mut e)?; |
| 647 | 109 |
| 648 | } |
| 649 | }; |
| 650 | Ok((kind, e.bytes)) |
| 651 | } |
| 652 | fn decode(kind: u16, b: &[u8]) -> io::Result<Self> { |
| 653 | let mut d = Decoder { |
| 654 | bytes: b, |
| 655 | at: 0, |
| 656 | selection_limit: if kind == 6 { |
| 657 | MAX_SELECTION_IDS |
| 658 | } else { |
| 659 | LEGACY_VECTOR_ITEMS |
| 660 | }, |
| 661 | }; |
| 662 | let m = match kind { |
| 663 | 1 => { |
| 664 | let start = Start::dec(&mut d)?; |
| 665 | if matches!(start.action, Action::Preview { .. }) { |
| 666 | return Err(invalid()); |
| 667 | } |
| 668 | Self::Start(start) |
| 669 | } |
| 670 | 2 => { |
| 671 | let challenge_id = Wire::dec(&mut d)?; |
| 672 | let secret = Secret(Vec::<u8>::dec(&mut d)?); |
| 673 | if secret.0.len() > 1024 || std::str::from_utf8(&secret.0).is_err() { |
| 674 | return Err(invalid()); |
| 675 | } |
| 676 | Self::Password { |
| 677 | challenge_id, |
| 678 | secret, |
| 679 | } |
| 680 | } |
| 681 | 3 => Self::Cancel, |
| 682 | 4 => { |
| 683 | let start = Start::dec(&mut d)?; |
| 684 | if !matches!(&start.action, Action::Extract { .. }) { |
| 685 | return Err(invalid()); |
| 686 | } |
| 687 | Self::StartInteractive(start) |
| 688 | } |
| 689 | 5 => Self::ConflictReply { |
| 690 | challenge_id: Wire::dec(&mut d)?, |
| 691 | choice: Wire::dec(&mut d)?, |
| 692 | apply_to_remaining: Wire::dec(&mut d)?, |
| 693 | }, |
| 694 | 6 => { |
| 695 | let start = GuardedStart::dec(&mut d)?; |
| 696 | let limit = LEGACY_VECTOR_ITEMS.max(start.ceiling.entries as usize); |
| 697 | if !start.valid() |
| 698 | || matches!( |
| 699 | &start.start.action, |
| 700 | Action::Extract { selection: Selection::EntryIds(ids), .. } if ids.len() > limit |
| 701 | ) |
| 702 | { |
| 703 | return Err(invalid()); |
| 704 | } |
| 705 | Self::StartGuarded(start) |
| 706 | } |
| 707 | 7 => Self::ResourceReply { |
| 708 | challenge_id: Wire::dec(&mut d)?, |
| 709 | scope: Wire::dec(&mut d)?, |
| 710 | allow: Wire::dec(&mut d)?, |
| 711 | }, |
| 712 | 110 => Self::ResourceRequired { |
| 713 | challenge_id: Wire::dec(&mut d)?, |
| 714 | action: Wire::dec(&mut d)?, |
| 715 | scope: Wire::dec(&mut d)?, |
| 716 | usage: Wire::dec(&mut d)?, |
| 717 | soft_limits: Wire::dec(&mut d)?, |
| 718 | ceiling: Wire::dec(&mut d)?, |
| 719 | }, |
| 720 | 100 => Self::Accepted { |
| 721 | action: Wire::dec(&mut d)?, |
| 722 | encryption: Wire::dec(&mut d)?, |
| 723 | }, |
| 724 | 101 => Self::Entry(Wire::dec(&mut d)?), |
| 725 | 102 => Self::ListEnd { |
| 726 | entry_count: Wire::dec(&mut d)?, |
| 727 | list_id: Wire::dec(&mut d)?, |
| 728 | }, |
| 729 | 103 => Self::PasswordRequired { |
| 730 | challenge_id: Wire::dec(&mut d)?, |
| 731 | attempt: Wire::dec(&mut d)?, |
| 732 | purpose: Wire::dec(&mut d)?, |
| 733 | }, |
| 734 | 104 => Self::Progress { |
| 735 | stage: Wire::dec(&mut d)?, |
| 736 | processed_bytes: Wire::dec(&mut d)?, |
| 737 | total_bytes: Wire::dec(&mut d)?, |
| 738 | completed_entries: Wire::dec(&mut d)?, |
| 739 | }, |
| 740 | 105 => Self::Output { |
| 741 | entry_id: Wire::dec(&mut d)?, |
| 742 | relative_name: Wire::dec(&mut d)?, |
| 743 | disposition: Wire::dec(&mut d)?, |
| 744 | }, |
| 745 | 111 => Self::PreviewBegin(Wire::dec(&mut d)?), |
| 746 | 112 => { |
| 747 | let entry_id = Wire::dec(&mut d)?; |
| 748 | let offset = Wire::dec(&mut d)?; |
| 749 | let length = u32::dec(&mut d)? as usize; |
| 750 | if !valid_preview_chunk(offset, length) { |
| 751 | return Err(invalid()); |
| 752 | } |
| 753 | let bytes = d.take(length)?; |
| 754 | let mut data = Vec::new(); |
| 755 | data.try_reserve_exact(length).map_err(|_| invalid())?; |
| 756 | data.extend_from_slice(bytes); |
| 757 | Self::PreviewChunk { |
| 758 | entry_id, |
| 759 | offset, |
| 760 | data, |
| 761 | } |
| 762 | } |
| 763 | 113 => { |
| 764 | let entry_id = Wire::dec(&mut d)?; |
| 765 | let total_bytes = u64::dec(&mut d)?; |
| 766 | if total_bytes > MAX_PREVIEW_RGBA_BYTES { |
| 767 | return Err(invalid()); |
| 768 | } |
| 769 | Self::PreviewEnd { |
| 770 | entry_id, |
| 771 | total_bytes, |
| 772 | sha256: Wire::dec(&mut d)?, |
| 773 | } |
| 774 | } |
| 775 | 114 => { |
| 776 | let reason = Wire::dec(&mut d)?; |
| 777 | let volume = NativePath::dec(&mut d)?; |
| 778 | if !volume.is_basename() { |
| 779 | return Err(invalid()); |
| 780 | } |
| 781 | Self::VolumeProblem { reason, volume } |
| 782 | } |
| 783 | 106 => { |
| 784 | let t = Terminal::dec(&mut d)?; |
| 785 | if t.detail.len() > 4096 { |
| 786 | return Err(invalid()); |
| 787 | } |
| 788 | Self::Terminal(t) |
| 789 | } |
| 790 | 107 => Self::CancelAck { |
| 791 | request_sequence: Wire::dec(&mut d)?, |
| 792 | status: Wire::dec(&mut d)?, |
| 793 | }, |
| 794 | 108 => Self::ConflictRequired { |
| 795 | challenge_id: Wire::dec(&mut d)?, |
| 796 | entry_id: Wire::dec(&mut d)?, |
| 797 | relative_name: Wire::dec(&mut d)?, |
| 798 | incoming_size: Wire::dec(&mut d)?, |
| 799 | existing_size: Wire::dec(&mut d)?, |
| 800 | }, |
| 801 | 109 => Self::ConflictResolved { |
| 802 | entry_id: Wire::dec(&mut d)?, |
| 803 | relative_name: Wire::dec(&mut d)?, |
| 804 | choice: Wire::dec(&mut d)?, |
| 805 | }, |
| 806 | _ => return Err(invalid()), |
| 807 | }; |
| 808 | if d.at != b.len() { |
| 809 | return Err(invalid()); |
| 810 | } |
| 811 | Ok(m) |
| 812 | } |
| 813 | } |
| 814 | pub struct Reader<R> { |
| 815 | input: R, |
| 816 | sequence: u64, |
| 817 | job: Option<u64>, |
| 818 | terminal: bool, |
| 819 | } |
| 820 | impl<R: Read> Reader<R> { |
| 821 | pub fn new(input: R) -> Self { |
| 822 | Self { |
| 823 | input, |
| 824 | sequence: 0, |
| 825 | job: None, |
| 826 | terminal: false, |
| 827 | } |
| 828 | } |
| 829 | pub fn read(&mut self) -> io::Result<Option<(u64, u64, Message)>> { |
| 830 | let mut h = [0u8; 32]; |
| 831 | loop { |
| 832 | match self.input.read(&mut h[..1]) { |
| 833 | Ok(0) => return Ok(None), |
| 834 | Ok(_) => break, |
| 835 | Err(e) if e.kind() == io::ErrorKind::Interrupted => continue, |
| 836 | Err(e) => return Err(e), |
| 837 | } |
| 838 | } |
| 839 | if self.terminal { |
| 840 | return Err(invalid()); |
| 841 | } |
| 842 | self.input.read_exact(&mut h[1..])?; |
| 843 | let len = u32::from_le_bytes(h[8..12].try_into().unwrap()) as usize; |
| 844 | let job = u64::from_le_bytes(h[16..24].try_into().unwrap()); |
| 845 | let seq = u64::from_le_bytes(h[24..32].try_into().unwrap()); |
| 846 | if &h[..4] != b"ZAW1" |
| 847 | || h[4..6] != 1u16.to_le_bytes() |
| 848 | || h[12..16] != [0; 4] |
| 849 | || len > MAX_BODY |
| 850 | || job == 0 |
| 851 | || self.job.is_some_and(|j| j != job) |
| 852 | || seq != self.sequence |
| 853 | || seq >= MAX_FRAMES |
| 854 | { |
| 855 | return Err(invalid()); |
| 856 | } |
| 857 | self.job = Some(job); |
| 858 | self.sequence += 1; |
| 859 | let mut b = zeroize::Zeroizing::new(vec![0; len]); |
| 860 | self.input.read_exact(&mut b)?; |
| 861 | let message = Message::decode(u16::from_le_bytes(h[6..8].try_into().unwrap()), &b)?; |
| 862 | self.terminal = matches!(message, Message::Terminal(_)); |
| 863 | Ok(Some((job, seq, message))) |
| 864 | } |
| 865 | } |
| 866 | pub struct Writer<W> { |
| 867 | output: W, |
| 868 | sequence: u64, |
| 869 | job: u64, |
| 870 | terminal: bool, |
| 871 | } |
| 872 | impl<W: Write> Writer<W> { |
| 873 | pub fn new(output: W, job: u64) -> Self { |
| 874 | Self { |
| 875 | output, |
| 876 | sequence: 0, |
| 877 | job, |
| 878 | terminal: false, |
| 879 | } |
| 880 | } |
| 881 | pub fn send(&mut self, m: &Message) -> io::Result<()> { |
| 882 | if self.job == 0 || self.terminal || self.sequence >= MAX_FRAMES { |
| 883 | return Err(invalid()); |
| 884 | } |
| 885 | let (kind, b) = m.encode()?; |
| 886 | let b = zeroize::Zeroizing::new(b); |
| 887 | let mut h = Vec::with_capacity(32); |
| 888 | h.extend(b"ZAW1"); |
| 889 | h.extend(1u16.to_le_bytes()); |
| 890 | h.extend(kind.to_le_bytes()); |
| 891 | h.extend((b.len() as u32).to_le_bytes()); |
| 892 | h.extend(0u32.to_le_bytes()); |
| 893 | h.extend(self.job.to_le_bytes()); |
| 894 | h.extend(self.sequence.to_le_bytes()); |
| 895 | self.output.write_all(&h)?; |
| 896 | self.output.write_all(&b)?; |
| 897 | self.output.flush()?; |
| 898 | self.sequence += 1; |
| 899 | self.terminal = matches!(m, Message::Terminal(_)); |
| 900 | Ok(()) |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | #[cfg(test)] |
| 905 | #[path = "../../../tests/rust-archive-worker/selection_wire.rs"] |
| 906 | mod tests; |