ZIP64 locator 기반 감지와 검증을 보완함
- 작성자
- 시로사키 하나
- 작성 시각
- 커밋
bcc9c5cd729812921878072a22953412dfe5dd78- 부모
- 608bf6f
변경된 파일 3개
+85-7| @@ -392,11 +392,20 @@ fn zip( | ||
| 392 | 392 | let mut central_size = u32at(h, 12) as u64; |
| 393 | 393 | let mut central = u32at(h, 16) as u64; |
| 394 | 394 | let mut central_end = tail_at + end as u64; |
| 395 | 삭제: if count == u16::MAX as u64 || central_size == u32::MAX as u64 || central == u32::MAX as u64 { | |
| 396 | 삭제: if central_end < 20 { | |
| 395 | 추가: let required_zip64 = | |
| 396 | 추가: count == u16::MAX as u64 || central_size == u32::MAX as u64 || central == u32::MAX as u64; | |
| 397 | 추가: let locator = if central_end >= 20 { | |
| 398 | 추가: bytes(source, central_end - 20, 20)? | |
| 399 | 추가: } else { | |
| 400 | 추가: Vec::new() | |
| 401 | 추가: }; | |
| 402 | 추가: // 중앙 디렉터리 주석의 동일 서명을 locator로 오인하지 않음 | |
| 403 | 추가: let has_locator = | |
| 404 | 추가: locator.starts_with(b"PK\x06\x07") && add(central, central_size)? != central_end; | |
| 405 | 추가: if required_zip64 || has_locator { | |
| 406 | 추가: if locator.len() != 20 { | |
| 397 | 407 | return Err(ErrorCode::Integrity); |
| 398 | 408 | } |
| 399 | 삭제: let locator = bytes(source, central_end - 20, 20)?; | |
| 400 | 409 | if &locator[..4] != b"PK\x06\x07" || u32at(&locator, 4) != 0 || u32at(&locator, 16) != 1 { |
| 401 | 410 | return Err(ErrorCode::UnsupportedFeature); |
| 402 | 411 | } |
| @@ -149,6 +149,47 @@ fn zip64_offset_only_keeps_32_bit_descriptors() { | ||
| 149 | 149 | } |
| 150 | 150 | } |
| 151 | 151 | #[test] |
| 152 | 추가: fn zip64_locator_with_ordinary_eocd_fields_interoperates() { | |
| 153 | 추가: let s = Scratch::new(); | |
| 154 | 추가: generate(&s, "locator64", 20); | |
| 155 | 추가: assert_eq!(fs::metadata(s.0.join("input")).unwrap().len(), 260); | |
| 156 | 추가: let events = execute(Start { | |
| 157 | 추가: action: Action::List { | |
| 158 | 추가: archive: s.path("input"), | |
| 159 | 추가: }, | |
| 160 | 추가: limits: Limits::default(), | |
| 161 | 추가: }); | |
| 162 | 추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); | |
| 163 | 추가: let events = execute(start(&s, false, Limits::default().output)); | |
| 164 | 추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); | |
| 165 | 추가: assert_zero_file(&s.0.join("output/payload.bin"), 20); | |
| 166 | 추가: } | |
| 167 | 추가: #[test] | |
| 168 | 추가: fn zip64_locator_corruption_never_publishes() { | |
| 169 | 추가: for (kind, error) in [ | |
| 170 | 추가: ("locator64-bad-offset", ErrorCode::Integrity), | |
| 171 | 추가: ("locator64-bad-size", ErrorCode::Integrity), | |
| 172 | 추가: ("locator64-conflicting-count", ErrorCode::Integrity), | |
| 173 | 추가: ("locator64-multidisk", ErrorCode::UnsupportedFeature), | |
| 174 | 추가: ] { | |
| 175 | 추가: let s = Scratch::new(); | |
| 176 | 추가: generate(&s, kind, 20); | |
| 177 | 추가: let events = execute(start(&s, false, Limits::default().output)); | |
| 178 | 추가: assert_eq!(terminal(&events).error, error, "{kind}: {events:?}"); | |
| 179 | 추가: assert_eq!(terminal(&events).publication, Publication::None); | |
| 180 | 추가: assert_eq!(terminal(&events).cleanup, Cleanup::Complete); | |
| 181 | 추가: assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); | |
| 182 | 추가: } | |
| 183 | 추가: } | |
| 184 | 추가: #[test] | |
| 185 | 추가: fn classic_directory_comment_is_not_a_zip64_locator() { | |
| 186 | 추가: let s = Scratch::new(); | |
| 187 | 추가: generate(&s, "classic-locator-decoy", 20); | |
| 188 | 추가: let events = execute(start(&s, false, Limits::default().output)); | |
| 189 | 추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); | |
| 190 | 추가: assert_zero_file(&s.0.join("output/payload.bin"), 20); | |
| 191 | 추가: } | |
| 192 | 추가: #[test] | |
| 152 | 193 | fn untrusted_zip64_u64_sizes_and_offsets_never_allocate_or_publish() { |
| 153 | 194 | for (kind, error) in [ |
| 154 | 195 | ("central64-size-overflow", ErrorCode::Limit), |
| @@ -67,7 +67,7 @@ def generate(directory, kind, size): | ||
| 67 | 67 | if kind == "central64": |
| 68 | 68 | with zipfile.ZipFile(output) as reference: |
| 69 | 69 | assert reference.read(fixtures.NAME) == bytes(size) |
| 70 | 삭제: elif kind == "descriptor64": | |
| 70 | 추가: elif kind == "descriptor64" or kind.startswith("locator64"): | |
| 71 | 71 | class Pipe(io.BytesIO): |
| 72 | 72 | def seekable(self): |
| 73 | 73 | return False |
| @@ -77,9 +77,37 @@ def generate(directory, kind, size): | ||
| 77 | 77 | with zipfile.ZipFile(data, "w", compression=zipfile.ZIP_STORED) as archive: |
| 78 | 78 | with archive.open(fixtures.NAME, "w", force_zip64=True) as target: |
| 79 | 79 | target.write(bytes(size)) |
| 80 | 삭제: output.write_bytes(data.getvalue()) | |
| 81 | 삭제: with zipfile.ZipFile(output) as reference: | |
| 82 | 삭제: assert reference.read(fixtures.NAME) == bytes(size) | |
| 80 | 추가: original = data.getvalue() | |
| 81 | 추가: if kind.startswith("locator64"): | |
| 82 | 추가: end = original[-22:] | |
| 83 | 추가: count, central_size, central_at = struct.unpack_from("<HII", end, 10) | |
| 84 | 추가: record_at = len(original) - 22 | |
| 85 | 추가: record = bytearray(struct.pack( | |
| 86 | 추가: "<4sQHHIIQQQQ", b"PK\x06\x06", 44, 45, 45, | |
| 87 | 추가: 0, 0, count, count, central_size, central_at, | |
| 88 | 추가: )) | |
| 89 | 추가: locator = bytearray(struct.pack("<4sIQI", b"PK\x06\x07", 0, record_at, 1)) | |
| 90 | 추가: if kind == "locator64-bad-offset": | |
| 91 | 추가: struct.pack_into("<Q", locator, 8, (1 << 64) - 1) | |
| 92 | 추가: elif kind == "locator64-bad-size": | |
| 93 | 추가: struct.pack_into("<Q", record, 4, (1 << 64) - 1) | |
| 94 | 추가: elif kind == "locator64-conflicting-count": | |
| 95 | 추가: struct.pack_into("<QQ", record, 24, count + 1, count + 1) | |
| 96 | 추가: elif kind == "locator64-multidisk": | |
| 97 | 추가: struct.pack_into("<I", locator, 16, 2) | |
| 98 | 추가: original = original[:-22] + record + locator + end | |
| 99 | 추가: if kind == "locator64": | |
| 100 | 추가: assert len(original) == 260 | |
| 101 | 추가: assert count == 1 and central_size < 0xffffffff and central_at < 0xffffffff | |
| 102 | 추가: output.write_bytes(original) | |
| 103 | 추가: if kind in ("descriptor64", "locator64"): | |
| 104 | 추가: with zipfile.ZipFile(output) as reference: | |
| 105 | 추가: assert reference.read(fixtures.NAME) == bytes(size) | |
| 106 | 추가: elif kind == "classic-locator-decoy": | |
| 107 | 추가: info = zipfile.ZipInfo(fixtures.NAME) | |
| 108 | 추가: info.comment = struct.pack("<4sIQI", b"PK\x06\x07", 0, 0, 1) | |
| 109 | 추가: with zipfile.ZipFile(output, "x") as archive: | |
| 110 | 추가: archive.writestr(info, bytes(size)) | |
| 83 | 111 | elif kind == "gzip-bomb-joined": |
| 84 | 112 | with output.open("xb") as raw: |
| 85 | 113 | for level in (6, 0): |