ZIP 복구에서 로컬 ZIP64 크기 검사를 제거함
- 작성자
- 시로사키 하나
- 작성 시각
- 커밋
e1e4950b9eebcab4daba4da45bd874e6efa3c7ae- 부모
- c8fee9f
변경된 파일 4개
+191-3| @@ -469,17 +469,15 @@ fn central( | ||
| 469 | 469 | return Err(ErrorCode::Integrity); |
| 470 | 470 | } |
| 471 | 471 | let local = bytes(source, offset, 30, cd)?; |
| 472 | 삭제: declared(u32at(&local, 22), u32at(&local, 18))?; | |
| 473 | 472 | let ln = u16at(&local, 26) as usize; |
| 474 | 473 | let le = u16at(&local, 28) as usize; |
| 475 | 474 | charge(&mut metadata, ln, le, check)?; |
| 475 | 추가: // 로컬 extra 내용은 읽지 않고 중앙 목록 앞의 경계만 확인함. | |
| 476 | 476 | let start = add(offset, 30 + ln as u64 + le as u64)?; |
| 477 | 477 | if start > cd { |
| 478 | 478 | return Err(ErrorCode::Integrity); |
| 479 | 479 | } |
| 480 | 480 | let local_raw = bytes(source, offset + 30, ln, cd)?; |
| 481 | 삭제: let local_extra = bytes(source, offset + 30 + ln as u64, le, cd)?; | |
| 482 | 삭제: extra(&local_extra)?; | |
| 483 | 481 | let mut stop = add(start, packed)?; |
| 484 | 482 | if stop > cd { |
| 485 | 483 | return Err(ErrorCode::Integrity); |
| @@ -0,0 +1,46 @@ | ||
| 1 | 추가: import io | |
| 2 | 추가: from pathlib import Path | |
| 3 | 추가: import struct | |
| 4 | 추가: import sys | |
| 5 | 추가: import zipfile | |
| 6 | 추가: | |
| 7 | 추가: | |
| 8 | 추가: PAYLOAD = b"Central directory recovery reference\n" | |
| 9 | 추가: | |
| 10 | 추가: | |
| 11 | 추가: def verify(path): | |
| 12 | 추가: with zipfile.ZipFile(path) as archive: | |
| 13 | 추가: assert archive.namelist() == ["folder/", "folder/hello.txt"] | |
| 14 | 추가: assert archive.read("folder/") == b"" | |
| 15 | 추가: assert archive.read("folder/hello.txt") == PAYLOAD | |
| 16 | 추가: | |
| 17 | 추가: | |
| 18 | 추가: def generate(path): | |
| 19 | 추가: stream = io.BytesIO() | |
| 20 | 추가: with zipfile.ZipFile(stream, "w") as archive: | |
| 21 | 추가: with archive.open("folder/", "w", force_zip64=True): | |
| 22 | 추가: pass | |
| 23 | 추가: archive.writestr("folder/hello.txt", PAYLOAD) | |
| 24 | 추가: original = stream.getvalue() | |
| 25 | 추가: damaged = bytearray(original) | |
| 26 | 추가: name_length, extra_length = struct.unpack_from("<HH", damaged, 26) | |
| 27 | 추가: extra = 30 + name_length | |
| 28 | 추가: assert extra_length == 20 | |
| 29 | 추가: assert struct.unpack_from("<HHQQ", damaged, extra) == (1, 16, 0, 0) | |
| 30 | 추가: assert struct.unpack_from("<II", damaged, 18) == (0xFFFFFFFF, 0xFFFFFFFF) | |
| 31 | 추가: struct.pack_into("<QQ", damaged, extra + 4, 0xFFFFFFFFFFFFFFFF, 0xEEEEEEEEEEEEEEEE) | |
| 32 | 추가: assert damaged[:extra + 4] == original[:extra + 4] | |
| 33 | 추가: assert damaged[extra + 20:] == original[extra + 20:] | |
| 34 | 추가: with zipfile.ZipFile(io.BytesIO(damaged)) as archive: | |
| 35 | 추가: folder = archive.infolist()[0] | |
| 36 | 추가: assert folder.file_size == folder.compress_size == folder.CRC == 0 | |
| 37 | 추가: assert folder.extra == b"" | |
| 38 | 추가: path.write_bytes(damaged) | |
| 39 | 추가: verify(path) | |
| 40 | 추가: | |
| 41 | 추가: | |
| 42 | 추가: if __name__ == "__main__": | |
| 43 | 추가: if sys.argv[1] == "--verify": | |
| 44 | 추가: verify(Path(sys.argv[2])) | |
| 45 | 추가: else: | |
| 46 | 추가: generate(Path(sys.argv[1])) |
| @@ -829,3 +829,62 @@ fn candidate_body_probes_do_not_bypass_recovery_quotas() { | ||
| 829 | 829 | assert_eq!(fs::read(s.0.join("damaged.zip")).unwrap(), input); |
| 830 | 830 | } |
| 831 | 831 | } |
| 832 | 추가: | |
| 833 | 추가: #[test] | |
| 834 | 추가: fn corrupt_local_zip64_directory_sizes_recover_list_and_extract() { | |
| 835 | 추가: let s = Scratch::new(); | |
| 836 | 추가: let fixture = PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 837 | 추가: .join("../../tests/rust-archive-worker/local_zip64_fixture.py"); | |
| 838 | 추가: let generated = Command::new("python3") | |
| 839 | 추가: .arg("-B") | |
| 840 | 추가: .arg(&fixture) | |
| 841 | 추가: .arg(s.0.join("damaged.zip")) | |
| 842 | 추가: .output() | |
| 843 | 추가: .unwrap(); | |
| 844 | 추가: assert!(generated.status.success(), "{generated:?}"); | |
| 845 | 추가: let original = fs::read(s.0.join("damaged.zip")).unwrap(); | |
| 846 | 추가: let events = execute(start(&s)); | |
| 847 | 추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); | |
| 848 | 추가: let end = recovery_end(&events); | |
| 849 | 추가: assert_eq!(end.mode, RecoveryMode::CentralDirectory); | |
| 850 | 추가: assert_eq!(end.known_entries, Some(2)); | |
| 851 | 추가: assert_eq!(end.verified_entries, 2); | |
| 852 | 추가: assert!(!end.unknown_loss); | |
| 853 | 추가: assert!(events.iter().any(|event| matches!( | |
| 854 | 추가: event, | |
| 855 | 추가: Message::RecoveryEntry(entry) | |
| 856 | 추가: if entry.name.as_deref() == Some("folder") | |
| 857 | 추가: && entry.reason == RecoveryReason::HeaderRebuilt | |
| 858 | 추가: && entry.status == RecoveryStatus::Verified | |
| 859 | 추가: ))); | |
| 860 | 추가: assert_eq!(fs::read(s.0.join("damaged.zip")).unwrap(), original); | |
| 861 | 추가: let checked = Command::new("python3") | |
| 862 | 추가: .arg("-B") | |
| 863 | 추가: .arg(&fixture) | |
| 864 | 추가: .arg("--verify") | |
| 865 | 추가: .arg(s.0.join("recovered.zip")) | |
| 866 | 추가: .output() | |
| 867 | 추가: .unwrap(); | |
| 868 | 추가: assert!(checked.status.success(), "{checked:?}"); | |
| 869 | 추가: let recovered = fs::read(s.0.join("recovered.zip")).unwrap(); | |
| 870 | 추가: fs::write(s.0.join("input"), &recovered).unwrap(); | |
| 871 | 추가: let id = listing(&s); | |
| 872 | 추가: let extracted = extract( | |
| 873 | 추가: &s, | |
| 874 | 추가: &recovered, | |
| 875 | 추가: None, | |
| 876 | 추가: Selection::All, | |
| 877 | 추가: Some(id), | |
| 878 | 추가: Limits::default(), | |
| 879 | 추가: ); | |
| 880 | 추가: assert_eq!( | |
| 881 | 추가: terminal(&extracted).outcome, | |
| 882 | 추가: Outcome::Completed, | |
| 883 | 추가: "{extracted:?}" | |
| 884 | 추가: ); | |
| 885 | 추가: assert!(s.0.join("output/folder").is_dir()); | |
| 886 | 추가: assert_eq!( | |
| 887 | 추가: fs::read(s.0.join("output/folder/hello.txt")).unwrap(), | |
| 888 | 추가: b"Central directory recovery reference\n" | |
| 889 | 추가: ); | |
| 890 | 추가: } |
| @@ -325,3 +325,88 @@ fn validation_only_decode_preserves_exact_stream_limits_and_cancellation() { | ||
| 325 | 325 | assert_eq!(std::fs::read_dir(&s.0).unwrap().count(), 1); |
| 326 | 326 | } |
| 327 | 327 | } |
| 328 | 추가: | |
| 329 | 추가: #[test] | |
| 330 | 추가: fn central_rejects_local_extra_past_directory_or_file_boundary() { | |
| 331 | 추가: let original = include_bytes!("../zip-recovery-fixtures/control.zip"); | |
| 332 | 추가: let cd = u32at(original, original.len() - 6) as usize; | |
| 333 | 추가: let name_length = u16at(original, 26) as usize; | |
| 334 | 추가: for extra in [(cd - 30 - name_length + 1) as u16, u16::MAX] { | |
| 335 | 추가: let mut damaged = original.to_vec(); | |
| 336 | 추가: damaged[28..30].copy_from_slice(&extra.to_le_bytes()); | |
| 337 | 추가: let s = Scratch::new(&damaged); | |
| 338 | 추가: let mut source = File::open(s.path()).unwrap(); | |
| 339 | 추가: assert!(matches!( | |
| 340 | 추가: central( | |
| 341 | 추가: &mut source, | |
| 342 | 추가: damaged.len() as u64, | |
| 343 | 추가: Limits::default(), | |
| 344 | 추가: &mut || Ok(()) | |
| 345 | 추가: ), | |
| 346 | 추가: Err(ErrorCode::Integrity) | |
| 347 | 추가: )); | |
| 348 | 추가: } | |
| 349 | 추가: } | |
| 350 | 추가: | |
| 351 | 추가: #[test] | |
| 352 | 추가: fn central_rejects_overlapping_local_offsets() { | |
| 353 | 추가: let mut damaged = include_bytes!("../zip-recovery-fixtures/control.zip").to_vec(); | |
| 354 | 추가: let cd = u32at(&damaged, damaged.len() - 6) as usize; | |
| 355 | 추가: let second = cd | |
| 356 | 추가: + 46 | |
| 357 | 추가: + u16at(&damaged, cd + 28) as usize | |
| 358 | 추가: + u16at(&damaged, cd + 30) as usize | |
| 359 | 추가: + u16at(&damaged, cd + 32) as usize; | |
| 360 | 추가: damaged[second + 42..second + 46].copy_from_slice(&0u32.to_le_bytes()); | |
| 361 | 추가: let s = Scratch::new(&damaged); | |
| 362 | 추가: let mut source = File::open(s.path()).unwrap(); | |
| 363 | 추가: assert!(matches!( | |
| 364 | 추가: central( | |
| 365 | 추가: &mut source, | |
| 366 | 추가: damaged.len() as u64, | |
| 367 | 추가: Limits::default(), | |
| 368 | 추가: &mut || Ok(()) | |
| 369 | 추가: ), | |
| 370 | 추가: Err(ErrorCode::Integrity) | |
| 371 | 추가: )); | |
| 372 | 추가: } | |
| 373 | 추가: | |
| 374 | 추가: #[test] | |
| 375 | 추가: fn central_zip64_fields_and_extra_remain_unsupported() { | |
| 376 | 추가: let original = include_bytes!("../zip-recovery-fixtures/control.zip"); | |
| 377 | 추가: let cd = u32at(original, original.len() - 6) as usize; | |
| 378 | 추가: for field in [20, 24, 42] { | |
| 379 | 추가: let mut damaged = original.to_vec(); | |
| 380 | 추가: damaged[cd + field..cd + field + 4].copy_from_slice(&u32::MAX.to_le_bytes()); | |
| 381 | 추가: let s = Scratch::new(&damaged); | |
| 382 | 추가: let mut source = File::open(s.path()).unwrap(); | |
| 383 | 추가: assert!(matches!( | |
| 384 | 추가: central( | |
| 385 | 추가: &mut source, | |
| 386 | 추가: damaged.len() as u64, | |
| 387 | 추가: Limits::default(), | |
| 388 | 추가: &mut || Ok(()) | |
| 389 | 추가: ), | |
| 390 | 추가: Err(ErrorCode::UnsupportedFeature) | |
| 391 | 추가: )); | |
| 392 | 추가: } | |
| 393 | 추가: let mut damaged = original.to_vec(); | |
| 394 | 추가: let extra = cd + 46 + u16at(original, cd + 28) as usize; | |
| 395 | 추가: assert_eq!(u16at(original, cd + 30), 0); | |
| 396 | 추가: damaged.splice(extra..extra, [1, 0, 0, 0]); | |
| 397 | 추가: damaged[cd + 30..cd + 32].copy_from_slice(&4u16.to_le_bytes()); | |
| 398 | 추가: let end = damaged.len() - 22; | |
| 399 | 추가: let size = u32at(original, original.len() - 10) + 4; | |
| 400 | 추가: damaged[end + 12..end + 16].copy_from_slice(&size.to_le_bytes()); | |
| 401 | 추가: let s = Scratch::new(&damaged); | |
| 402 | 추가: let mut source = File::open(s.path()).unwrap(); | |
| 403 | 추가: assert!(matches!( | |
| 404 | 추가: central( | |
| 405 | 추가: &mut source, | |
| 406 | 추가: damaged.len() as u64, | |
| 407 | 추가: Limits::default(), | |
| 408 | 추가: &mut || Ok(()) | |
| 409 | 추가: ), | |
| 410 | 추가: Err(ErrorCode::UnsupportedFeature) | |
| 411 | 추가: )); | |
| 412 | 추가: } |