| 1 | use super::*; |
| 2 | |
| 3 | // Info-ZIP 3.0(`zip -X`, UTF-8 로캘)이 만든 표본은 UTF-8 플래그 없이 UTF-8 이름을 기록함. |
| 4 | const KOREAN: &[u8] = include_bytes!("fixtures/infozip-korean.zip"); |
| 5 | const KOREAN_TREE: &[u8] = include_bytes!("fixtures/infozip-korean-tree.zip"); |
| 6 | const ZIPCRYPTO_ASCII_TREE: &[u8] = include_bytes!("fixtures/infozip-zipcrypto-ascii-tree.zip"); |
| 7 | const ZIPCRYPTO_KOREAN_TREE: &[u8] = include_bytes!("fixtures/infozip-zipcrypto-korean-tree.zip"); |
| 8 | const NOTE: &[u8] = b"Zipelo encrypted interoperability\n"; |
| 9 | |
| 10 | fn crc(data: &[u8]) -> u32 { |
| 11 | let mut crc = !0u32; |
| 12 | for byte in data { |
| 13 | crc ^= *byte as u32; |
| 14 | for _ in 0..8 { |
| 15 | crc = if crc & 1 != 0 { |
| 16 | (crc >> 1) ^ 0xedb8_8320 |
| 17 | } else { |
| 18 | crc >> 1 |
| 19 | }; |
| 20 | } |
| 21 | } |
| 22 | !crc |
| 23 | } |
| 24 | |
| 25 | /// Info-ZIP Unicode Path 확장 필드(0x7075)를 만듦. |
| 26 | fn unicode_path(raw: &[u8], name: &str) -> Vec<u8> { |
| 27 | let mut value = vec![1]; |
| 28 | value.extend(crc(raw).to_le_bytes()); |
| 29 | value.extend(name.as_bytes()); |
| 30 | let mut extra = 0x7075u16.to_le_bytes().to_vec(); |
| 31 | extra.extend((value.len() as u16).to_le_bytes()); |
| 32 | extra.extend(value); |
| 33 | extra |
| 34 | } |
| 35 | |
| 36 | /// 저장 방식 항목 하나짜리 ZIP을 만듦. |
| 37 | fn single_entry_zip(raw: &[u8], flags: u16, extra: &[u8], data: &[u8]) -> Vec<u8> { |
| 38 | stored_entries_zip(&[(raw, flags, extra, data)]) |
| 39 | } |
| 40 | |
| 41 | /// 확장 필드를 로컬과 중앙 헤더에 똑같이 기록함. |
| 42 | fn stored_entries_zip(entries: &[(&[u8], u16, &[u8], &[u8])]) -> Vec<u8> { |
| 43 | let mut output = Vec::new(); |
| 44 | let mut central = Vec::new(); |
| 45 | for &(raw, flags, extra, data) in entries { |
| 46 | let offset = output.len() as u32; |
| 47 | let checksum = crc(data); |
| 48 | output.extend(b"PK\x03\x04"); |
| 49 | for value in [20u16, flags, 0, 0, 33] { |
| 50 | output.extend(value.to_le_bytes()); |
| 51 | } |
| 52 | for value in [checksum, data.len() as u32, data.len() as u32] { |
| 53 | output.extend(value.to_le_bytes()); |
| 54 | } |
| 55 | output.extend((raw.len() as u16).to_le_bytes()); |
| 56 | output.extend((extra.len() as u16).to_le_bytes()); |
| 57 | output.extend(raw); |
| 58 | output.extend(extra); |
| 59 | output.extend(data); |
| 60 | central.extend(b"PK\x01\x02"); |
| 61 | for value in [0x0314u16, 20, flags, 0, 0, 33] { |
| 62 | central.extend(value.to_le_bytes()); |
| 63 | } |
| 64 | for value in [checksum, data.len() as u32, data.len() as u32] { |
| 65 | central.extend(value.to_le_bytes()); |
| 66 | } |
| 67 | central.extend((raw.len() as u16).to_le_bytes()); |
| 68 | for value in [extra.len() as u16, 0, 0, 0] { |
| 69 | central.extend(value.to_le_bytes()); |
| 70 | } |
| 71 | central.extend((0o100644u32 << 16).to_le_bytes()); |
| 72 | central.extend(offset.to_le_bytes()); |
| 73 | central.extend(raw); |
| 74 | central.extend(extra); |
| 75 | } |
| 76 | let central_at = output.len() as u32; |
| 77 | output.extend(¢ral); |
| 78 | output.extend(b"PK\x05\x06"); |
| 79 | for value in [0u16, 0, entries.len() as u16, entries.len() as u16] { |
| 80 | output.extend(value.to_le_bytes()); |
| 81 | } |
| 82 | output.extend((central.len() as u32).to_le_bytes()); |
| 83 | output.extend(central_at.to_le_bytes()); |
| 84 | output.extend(0u16.to_le_bytes()); |
| 85 | output |
| 86 | } |
| 87 | |
| 88 | fn listed_names(bytes: &[u8]) -> std::result::Result<Vec<String>, ErrorCode> { |
| 89 | let s = Scratch::new(); |
| 90 | fs::write(s.0.join("input"), bytes).unwrap(); |
| 91 | let events = execute(Start { |
| 92 | action: Action::List { |
| 93 | archive: s.path("input"), |
| 94 | }, |
| 95 | limits: Limits::default(), |
| 96 | }); |
| 97 | let result = terminal(&events); |
| 98 | if result.outcome != Outcome::Completed { |
| 99 | return Err(result.error); |
| 100 | } |
| 101 | Ok(events |
| 102 | .iter() |
| 103 | .filter_map(|e| match e { |
| 104 | Message::Entry(entry) => Some(entry.name.clone()), |
| 105 | _ => None, |
| 106 | }) |
| 107 | .collect()) |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn infozip_utf8_names_without_flag_list_and_extract() { |
| 112 | assert_eq!(listed_names(KOREAN).unwrap(), ["README.md", "회의록.txt"]); |
| 113 | assert_eq!( |
| 114 | listed_names(KOREAN_TREE).unwrap(), |
| 115 | ["README.md", "회의록.txt", "폴더", "폴더/메모.txt"] |
| 116 | ); |
| 117 | for bytes in [KOREAN, KOREAN_TREE] { |
| 118 | let s = Scratch::new(); |
| 119 | let events = extract(&s, bytes, None, Selection::All, None, Limits::default()); |
| 120 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 121 | assert_eq!(fs::read(s.0.join("output/회의록.txt")).unwrap(), b"hi\n"); |
| 122 | assert_eq!(fs::read(s.0.join("output/README.md")).unwrap(), b"x\n"); |
| 123 | } |
| 124 | let s = Scratch::new(); |
| 125 | let events = extract( |
| 126 | &s, |
| 127 | KOREAN_TREE, |
| 128 | None, |
| 129 | Selection::All, |
| 130 | None, |
| 131 | Limits::default(), |
| 132 | ); |
| 133 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 134 | assert_eq!(fs::read(s.0.join("output/폴더/메모.txt")).unwrap(), NOTE); |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn infozip_multi_entry_zipcrypto_trees_list_and_extract() { |
| 139 | for (bytes, file) in [ |
| 140 | (ZIPCRYPTO_ASCII_TREE, "docs/notes.txt"), |
| 141 | (ZIPCRYPTO_KOREAN_TREE, "폴더/메모.txt"), |
| 142 | ] { |
| 143 | let names = listed_names(bytes).unwrap(); |
| 144 | assert!(names.iter().any(|n| n == file), "{names:?}"); |
| 145 | let s = Scratch::new(); |
| 146 | let events = interactive(encrypted_start(&s, bytes), |id, _, purpose| { |
| 147 | assert_eq!(purpose, PasswordPurpose::Decrypt); |
| 148 | password_reply(id, TEST_PASSWORD) |
| 149 | }); |
| 150 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 151 | assert_eq!(fs::read(s.0.join("output").join(file)).unwrap(), NOTE); |
| 152 | assert_eq!(fs::read(s.0.join("output/README.md")).unwrap(), b"x\n"); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn unicode_path_extra_is_used_only_when_its_crc_matches() { |
| 158 | // CP949 "회의록.txt": UTF-8이 아니므로 확장 필드 없이는 지원하지 않음. |
| 159 | let cp949 = b"\xc8\xb8\xc0\xc7\xb7\xcf.txt"; |
| 160 | let extra = unicode_path(cp949, "회의록.txt"); |
| 161 | assert_eq!( |
| 162 | listed_names(&single_entry_zip(cp949, 0, &extra, b"hi\n")).unwrap(), |
| 163 | ["회의록.txt"] |
| 164 | ); |
| 165 | let s = Scratch::new(); |
| 166 | let bytes = single_entry_zip(cp949, 0, &extra, b"hi\n"); |
| 167 | let events = extract(&s, &bytes, None, Selection::All, None, Limits::default()); |
| 168 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 169 | assert_eq!(fs::read(s.0.join("output/회의록.txt")).unwrap(), b"hi\n"); |
| 170 | |
| 171 | // CRC가 다른 확장 필드는 이름이 바뀐 뒤 남은 낡은 값이므로 무시함. |
| 172 | let stale = unicode_path(b"old.txt", "회의록.txt"); |
| 173 | assert_eq!( |
| 174 | listed_names(&single_entry_zip(b"new.txt", 0, &stale, b"hi\n")).unwrap(), |
| 175 | ["new.txt"] |
| 176 | ); |
| 177 | let stale = unicode_path(b"other", "회의록.txt"); |
| 178 | assert_eq!( |
| 179 | listed_names(&single_entry_zip(cp949, 0, &stale, b"hi\n")), |
| 180 | Err(ErrorCode::UnsupportedFeature) |
| 181 | ); |
| 182 | // UTF-8 플래그가 있으면 이름 자체가 UTF-8이므로 확장 필드를 보지 않음. |
| 183 | let ignored = unicode_path("a.txt".as_bytes(), "b.txt"); |
| 184 | assert_eq!( |
| 185 | listed_names(&single_entry_zip(b"a.txt", 0x0800, &ignored, b"hi\n")).unwrap(), |
| 186 | ["a.txt"] |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | fn assert_rejected_without_output(bytes: &[u8], error: ErrorCode) { |
| 191 | assert_eq!(listed_names(bytes), Err(error)); |
| 192 | let s = Scratch::new(); |
| 193 | let events = extract(&s, bytes, None, Selection::All, None, Limits::default()); |
| 194 | let result = terminal(&events); |
| 195 | assert_eq!(result.outcome, Outcome::Failed, "{events:?}"); |
| 196 | assert_eq!(result.error, error, "{events:?}"); |
| 197 | assert_eq!(result.publication, Publication::None); |
| 198 | assert_eq!(result.created_count, 0); |
| 199 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn duplicate_unicode_path_extras_are_rejected_before_writes() { |
| 204 | let raw = b"safe.txt"; |
| 205 | for second in ["회의록.txt", "다른.txt"] { |
| 206 | let extra = [unicode_path(raw, "회의록.txt"), unicode_path(raw, second)].concat(); |
| 207 | let bytes = single_entry_zip(raw, 0, &extra, b"hi\n"); |
| 208 | assert_rejected_without_output(&bytes, ErrorCode::Integrity); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | #[test] |
| 213 | fn converted_names_colliding_with_other_paths_are_rejected_before_writes() { |
| 214 | let first = b"first.txt"; |
| 215 | let second = b"second.txt"; |
| 216 | let original = stored_entries_zip(&[(first, 0, &[], b"first"), (second, 0, &[], b"second")]); |
| 217 | assert_eq!( |
| 218 | listed_names(&original).unwrap(), |
| 219 | ["first.txt", "second.txt"] |
| 220 | ); |
| 221 | for (first_name, second_name) in [ |
| 222 | ("회의록.txt", "회의록.txt"), |
| 223 | ("폴더/Memo.txt", "폴더/memo.txt"), |
| 224 | ("폴더", "폴더/메모.txt"), |
| 225 | ] { |
| 226 | let first_extra = unicode_path(first, first_name); |
| 227 | let second_extra = unicode_path(second, second_name); |
| 228 | for (raw, extra, name) in [ |
| 229 | (first.as_slice(), &first_extra, first_name), |
| 230 | (second.as_slice(), &second_extra, second_name), |
| 231 | ] { |
| 232 | let bytes = single_entry_zip(raw, 0, extra, b"hi\n"); |
| 233 | assert_eq!(listed_names(&bytes).unwrap(), [name]); |
| 234 | } |
| 235 | let bytes = stored_entries_zip(&[ |
| 236 | (first, 0, &first_extra, b"first"), |
| 237 | (second, 0, &second_extra, b"second"), |
| 238 | ]); |
| 239 | assert_rejected_without_output(&bytes, ErrorCode::Conflict); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | #[test] |
| 244 | fn legacy_and_hostile_names_stay_rejected() { |
| 245 | let cp949 = b"\xc8\xb8\xc0\xc7\xb7\xcf.txt"; |
| 246 | // 코드 페이지를 추측하지 않음. |
| 247 | assert_eq!( |
| 248 | listed_names(&single_entry_zip(cp949, 0, &[], b"hi\n")), |
| 249 | Err(ErrorCode::UnsupportedFeature) |
| 250 | ); |
| 251 | // UTF-8 플래그와 맞지 않는 이름은 손상으로 봄. |
| 252 | assert_eq!( |
| 253 | listed_names(&single_entry_zip(cp949, 0x0800, &[], b"hi\n")), |
| 254 | Err(ErrorCode::Integrity) |
| 255 | ); |
| 256 | // 확장 필드로 들어온 이름도 같은 경로 검사를 거침. |
| 257 | for hostile in ["../escape.txt", "/absolute.txt", "C:drive.txt", "CON.txt"] { |
| 258 | let extra = unicode_path(b"safe.txt", hostile); |
| 259 | assert_eq!( |
| 260 | listed_names(&single_entry_zip(b"safe.txt", 0, &extra, b"hi\n")), |
| 261 | Err(ErrorCode::UnsafePath), |
| 262 | "{hostile}" |
| 263 | ); |
| 264 | } |
| 265 | for hostile in ["../회의록.txt", "폴더/../../x"] { |
| 266 | assert_eq!( |
| 267 | listed_names(&single_entry_zip(hostile.as_bytes(), 0, &[], b"hi\n")), |
| 268 | Err(ErrorCode::UnsafePath), |
| 269 | "{hostile}" |
| 270 | ); |
| 271 | } |
| 272 | // 확장 필드 이름이 UTF-8이 아니거나 형식이 다르면 손상으로 봄. |
| 273 | let mut broken = unicode_path(b"safe.txt", "x"); |
| 274 | let len = broken.len(); |
| 275 | broken[len - 1] = 0xff; |
| 276 | assert_eq!( |
| 277 | listed_names(&single_entry_zip(b"safe.txt", 0, &broken, b"hi\n")), |
| 278 | Err(ErrorCode::Integrity) |
| 279 | ); |
| 280 | let mut short = 0x7075u16.to_le_bytes().to_vec(); |
| 281 | short.extend([4, 0, 1, 0, 0, 0]); |
| 282 | assert_eq!( |
| 283 | listed_names(&single_entry_zip(b"safe.txt", 0, &short, b"hi\n")), |
| 284 | Err(ErrorCode::Integrity) |
| 285 | ); |
| 286 | // APPNOTE 4.6.9에 따라 모르는 버전의 확장 필드는 무시함. |
| 287 | let mut version = unicode_path(b"safe.txt", "x"); |
| 288 | version[4] = 2; |
| 289 | assert_eq!( |
| 290 | listed_names(&single_entry_zip(b"safe.txt", 0, &version, b"hi\n")).unwrap(), |
| 291 | ["safe.txt"] |
| 292 | ); |
| 293 | } |