| 1 | use std::{ |
| 2 | fs, |
| 3 | io::Write, |
| 4 | path::PathBuf, |
| 5 | process::{Command, Stdio}, |
| 6 | sync::atomic::{AtomicU64, Ordering}, |
| 7 | }; |
| 8 | use zipelo_archive_worker::{ |
| 9 | archive, |
| 10 | protocol::*, |
| 11 | wire::{Reader, Writer}, |
| 12 | }; |
| 13 | static NEXT: AtomicU64 = AtomicU64::new(0); |
| 14 | struct Scratch(PathBuf); |
| 15 | impl Scratch { |
| 16 | fn new() -> Self { |
| 17 | let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 18 | .parent() |
| 19 | .unwrap() |
| 20 | .parent() |
| 21 | .unwrap() |
| 22 | .join("tests/rust-archive-worker/runs") |
| 23 | .join(format!( |
| 24 | "{}-{}", |
| 25 | std::process::id(), |
| 26 | NEXT.fetch_add(1, Ordering::Relaxed) |
| 27 | )); |
| 28 | fs::create_dir_all(path.parent().unwrap()).unwrap(); |
| 29 | let builder = fs::DirBuilder::new(); |
| 30 | #[cfg(unix)] |
| 31 | let mut builder = builder; |
| 32 | #[cfg(unix)] |
| 33 | { |
| 34 | use std::os::unix::fs::DirBuilderExt; |
| 35 | builder.mode(0o700); |
| 36 | } |
| 37 | builder.create(&path).unwrap(); |
| 38 | Self(path) |
| 39 | } |
| 40 | fn path(&self, name: &str) -> NativePath { |
| 41 | NativePath::from_path(&self.0.join(name)) |
| 42 | } |
| 43 | } |
| 44 | impl Drop for Scratch { |
| 45 | fn drop(&mut self) { |
| 46 | fs::remove_dir_all(&self.0).unwrap(); |
| 47 | } |
| 48 | } |
| 49 | fn execute(start: Start) -> Vec<Message> { |
| 50 | let mut data = Vec::new(); |
| 51 | Writer::new(&mut data, 17) |
| 52 | .send(&Message::Start(start)) |
| 53 | .unwrap(); |
| 54 | let mut command = Command::new(env!("CARGO_BIN_EXE_zipelo-archive-worker")); |
| 55 | command |
| 56 | .stdin(Stdio::piped()) |
| 57 | .stdout(Stdio::piped()) |
| 58 | .stderr(Stdio::piped()); |
| 59 | #[cfg(target_os = "linux")] |
| 60 | { |
| 61 | use std::os::unix::process::CommandExt; |
| 62 | #[repr(C)] |
| 63 | struct Limit { |
| 64 | soft: usize, |
| 65 | hard: usize, |
| 66 | } |
| 67 | unsafe extern "C" { |
| 68 | fn setrlimit(resource: i32, limit: *const Limit) -> i32; |
| 69 | } |
| 70 | // 자식 프로세스의 주소 공간만 512 MiB로 제한함. |
| 71 | unsafe { |
| 72 | command.pre_exec(|| { |
| 73 | let limit = Limit { |
| 74 | soft: 512 * 1024 * 1024, |
| 75 | hard: 512 * 1024 * 1024, |
| 76 | }; |
| 77 | if setrlimit(9, &limit) == 0 { |
| 78 | Ok(()) |
| 79 | } else { |
| 80 | Err(std::io::Error::last_os_error()) |
| 81 | } |
| 82 | }); |
| 83 | } |
| 84 | } |
| 85 | let mut child = command.spawn().unwrap(); |
| 86 | child.stdin.take().unwrap().write_all(&data).unwrap(); |
| 87 | let output = child.wait_with_output().unwrap(); |
| 88 | assert!(output.status.success(), "{:?}", output.stderr); |
| 89 | assert!(output.stderr.is_empty()); |
| 90 | let mut reader = Reader::new(output.stdout.as_slice()); |
| 91 | let mut events = Vec::new(); |
| 92 | while let Some((job, _, event)) = reader.read().unwrap() { |
| 93 | assert_eq!(job, 17); |
| 94 | events.push(event); |
| 95 | } |
| 96 | assert!(matches!(events.last(), Some(Message::Terminal(_)))); |
| 97 | events |
| 98 | } |
| 99 | fn terminal(events: &[Message]) -> &Terminal { |
| 100 | match events.last().unwrap() { |
| 101 | Message::Terminal(t) => t, |
| 102 | _ => panic!("missing terminal"), |
| 103 | } |
| 104 | } |
| 105 | fn extract( |
| 106 | s: &Scratch, |
| 107 | bytes: &[u8], |
| 108 | gzip: Option<&str>, |
| 109 | selection: Selection, |
| 110 | list_id: Option<ListId>, |
| 111 | limits: Limits, |
| 112 | ) -> Vec<Message> { |
| 113 | fs::write(s.0.join("input"), bytes).unwrap(); |
| 114 | if !s.0.join("output").exists() { |
| 115 | let d = fs::DirBuilder::new(); |
| 116 | #[cfg(unix)] |
| 117 | let mut d = d; |
| 118 | #[cfg(unix)] |
| 119 | { |
| 120 | use std::os::unix::fs::DirBuilderExt; |
| 121 | d.mode(0o700); |
| 122 | } |
| 123 | d.create(s.0.join("output")).unwrap(); |
| 124 | } |
| 125 | execute(Start { |
| 126 | action: Action::Extract { |
| 127 | archive: s.path("input"), |
| 128 | destination: s.path("output"), |
| 129 | selection, |
| 130 | list_id, |
| 131 | gzip_output_name: gzip.map(str::to_owned), |
| 132 | }, |
| 133 | limits, |
| 134 | }) |
| 135 | } |
| 136 | const PAYLOAD: &[u8] = b"Zipelo worker interoperability\n"; |
| 137 | fn basic(bytes: &[u8], gzip: Option<&str>, output: &str) { |
| 138 | let s = Scratch::new(); |
| 139 | let events = extract(&s, bytes, gzip, Selection::All, None, Limits::default()); |
| 140 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 141 | assert_eq!(terminal(&events).publication, Publication::Complete); |
| 142 | assert_eq!(fs::read(s.0.join("output").join(output)).unwrap(), PAYLOAD); |
| 143 | } |
| 144 | #[test] |
| 145 | fn stored_zip() { |
| 146 | basic( |
| 147 | include_bytes!("fixtures/store.zip"), |
| 148 | None, |
| 149 | "folder/hello.txt", |
| 150 | ); |
| 151 | } |
| 152 | #[test] |
| 153 | fn deflated_zip() { |
| 154 | basic( |
| 155 | include_bytes!("fixtures/deflate.zip"), |
| 156 | None, |
| 157 | "folder/hello.txt", |
| 158 | ); |
| 159 | } |
| 160 | #[test] |
| 161 | fn tar() { |
| 162 | basic( |
| 163 | include_bytes!("fixtures/sample.tar"), |
| 164 | None, |
| 165 | "folder/hello.txt", |
| 166 | ); |
| 167 | } |
| 168 | #[test] |
| 169 | fn tar_gzip() { |
| 170 | basic( |
| 171 | include_bytes!("fixtures/sample.tar.gz"), |
| 172 | None, |
| 173 | "folder/hello.txt", |
| 174 | ); |
| 175 | } |
| 176 | #[test] |
| 177 | fn gzip_explicit_name_ignores_fname() { |
| 178 | basic( |
| 179 | include_bytes!("fixtures/sample.gz"), |
| 180 | Some("chosen.txt"), |
| 181 | "chosen.txt", |
| 182 | ); |
| 183 | } |
| 184 | #[test] |
| 185 | fn gzip_concatenated_members() { |
| 186 | let s = Scratch::new(); |
| 187 | let events = extract( |
| 188 | &s, |
| 189 | include_bytes!("fixtures/concat.gz"), |
| 190 | Some("combined"), |
| 191 | Selection::All, |
| 192 | None, |
| 193 | Limits::default(), |
| 194 | ); |
| 195 | assert_eq!(terminal(&events).outcome, Outcome::Completed); |
| 196 | assert_eq!(fs::read(s.0.join("output/combined")).unwrap(), b"onetwo"); |
| 197 | } |
| 198 | #[test] |
| 199 | fn gzip_requires_leaf() { |
| 200 | for name in [None, Some("../escape"), Some("a/b")] { |
| 201 | let s = Scratch::new(); |
| 202 | let e = extract( |
| 203 | &s, |
| 204 | include_bytes!("fixtures/sample.gz"), |
| 205 | name, |
| 206 | Selection::All, |
| 207 | None, |
| 208 | Limits::default(), |
| 209 | ); |
| 210 | assert_eq!(terminal(&e).error, ErrorCode::InvalidRequest); |
| 211 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 212 | } |
| 213 | } |
| 214 | #[test] |
| 215 | fn archive_rejects_gzip_name() { |
| 216 | let s = Scratch::new(); |
| 217 | let e = extract( |
| 218 | &s, |
| 219 | include_bytes!("fixtures/store.zip"), |
| 220 | Some("a"), |
| 221 | Selection::All, |
| 222 | None, |
| 223 | Limits::default(), |
| 224 | ); |
| 225 | assert_eq!(terminal(&e).error, ErrorCode::InvalidRequest); |
| 226 | } |
| 227 | #[test] |
| 228 | fn no_clobber_and_preflight_no_partial_outputs() { |
| 229 | let s = Scratch::new(); |
| 230 | fs::create_dir(s.0.join("output")).unwrap(); |
| 231 | #[cfg(unix)] |
| 232 | { |
| 233 | use std::os::unix::fs::PermissionsExt; |
| 234 | fs::set_permissions(s.0.join("output"), fs::Permissions::from_mode(0o700)).unwrap(); |
| 235 | } |
| 236 | fs::create_dir(s.0.join("output/folder")).unwrap(); |
| 237 | #[cfg(unix)] |
| 238 | { |
| 239 | use std::os::unix::fs::PermissionsExt; |
| 240 | fs::set_permissions(s.0.join("output/folder"), fs::Permissions::from_mode(0o700)).unwrap(); |
| 241 | } |
| 242 | fs::write(s.0.join("output/folder/hello.txt"), b"preserve").unwrap(); |
| 243 | let e = extract( |
| 244 | &s, |
| 245 | include_bytes!("fixtures/store.zip"), |
| 246 | None, |
| 247 | Selection::All, |
| 248 | None, |
| 249 | Limits::default(), |
| 250 | ); |
| 251 | assert_eq!(terminal(&e).error, ErrorCode::Conflict); |
| 252 | assert_eq!( |
| 253 | fs::read(s.0.join("output/folder/hello.txt")).unwrap(), |
| 254 | b"preserve" |
| 255 | ); |
| 256 | assert!(!s.0.join("output/empty").exists()); |
| 257 | } |
| 258 | #[test] |
| 259 | fn unsafe_archives_reject_without_writes() { |
| 260 | for bytes in [ |
| 261 | include_bytes!("fixtures/traversal.zip").as_slice(), |
| 262 | include_bytes!("fixtures/absolute.zip"), |
| 263 | include_bytes!("fixtures/drive.zip"), |
| 264 | include_bytes!("fixtures/reserved.zip"), |
| 265 | include_bytes!("fixtures/link.zip"), |
| 266 | include_bytes!("fixtures/collision.zip"), |
| 267 | include_bytes!("fixtures/parent-collision.zip"), |
| 268 | ] { |
| 269 | let s = Scratch::new(); |
| 270 | let e = extract(&s, bytes, None, Selection::All, None, Limits::default()); |
| 271 | assert_eq!(terminal(&e).outcome, Outcome::Failed, "{e:?}"); |
| 272 | assert_eq!(terminal(&e).created_count, 0); |
| 273 | assert_eq!(terminal(&e).publication, Publication::None); |
| 274 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 275 | } |
| 276 | } |
| 277 | #[test] |
| 278 | fn bomb_rejects_before_writes() { |
| 279 | let s = Scratch::new(); |
| 280 | let e = extract( |
| 281 | &s, |
| 282 | include_bytes!("fixtures/bomb.zip"), |
| 283 | None, |
| 284 | Selection::All, |
| 285 | None, |
| 286 | Limits { |
| 287 | output: 1024, |
| 288 | ..Limits::default() |
| 289 | }, |
| 290 | ); |
| 291 | assert_eq!(terminal(&e).error, ErrorCode::Limit); |
| 292 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 293 | } |
| 294 | #[test] |
| 295 | fn corrupt_crc_and_truncation_reject_without_writes() { |
| 296 | let mut zip = include_bytes!("fixtures/store.zip").to_vec(); |
| 297 | let at = zip |
| 298 | .windows(PAYLOAD.len()) |
| 299 | .position(|b| b == PAYLOAD) |
| 300 | .unwrap(); |
| 301 | zip[at] ^= 1; |
| 302 | for bytes in [zip.as_slice(), &include_bytes!("fixtures/store.zip")[..40]] { |
| 303 | let s = Scratch::new(); |
| 304 | let e = extract(&s, bytes, None, Selection::All, None, Limits::default()); |
| 305 | assert_eq!(terminal(&e).outcome, Outcome::Failed); |
| 306 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 307 | } |
| 308 | } |
| 309 | fn listing(s: &Scratch) -> ListId { |
| 310 | let e = execute(Start { |
| 311 | action: Action::List { |
| 312 | archive: s.path("input"), |
| 313 | }, |
| 314 | limits: Limits::default(), |
| 315 | }); |
| 316 | assert_eq!(terminal(&e).outcome, Outcome::Completed); |
| 317 | e.into_iter() |
| 318 | .find_map(|m| { |
| 319 | if let Message::ListEnd { list_id, .. } = m { |
| 320 | Some(list_id) |
| 321 | } else { |
| 322 | None |
| 323 | } |
| 324 | }) |
| 325 | .unwrap() |
| 326 | } |
| 327 | #[test] |
| 328 | fn list_and_selected_extract() { |
| 329 | let s = Scratch::new(); |
| 330 | let bytes = include_bytes!("fixtures/store.zip"); |
| 331 | fs::write(s.0.join("input"), bytes).unwrap(); |
| 332 | let id = listing(&s); |
| 333 | let e = extract( |
| 334 | &s, |
| 335 | bytes, |
| 336 | None, |
| 337 | Selection::EntryIds(vec![0]), |
| 338 | Some(id), |
| 339 | Limits::default(), |
| 340 | ); |
| 341 | assert_eq!(terminal(&e).outcome, Outcome::Completed); |
| 342 | assert!(!s.0.join("output/empty").exists()); |
| 343 | assert_eq!( |
| 344 | fs::read(s.0.join("output/folder/hello.txt")).unwrap(), |
| 345 | PAYLOAD |
| 346 | ); |
| 347 | } |
| 348 | #[test] |
| 349 | fn stale_list_content_rejected() { |
| 350 | let s = Scratch::new(); |
| 351 | fs::write(s.0.join("input"), include_bytes!("fixtures/store.zip")).unwrap(); |
| 352 | let mut id = listing(&s); |
| 353 | id.archive_sha256[0] ^= 1; |
| 354 | let e = extract( |
| 355 | &s, |
| 356 | include_bytes!("fixtures/store.zip"), |
| 357 | None, |
| 358 | Selection::EntryIds(vec![0]), |
| 359 | Some(id), |
| 360 | Limits::default(), |
| 361 | ); |
| 362 | assert_eq!(terminal(&e).error, ErrorCode::SourceChanged); |
| 363 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 364 | } |
| 365 | #[test] |
| 366 | fn missing_duplicate_and_out_of_range_selection() { |
| 367 | for ids in [vec![], vec![0, 0], vec![999]] { |
| 368 | let s = Scratch::new(); |
| 369 | fs::write(s.0.join("input"), include_bytes!("fixtures/store.zip")).unwrap(); |
| 370 | let id = listing(&s); |
| 371 | let e = extract( |
| 372 | &s, |
| 373 | include_bytes!("fixtures/store.zip"), |
| 374 | None, |
| 375 | Selection::EntryIds(ids), |
| 376 | Some(id), |
| 377 | Limits::default(), |
| 378 | ); |
| 379 | assert_eq!(terminal(&e).error, ErrorCode::InvalidRequest); |
| 380 | } |
| 381 | let s = Scratch::new(); |
| 382 | let e = extract( |
| 383 | &s, |
| 384 | include_bytes!("fixtures/store.zip"), |
| 385 | None, |
| 386 | Selection::EntryIds(vec![0]), |
| 387 | None, |
| 388 | Limits::default(), |
| 389 | ); |
| 390 | assert_eq!(terminal(&e).error, ErrorCode::InvalidRequest); |
| 391 | } |
| 392 | #[test] |
| 393 | fn create_store_and_deflate_zip() { |
| 394 | for method in [Method::Store, Method::Deflate] { |
| 395 | let s = Scratch::new(); |
| 396 | fs::write(s.0.join("source"), PAYLOAD).unwrap(); |
| 397 | let e = execute(Start { |
| 398 | action: Action::CreateZip { |
| 399 | sources: vec![Source { |
| 400 | path: s.path("source"), |
| 401 | archive_name: "dir/hello.txt".into(), |
| 402 | }], |
| 403 | output_file: s.path("result.zip"), |
| 404 | method, |
| 405 | encryption: Encryption::None, |
| 406 | }, |
| 407 | limits: Limits::default(), |
| 408 | }); |
| 409 | assert_eq!(terminal(&e).outcome, Outcome::Completed, "{e:?}"); |
| 410 | let bytes = fs::read(s.0.join("result.zip")).unwrap(); |
| 411 | let parsed = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())).unwrap(); |
| 412 | assert_eq!(parsed.members[0].data, PAYLOAD); |
| 413 | } |
| 414 | } |
| 415 | #[test] |
| 416 | fn encrypted_creation_is_explicitly_unsupported() { |
| 417 | let s = Scratch::new(); |
| 418 | let e = execute(Start { |
| 419 | action: Action::CreateZip { |
| 420 | sources: vec![], |
| 421 | output_file: s.path("result.zip"), |
| 422 | method: Method::Store, |
| 423 | encryption: Encryption::Aes256, |
| 424 | }, |
| 425 | limits: Limits::default(), |
| 426 | }); |
| 427 | assert_eq!(terminal(&e).error, ErrorCode::UnsupportedFeature); |
| 428 | assert!(!s.0.join("result.zip").exists()); |
| 429 | } |
| 430 | #[test] |
| 431 | fn name_policy() { |
| 432 | for name in [ |
| 433 | "../a", "/a", "a\\b", "C:a", "a/../b", "a.", "a ", "COM1.txt", "a//b", "a/./b", |
| 434 | ] { |
| 435 | assert!(archive::valid_name(name, false).is_err(), "{name}"); |
| 436 | } |
| 437 | assert!(archive::valid_name("folder/한글.txt", false).is_ok()); |
| 438 | } |
| 439 | #[test] |
| 440 | fn frame_round_trip_and_secret_redaction() { |
| 441 | let mut b = Vec::new(); |
| 442 | Writer::new(&mut b, 42).send(&Message::Cancel).unwrap(); |
| 443 | let mut reader = Reader::new(b.as_slice()); |
| 444 | assert!(matches!( |
| 445 | reader.read().unwrap(), |
| 446 | Some((42, 0, Message::Cancel)) |
| 447 | )); |
| 448 | assert!(reader.read().unwrap().is_none()); |
| 449 | assert!( |
| 450 | !format!( |
| 451 | "{:?}", |
| 452 | Message::Password { |
| 453 | challenge_id: 1, |
| 454 | secret: Secret(b"private-password".to_vec()) |
| 455 | } |
| 456 | ) |
| 457 | .contains("private-password") |
| 458 | ); |
| 459 | } |
| 460 | #[test] |
| 461 | fn invalid_headers_are_rejected() { |
| 462 | let mut b = Vec::new(); |
| 463 | Writer::new(&mut b, 42).send(&Message::Cancel).unwrap(); |
| 464 | for (offset, value) in [(0, 0), (4, 2), (12, 1), (16, 0), (24, 1)] { |
| 465 | let mut bad = b.clone(); |
| 466 | bad[offset] = value; |
| 467 | assert!(Reader::new(bad.as_slice()).read().is_err()); |
| 468 | } |
| 469 | let mut big = b.clone(); |
| 470 | big[8..12].copy_from_slice(&1_048_577u32.to_le_bytes()); |
| 471 | assert!(Reader::new(big.as_slice()).read().is_err()); |
| 472 | assert!(Reader::new(&b[..16]).read().is_err()); |
| 473 | } |
| 474 | #[test] |
| 475 | fn terminal_is_final_frame() { |
| 476 | let t = Terminal { |
| 477 | outcome: Outcome::Failed, |
| 478 | error: ErrorCode::Io, |
| 479 | publication: Publication::Uncertain, |
| 480 | created_count: 0, |
| 481 | retained_partial_count: 0, |
| 482 | durability: Durability::Unconfirmed, |
| 483 | cleanup: Cleanup::Unconfirmed, |
| 484 | detail: "uncertain".into(), |
| 485 | }; |
| 486 | let mut b = Vec::new(); |
| 487 | let mut w = Writer::new(&mut b, 1); |
| 488 | w.send(&Message::Terminal(t)).unwrap(); |
| 489 | assert!(w.send(&Message::Cancel).is_err()); |
| 490 | } |
| 491 | #[cfg(unix)] |
| 492 | #[test] |
| 493 | fn symlink_parent_escape_is_rejected() { |
| 494 | use std::os::unix::fs::symlink; |
| 495 | let s = Scratch::new(); |
| 496 | let d = fs::DirBuilder::new(); |
| 497 | #[cfg(unix)] |
| 498 | let mut d = d; |
| 499 | use std::os::unix::fs::DirBuilderExt; |
| 500 | d.mode(0o700); |
| 501 | d.create(s.0.join("output")).unwrap(); |
| 502 | d.create(s.0.join("outside")).unwrap(); |
| 503 | symlink(s.0.join("outside"), s.0.join("output/folder")).unwrap(); |
| 504 | let e = extract( |
| 505 | &s, |
| 506 | include_bytes!("fixtures/store.zip"), |
| 507 | None, |
| 508 | Selection::All, |
| 509 | None, |
| 510 | Limits::default(), |
| 511 | ); |
| 512 | assert_eq!(terminal(&e).error, ErrorCode::UnsafePath); |
| 513 | assert_eq!(fs::read_dir(s.0.join("outside")).unwrap().count(), 0); |
| 514 | } |
| 515 | |
| 516 | #[test] |
| 517 | fn changed_source_is_reopen_error_before_decode() { |
| 518 | let s = Scratch::new(); |
| 519 | let bytes = include_bytes!("fixtures/store.zip"); |
| 520 | fs::write(s.0.join("input"), bytes).unwrap(); |
| 521 | let id = listing(&s); |
| 522 | let mut changed = bytes.to_vec(); |
| 523 | changed[40] ^= 1; |
| 524 | let e = extract( |
| 525 | &s, |
| 526 | &changed, |
| 527 | None, |
| 528 | Selection::EntryIds(vec![0]), |
| 529 | Some(id), |
| 530 | Limits::default(), |
| 531 | ); |
| 532 | assert_eq!(terminal(&e).error, ErrorCode::SourceChanged); |
| 533 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 534 | } |
| 535 | #[test] |
| 536 | fn cancel_ack_and_terminal_are_distinct() { |
| 537 | let s = Scratch::new(); |
| 538 | fs::write(s.0.join("source"), vec![0u8; 16 * 1024 * 1024]).unwrap(); |
| 539 | let start = Start { |
| 540 | action: Action::CreateZip { |
| 541 | sources: vec![Source { |
| 542 | path: s.path("source"), |
| 543 | archive_name: "data".into(), |
| 544 | }], |
| 545 | output_file: s.path("result.zip"), |
| 546 | method: Method::Deflate, |
| 547 | encryption: Encryption::None, |
| 548 | }, |
| 549 | limits: Limits::default(), |
| 550 | }; |
| 551 | let mut child = Command::new(env!("CARGO_BIN_EXE_zipelo-archive-worker")) |
| 552 | .stdin(Stdio::piped()) |
| 553 | .stdout(Stdio::piped()) |
| 554 | .stderr(Stdio::piped()) |
| 555 | .spawn() |
| 556 | .unwrap(); |
| 557 | let mut writer = Writer::new(child.stdin.take().unwrap(), 19); |
| 558 | let mut reader = Reader::new(child.stdout.take().unwrap()); |
| 559 | writer.send(&Message::Start(start)).unwrap(); |
| 560 | assert!(matches!( |
| 561 | reader.read().unwrap(), |
| 562 | Some((19, 0, Message::Accepted { .. })) |
| 563 | )); |
| 564 | writer.send(&Message::Cancel).unwrap(); |
| 565 | writer.send(&Message::Cancel).unwrap(); |
| 566 | let mut events = Vec::new(); |
| 567 | while let Some((_, _, m)) = reader.read().unwrap() { |
| 568 | events.push(m); |
| 569 | } |
| 570 | assert!(child.wait().unwrap().success()); |
| 571 | let terminal = terminal(&events); |
| 572 | assert!(matches!( |
| 573 | terminal.outcome, |
| 574 | Outcome::Cancelled | Outcome::Completed |
| 575 | )); |
| 576 | for m in &events { |
| 577 | if let Message::CancelAck { |
| 578 | request_sequence, |
| 579 | status, |
| 580 | } = m |
| 581 | { |
| 582 | assert!([1, 2].contains(request_sequence)); |
| 583 | assert!(matches!( |
| 584 | status, |
| 585 | CancelStatus::Requested |
| 586 | | CancelStatus::AlreadyRequested |
| 587 | | CancelStatus::TooLate |
| 588 | | CancelStatus::Finished |
| 589 | )); |
| 590 | } |
| 591 | } |
| 592 | assert!( |
| 593 | events |
| 594 | .iter() |
| 595 | .any(|m| matches!(m, Message::CancelAck { .. })), |
| 596 | "cancel must be consumed during bounded source read: {events:?}" |
| 597 | ); |
| 598 | } |
| 599 | |
| 600 | #[test] |
| 601 | fn encrypted_zip_rejects_without_outputs() { |
| 602 | let mut bytes = include_bytes!("fixtures/store.zip").to_vec(); |
| 603 | bytes[6] |= 1; |
| 604 | let central = bytes.windows(4).position(|b| b == b"PK\x01\x02").unwrap(); |
| 605 | bytes[central + 8] |= 1; |
| 606 | let s = Scratch::new(); |
| 607 | let e = extract(&s, &bytes, None, Selection::All, None, Limits::default()); |
| 608 | assert_eq!(terminal(&e).error, ErrorCode::UnsupportedFeature); |
| 609 | assert_eq!(terminal(&e).publication, Publication::None); |
| 610 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 611 | } |
| 612 | #[test] |
| 613 | fn uncertain_report_survives_codec() { |
| 614 | let t = Terminal { |
| 615 | outcome: Outcome::Failed, |
| 616 | error: ErrorCode::Io, |
| 617 | publication: Publication::Uncertain, |
| 618 | created_count: 2, |
| 619 | retained_partial_count: 1, |
| 620 | durability: Durability::Failed, |
| 621 | cleanup: Cleanup::Failed, |
| 622 | detail: "publication uncertain".into(), |
| 623 | }; |
| 624 | let mut bytes = Vec::new(); |
| 625 | Writer::new(&mut bytes, 88) |
| 626 | .send(&Message::Terminal(t.clone())) |
| 627 | .unwrap(); |
| 628 | let mut reader = Reader::new(bytes.as_slice()); |
| 629 | match reader.read().unwrap() { |
| 630 | Some((88, 0, Message::Terminal(actual))) => assert_eq!(actual, t), |
| 631 | other => panic!("{other:?}"), |
| 632 | }; |
| 633 | assert!(reader.read().unwrap().is_none()); |
| 634 | } |
| 635 | |
| 636 | fn path_fixture_zip(names: &[String]) -> Vec<u8> { |
| 637 | let mut output = Vec::new(); |
| 638 | let mut central = Vec::new(); |
| 639 | for name in names { |
| 640 | let offset = output.len() as u32; |
| 641 | output.extend(b"PK\x03\x04"); |
| 642 | for value in [20u16, 0x0800, 0, 0, 33] { |
| 643 | output.extend(value.to_le_bytes()); |
| 644 | } |
| 645 | for value in [0u32, 0, 0] { |
| 646 | output.extend(value.to_le_bytes()); |
| 647 | } |
| 648 | output.extend((name.len() as u16).to_le_bytes()); |
| 649 | output.extend(0u16.to_le_bytes()); |
| 650 | output.extend(name.as_bytes()); |
| 651 | central.extend(b"PK\x01\x02"); |
| 652 | for value in [0x0314u16, 20, 0x0800, 0, 0, 33] { |
| 653 | central.extend(value.to_le_bytes()); |
| 654 | } |
| 655 | for value in [0u32, 0, 0] { |
| 656 | central.extend(value.to_le_bytes()); |
| 657 | } |
| 658 | central.extend((name.len() as u16).to_le_bytes()); |
| 659 | for value in [0u16, 0, 0, 0] { |
| 660 | central.extend(value.to_le_bytes()); |
| 661 | } |
| 662 | central.extend((0o100600u32 << 16).to_le_bytes()); |
| 663 | central.extend(offset.to_le_bytes()); |
| 664 | central.extend(name.as_bytes()); |
| 665 | } |
| 666 | let offset = output.len() as u32; |
| 667 | output.extend(¢ral); |
| 668 | output.extend(b"PK\x05\x06"); |
| 669 | for value in [0u16, 0, names.len() as u16, names.len() as u16] { |
| 670 | output.extend(value.to_le_bytes()); |
| 671 | } |
| 672 | output.extend((central.len() as u32).to_le_bytes()); |
| 673 | output.extend(offset.to_le_bytes()); |
| 674 | output.extend(0u16.to_le_bytes()); |
| 675 | output |
| 676 | } |
| 677 | |
| 678 | fn assert_metadata_limit(names: &[String]) { |
| 679 | let scratch = Scratch::new(); |
| 680 | let events = extract( |
| 681 | &scratch, |
| 682 | &path_fixture_zip(names), |
| 683 | None, |
| 684 | Selection::All, |
| 685 | None, |
| 686 | Limits::default(), |
| 687 | ); |
| 688 | let result = terminal(&events); |
| 689 | assert_eq!(result.outcome, Outcome::Failed, "{events:?}"); |
| 690 | assert_eq!(result.error, ErrorCode::Limit, "{events:?}"); |
| 691 | assert_eq!(result.publication, Publication::None); |
| 692 | assert_eq!(result.created_count, 0); |
| 693 | assert_eq!(fs::read_dir(scratch.0.join("output")).unwrap().count(), 0); |
| 694 | } |
| 695 | |
| 696 | #[test] |
| 697 | fn deep_zip_returns_limit_without_abort_or_writes() { |
| 698 | let names = (0..100) |
| 699 | .map(|i| format!("{}file{i:03}", "a/".repeat(2000))) |
| 700 | .collect::<Vec<_>>(); |
| 701 | assert_metadata_limit(&names); |
| 702 | } |
| 703 | |
| 704 | #[test] |
| 705 | fn shallow_zip_parent_metadata_is_bounded() { |
| 706 | let names = (0..500) |
| 707 | .map(|i| { |
| 708 | format!( |
| 709 | "d{i:04}{}/{}file", |
| 710 | "a".repeat(180), |
| 711 | format!("{}/", "b".repeat(200)).repeat(7) |
| 712 | ) |
| 713 | }) |
| 714 | .collect::<Vec<_>>(); |
| 715 | assert!( |
| 716 | names |
| 717 | .iter() |
| 718 | .all(|n| n.split('/').count() <= archive::MAX_PATH_DEPTH) |
| 719 | ); |
| 720 | assert!( |
| 721 | archive::parse( |
| 722 | &path_fixture_zip(&names), |
| 723 | Limits::default(), |
| 724 | false, |
| 725 | &mut || Ok(()) |
| 726 | ) |
| 727 | .is_ok() |
| 728 | ); |
| 729 | assert_metadata_limit(&names); |
| 730 | } |
| 731 | |
| 732 | #[test] |
| 733 | fn aggregate_archive_name_metadata_is_bounded() { |
| 734 | let names = (0..1100) |
| 735 | .map(|i| { |
| 736 | format!( |
| 737 | "d{i:04}{}/{}file", |
| 738 | "a".repeat(180), |
| 739 | format!("{}/", "b".repeat(200)).repeat(9) |
| 740 | ) |
| 741 | }) |
| 742 | .collect::<Vec<_>>(); |
| 743 | let bytes = path_fixture_zip(&names); |
| 744 | assert!((bytes.len() as u64) < Limits::default().input); |
| 745 | assert!(matches!( |
| 746 | archive::parse(&bytes, Limits::default(), false, &mut || Ok(())), |
| 747 | Err(ErrorCode::Limit) |
| 748 | )); |
| 749 | assert_metadata_limit(&names); |
| 750 | } |
| 751 | |
| 752 | #[test] |
| 753 | fn path_depth_boundary_is_explicit() { |
| 754 | let accepted = format!("{}file", "a/".repeat(archive::MAX_PATH_DEPTH - 1)); |
| 755 | let rejected = format!("{}file", "a/".repeat(archive::MAX_PATH_DEPTH)); |
| 756 | assert!(archive::valid_name(&accepted, false).is_ok()); |
| 757 | assert_eq!(archive::valid_name(&rejected, false), Err(ErrorCode::Limit)); |
| 758 | } |
| 759 | |
| 760 | fn zeros_gzip(length: usize) -> Vec<u8> { |
| 761 | let mut gzip = vec![31, 139, 8, 0, 0, 0, 0, 0, 0, 255]; |
| 762 | let mut left = length; |
| 763 | loop { |
| 764 | let size = left.min(u16::MAX as usize) as u16; |
| 765 | left -= size as usize; |
| 766 | gzip.push(u8::from(left == 0)); |
| 767 | gzip.extend(size.to_le_bytes()); |
| 768 | gzip.extend((!size).to_le_bytes()); |
| 769 | gzip.resize(gzip.len() + size as usize, 0); |
| 770 | if left == 0 { |
| 771 | break; |
| 772 | } |
| 773 | } |
| 774 | gzip.extend(zlib_rs::crc32::crc32(0, &vec![0; length]).to_le_bytes()); |
| 775 | gzip.extend((length as u32).to_le_bytes()); |
| 776 | gzip |
| 777 | } |
| 778 | |
| 779 | #[test] |
| 780 | fn zero_payload_gzip_is_not_tar() { |
| 781 | for length in [512, 1024, 1536] { |
| 782 | let scratch = Scratch::new(); |
| 783 | let bytes = zeros_gzip(length); |
| 784 | let parsed = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())).unwrap(); |
| 785 | assert_eq!(parsed.format, Format::Gzip); |
| 786 | let events = extract( |
| 787 | &scratch, |
| 788 | &bytes, |
| 789 | Some("zeros.bin"), |
| 790 | Selection::All, |
| 791 | None, |
| 792 | Limits::default(), |
| 793 | ); |
| 794 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 795 | assert_eq!( |
| 796 | fs::read(scratch.0.join("output/zeros.bin")).unwrap(), |
| 797 | vec![0; length] |
| 798 | ); |
| 799 | fs::write(scratch.0.join("input"), &bytes).unwrap(); |
| 800 | let list_id = listing(&scratch); |
| 801 | assert_eq!(list_id.format, Format::Gzip); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | const SEVEN_PAYLOAD: &[u8] = b"Zipelo 7z interoperability\n"; |
| 806 | fn seven_fixture(name: &str) -> Vec<u8> { |
| 807 | fs::read( |
| 808 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 809 | .join("../../tests/rust-archive-worker/fixtures/7z") |
| 810 | .join(name), |
| 811 | ) |
| 812 | .unwrap() |
| 813 | } |
| 814 | #[test] |
| 815 | fn seven_zip_lzma_lzma2_and_copy_interoperate() { |
| 816 | for name in [ |
| 817 | "copy.7z", |
| 818 | "lzma.7z", |
| 819 | "lzma2.7z", |
| 820 | "properties-0.7z", |
| 821 | "properties-1.7z", |
| 822 | "properties-2.7z", |
| 823 | "properties-3.7z", |
| 824 | "libarchive-lzma1.7z", |
| 825 | "libarchive-lzma2.7z", |
| 826 | ] { |
| 827 | let bytes = seven_fixture(name); |
| 828 | let catalog = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())) |
| 829 | .unwrap_or_else(|e| panic!("{name}: {e:?}")); |
| 830 | assert_eq!(catalog.format, Format::SevenZip); |
| 831 | assert_eq!(catalog.members[0].data, SEVEN_PAYLOAD.repeat(100), "{name}"); |
| 832 | let s = Scratch::new(); |
| 833 | let events = extract(&s, &bytes, None, Selection::All, None, Limits::default()); |
| 834 | assert_eq!( |
| 835 | terminal(&events).outcome, |
| 836 | Outcome::Completed, |
| 837 | "{name}: {events:?}" |
| 838 | ); |
| 839 | assert_eq!( |
| 840 | fs::read(s.0.join("output/folder/hello.txt")).unwrap(), |
| 841 | SEVEN_PAYLOAD.repeat(100) |
| 842 | ); |
| 843 | } |
| 844 | } |
| 845 | #[test] |
| 846 | fn seven_zip_encoded_header_solid_and_empty_entries() { |
| 847 | let s = Scratch::new(); |
| 848 | let bytes = seven_fixture("encoded-solid.7z"); |
| 849 | let catalog = archive::parse(&bytes, Limits::default(), false, &mut || Ok(())).unwrap(); |
| 850 | assert_eq!(catalog.members.len(), 4); |
| 851 | let events = extract(&s, &bytes, None, Selection::All, None, Limits::default()); |
| 852 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 853 | assert_eq!(fs::read(s.0.join("output/folder/empty")).unwrap(), b""); |
| 854 | assert_eq!( |
| 855 | fs::read(s.0.join("output/folder/hello.txt")).unwrap(), |
| 856 | SEVEN_PAYLOAD.repeat(100) |
| 857 | ); |
| 858 | let mut reverse = SEVEN_PAYLOAD.repeat(100); |
| 859 | reverse.reverse(); |
| 860 | assert_eq!(fs::read(s.0.join("output/한글.txt")).unwrap(), reverse); |
| 861 | } |
| 862 | #[test] |
| 863 | fn seven_zip_long_matches_and_lzma2_chunks() { |
| 864 | for name in ["long-match.7z", "lzma2-chunks.7z"] { |
| 865 | let bytes = seven_fixture(name); |
| 866 | let catalog = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())) |
| 867 | .unwrap_or_else(|e| panic!("{name}: {e:?}")); |
| 868 | if name == "long-match.7z" { |
| 869 | assert_eq!(catalog.members[0].data, SEVEN_PAYLOAD.repeat(10000)); |
| 870 | } else { |
| 871 | let expected = (0..100000u64) |
| 872 | .map(|i| (((i * 1103515245 + (i >> 2) * 12345) >> ((i % 4) * 8)) & 255) as u8) |
| 873 | .collect::<Vec<_>>(); |
| 874 | assert_eq!(catalog.members[0].data, expected); |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | #[test] |
| 879 | fn seven_zip_malicious_inputs_never_write() { |
| 880 | for name in [ |
| 881 | "unsafe-dotdot.7z", |
| 882 | "unsafe-absolute.7z", |
| 883 | "unsafe-drive.7z", |
| 884 | "unsafe-backslash.7z", |
| 885 | "unsafe-deep.7z", |
| 886 | "symlink.7z", |
| 887 | "collision.7z", |
| 888 | "bad-payload.7z", |
| 889 | "bad-crc.7z", |
| 890 | "bad-dictionary.7z", |
| 891 | ] { |
| 892 | let s = Scratch::new(); |
| 893 | let bytes = seven_fixture(name); |
| 894 | let events = extract(&s, &bytes, None, Selection::All, None, Limits::default()); |
| 895 | assert_eq!( |
| 896 | terminal(&events).outcome, |
| 897 | Outcome::Failed, |
| 898 | "{name}: {events:?}" |
| 899 | ); |
| 900 | assert_eq!(terminal(&events).publication, Publication::None); |
| 901 | assert_eq!(terminal(&events).created_count, 0); |
| 902 | assert!(!events.iter().any(|e| matches!(e, Message::Output { .. }))); |
| 903 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 904 | assert!(!s.0.join("outside.txt").exists()); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | #[test] |
| 909 | fn seven_zip_format_wire_tag_preserves_existing_encodings() { |
| 910 | for (tag, format) in [ |
| 911 | Format::Zip, |
| 912 | Format::Tar, |
| 913 | Format::Gzip, |
| 914 | Format::TarGzip, |
| 915 | Format::SevenZip, |
| 916 | ] |
| 917 | .into_iter() |
| 918 | .enumerate() |
| 919 | { |
| 920 | let token = ListId { |
| 921 | catalog_version: 1, |
| 922 | format, |
| 923 | source_object: SourceObject::Unix { |
| 924 | device: 1, |
| 925 | inode: 2, |
| 926 | }, |
| 927 | archive_bytes: 3, |
| 928 | archive_sha256: [4; 32], |
| 929 | }; |
| 930 | let mut bytes = Vec::new(); |
| 931 | Writer::new(&mut bytes, 17) |
| 932 | .send(&Message::ListEnd { |
| 933 | entry_count: 5, |
| 934 | list_id: token.clone(), |
| 935 | }) |
| 936 | .unwrap(); |
| 937 | assert_eq!(bytes[38], tag as u8); |
| 938 | match Reader::new(bytes.as_slice()).read().unwrap().unwrap().2 { |
| 939 | Message::ListEnd { |
| 940 | entry_count, |
| 941 | list_id, |
| 942 | } => { |
| 943 | assert_eq!(entry_count, 5); |
| 944 | assert_eq!(list_id, token); |
| 945 | } |
| 946 | _ => panic!("expected ListEnd"), |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | #[test] |
| 951 | fn seven_zip_listing_token_and_selection() { |
| 952 | let s = Scratch::new(); |
| 953 | let bytes = seven_fixture("encoded-solid.7z"); |
| 954 | fs::write(s.0.join("input"), &bytes).unwrap(); |
| 955 | let events = execute(Start { |
| 956 | action: Action::List { |
| 957 | archive: s.path("input"), |
| 958 | }, |
| 959 | limits: Limits::default(), |
| 960 | }); |
| 961 | assert_eq!(terminal(&events).outcome, Outcome::Completed); |
| 962 | let list_id = events |
| 963 | .into_iter() |
| 964 | .find_map(|e| { |
| 965 | if let Message::ListEnd { list_id, .. } = e { |
| 966 | Some(list_id) |
| 967 | } else { |
| 968 | None |
| 969 | } |
| 970 | }) |
| 971 | .unwrap(); |
| 972 | assert_eq!(list_id.format, Format::SevenZip); |
| 973 | let events = extract( |
| 974 | &s, |
| 975 | &bytes, |
| 976 | None, |
| 977 | Selection::EntryIds(vec![3]), |
| 978 | Some(list_id), |
| 979 | Limits::default(), |
| 980 | ); |
| 981 | assert_eq!(terminal(&events).outcome, Outcome::Completed); |
| 982 | assert_eq!(terminal(&events).created_count, 1); |
| 983 | assert!(!s.0.join("output/folder").exists()); |
| 984 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 1); |
| 985 | } |
| 986 | #[test] |
| 987 | fn seven_zip_limits_truncation_and_existing_files_are_preserved() { |
| 988 | let bytes = seven_fixture("lzma2.7z"); |
| 989 | for cut in [0, 1, 6, 12, 31, 32, bytes.len() - 1] { |
| 990 | let s = Scratch::new(); |
| 991 | let events = extract( |
| 992 | &s, |
| 993 | &bytes[..cut], |
| 994 | None, |
| 995 | Selection::All, |
| 996 | None, |
| 997 | Limits::default(), |
| 998 | ); |
| 999 | assert_eq!(terminal(&events).outcome, Outcome::Failed); |
| 1000 | assert_eq!(terminal(&events).publication, Publication::None); |
| 1001 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 1002 | } |
| 1003 | let s = Scratch::new(); |
| 1004 | let events = extract( |
| 1005 | &s, |
| 1006 | &bytes, |
| 1007 | None, |
| 1008 | Selection::All, |
| 1009 | None, |
| 1010 | Limits { |
| 1011 | output: 100, |
| 1012 | ..Limits::default() |
| 1013 | }, |
| 1014 | ); |
| 1015 | assert_eq!(terminal(&events).error, ErrorCode::Limit); |
| 1016 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 1017 | fs::create_dir(s.0.join("output/folder")).unwrap(); |
| 1018 | #[cfg(unix)] |
| 1019 | { |
| 1020 | use std::os::unix::fs::PermissionsExt; |
| 1021 | fs::set_permissions(s.0.join("output/folder"), fs::Permissions::from_mode(0o700)).unwrap(); |
| 1022 | } |
| 1023 | fs::write(s.0.join("output/folder/hello.txt"), b"preserve").unwrap(); |
| 1024 | let events = extract(&s, &bytes, None, Selection::All, None, Limits::default()); |
| 1025 | assert_eq!(terminal(&events).error, ErrorCode::Conflict); |
| 1026 | assert_eq!( |
| 1027 | fs::read(s.0.join("output/folder/hello.txt")).unwrap(), |
| 1028 | b"preserve" |
| 1029 | ); |
| 1030 | let s = Scratch::new(); |
| 1031 | let events = extract( |
| 1032 | &s, |
| 1033 | &seven_fixture("bad-trailing.7z"), |
| 1034 | None, |
| 1035 | Selection::All, |
| 1036 | None, |
| 1037 | Limits::default(), |
| 1038 | ); |
| 1039 | assert_eq!(terminal(&events).error, ErrorCode::Integrity); |
| 1040 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 1041 | } |
| 1042 | #[test] |
| 1043 | fn seven_zip_parser_honors_cancellation_checkpoints() { |
| 1044 | let bytes = seven_fixture("long-match.7z"); |
| 1045 | let mut calls = 0; |
| 1046 | let result = archive::parse(&bytes, Limits::default(), true, &mut || { |
| 1047 | calls += 1; |
| 1048 | if calls >= 5 { |
| 1049 | Err(ErrorCode::None) |
| 1050 | } else { |
| 1051 | Ok(()) |
| 1052 | } |
| 1053 | }); |
| 1054 | assert!(matches!(result, Err(ErrorCode::None))); |
| 1055 | } |
| 1056 | #[test] |
| 1057 | fn pr64_malicious_corpus_is_rejected_without_writes() { |
| 1058 | let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 1059 | .join("../../tests/malicious-archive-fixtures/fixtures"); |
| 1060 | let cases = [ |
| 1061 | ("zip-dotdot.zip", ErrorCode::UnsafePath, 131072, None), |
| 1062 | ( |
| 1063 | "zip-absolute-posix.zip", |
| 1064 | ErrorCode::UnsafePath, |
| 1065 | 131072, |
| 1066 | None, |
| 1067 | ), |
| 1068 | ( |
| 1069 | "zip-absolute-drive.zip", |
| 1070 | ErrorCode::UnsafePath, |
| 1071 | 131072, |
| 1072 | None, |
| 1073 | ), |
| 1074 | ( |
| 1075 | "zip-backslash-dotdot.zip", |
| 1076 | ErrorCode::UnsafePath, |
| 1077 | 131072, |
| 1078 | None, |
| 1079 | ), |
| 1080 | ("tar-dotdot.tar", ErrorCode::UnsafePath, 131072, None), |
| 1081 | ("targz-dotdot.tar.gz", ErrorCode::UnsafePath, 131072, None), |
| 1082 | ( |
| 1083 | "tar-absolute-posix.tar", |
| 1084 | ErrorCode::UnsafePath, |
| 1085 | 131072, |
| 1086 | None, |
| 1087 | ), |
| 1088 | ("zip-symlink-escape.zip", ErrorCode::Conflict, 131072, None), |
| 1089 | ("tar-symlink-escape.tar", ErrorCode::Conflict, 131072, None), |
| 1090 | ("zip-output-limit.zip", ErrorCode::Limit, 1024, None), |
| 1091 | ( |
| 1092 | "gzip-output-limit.gz", |
| 1093 | ErrorCode::Limit, |
| 1094 | 1024, |
| 1095 | Some("payload.bin"), |
| 1096 | ), |
| 1097 | ("targz-output-limit.tar.gz", ErrorCode::Limit, 1024, None), |
| 1098 | ( |
| 1099 | "zip-bad-local-header.zip", |
| 1100 | ErrorCode::Integrity, |
| 1101 | 131072, |
| 1102 | None, |
| 1103 | ), |
| 1104 | ("tar-bad-checksum.tar", ErrorCode::Integrity, 131072, None), |
| 1105 | ( |
| 1106 | "gzip-reserved-flags.gz", |
| 1107 | ErrorCode::Integrity, |
| 1108 | 131072, |
| 1109 | Some("payload.bin"), |
| 1110 | ), |
| 1111 | ]; |
| 1112 | for (name, expected, output, gzip) in cases { |
| 1113 | let s = Scratch::new(); |
| 1114 | let bytes = fs::read(root.join(name)).unwrap(); |
| 1115 | fs::write(s.0.join("outside.txt"), b"preserve").unwrap(); |
| 1116 | let events = extract( |
| 1117 | &s, |
| 1118 | &bytes, |
| 1119 | gzip, |
| 1120 | Selection::All, |
| 1121 | None, |
| 1122 | Limits { |
| 1123 | input: 1048576, |
| 1124 | output, |
| 1125 | entries: 64, |
| 1126 | }, |
| 1127 | ); |
| 1128 | assert_eq!( |
| 1129 | terminal(&events).outcome, |
| 1130 | Outcome::Failed, |
| 1131 | "{name}: {events:?}" |
| 1132 | ); |
| 1133 | assert_eq!(terminal(&events).error, expected, "{name}"); |
| 1134 | assert_eq!(terminal(&events).publication, Publication::None); |
| 1135 | assert_eq!(terminal(&events).created_count, 0); |
| 1136 | assert_eq!(terminal(&events).retained_partial_count, 0); |
| 1137 | assert!(!events.iter().any(|e| matches!(e, Message::Output { .. }))); |
| 1138 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 1139 | assert_eq!(fs::read(s.0.join("outside.txt")).unwrap(), b"preserve"); |
| 1140 | assert_eq!(fs::read(s.0.join("input")).unwrap(), bytes); |
| 1141 | } |
| 1142 | } |
| 1143 | #[cfg(target_os = "linux")] |
| 1144 | #[test] |
| 1145 | fn seven_zip_reference_reader_accepts_synthetic_fixtures() { |
| 1146 | let helper = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 1147 | .join("../../tests/rust-archive-worker/seven_zip_reference.py"); |
| 1148 | assert!( |
| 1149 | Command::new("python3") |
| 1150 | .arg(helper) |
| 1151 | .status() |
| 1152 | .unwrap() |
| 1153 | .success() |
| 1154 | ); |
| 1155 | } |