| 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 | let checksum = crc(data); |
| 39 | let mut output = b"PK\x03\x04".to_vec(); |
| 40 | for value in [20u16, flags, 0, 0, 33] { |
| 41 | output.extend(value.to_le_bytes()); |
| 42 | } |
| 43 | for value in [checksum, data.len() as u32, data.len() as u32] { |
| 44 | output.extend(value.to_le_bytes()); |
| 45 | } |
| 46 | output.extend((raw.len() as u16).to_le_bytes()); |
| 47 | output.extend((extra.len() as u16).to_le_bytes()); |
| 48 | output.extend(raw); |
| 49 | output.extend(extra); |
| 50 | output.extend(data); |
| 51 | let central_at = output.len() as u32; |
| 52 | let mut central = b"PK\x01\x02".to_vec(); |
| 53 | for value in [0x0314u16, 20, flags, 0, 0, 33] { |
| 54 | central.extend(value.to_le_bytes()); |
| 55 | } |
| 56 | for value in [checksum, data.len() as u32, data.len() as u32] { |
| 57 | central.extend(value.to_le_bytes()); |
| 58 | } |
| 59 | central.extend((raw.len() as u16).to_le_bytes()); |
| 60 | for value in [extra.len() as u16, 0, 0, 0] { |
| 61 | central.extend(value.to_le_bytes()); |
| 62 | } |
| 63 | central.extend((0o100644u32 << 16).to_le_bytes()); |
| 64 | central.extend(0u32.to_le_bytes()); |
| 65 | central.extend(raw); |
| 66 | central.extend(extra); |
| 67 | output.extend(¢ral); |
| 68 | output.extend(b"PK\x05\x06"); |
| 69 | for value in [0u16, 0, 1, 1] { |
| 70 | output.extend(value.to_le_bytes()); |
| 71 | } |
| 72 | output.extend((central.len() as u32).to_le_bytes()); |
| 73 | output.extend(central_at.to_le_bytes()); |
| 74 | output.extend(0u16.to_le_bytes()); |
| 75 | output |
| 76 | } |
| 77 | |
| 78 | fn listed_names(bytes: &[u8]) -> std::result::Result<Vec<String>, ErrorCode> { |
| 79 | let s = Scratch::new(); |
| 80 | fs::write(s.0.join("input"), bytes).unwrap(); |
| 81 | let events = execute(Start { |
| 82 | action: Action::List { |
| 83 | archive: s.path("input"), |
| 84 | }, |
| 85 | limits: Limits::default(), |
| 86 | }); |
| 87 | let result = terminal(&events); |
| 88 | if result.outcome != Outcome::Completed { |
| 89 | return Err(result.error); |
| 90 | } |
| 91 | Ok(events |
| 92 | .iter() |
| 93 | .filter_map(|e| match e { |
| 94 | Message::Entry(entry) => Some(entry.name.clone()), |
| 95 | _ => None, |
| 96 | }) |
| 97 | .collect()) |
| 98 | } |
| 99 | |
| 100 | /// 스트리밍 목록과 메모리 파서가 같은 이름 또는 같은 오류를 내는지 확인함. |
| 101 | fn names_agree(bytes: &[u8]) -> std::result::Result<Vec<String>, ErrorCode> { |
| 102 | let streamed = listed_names(bytes); |
| 103 | let parsed = archive::parse(bytes, Limits::default(), false, &mut || Ok(())) |
| 104 | .map(|c| c.members.into_iter().map(|m| m.entry.name).collect()); |
| 105 | assert_eq!(streamed, parsed); |
| 106 | streamed |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn infozip_utf8_names_without_flag_list_and_extract() { |
| 111 | assert_eq!(names_agree(KOREAN).unwrap(), ["README.md", "회의록.txt"]); |
| 112 | assert_eq!( |
| 113 | names_agree(KOREAN_TREE).unwrap(), |
| 114 | ["README.md", "회의록.txt", "폴더", "폴더/메모.txt"] |
| 115 | ); |
| 116 | for bytes in [KOREAN, KOREAN_TREE] { |
| 117 | let s = Scratch::new(); |
| 118 | let events = extract(&s, bytes, None, Selection::All, None, Limits::default()); |
| 119 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 120 | assert_eq!(fs::read(s.0.join("output/회의록.txt")).unwrap(), b"hi\n"); |
| 121 | assert_eq!(fs::read(s.0.join("output/README.md")).unwrap(), b"x\n"); |
| 122 | } |
| 123 | let s = Scratch::new(); |
| 124 | let events = extract( |
| 125 | &s, |
| 126 | KOREAN_TREE, |
| 127 | None, |
| 128 | Selection::All, |
| 129 | None, |
| 130 | Limits::default(), |
| 131 | ); |
| 132 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 133 | assert_eq!(fs::read(s.0.join("output/폴더/메모.txt")).unwrap(), NOTE); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn infozip_multi_entry_zipcrypto_trees_list_and_extract() { |
| 138 | for (bytes, file) in [ |
| 139 | (ZIPCRYPTO_ASCII_TREE, "docs/notes.txt"), |
| 140 | (ZIPCRYPTO_KOREAN_TREE, "폴더/메모.txt"), |
| 141 | ] { |
| 142 | let names = names_agree(bytes).unwrap(); |
| 143 | assert!(names.iter().any(|n| n == file), "{names:?}"); |
| 144 | let s = Scratch::new(); |
| 145 | let events = interactive(encrypted_start(&s, bytes), |id, _, purpose| { |
| 146 | assert_eq!(purpose, PasswordPurpose::Decrypt); |
| 147 | password_reply(id, TEST_PASSWORD) |
| 148 | }); |
| 149 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 150 | assert_eq!(fs::read(s.0.join("output").join(file)).unwrap(), NOTE); |
| 151 | assert_eq!(fs::read(s.0.join("output/README.md")).unwrap(), b"x\n"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn unicode_path_extra_is_used_only_when_its_crc_matches() { |
| 157 | // CP949 "회의록.txt": UTF-8이 아니므로 확장 필드 없이는 지원하지 않음. |
| 158 | let cp949 = b"\xc8\xb8\xc0\xc7\xb7\xcf.txt"; |
| 159 | let extra = unicode_path(cp949, "회의록.txt"); |
| 160 | assert_eq!( |
| 161 | names_agree(&single_entry_zip(cp949, 0, &extra, b"hi\n")).unwrap(), |
| 162 | ["회의록.txt"] |
| 163 | ); |
| 164 | let s = Scratch::new(); |
| 165 | let bytes = single_entry_zip(cp949, 0, &extra, b"hi\n"); |
| 166 | let events = extract(&s, &bytes, None, Selection::All, None, Limits::default()); |
| 167 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 168 | assert_eq!(fs::read(s.0.join("output/회의록.txt")).unwrap(), b"hi\n"); |
| 169 | |
| 170 | // CRC가 다른 확장 필드는 이름이 바뀐 뒤 남은 낡은 값이므로 무시함. |
| 171 | let stale = unicode_path(b"old.txt", "회의록.txt"); |
| 172 | assert_eq!( |
| 173 | names_agree(&single_entry_zip(b"new.txt", 0, &stale, b"hi\n")).unwrap(), |
| 174 | ["new.txt"] |
| 175 | ); |
| 176 | let stale = unicode_path(b"other", "회의록.txt"); |
| 177 | assert_eq!( |
| 178 | names_agree(&single_entry_zip(cp949, 0, &stale, b"hi\n")), |
| 179 | Err(ErrorCode::UnsupportedFeature) |
| 180 | ); |
| 181 | // UTF-8 플래그가 있으면 이름 자체가 UTF-8이므로 확장 필드를 보지 않음. |
| 182 | let ignored = unicode_path("a.txt".as_bytes(), "b.txt"); |
| 183 | assert_eq!( |
| 184 | names_agree(&single_entry_zip(b"a.txt", 0x0800, &ignored, b"hi\n")).unwrap(), |
| 185 | ["a.txt"] |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | #[test] |
| 190 | fn legacy_and_hostile_names_stay_rejected() { |
| 191 | let cp949 = b"\xc8\xb8\xc0\xc7\xb7\xcf.txt"; |
| 192 | // 코드 페이지를 추측하지 않음. |
| 193 | assert_eq!( |
| 194 | names_agree(&single_entry_zip(cp949, 0, &[], b"hi\n")), |
| 195 | Err(ErrorCode::UnsupportedFeature) |
| 196 | ); |
| 197 | // UTF-8 플래그와 맞지 않는 이름은 손상으로 봄. |
| 198 | assert_eq!( |
| 199 | names_agree(&single_entry_zip(cp949, 0x0800, &[], b"hi\n")), |
| 200 | Err(ErrorCode::Integrity) |
| 201 | ); |
| 202 | // 확장 필드로 들어온 이름도 같은 경로 검사를 거침. |
| 203 | for hostile in ["../escape.txt", "/absolute.txt", "C:drive.txt", "CON.txt"] { |
| 204 | let extra = unicode_path(b"safe.txt", hostile); |
| 205 | assert_eq!( |
| 206 | names_agree(&single_entry_zip(b"safe.txt", 0, &extra, b"hi\n")), |
| 207 | Err(ErrorCode::UnsafePath), |
| 208 | "{hostile}" |
| 209 | ); |
| 210 | } |
| 211 | for hostile in ["../회의록.txt", "폴더/../../x"] { |
| 212 | assert_eq!( |
| 213 | names_agree(&single_entry_zip(hostile.as_bytes(), 0, &[], b"hi\n")), |
| 214 | Err(ErrorCode::UnsafePath), |
| 215 | "{hostile}" |
| 216 | ); |
| 217 | } |
| 218 | // 확장 필드 이름이 UTF-8이 아니거나 형식이 다르면 손상으로 봄. |
| 219 | let mut broken = unicode_path(b"safe.txt", "x"); |
| 220 | let len = broken.len(); |
| 221 | broken[len - 1] = 0xff; |
| 222 | assert_eq!( |
| 223 | names_agree(&single_entry_zip(b"safe.txt", 0, &broken, b"hi\n")), |
| 224 | Err(ErrorCode::Integrity) |
| 225 | ); |
| 226 | let mut short = 0x7075u16.to_le_bytes().to_vec(); |
| 227 | short.extend([4, 0, 1, 0, 0, 0]); |
| 228 | assert_eq!( |
| 229 | names_agree(&single_entry_zip(b"safe.txt", 0, &short, b"hi\n")), |
| 230 | Err(ErrorCode::Integrity) |
| 231 | ); |
| 232 | // APPNOTE 4.6.9에 따라 모르는 버전의 확장 필드는 무시함. |
| 233 | let mut version = unicode_path(b"safe.txt", "x"); |
| 234 | version[4] = 2; |
| 235 | assert_eq!( |
| 236 | names_agree(&single_entry_zip(b"safe.txt", 0, &version, b"hi\n")).unwrap(), |
| 237 | ["safe.txt"] |
| 238 | ); |
| 239 | } |