| | @@ -610,3 +610,120 @@ fn fake_eocd_in_archive_comment_keeps_real_central_directory() { |
| 610 | 610 | } |
| 611 | 611 | } |
| 612 | 612 | } |
| 613 | +추가: |
| 614 | +추가: #[test] |
| 615 | +추가: fn nested_central_directories_in_comments_cannot_hide_original_members() { |
| 616 | +추가: let source = include_bytes!("../zip-recovery-fixtures/broken-local-header.zip"); |
| 617 | +추가: for entries in [1usize, 3, 6] { |
| 618 | +추가: let mut stop = 1372; |
| 619 | +추가: for _ in 0..entries { |
| 620 | +추가: stop += 46 |
| 621 | +추가: + u16::from_le_bytes(source[stop + 28..stop + 30].try_into().unwrap()) as usize |
| 622 | +추가: + u16::from_le_bytes(source[stop + 30..stop + 32].try_into().unwrap()) as usize |
| 623 | +추가: + u16::from_le_bytes(source[stop + 32..stop + 34].try_into().unwrap()) as usize; |
| 624 | +추가: } |
| 625 | +추가: for depth in [1, 3] { |
| 626 | +추가: for locator in [false, true] { |
| 627 | +추가: for maximum_comment in [false, true] { |
| 628 | +추가: let mut input = source.to_vec(); |
| 629 | +추가: let mut ends = vec![source.len() - 22]; |
| 630 | +추가: for _ in 0..depth { |
| 631 | +추가: let cd = input.len(); |
| 632 | +추가: input.extend(&source[1372..stop]); |
| 633 | +추가: if locator { |
| 634 | +추가: input.extend(b"PK\x06\x07"); |
| 635 | +추가: input.extend([0; 16]); |
| 636 | +추가: } |
| 637 | +추가: let mut end = source[source.len() - 22..].to_vec(); |
| 638 | +추가: end[8..10].copy_from_slice(&(entries as u16).to_le_bytes()); |
| 639 | +추가: end[10..12].copy_from_slice(&(entries as u16).to_le_bytes()); |
| 640 | +추가: end[12..16].copy_from_slice(&((stop - 1372) as u32).to_le_bytes()); |
| 641 | +추가: end[16..20].copy_from_slice(&(cd as u32).to_le_bytes()); |
| 642 | +추가: ends.push(input.len()); |
| 643 | +추가: input.extend(end); |
| 644 | +추가: } |
| 645 | +추가: if maximum_comment { |
| 646 | +추가: input.resize(source.len() + u16::MAX as usize, 0); |
| 647 | +추가: } |
| 648 | +추가: let len = input.len(); |
| 649 | +추가: for end in ends { |
| 650 | +추가: input[end + 20..end + 22] |
| 651 | +추가: .copy_from_slice(&((len - end - 22) as u16).to_le_bytes()); |
| 652 | +추가: } |
| 653 | +추가: assert_full_comment_recovery( |
| 654 | +추가: &input, |
| 655 | +추가: &format!("{entries}/{depth}/{locator}/{maximum_comment}"), |
| 656 | +추가: ); |
| 657 | +추가: } |
| 658 | +추가: } |
| 659 | +추가: } |
| 660 | +추가: } |
| 661 | +추가: } |
| 662 | +추가: |
| 663 | +추가: fn assert_full_comment_recovery(input: &[u8], case: &str) { |
| 664 | +추가: let s = Scratch::new(); |
| 665 | +추가: fs::write(s.0.join("damaged.zip"), input).unwrap(); |
| 666 | +추가: let events = execute(start(&s)); |
| 667 | +추가: let terminal = terminal(&events); |
| 668 | +추가: assert_eq!(terminal.error, ErrorCode::None, "{case}"); |
| 669 | +추가: assert_eq!(terminal.outcome, Outcome::Completed); |
| 670 | +추가: assert_eq!(terminal.publication, Publication::Complete); |
| 671 | +추가: assert_eq!(terminal.created_count, 1); |
| 672 | +추가: let summary = recovery_end(&events); |
| 673 | +추가: assert!(summary.complete()); |
| 674 | +추가: assert_eq!(summary.mode, RecoveryMode::CentralDirectory); |
| 675 | +추가: assert_eq!(summary.known_entries, Some(6)); |
| 676 | +추가: assert_eq!(summary.verified_entries, 6); |
| 677 | +추가: assert_eq!( |
| 678 | +추가: summary.source_sha256, |
| 679 | +추가: <[u8; 32]>::from(Sha256::digest(input)) |
| 680 | +추가: ); |
| 681 | +추가: assert_eq!(fs::read(s.0.join("damaged.zip")).unwrap(), input); |
| 682 | +추가: let checked = Command::new("python3") |
| 683 | +추가: .arg("-B") |
| 684 | +추가: .arg( |
| 685 | +추가: PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 686 | +추가: .join("../../tests/zip-recovery-fixtures/verify.py"), |
| 687 | +추가: ) |
| 688 | +추가: .args(["--recovered", "broken-local-header.zip"]) |
| 689 | +추가: .arg(s.0.join("recovered.zip")) |
| 690 | +추가: .output() |
| 691 | +추가: .unwrap(); |
| 692 | +추가: assert!(checked.status.success(), "{case}: {checked:?}"); |
| 693 | +추가: } |
| 694 | +추가: |
| 695 | +추가: #[test] |
| 696 | +추가: fn nested_directory_in_central_entry_comment_is_not_an_outer_archive() { |
| 697 | +추가: let source = include_bytes!("../zip-recovery-fixtures/broken-local-header.zip"); |
| 698 | +추가: for wraps_real_end in [false, true] { |
| 699 | +추가: let mut fake_cd = source[1372..1427].to_vec(); |
| 700 | +추가: if wraps_real_end { |
| 701 | +추가: fake_cd[32..34].copy_from_slice(&22u16.to_le_bytes()); |
| 702 | +추가: } |
| 703 | +추가: let mut input = source[..source.len() - 22].to_vec(); |
| 704 | +추가: let last = input.windows(4).rposition(|w| w == b"PK\x01\x02").unwrap(); |
| 705 | +추가: let comment_len = fake_cd.len() + if wraps_real_end { 0 } else { 22 }; |
| 706 | +추가: input[last + 32..last + 34].copy_from_slice(&(comment_len as u16).to_le_bytes()); |
| 707 | +추가: let fake_offset = input.len(); |
| 708 | +추가: input.extend(&fake_cd); |
| 709 | +추가: let mut real_end = source[source.len() - 22..].to_vec(); |
| 710 | +추가: real_end[12..16].copy_from_slice(&((340 + comment_len) as u32).to_le_bytes()); |
| 711 | +추가: let mut fake_end = source[source.len() - 22..].to_vec(); |
| 712 | +추가: fake_end[8..10].copy_from_slice(&1u16.to_le_bytes()); |
| 713 | +추가: fake_end[10..12].copy_from_slice(&1u16.to_le_bytes()); |
| 714 | +추가: fake_end[12..16].copy_from_slice( |
| 715 | +추가: &((fake_cd.len() + if wraps_real_end { 22 } else { 0 }) as u32).to_le_bytes(), |
| 716 | +추가: ); |
| 717 | +추가: fake_end[16..20].copy_from_slice(&(fake_offset as u32).to_le_bytes()); |
| 718 | +추가: if wraps_real_end { |
| 719 | +추가: real_end[20..22].copy_from_slice(&22u16.to_le_bytes()); |
| 720 | +추가: input.extend(real_end); |
| 721 | +추가: input.extend(fake_end); |
| 722 | +추가: } else { |
| 723 | +추가: fake_end[20..22].copy_from_slice(&22u16.to_le_bytes()); |
| 724 | +추가: input.extend(fake_end); |
| 725 | +추가: input.extend(real_end); |
| 726 | +추가: } |
| 727 | +추가: assert_full_comment_recovery(&input, &format!("central-comment/{wraps_real_end}")); |
| 728 | +추가: } |
| 729 | +추가: } |