team-ljyzipelo

공개

ZIP 주석 안의 중첩 중앙 목록 후보를 제외함

작성자
시로사키 하나
작성 시각
커밋
16d8f5e456d00d62c9351938776485e4875db1a8
부모
1e285b5
이 시점의 파일 보기

변경된 파일 2개

+127-2
수정experiments/rust-archive-worker/src/zip_recovery.rs+10-2
@@ -197,6 +197,7 @@ fn end_record(
197197 tail_at: u64,
198198 check: &mut impl Monitor,
199199) -> Result<Option<usize>> {
200추가: let mut selected: Option<(usize, u64)> = None;
200201 for end in (0..=tail.len() - 22).rev() {
201202 if &tail[end..end + 4] != b"PK\x05\x06"
202203 || end + 22 + u16at(tail, end + 20) as usize != tail.len()
@@ -232,9 +233,16 @@ fn end_record(
232233 if !directory_span(source, offset, stop, check)? {
233234 continue;
234235 }
235삭제: return Ok(Some(end));
236추가: let comment_at = at + 22;
237추가: // 선행 후보의 중앙 목록이나 주석에 속한 후행 목록은 선택하지 않음.
238추가: if selected.is_none_or(|(nested_end, nested_directory)| {
239추가: (offset < nested_directory && nested_directory < stop)
240추가: || (comment_at <= nested_directory && end + 22 <= nested_end)
241추가: }) {
242추가: selected = Some((end, offset));
243추가: }
236244 }
237삭제: Ok(None)
245추가: Ok(selected.map(|(end, _)| end))
238246}
239247
240248fn central(
수정tests/rust-archive-worker/recovery.rs+117-0
@@ -610,3 +610,120 @@ fn fake_eocd_in_archive_comment_keeps_real_central_directory() {
610610 }
611611 }
612612}
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추가: }