Merge pull request #146 from Team-LJY/fix/worker-real-path-tests
작업자 시험을 실제 경로로 옮기고 ZIP 확장 해석 통합
- 작성자
- LeeJunyoung
- 작성 시각
- 커밋
463b0dae6844f8ba89adfe0d70c27f144bdf3a3d
변경된 파일 20개
+686-805| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | use crate::protocol::*; |
| 2 | 2 | use std::collections::{BTreeMap, BTreeSet}; |
| 3 | 삭제: use zlib_rs::{Deflate, DeflateFlush, Inflate, InflateFlush, Status, crc32::crc32}; | |
| 3 | 추가: use zlib_rs::{Deflate, DeflateFlush, Status, crc32::crc32}; | |
| 4 | 4 | |
| 5 | 5 | type Result<T> = std::result::Result<T, ErrorCode>; |
| 6 | 6 | pub const MAX_PATH_DEPTH: usize = 64; |
| @@ -56,9 +56,6 @@ impl MetadataBudget { | ||
| 56 | 56 | pub(crate) fn charge_bytes(&mut self, bytes: usize, limits: MetadataLimits) -> Result<()> { |
| 57 | 57 | self.charge_cost(bytes, 0, limits) |
| 58 | 58 | } |
| 59 | 삭제: pub(crate) fn charge(&mut self, name: &str) -> Result<()> { | |
| 60 | 삭제: self.charge_with(name, MetadataLimits::default()) | |
| 61 | 삭제: } | |
| 62 | 59 | pub(crate) fn charge_with(&mut self, name: &str, limits: MetadataLimits) -> Result<()> { |
| 63 | 60 | // 이름 복제와 인덱스 노드 비용을 할당 전에 계산함. |
| 64 | 61 | let cost = name |
| @@ -90,17 +87,6 @@ pub struct Catalog { | ||
| 90 | 87 | pub format: Format, |
| 91 | 88 | pub members: Vec<Member>, |
| 92 | 89 | } |
| 93 | 삭제: fn part(b: &[u8], at: usize, n: usize) -> Result<&[u8]> { | |
| 94 | 삭제: b.get(at..at.checked_add(n).ok_or(ErrorCode::Integrity)?) | |
| 95 | 삭제: .ok_or(ErrorCode::Integrity) | |
| 96 | 삭제: } | |
| 97 | 삭제: fn u16at(b: &[u8], at: usize) -> Result<u16> { | |
| 98 | 삭제: Ok(u16::from_le_bytes(part(b, at, 2)?.try_into().unwrap())) | |
| 99 | 삭제: } | |
| 100 | 삭제: fn u32at(b: &[u8], at: usize) -> Result<u32> { | |
| 101 | 삭제: Ok(u32::from_le_bytes(part(b, at, 4)?.try_into().unwrap())) | |
| 102 | 삭제: } | |
| 103 | 삭제: | |
| 104 | 90 | pub fn valid_name(name: &str, directory: bool) -> Result<String> { |
| 105 | 91 | let name = if directory { |
| 106 | 92 | name.strip_suffix('/').unwrap_or(name) |
| @@ -218,61 +204,6 @@ fn bounded_append(output: &mut Vec<u8>, data: &[u8], limit: u64) -> Result<()> { | ||
| 218 | 204 | output.extend_from_slice(data); |
| 219 | 205 | Ok(()) |
| 220 | 206 | } |
| 221 | 삭제: fn inflate( | |
| 222 | 삭제: input: &[u8], | |
| 223 | 삭제: gzip: bool, | |
| 224 | 삭제: limit: u64, | |
| 225 | 삭제: check: &mut impl crate::resources::Monitor, | |
| 226 | 삭제: ) -> Result<(Vec<u8>, usize)> { | |
| 227 | 삭제: let mut stream = Inflate::new(gzip, if gzip { 31 } else { 15 }); | |
| 228 | 삭제: let mut out = Vec::new(); | |
| 229 | 삭제: let mut buffer = [0; 65536]; | |
| 230 | 삭제: loop { | |
| 231 | 삭제: check.check()?; | |
| 232 | 삭제: let before_in = stream.total_in(); | |
| 233 | 삭제: let before_out = stream.total_out(); | |
| 234 | 삭제: let status = stream | |
| 235 | 삭제: .decompress( | |
| 236 | 삭제: &input[before_in as usize..], | |
| 237 | 삭제: &mut buffer, | |
| 238 | 삭제: InflateFlush::NoFlush, | |
| 239 | 삭제: ) | |
| 240 | 삭제: .map_err(|_| ErrorCode::Integrity)?; | |
| 241 | 삭제: let written = (stream.total_out() - before_out) as usize; | |
| 242 | 삭제: bounded_append(&mut out, &buffer[..written], limit)?; | |
| 243 | 삭제: if status == Status::StreamEnd { | |
| 244 | 삭제: return Ok((out, stream.total_in() as usize)); | |
| 245 | 삭제: } | |
| 246 | 삭제: if stream.total_in() == before_in && stream.total_out() == before_out { | |
| 247 | 삭제: return Err(ErrorCode::Integrity); | |
| 248 | 삭제: } | |
| 249 | 삭제: } | |
| 250 | 삭제: } | |
| 251 | 삭제: fn gzip( | |
| 252 | 삭제: input: &[u8], | |
| 253 | 삭제: limits: Limits, | |
| 254 | 삭제: check: &mut impl crate::resources::Monitor, | |
| 255 | 삭제: ) -> Result<Vec<u8>> { | |
| 256 | 삭제: let mut at = 0; | |
| 257 | 삭제: let mut result = Vec::new(); | |
| 258 | 삭제: while at < input.len() { | |
| 259 | 삭제: if !input[at..].starts_with(&[31, 139, 8]) { | |
| 260 | 삭제: return Err(ErrorCode::Integrity); | |
| 261 | 삭제: } | |
| 262 | 삭제: let (out, used) = inflate( | |
| 263 | 삭제: &input[at..], | |
| 264 | 삭제: true, | |
| 265 | 삭제: limits.output - result.len() as u64, | |
| 266 | 삭제: check, | |
| 267 | 삭제: )?; | |
| 268 | 삭제: if used == 0 { | |
| 269 | 삭제: return Err(ErrorCode::Integrity); | |
| 270 | 삭제: } | |
| 271 | 삭제: bounded_append(&mut result, &out, limits.output)?; | |
| 272 | 삭제: at += used; | |
| 273 | 삭제: } | |
| 274 | 삭제: Ok(result) | |
| 275 | 삭제: } | |
| 276 | 207 | pub(crate) fn tar_string(b: &[u8]) -> Result<String> { |
| 277 | 208 | let n = b.iter().position(|v| *v == 0).unwrap_or(b.len()); |
| 278 | 209 | if b[n..].iter().any(|v| *v != 0) { |
| @@ -294,128 +225,6 @@ pub(crate) fn octal(b: &[u8]) -> Result<u64> { | ||
| 294 | 225 | } |
| 295 | 226 | u64::from_str_radix(text, 8).map_err(|_| ErrorCode::Integrity) |
| 296 | 227 | } |
| 297 | 삭제: fn tar( | |
| 298 | 삭제: input: &[u8], | |
| 299 | 삭제: limits: Limits, | |
| 300 | 삭제: decode: bool, | |
| 301 | 삭제: check: &mut impl crate::resources::Monitor, | |
| 302 | 삭제: ) -> Result<Vec<Member>> { | |
| 303 | 삭제: let mut at = 0; | |
| 304 | 삭제: let mut members = Vec::new(); | |
| 305 | 삭제: let mut total = 0u64; | |
| 306 | 삭제: let mut metadata = MetadataBudget::default(); | |
| 307 | 삭제: loop { | |
| 308 | 삭제: check.check()?; | |
| 309 | 삭제: let header = part(input, at, 512)?; | |
| 310 | 삭제: if header.iter().all(|b| *b == 0) { | |
| 311 | 삭제: if part(input, at + 512, 512)?.iter().any(|b| *b != 0) | |
| 312 | 삭제: || input[at..].iter().any(|b| *b != 0) | |
| 313 | 삭제: || input.len() % 512 != 0 | |
| 314 | 삭제: { | |
| 315 | 삭제: return Err(ErrorCode::Integrity); | |
| 316 | 삭제: } | |
| 317 | 삭제: break; | |
| 318 | 삭제: } | |
| 319 | 삭제: if members.len() >= limits.entries as usize { | |
| 320 | 삭제: return Err(ErrorCode::Limit); | |
| 321 | 삭제: } | |
| 322 | 삭제: let expected = octal(&header[148..156])?; | |
| 323 | 삭제: let checksum = header | |
| 324 | 삭제: .iter() | |
| 325 | 삭제: .enumerate() | |
| 326 | 삭제: .map(|(i, b)| { | |
| 327 | 삭제: if (148..156).contains(&i) { | |
| 328 | 삭제: 32u64 | |
| 329 | 삭제: } else { | |
| 330 | 삭제: *b as u64 | |
| 331 | 삭제: } | |
| 332 | 삭제: }) | |
| 333 | 삭제: .sum::<u64>(); | |
| 334 | 삭제: if checksum != expected { | |
| 335 | 삭제: return Err(ErrorCode::Integrity); | |
| 336 | 삭제: } | |
| 337 | 삭제: let mut name = tar_string(&header[..100])?; | |
| 338 | 삭제: if &header[257..263] == b"ustar\0" || &header[257..263] == b"ustar " { | |
| 339 | 삭제: let prefix = tar_string(&header[345..500])?; | |
| 340 | 삭제: if !prefix.is_empty() { | |
| 341 | 삭제: name = format!("{prefix}/{name}"); | |
| 342 | 삭제: } | |
| 343 | 삭제: } else if header[257..263].iter().any(|b| *b != 0) { | |
| 344 | 삭제: return Err(ErrorCode::UnsupportedFeature); | |
| 345 | 삭제: } | |
| 346 | 삭제: let kind = match header[156] { | |
| 347 | 삭제: 0 | b'0' => EntryKind::File, | |
| 348 | 삭제: b'5' => EntryKind::Directory, | |
| 349 | 삭제: b'1' | b'2' => EntryKind::Link, | |
| 350 | 삭제: b'x' | b'g' | b'L' | b'K' => return Err(ErrorCode::UnsupportedFeature), | |
| 351 | 삭제: _ => EntryKind::Other, | |
| 352 | 삭제: }; | |
| 353 | 삭제: let size = octal(&header[124..136])?; | |
| 354 | 삭제: if kind != EntryKind::File && size != 0 { | |
| 355 | 삭제: return Err(ErrorCode::Integrity); | |
| 356 | 삭제: } | |
| 357 | 삭제: total = total | |
| 358 | 삭제: .checked_add(size) | |
| 359 | 삭제: .filter(|n| *n <= limits.output) | |
| 360 | 삭제: .ok_or(ErrorCode::Limit)?; | |
| 361 | 삭제: let size = usize::try_from(size).map_err(|_| ErrorCode::Limit)?; | |
| 362 | 삭제: let bytes = part(input, at + 512, size)?; | |
| 363 | 삭제: metadata.charge(&name)?; | |
| 364 | 삭제: let name = valid_name(&name, kind == EntryKind::Directory)?; | |
| 365 | 삭제: let data = if decode { bytes.to_vec() } else { Vec::new() }; | |
| 366 | 삭제: members.push(Member { | |
| 367 | 삭제: entry: Entry { | |
| 368 | 삭제: entry_id: members.len() as u32, | |
| 369 | 삭제: name, | |
| 370 | 삭제: kind, | |
| 371 | 삭제: size: size as u64, | |
| 372 | 삭제: compressed_size: size as u64, | |
| 373 | 삭제: encrypted: false, | |
| 374 | 삭제: }, | |
| 375 | 삭제: data, | |
| 376 | 삭제: }); | |
| 377 | 삭제: at = at | |
| 378 | 삭제: .checked_add(512) | |
| 379 | 삭제: .and_then(|n| n.checked_add(size.div_ceil(512) * 512)) | |
| 380 | 삭제: .ok_or(ErrorCode::Integrity)?; | |
| 381 | 삭제: } | |
| 382 | 삭제: validate_members(&members)?; | |
| 383 | 삭제: Ok(members) | |
| 384 | 삭제: } | |
| 385 | 삭제: #[derive(Clone, Copy, PartialEq, Eq)] | |
| 386 | 삭제: pub(crate) struct AesExtra { | |
| 387 | 삭제: pub version: u16, | |
| 388 | 삭제: pub method: u16, | |
| 389 | 삭제: } | |
| 390 | 삭제: pub(crate) fn aes_extra(extra: &[u8]) -> Result<Option<AesExtra>> { | |
| 391 | 삭제: let mut at = 0; | |
| 392 | 삭제: let mut found = None; | |
| 393 | 삭제: while at < extra.len() { | |
| 394 | 삭제: let tag = u16at(extra, at)?; | |
| 395 | 삭제: let len = u16at(extra, at + 2)? as usize; | |
| 396 | 삭제: let value = part(extra, at + 4, len)?; | |
| 397 | 삭제: if tag == 1 { | |
| 398 | 삭제: return Err(ErrorCode::UnsupportedFeature); | |
| 399 | 삭제: } | |
| 400 | 삭제: if tag == 0x9901 { | |
| 401 | 삭제: if found.is_some() || len < 7 { | |
| 402 | 삭제: return Err(ErrorCode::Integrity); | |
| 403 | 삭제: } | |
| 404 | 삭제: let version = u16at(value, 0)?; | |
| 405 | 삭제: let method = u16at(value, 5)?; | |
| 406 | 삭제: if !matches!(version, 1 | 2) | |
| 407 | 삭제: || &value[2..4] != b"AE" | |
| 408 | 삭제: || value[4] != 3 | |
| 409 | 삭제: || !matches!(method, 0 | 8) | |
| 410 | 삭제: { | |
| 411 | 삭제: return Err(ErrorCode::UnsupportedFeature); | |
| 412 | 삭제: } | |
| 413 | 삭제: found = Some(AesExtra { version, method }); | |
| 414 | 삭제: } | |
| 415 | 삭제: at += 4 + len; | |
| 416 | 삭제: } | |
| 417 | 삭제: Ok(found) | |
| 418 | 삭제: } | |
| 419 | 228 | /// ZIP 항목 이름을 해석함. |
| 420 | 229 | /// |
| 421 | 230 | /// - UTF-8 플래그(비트 11)가 있으면 이름은 UTF-8이어야 하며 아니면 손상으로 봄. |
| @@ -431,20 +240,7 @@ pub(crate) fn zip_name(raw: &[u8], flags: u16, extra: &[u8]) -> Result<String> { | ||
| 431 | 240 | .map(str::to_owned) |
| 432 | 241 | .map_err(|_| ErrorCode::Integrity); |
| 433 | 242 | } |
| 434 | 삭제: let mut at = 0; | |
| 435 | 삭제: let mut unicode = None; | |
| 436 | 삭제: while at < extra.len() { | |
| 437 | 삭제: let tag = u16at(extra, at)?; | |
| 438 | 삭제: let len = u16at(extra, at + 2)? as usize; | |
| 439 | 삭제: let value = part(extra, at + 4, len)?; | |
| 440 | 삭제: if tag == 0x7075 { | |
| 441 | 삭제: if unicode.is_some() || len < 5 { | |
| 442 | 삭제: return Err(ErrorCode::Integrity); | |
| 443 | 삭제: } | |
| 444 | 삭제: unicode = Some(value); | |
| 445 | 삭제: } | |
| 446 | 삭제: at += 4 + len; | |
| 447 | 삭제: } | |
| 243 | 추가: let unicode = crate::zip_extra::parse(extra, crate::zip_extra::Policy::Names)?.unicode; | |
| 448 | 244 | if let Some(value) = unicode |
| 449 | 245 | && value[0] == 1 |
| 450 | 246 | && u32::from_le_bytes(value[1..5].try_into().unwrap()) == crc32(0, raw) |
| @@ -457,283 +253,6 @@ pub(crate) fn zip_name(raw: &[u8], flags: u16, extra: &[u8]) -> Result<String> { | ||
| 457 | 253 | .map(str::to_owned) |
| 458 | 254 | .map_err(|_| ErrorCode::UnsupportedFeature) |
| 459 | 255 | } |
| 460 | 삭제: fn zip( | |
| 461 | 삭제: input: &[u8], | |
| 462 | 삭제: limits: Limits, | |
| 463 | 삭제: decode: bool, | |
| 464 | 삭제: password: Option<&[u8]>, | |
| 465 | 삭제: check: &mut impl crate::resources::Monitor, | |
| 466 | 삭제: ) -> Result<Vec<Member>> { | |
| 467 | 삭제: if input.len() < 22 { | |
| 468 | 삭제: return Err(ErrorCode::Integrity); | |
| 469 | 삭제: } | |
| 470 | 삭제: let end = (input.len().saturating_sub(65557)..=input.len() - 22) | |
| 471 | 삭제: .rev() | |
| 472 | 삭제: .find(|&i| { | |
| 473 | 삭제: input.get(i..i + 4) == Some(b"PK\x05\x06") | |
| 474 | 삭제: && u16at(input, i + 20) | |
| 475 | 삭제: .ok() | |
| 476 | 삭제: .is_some_and(|n| i + 22 + n as usize == input.len()) | |
| 477 | 삭제: }) | |
| 478 | 삭제: .ok_or(ErrorCode::Integrity)?; | |
| 479 | 삭제: let count = u16at(input, end + 10)? as usize; | |
| 480 | 삭제: if u16at(input, end + 4)? != 0 | |
| 481 | 삭제: || u16at(input, end + 6)? != 0 | |
| 482 | 삭제: || count != u16at(input, end + 8)? as usize | |
| 483 | 삭제: { | |
| 484 | 삭제: return Err(ErrorCode::UnsupportedFeature); | |
| 485 | 삭제: } | |
| 486 | 삭제: if count == 65535 || u32at(input, end + 12)? == u32::MAX || u32at(input, end + 16)? == u32::MAX | |
| 487 | 삭제: { | |
| 488 | 삭제: return Err(ErrorCode::UnsupportedFeature); | |
| 489 | 삭제: } | |
| 490 | 삭제: if count > limits.entries as usize { | |
| 491 | 삭제: return Err(ErrorCode::Limit); | |
| 492 | 삭제: } | |
| 493 | 삭제: let central = u32at(input, end + 16)? as usize; | |
| 494 | 삭제: if central.checked_add(u32at(input, end + 12)? as usize) != Some(end) { | |
| 495 | 삭제: return Err(ErrorCode::Integrity); | |
| 496 | 삭제: } | |
| 497 | 삭제: let mut at = central; | |
| 498 | 삭제: let mut members = Vec::new(); | |
| 499 | 삭제: let mut spans = Vec::new(); | |
| 500 | 삭제: let mut total = 0u64; | |
| 501 | 삭제: let mut metadata = MetadataBudget::default(); | |
| 502 | 삭제: for id in 0..count { | |
| 503 | 삭제: check.check()?; | |
| 504 | 삭제: let h = part(input, at, 46)?; | |
| 505 | 삭제: if &h[..4] != b"PK\x01\x02" { | |
| 506 | 삭제: return Err(ErrorCode::Integrity); | |
| 507 | 삭제: } | |
| 508 | 삭제: let flags = u16at(h, 8)?; | |
| 509 | 삭제: let method = u16at(h, 10)?; | |
| 510 | 삭제: if flags & !0x080f != 0 || !matches!(method, 0 | 8 | 99) || u16at(h, 34)? != 0 { | |
| 511 | 삭제: return Err(ErrorCode::UnsupportedFeature); | |
| 512 | 삭제: } | |
| 513 | 삭제: let crc = u32at(h, 16)?; | |
| 514 | 삭제: let compressed = u32at(h, 20)? as usize; | |
| 515 | 삭제: let size = u32at(h, 24)? as usize; | |
| 516 | 삭제: if compressed == u32::MAX as usize || size == u32::MAX as usize { | |
| 517 | 삭제: return Err(ErrorCode::UnsupportedFeature); | |
| 518 | 삭제: } | |
| 519 | 삭제: total = total | |
| 520 | 삭제: .checked_add(size as u64) | |
| 521 | 삭제: .filter(|n| *n <= limits.output) | |
| 522 | 삭제: .ok_or(ErrorCode::Limit)?; | |
| 523 | 삭제: let name_len = u16at(h, 28)? as usize; | |
| 524 | 삭제: let extra_len = u16at(h, 30)? as usize; | |
| 525 | 삭제: let comment_len = u16at(h, 32)? as usize; | |
| 526 | 삭제: let extra = aes_extra(part(input, at + 46 + name_len, extra_len)?)?; | |
| 527 | 삭제: let encrypted = flags & 1 != 0; | |
| 528 | 삭제: if (method == 99) != extra.is_some() || (method == 99 && !encrypted) { | |
| 529 | 삭제: return Err(ErrorCode::Integrity); | |
| 530 | 삭제: } | |
| 531 | 삭제: if extra.is_some_and(|e| e.version == 2) && crc != 0 { | |
| 532 | 삭제: return Err(ErrorCode::Integrity); | |
| 533 | 삭제: } | |
| 534 | 삭제: let raw = part(input, at + 46, name_len)?; | |
| 535 | 삭제: let name = zip_name(raw, flags, part(input, at + 46 + name_len, extra_len)?)?; | |
| 536 | 삭제: let name = name.as_str(); | |
| 537 | 삭제: let mode = u32at(h, 38)? >> 16; | |
| 538 | 삭제: let kind = match mode & 0xf000 { | |
| 539 | 삭제: 0xa000 => EntryKind::Link, | |
| 540 | 삭제: 0x4000 => EntryKind::Directory, | |
| 541 | 삭제: 0 | 0x8000 => { | |
| 542 | 삭제: if name.ends_with('/') { | |
| 543 | 삭제: EntryKind::Directory | |
| 544 | 삭제: } else { | |
| 545 | 삭제: EntryKind::File | |
| 546 | 삭제: } | |
| 547 | 삭제: } | |
| 548 | 삭제: _ => EntryKind::Other, | |
| 549 | 삭제: }; | |
| 550 | 삭제: metadata.charge(name)?; | |
| 551 | 삭제: let name = valid_name(name, kind == EntryKind::Directory)?; | |
| 552 | 삭제: let local = u32at(h, 42)? as usize; | |
| 553 | 삭제: let lh = part(input, local, 30)?; | |
| 554 | 삭제: if &lh[..4] != b"PK\x03\x04" || u16at(lh, 6)? != flags || u16at(lh, 8)? != method { | |
| 555 | 삭제: return Err(ErrorCode::Integrity); | |
| 556 | 삭제: } | |
| 557 | 삭제: let ln = u16at(lh, 26)? as usize; | |
| 558 | 삭제: let le = u16at(lh, 28)? as usize; | |
| 559 | 삭제: if aes_extra(part(input, local + 30 + ln, le)?)? != extra | |
| 560 | 삭제: || u16at(lh, 10)? != u16at(h, 12)? | |
| 561 | 삭제: || part(input, local + 30, ln)? != raw | |
| 562 | 삭제: { | |
| 563 | 삭제: return Err(ErrorCode::Integrity); | |
| 564 | 삭제: } | |
| 565 | 삭제: if flags & 8 == 0 | |
| 566 | 삭제: && (u32at(lh, 14)? != crc | |
| 567 | 삭제: || u32at(lh, 18)? as usize != compressed | |
| 568 | 삭제: || u32at(lh, 22)? as usize != size) | |
| 569 | 삭제: { | |
| 570 | 삭제: return Err(ErrorCode::Integrity); | |
| 571 | 삭제: } | |
| 572 | 삭제: let start = local | |
| 573 | 삭제: .checked_add(30 + ln + le) | |
| 574 | 삭제: .ok_or(ErrorCode::Integrity)?; | |
| 575 | 삭제: let packed = part(input, start, compressed)?; | |
| 576 | 삭제: let mut stop = start + compressed; | |
| 577 | 삭제: if flags & 8 != 0 { | |
| 578 | 삭제: if input.get(stop..stop + 4) == Some(b"PK\x07\x08") { | |
| 579 | 삭제: stop += 4; | |
| 580 | 삭제: } | |
| 581 | 삭제: if u32at(input, stop)? != crc | |
| 582 | 삭제: || u32at(input, stop + 4)? as usize != compressed | |
| 583 | 삭제: || u32at(input, stop + 8)? as usize != size | |
| 584 | 삭제: { | |
| 585 | 삭제: return Err(ErrorCode::Integrity); | |
| 586 | 삭제: } | |
| 587 | 삭제: stop += 12; | |
| 588 | 삭제: } | |
| 589 | 삭제: if stop > central { | |
| 590 | 삭제: return Err(ErrorCode::Integrity); | |
| 591 | 삭제: } | |
| 592 | 삭제: spans.push((local, stop)); | |
| 593 | 삭제: if encrypted && compressed < if extra.is_some() { 28 } else { 12 } { | |
| 594 | 삭제: return Err(ErrorCode::Integrity); | |
| 595 | 삭제: } | |
| 596 | 삭제: let data = if decode { | |
| 597 | 삭제: let decrypted; | |
| 598 | 삭제: let packed = if encrypted { | |
| 599 | 삭제: let password = password.ok_or(ErrorCode::WrongPassword)?; | |
| 600 | 삭제: decrypted = if extra.is_some() { | |
| 601 | 삭제: crate::crypto::aes_decrypt(packed, password, check)? | |
| 602 | 삭제: } else { | |
| 603 | 삭제: let verifier = if flags & 8 != 0 { | |
| 604 | 삭제: (u16at(h, 12)? >> 8) as u8 | |
| 605 | 삭제: } else { | |
| 606 | 삭제: (crc >> 24) as u8 | |
| 607 | 삭제: }; | |
| 608 | 삭제: crate::crypto::zipcrypto_decrypt(packed, password, verifier, check)? | |
| 609 | 삭제: }; | |
| 610 | 삭제: decrypted.as_slice() | |
| 611 | 삭제: } else { | |
| 612 | 삭제: packed | |
| 613 | 삭제: }; | |
| 614 | 삭제: let actual_method = extra.map_or(method, |e| e.method); | |
| 615 | 삭제: let bytes = if actual_method == 0 { | |
| 616 | 삭제: if packed.len() != size { | |
| 617 | 삭제: return Err(ErrorCode::Integrity); | |
| 618 | 삭제: } | |
| 619 | 삭제: packed.to_vec() | |
| 620 | 삭제: } else { | |
| 621 | 삭제: let (out, used) = inflate(packed, false, size as u64, check)?; | |
| 622 | 삭제: if used != packed.len() { | |
| 623 | 삭제: return Err(ErrorCode::Integrity); | |
| 624 | 삭제: } | |
| 625 | 삭제: out | |
| 626 | 삭제: }; | |
| 627 | 삭제: if bytes.len() != size | |
| 628 | 삭제: || (extra.is_none_or(|e| e.version == 1) && crc32(0, &bytes) != crc) | |
| 629 | 삭제: { | |
| 630 | 삭제: return Err(ErrorCode::Integrity); | |
| 631 | 삭제: } | |
| 632 | 삭제: bytes | |
| 633 | 삭제: } else { | |
| 634 | 삭제: Vec::new() | |
| 635 | 삭제: }; | |
| 636 | 삭제: if kind == EntryKind::Directory && size != 0 { | |
| 637 | 삭제: return Err(ErrorCode::Integrity); | |
| 638 | 삭제: } | |
| 639 | 삭제: members.push(Member { | |
| 640 | 삭제: entry: Entry { | |
| 641 | 삭제: entry_id: id as u32, | |
| 642 | 삭제: name, | |
| 643 | 삭제: kind, | |
| 644 | 삭제: size: size as u64, | |
| 645 | 삭제: compressed_size: compressed as u64, | |
| 646 | 삭제: encrypted, | |
| 647 | 삭제: }, | |
| 648 | 삭제: data, | |
| 649 | 삭제: }); | |
| 650 | 삭제: at = at | |
| 651 | 삭제: .checked_add(46 + name_len + extra_len + comment_len) | |
| 652 | 삭제: .ok_or(ErrorCode::Integrity)?; | |
| 653 | 삭제: } | |
| 654 | 삭제: if at != end { | |
| 655 | 삭제: return Err(ErrorCode::Integrity); | |
| 656 | 삭제: } | |
| 657 | 삭제: spans.sort_unstable(); | |
| 658 | 삭제: let mut next = 0; | |
| 659 | 삭제: for (start, stop) in spans { | |
| 660 | 삭제: if start != next { | |
| 661 | 삭제: return Err(ErrorCode::Integrity); | |
| 662 | 삭제: } | |
| 663 | 삭제: next = stop; | |
| 664 | 삭제: } | |
| 665 | 삭제: if next != central { | |
| 666 | 삭제: return Err(ErrorCode::Integrity); | |
| 667 | 삭제: } | |
| 668 | 삭제: validate_members(&members)?; | |
| 669 | 삭제: Ok(members) | |
| 670 | 삭제: } | |
| 671 | 삭제: pub fn parse( | |
| 672 | 삭제: input: &[u8], | |
| 673 | 삭제: limits: Limits, | |
| 674 | 삭제: decode: bool, | |
| 675 | 삭제: check: &mut impl crate::resources::Monitor, | |
| 676 | 삭제: ) -> Result<Catalog> { | |
| 677 | 삭제: parse_with_password(input, limits, decode, None, check) | |
| 678 | 삭제: } | |
| 679 | 삭제: pub fn parse_with_password( | |
| 680 | 삭제: input: &[u8], | |
| 681 | 삭제: limits: Limits, | |
| 682 | 삭제: decode: bool, | |
| 683 | 삭제: password: Option<&[u8]>, | |
| 684 | 삭제: check: &mut impl crate::resources::Monitor, | |
| 685 | 삭제: ) -> Result<Catalog> { | |
| 686 | 삭제: if !limits.valid() { | |
| 687 | 삭제: return Err(ErrorCode::Limit); | |
| 688 | 삭제: } | |
| 689 | 삭제: let limits = limits.legacy(); | |
| 690 | 삭제: if input.len() as u64 > limits.input { | |
| 691 | 삭제: return Err(ErrorCode::Limit); | |
| 692 | 삭제: } | |
| 693 | 삭제: if input.starts_with(b"7z\xbc\xaf\x27\x1c") { | |
| 694 | 삭제: return Ok(Catalog { | |
| 695 | 삭제: format: Format::SevenZip, | |
| 696 | 삭제: members: crate::seven_zip::parse(input, limits, decode, check)?, | |
| 697 | 삭제: }); | |
| 698 | 삭제: } | |
| 699 | 삭제: if input.starts_with(b"PK\x03\x04") || input.starts_with(b"PK\x05\x06") { | |
| 700 | 삭제: return Ok(Catalog { | |
| 701 | 삭제: format: Format::Zip, | |
| 702 | 삭제: members: zip(input, limits, decode, password, check)?, | |
| 703 | 삭제: }); | |
| 704 | 삭제: } | |
| 705 | 삭제: if input.starts_with(&[31, 139]) { | |
| 706 | 삭제: let bytes = gzip(input, limits, check)?; | |
| 707 | 삭제: if bytes.len() >= 512 && matches!(&bytes[257..263], b"ustar\0" | b"ustar ") { | |
| 708 | 삭제: return Ok(Catalog { | |
| 709 | 삭제: format: Format::TarGzip, | |
| 710 | 삭제: members: tar(&bytes, limits, decode, check)?, | |
| 711 | 삭제: }); | |
| 712 | 삭제: } | |
| 713 | 삭제: let len = bytes.len() as u64; | |
| 714 | 삭제: return Ok(Catalog { | |
| 715 | 삭제: format: Format::Gzip, | |
| 716 | 삭제: members: vec![Member { | |
| 717 | 삭제: entry: Entry { | |
| 718 | 삭제: entry_id: 0, | |
| 719 | 삭제: name: "GZip stream".to_owned(), | |
| 720 | 삭제: kind: EntryKind::File, | |
| 721 | 삭제: size: len, | |
| 722 | 삭제: compressed_size: input.len() as u64, | |
| 723 | 삭제: encrypted: false, | |
| 724 | 삭제: }, | |
| 725 | 삭제: data: if decode { bytes } else { Vec::new() }, | |
| 726 | 삭제: }], | |
| 727 | 삭제: }); | |
| 728 | 삭제: } | |
| 729 | 삭제: if input.len() >= 512 { | |
| 730 | 삭제: return Ok(Catalog { | |
| 731 | 삭제: format: Format::Tar, | |
| 732 | 삭제: members: tar(input, limits, decode, check)?, | |
| 733 | 삭제: }); | |
| 734 | 삭제: } | |
| 735 | 삭제: Err(ErrorCode::UnsupportedFormat) | |
| 736 | 삭제: } | |
| 737 | 256 | fn deflate( |
| 738 | 257 | input: &[u8], |
| 739 | 258 | limit: u64, |
| @@ -44,28 +44,6 @@ fn authenticate( | ||
| 44 | 44 | } |
| 45 | 45 | Ok(mac) |
| 46 | 46 | } |
| 47 | 삭제: pub(crate) fn aes_decrypt( | |
| 48 | 삭제: packed: &[u8], | |
| 49 | 삭제: password: &[u8], | |
| 50 | 삭제: check: &mut impl crate::resources::Monitor, | |
| 51 | 삭제: ) -> Result<Zeroizing<Vec<u8>>> { | |
| 52 | 삭제: if packed.len() < 28 { | |
| 53 | 삭제: return Err(ErrorCode::Integrity); | |
| 54 | 삭제: } | |
| 55 | 삭제: check.check()?; | |
| 56 | 삭제: let key = keys(password, &packed[..16]); | |
| 57 | 삭제: if !bool::from(key[64..].ct_eq(&packed[16..18])) { | |
| 58 | 삭제: return Err(ErrorCode::WrongPassword); | |
| 59 | 삭제: } | |
| 60 | 삭제: let end = packed.len() - 10; | |
| 61 | 삭제: // 인증이 성공하기 전에는 복호화나 압축 해제를 시작하지 않음. | |
| 62 | 삭제: authenticate(&key[32..64], &packed[18..end], check)? | |
| 63 | 삭제: .verify_truncated_left(&packed[end..]) | |
| 64 | 삭제: .map_err(|_| ErrorCode::Integrity)?; | |
| 65 | 삭제: let mut bytes = Zeroizing::new(packed[18..end].to_vec()); | |
| 66 | 삭제: ctr(&mut bytes, &key[..32], check)?; | |
| 67 | 삭제: Ok(bytes) | |
| 68 | 삭제: } | |
| 69 | 47 | pub(crate) fn aes_encrypt( |
| 70 | 48 | packed: &[u8], |
| 71 | 49 | password: &[u8], |
| @@ -134,37 +112,6 @@ const CRC_TABLE: [u32; 256] = { | ||
| 134 | 112 | } |
| 135 | 113 | table |
| 136 | 114 | }; |
| 137 | 삭제: pub(crate) fn zipcrypto_decrypt( | |
| 138 | 삭제: packed: &[u8], | |
| 139 | 삭제: password: &[u8], | |
| 140 | 삭제: verifier: u8, | |
| 141 | 삭제: check: &mut impl crate::resources::Monitor, | |
| 142 | 삭제: ) -> Result<Zeroizing<Vec<u8>>> { | |
| 143 | 삭제: if packed.len() < 12 { | |
| 144 | 삭제: return Err(ErrorCode::Integrity); | |
| 145 | 삭제: } | |
| 146 | 삭제: let mut key = Zeroizing::new([0x12345678u32, 0x23456789, 0x34567890]); | |
| 147 | 삭제: for &byte in password { | |
| 148 | 삭제: zip_update(&mut key, byte); | |
| 149 | 삭제: } | |
| 150 | 삭제: let mut out = Zeroizing::new(Vec::with_capacity(packed.len() - 12)); | |
| 151 | 삭제: for (index, &byte) in packed.iter().enumerate() { | |
| 152 | 삭제: if index % 65536 == 0 { | |
| 153 | 삭제: check.check()?; | |
| 154 | 삭제: } | |
| 155 | 삭제: let temp = key[2] | 2; | |
| 156 | 삭제: let plain = byte ^ ((temp.wrapping_mul(temp ^ 1) >> 8) as u8); | |
| 157 | 삭제: zip_update(&mut key, plain); | |
| 158 | 삭제: if index == 11 && plain != verifier { | |
| 159 | 삭제: return Err(ErrorCode::WrongPassword); | |
| 160 | 삭제: } | |
| 161 | 삭제: if index >= 12 { | |
| 162 | 삭제: out.push(plain); | |
| 163 | 삭제: } | |
| 164 | 삭제: } | |
| 165 | 삭제: Ok(out) | |
| 166 | 삭제: } | |
| 167 | 삭제: | |
| 168 | 115 | pub(crate) enum Decryptor { |
| 169 | 116 | Plain, |
| 170 | 117 | Zip(Zeroizing<[u32; 3]>), |
| @@ -17,4 +17,5 @@ mod split_seven_zip; | ||
| 17 | 17 | mod spool; |
| 18 | 18 | mod streaming; |
| 19 | 19 | pub mod wire; |
| 20 | 추가: mod zip_extra; | |
| 20 | 21 | mod zip_recovery; |
| @@ -773,33 +773,3 @@ pub(crate) fn open_selected( | ||
| 773 | 773 | data, |
| 774 | 774 | }) |
| 775 | 775 | } |
| 776 | 삭제: pub(crate) fn parse( | |
| 777 | 삭제: input: &[u8], | |
| 778 | 삭제: limits: Limits, | |
| 779 | 삭제: decode: bool, | |
| 780 | 삭제: check: &mut impl crate::resources::Monitor, | |
| 781 | 삭제: ) -> Result<Vec<Member>> { | |
| 782 | 삭제: use std::io::{Read, Seek, SeekFrom, Write}; | |
| 783 | 삭제: let mut source = crate::spool::temporary()?; | |
| 784 | 삭제: crate::space::ensure(&source, input.len() as u64)?; | |
| 785 | 삭제: source.write_all(input).map_err(crate::space::io_error)?; | |
| 786 | 삭제: let mut catalog = open(&mut source, input.len() as u64, limits, decode, check)?; | |
| 787 | 삭제: if decode { | |
| 788 | 삭제: for (member, offset) in catalog.members.iter_mut().zip(catalog.offsets) { | |
| 789 | 삭제: catalog | |
| 790 | 삭제: .data | |
| 791 | 삭제: .seek(SeekFrom::Start(offset)) | |
| 792 | 삭제: .map_err(crate::space::io_error)?; | |
| 793 | 삭제: member | |
| 794 | 삭제: .data | |
| 795 | 삭제: .try_reserve_exact(member.entry.size as usize) | |
| 796 | 삭제: .map_err(|_| ErrorCode::Limit)?; | |
| 797 | 삭제: (&mut catalog.data) | |
| 798 | 삭제: .take(member.entry.size) | |
| 799 | 삭제: .read_to_end(&mut member.data) | |
| 800 | 삭제: .map_err(crate::space::io_error)?; | |
| 801 | 삭제: crate::resources::completed(Ok(()), check)?; | |
| 802 | 삭제: } | |
| 803 | 삭제: } | |
| 804 | 삭제: Ok(catalog.members) | |
| 805 | 삭제: } |
| @@ -3,6 +3,7 @@ use crate::{ | ||
| 3 | 3 | crypto, |
| 4 | 4 | protocol::*, |
| 5 | 5 | spool, |
| 6 | 추가: zip_extra::{self, Policy, disk_start, expanded, large_value, local_sizes}, | |
| 6 | 7 | }; |
| 7 | 8 | use std::{ |
| 8 | 9 | fs::File, |
| @@ -357,77 +358,6 @@ struct Packed { | ||
| 357 | 358 | aes: bool, |
| 358 | 359 | } |
| 359 | 360 | |
| 360 | 삭제: fn extra(bytes: &[u8]) -> Result<(Option<archive::AesExtra>, Option<Vec<u8>>)> { | |
| 361 | 삭제: let mut at = 0; | |
| 362 | 삭제: let mut aes = Vec::new(); | |
| 363 | 삭제: let mut large = None; | |
| 364 | 삭제: while at < bytes.len() { | |
| 365 | 삭제: if bytes.len() - at < 4 { | |
| 366 | 삭제: return Err(ErrorCode::Integrity); | |
| 367 | 삭제: } | |
| 368 | 삭제: let tag = u16at(bytes, at); | |
| 369 | 삭제: let len = u16at(bytes, at + 2) as usize; | |
| 370 | 삭제: let end = at + 4 + len; | |
| 371 | 삭제: if end > bytes.len() { | |
| 372 | 삭제: return Err(ErrorCode::Integrity); | |
| 373 | 삭제: } | |
| 374 | 삭제: if tag == 1 { | |
| 375 | 삭제: if large.is_some() { | |
| 376 | 삭제: return Err(ErrorCode::Integrity); | |
| 377 | 삭제: } | |
| 378 | 삭제: large = Some(bytes[at + 4..end].to_vec()); | |
| 379 | 삭제: } else { | |
| 380 | 삭제: aes.extend_from_slice(&bytes[at..end]); | |
| 381 | 삭제: } | |
| 382 | 삭제: at = end; | |
| 383 | 삭제: } | |
| 384 | 삭제: Ok((archive::aes_extra(&aes)?, large)) | |
| 385 | 삭제: } | |
| 386 | 삭제: fn large_value(value: u32, extra: &mut &[u8]) -> Result<u64> { | |
| 387 | 삭제: if value != u32::MAX { | |
| 388 | 삭제: return Ok(value as u64); | |
| 389 | 삭제: } | |
| 390 | 삭제: if extra.len() < 8 { | |
| 391 | 삭제: return Err(ErrorCode::Integrity); | |
| 392 | 삭제: } | |
| 393 | 삭제: let result = u64at(extra, 0); | |
| 394 | 삭제: *extra = &extra[8..]; | |
| 395 | 삭제: Ok(result) | |
| 396 | 삭제: } | |
| 397 | 삭제: fn expanded(value: u64, maximum: u64, large: u64) -> Result<u64> { | |
| 398 | 삭제: if value == maximum || value == large { | |
| 399 | 삭제: Ok(large) | |
| 400 | 삭제: } else { | |
| 401 | 삭제: Err(ErrorCode::Integrity) | |
| 402 | 삭제: } | |
| 403 | 삭제: } | |
| 404 | 삭제: fn disk_start(value: u16, extra: &mut &[u8]) -> Result<u32> { | |
| 405 | 삭제: if value != u16::MAX { | |
| 406 | 삭제: return Ok(value as u32); | |
| 407 | 삭제: } | |
| 408 | 삭제: if extra.len() < 4 { | |
| 409 | 삭제: return Err(ErrorCode::Integrity); | |
| 410 | 삭제: } | |
| 411 | 삭제: let result = u32at(extra, 0); | |
| 412 | 삭제: *extra = &extra[4..]; | |
| 413 | 삭제: Ok(result) | |
| 414 | 삭제: } | |
| 415 | 삭제: fn local_sizes(size: u32, compressed: u32, extra: Option<&[u8]>) -> Result<(u64, u64)> { | |
| 416 | 삭제: if let Some(extra) = extra { | |
| 417 | 삭제: // 로컬 ZIP64 추가 필드에는 두 크기가 함께 들어감. | |
| 418 | 삭제: if extra.len() < 16 { | |
| 419 | 삭제: return Err(ErrorCode::Integrity); | |
| 420 | 삭제: } | |
| 421 | 삭제: Ok(( | |
| 422 | 삭제: expanded(size as u64, u32::MAX as u64, u64at(extra, 0))?, | |
| 423 | 삭제: expanded(compressed as u64, u32::MAX as u64, u64at(extra, 8))?, | |
| 424 | 삭제: )) | |
| 425 | 삭제: } else if size == u32::MAX || compressed == u32::MAX { | |
| 426 | 삭제: Err(ErrorCode::Integrity) | |
| 427 | 삭제: } else { | |
| 428 | 삭제: Ok((size as u64, compressed as u64)) | |
| 429 | 삭제: } | |
| 430 | 삭제: } | |
| 431 | 361 | fn descriptor_size( |
| 432 | 362 | bytes: &[u8], |
| 433 | 363 | crc: u32, |
| @@ -572,10 +502,12 @@ fn zip( | ||
| 572 | 502 | } |
| 573 | 503 | let raw = bytes(source, cursor + 46, nn as usize)?; |
| 574 | 504 | let central_extra = bytes(source, cursor + 46 + nn, ne as usize)?; |
| 575 | 삭제: let (aes, large) = extra(¢ral_extra)?; | |
| 505 | 추가: let parsed_extra = zip_extra::parse(¢ral_extra, Policy::Streaming)?; | |
| 506 | 추가: let aes = parsed_extra.aes; | |
| 507 | 추가: let large = parsed_extra.zip64; | |
| 576 | 508 | let name = archive::zip_name(&raw, flags, ¢ral_extra)?; |
| 577 | 509 | let name = name.as_str(); |
| 578 | 삭제: let mut large = large.as_deref().unwrap_or(&[]); | |
| 510 | 추가: let mut large = large.unwrap_or(&[]); | |
| 579 | 511 | let size = large_value(u32at(&h, 24), &mut large)?; |
| 580 | 512 | let compressed = large_value(u32at(&h, 20), &mut large)?; |
| 581 | 513 | let local = large_value(u32at(&h, 42), &mut large)?; |
| @@ -653,7 +585,9 @@ fn zip( | ||
| 653 | 585 | if bytes(source, local + 30, ln as usize)? != raw { |
| 654 | 586 | return Err(ErrorCode::Integrity); |
| 655 | 587 | } |
| 656 | 삭제: let (local_aes, local_large) = extra(&bytes(source, local + 30 + ln, le as usize)?)?; | |
| 588 | 추가: let local_extra = bytes(source, local + 30 + ln, le as usize)?; | |
| 589 | 추가: let parsed_local = zip_extra::parse(&local_extra, Policy::Streaming)?; | |
| 590 | 추가: let (local_aes, local_large) = (parsed_local.aes, parsed_local.zip64); | |
| 657 | 591 | if local_aes != aes || local_large.as_ref().is_some_and(|values| values.len() < 16) { |
| 658 | 592 | return Err(ErrorCode::Integrity); |
| 659 | 593 | } |
| @@ -664,7 +598,7 @@ fn zip( | ||
| 664 | 598 | || local_large.is_some(); |
| 665 | 599 | if flags & 8 == 0 { |
| 666 | 600 | let (local_size, local_compressed) = |
| 667 | 삭제: local_sizes(u32at(&lh, 22), u32at(&lh, 18), local_large.as_deref())?; | |
| 601 | 추가: local_sizes(u32at(&lh, 22), u32at(&lh, 18), local_large)?; | |
| 668 | 602 | if u32at(&lh, 14) != crc || local_size != size || local_compressed != compressed { |
| 669 | 603 | return Err(ErrorCode::Integrity); |
| 670 | 604 | } |
| @@ -0,0 +1,149 @@ | ||
| 1 | 추가: use crate::protocol::ErrorCode; | |
| 2 | 추가: | |
| 3 | 추가: type Result<T> = std::result::Result<T, ErrorCode>; | |
| 4 | 추가: | |
| 5 | 추가: #[derive(Clone, Copy, PartialEq, Eq)] | |
| 6 | 추가: pub(crate) enum Policy { | |
| 7 | 추가: Streaming, | |
| 8 | 추가: Recovery, | |
| 9 | 추가: Names, | |
| 10 | 추가: } | |
| 11 | 추가: | |
| 12 | 추가: #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 13 | 추가: pub(crate) struct AesExtra { | |
| 14 | 추가: pub version: u16, | |
| 15 | 추가: pub method: u16, | |
| 16 | 추가: } | |
| 17 | 추가: | |
| 18 | 추가: #[derive(Default, Debug)] | |
| 19 | 추가: pub(crate) struct Extra<'a> { | |
| 20 | 추가: pub aes: Option<AesExtra>, | |
| 21 | 추가: pub zip64: Option<&'a [u8]>, | |
| 22 | 추가: pub unicode: Option<&'a [u8]>, | |
| 23 | 추가: } | |
| 24 | 추가: | |
| 25 | 추가: fn u16at(bytes: &[u8], at: usize) -> u16 { | |
| 26 | 추가: u16::from_le_bytes(bytes[at..at + 2].try_into().unwrap()) | |
| 27 | 추가: } | |
| 28 | 추가: fn u32at(bytes: &[u8], at: usize) -> u32 { | |
| 29 | 추가: u32::from_le_bytes(bytes[at..at + 4].try_into().unwrap()) | |
| 30 | 추가: } | |
| 31 | 추가: fn u64at(bytes: &[u8], at: usize) -> u64 { | |
| 32 | 추가: u64::from_le_bytes(bytes[at..at + 8].try_into().unwrap()) | |
| 33 | 추가: } | |
| 34 | 추가: | |
| 35 | 추가: fn aes(value: &[u8]) -> Result<AesExtra> { | |
| 36 | 추가: if value.len() < 7 { | |
| 37 | 추가: return Err(ErrorCode::Integrity); | |
| 38 | 추가: } | |
| 39 | 추가: let version = u16at(value, 0); | |
| 40 | 추가: let method = u16at(value, 5); | |
| 41 | 추가: if !matches!(version, 1 | 2) | |
| 42 | 추가: || &value[2..4] != b"AE" | |
| 43 | 추가: || value[4] != 3 | |
| 44 | 추가: || !matches!(method, 0 | 8) | |
| 45 | 추가: { | |
| 46 | 추가: return Err(ErrorCode::UnsupportedFeature); | |
| 47 | 추가: } | |
| 48 | 추가: Ok(AesExtra { version, method }) | |
| 49 | 추가: } | |
| 50 | 추가: | |
| 51 | 추가: pub(crate) fn parse(bytes: &[u8], policy: Policy) -> Result<Extra<'_>> { | |
| 52 | 추가: let mut found = Extra::default(); | |
| 53 | 추가: let mut deferred_aes = None; | |
| 54 | 추가: let mut duplicate_aes = false; | |
| 55 | 추가: let mut at = 0; | |
| 56 | 추가: while at < bytes.len() { | |
| 57 | 추가: if bytes.len() - at < 4 { | |
| 58 | 추가: return Err(ErrorCode::Integrity); | |
| 59 | 추가: } | |
| 60 | 추가: let tag = u16at(bytes, at); | |
| 61 | 추가: let length = u16at(bytes, at + 2) as usize; | |
| 62 | 추가: // 복구 경로는 확장 크기와 강한 암호화를 해석하지 않음. | |
| 63 | 추가: if policy == Policy::Recovery && matches!(tag, 1 | 0x0017 | 0x0018) { | |
| 64 | 추가: return Err(ErrorCode::UnsupportedFeature); | |
| 65 | 추가: } | |
| 66 | 추가: let end = at.checked_add(4 + length).ok_or(ErrorCode::Integrity)?; | |
| 67 | 추가: let value = bytes.get(at + 4..end).ok_or(ErrorCode::Integrity)?; | |
| 68 | 추가: match (policy, tag) { | |
| 69 | 추가: (Policy::Streaming, 1) => { | |
| 70 | 추가: if found.zip64.replace(value).is_some() { | |
| 71 | 추가: return Err(ErrorCode::Integrity); | |
| 72 | 추가: } | |
| 73 | 추가: } | |
| 74 | 추가: (Policy::Streaming, 0x9901) => { | |
| 75 | 추가: if deferred_aes.is_some() { | |
| 76 | 추가: duplicate_aes = true; | |
| 77 | 추가: } else { | |
| 78 | 추가: deferred_aes = Some(value); | |
| 79 | 추가: } | |
| 80 | 추가: } | |
| 81 | 추가: (Policy::Names, 0x7075) => { | |
| 82 | 추가: if found.unicode.is_some() || length < 5 { | |
| 83 | 추가: return Err(ErrorCode::Integrity); | |
| 84 | 추가: } | |
| 85 | 추가: found.unicode = Some(value); | |
| 86 | 추가: } | |
| 87 | 추가: _ => {} | |
| 88 | 추가: } | |
| 89 | 추가: at = end; | |
| 90 | 추가: } | |
| 91 | 추가: // 스트리밍은 전체 필드 경계를 확인한 뒤 AES 지원 여부를 판단함. | |
| 92 | 추가: if let Some(value) = deferred_aes { | |
| 93 | 추가: found.aes = Some(aes(value)?); | |
| 94 | 추가: if duplicate_aes { | |
| 95 | 추가: return Err(ErrorCode::Integrity); | |
| 96 | 추가: } | |
| 97 | 추가: } | |
| 98 | 추가: Ok(found) | |
| 99 | 추가: } | |
| 100 | 추가: | |
| 101 | 추가: pub(crate) fn large_value(value: u32, extra: &mut &[u8]) -> Result<u64> { | |
| 102 | 추가: if value != u32::MAX { | |
| 103 | 추가: return Ok(value as u64); | |
| 104 | 추가: } | |
| 105 | 추가: if extra.len() < 8 { | |
| 106 | 추가: return Err(ErrorCode::Integrity); | |
| 107 | 추가: } | |
| 108 | 추가: let result = u64at(extra, 0); | |
| 109 | 추가: *extra = &extra[8..]; | |
| 110 | 추가: Ok(result) | |
| 111 | 추가: } | |
| 112 | 추가: pub(crate) fn expanded(value: u64, maximum: u64, large: u64) -> Result<u64> { | |
| 113 | 추가: if value == maximum || value == large { | |
| 114 | 추가: Ok(large) | |
| 115 | 추가: } else { | |
| 116 | 추가: Err(ErrorCode::Integrity) | |
| 117 | 추가: } | |
| 118 | 추가: } | |
| 119 | 추가: pub(crate) fn disk_start(value: u16, extra: &mut &[u8]) -> Result<u32> { | |
| 120 | 추가: if value != u16::MAX { | |
| 121 | 추가: return Ok(value as u32); | |
| 122 | 추가: } | |
| 123 | 추가: if extra.len() < 4 { | |
| 124 | 추가: return Err(ErrorCode::Integrity); | |
| 125 | 추가: } | |
| 126 | 추가: let result = u32at(extra, 0); | |
| 127 | 추가: *extra = &extra[4..]; | |
| 128 | 추가: Ok(result) | |
| 129 | 추가: } | |
| 130 | 추가: pub(crate) fn local_sizes(size: u32, compressed: u32, extra: Option<&[u8]>) -> Result<(u64, u64)> { | |
| 131 | 추가: if let Some(extra) = extra { | |
| 132 | 추가: // 로컬 ZIP64 추가 필드에는 두 크기가 함께 들어감. | |
| 133 | 추가: if extra.len() < 16 { | |
| 134 | 추가: return Err(ErrorCode::Integrity); | |
| 135 | 추가: } | |
| 136 | 추가: Ok(( | |
| 137 | 추가: expanded(size as u64, u32::MAX as u64, u64at(extra, 0))?, | |
| 138 | 추가: expanded(compressed as u64, u32::MAX as u64, u64at(extra, 8))?, | |
| 139 | 추가: )) | |
| 140 | 추가: } else if size == u32::MAX || compressed == u32::MAX { | |
| 141 | 추가: Err(ErrorCode::Integrity) | |
| 142 | 추가: } else { | |
| 143 | 추가: Ok((size as u64, compressed as u64)) | |
| 144 | 추가: } | |
| 145 | 추가: } | |
| 146 | 추가: | |
| 147 | 추가: #[cfg(test)] | |
| 148 | 추가: #[path = "../../../tests/rust-archive-worker/zip_extra.rs"] | |
| 149 | 추가: mod tests; |
| @@ -38,24 +38,6 @@ fn bytes(source: &mut File, offset: u64, size: usize, end: u64) -> Result<Vec<u8 | ||
| 38 | 38 | io(source.read_exact(&mut data))?; |
| 39 | 39 | Ok(data) |
| 40 | 40 | } |
| 41 | 삭제: fn extra(data: &[u8]) -> Result<()> { | |
| 42 | 삭제: let mut at = 0; | |
| 43 | 삭제: while at < data.len() { | |
| 44 | 삭제: if data.len() - at < 4 { | |
| 45 | 삭제: return Err(ErrorCode::Integrity); | |
| 46 | 삭제: } | |
| 47 | 삭제: let tag = u16at(data, at); | |
| 48 | 삭제: let size = u16at(data, at + 2) as usize; | |
| 49 | 삭제: if matches!(tag, 1 | 0x0017 | 0x0018) { | |
| 50 | 삭제: return Err(ErrorCode::UnsupportedFeature); | |
| 51 | 삭제: } | |
| 52 | 삭제: at = at | |
| 53 | 삭제: .checked_add(4 + size) | |
| 54 | 삭제: .filter(|n| *n <= data.len()) | |
| 55 | 삭제: .ok_or(ErrorCode::Integrity)?; | |
| 56 | 삭제: } | |
| 57 | 삭제: Ok(()) | |
| 58 | 삭제: } | |
| 59 | 41 | struct Candidate { |
| 60 | 42 | finding: RecoveryEntry, |
| 61 | 43 | start: u64, |
| @@ -438,7 +420,7 @@ fn central( | ||
| 438 | 420 | charge(&mut metadata, nn, ne, check)?; |
| 439 | 421 | let raw = bytes(source, at + 46, nn, cd_end)?; |
| 440 | 422 | let ext = bytes(source, at + 46 + nn as u64, ne, cd_end)?; |
| 441 | 삭제: extra(&ext)?; | |
| 423 | 추가: crate::zip_extra::parse(&ext, crate::zip_extra::Policy::Recovery)?; | |
| 442 | 424 | let (size, packed) = declared(u32at(&h, 24), u32at(&h, 20))?; |
| 443 | 425 | let unix_kind = (u32at(&h, 38) >> 16) & 0xf000; |
| 444 | 426 | let kind = match unix_kind { |
| @@ -579,7 +561,7 @@ fn local( | ||
| 579 | 561 | } |
| 580 | 562 | let raw = bytes(source, at + 30, nn, len)?; |
| 581 | 563 | let ext = bytes(source, at + 30 + nn as u64, ne, len)?; |
| 582 | 삭제: extra(&ext)?; | |
| 564 | 추가: crate::zip_extra::parse(&ext, crate::zip_extra::Policy::Recovery)?; | |
| 583 | 565 | let kind = if raw.ends_with(b"/") { |
| 584 | 566 | EntryKind::Directory |
| 585 | 567 | } else { |
| @@ -17,6 +17,7 @@ python3 tests/ci/run_rust.py local --job rust-local | ||
| 17 | 17 | - 추적된 fixture를 Git blob과 대조합니다. Windows checkout 시 core.autocrlf=false를 사용해야 하며 기대값의 바이트를 바꾸지 않습니다. |
| 18 | 18 | - 개별 단계는 `build`, `test`, `cross-build`로 실행할 수 있습니다. 한 crate가 실패해도 다른 crate 결과를 수집합니다. |
| 19 | 19 | - GUI 앱은 컴파일하며 실제 화면 시나리오는 실행하지 않습니다. C# 시험과 역사적 hash 검사는 포함하지 않습니다. |
| 20 | 추가: - Linux 작업자의 작은 임시 공간 시험은 기본 실행에 포함합니다. 자식 프로세스에만 적용한 LD_PRELOAD 보조 코드로 2MiB 공유 용량, 부분 쓰기와 ENOSPC 및 핸들 종료 후 공간 반환을 재현합니다. 커널 tmpfs를 직접 마운트한 실기 검증은 아닙니다. | |
| 20 | 21 | |
| 21 | 22 | ## 출시 전 Windows 실기 확인 |
| 22 | 23 |
| @@ -149,7 +149,7 @@ fn empty_root_folder_is_preserved() { | ||
| 149 | 149 | let events = execute(start); |
| 150 | 150 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 151 | 151 | let bytes = fs::read(s.0.join("created.zip")).unwrap(); |
| 152 | 삭제: let catalog = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())).unwrap(); | |
| 152 | 추가: let catalog = worker_catalog(&bytes, Limits::default(), true, None).unwrap(); | |
| 153 | 153 | assert_eq!(catalog.members.len(), 1); |
| 154 | 154 | assert_eq!(catalog.members[0].entry.kind, EntryKind::Directory); |
| 155 | 155 | assert_eq!(catalog.members[0].entry.name, "documents"); |
| @@ -828,7 +828,7 @@ fn created_empty_files_and_directories_refresh_idle_deadline() { | ||
| 828 | 828 | assert_eq!(terminal(&events).publication, Publication::Complete); |
| 829 | 829 | assert_eq!(terminal(&events).created_count, 1); |
| 830 | 830 | let bytes = fs::read(s.0.join("new.zip")).unwrap(); |
| 831 | 삭제: let parsed = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())).unwrap(); | |
| 831 | 추가: let parsed = worker_catalog(&bytes, Limits::default(), true, None).unwrap(); | |
| 832 | 832 | assert_eq!(parsed.members.len(), 37); |
| 833 | 833 | assert!(parsed.members.iter().any(|member| { |
| 834 | 834 | member.entry.name == "source" && member.entry.kind == EntryKind::Directory |
| @@ -10,17 +10,21 @@ use zipelo_archive_worker::{ | ||
| 10 | 10 | protocol::*, |
| 11 | 11 | wire::{Reader, Writer}, |
| 12 | 12 | }; |
| 13 | 추가: mod real_catalog; | |
| 14 | 추가: use real_catalog::worker_catalog; | |
| 15 | 추가: | |
| 13 | 16 | static NEXT: AtomicU64 = AtomicU64::new(0); |
| 14 | 17 | struct Scratch(PathBuf); |
| 15 | 18 | impl Scratch { |
| 16 | 19 | fn new() -> Self { |
| 17 | 삭제: let path = PathBuf::from(std::env::var_os("ZIPELO_TEST_WORK_DIR").expect("runner 작업 경로 필요")) | |
| 18 | 삭제: .join("fixtures/rust-archive-worker") | |
| 19 | 삭제: .join(format!( | |
| 20 | 삭제: "{}-{}", | |
| 21 | 삭제: std::process::id(), | |
| 22 | 삭제: NEXT.fetch_add(1, Ordering::Relaxed) | |
| 23 | 삭제: )); | |
| 20 | 추가: let path = | |
| 21 | 추가: PathBuf::from(std::env::var_os("ZIPELO_TEST_WORK_DIR").expect("runner 작업 경로 필요")) | |
| 22 | 추가: .join("fixtures/rust-archive-worker") | |
| 23 | 추가: .join(format!( | |
| 24 | 추가: "{}-{}", | |
| 25 | 추가: std::process::id(), | |
| 26 | 추가: NEXT.fetch_add(1, Ordering::Relaxed) | |
| 27 | 추가: )); | |
| 24 | 28 | fs::create_dir_all(path.parent().unwrap()).unwrap(); |
| 25 | 29 | let builder = fs::DirBuilder::new(); |
| 26 | 30 | #[cfg(unix)] |
| @@ -414,7 +418,7 @@ fn create_store_and_deflate_zip() { | ||
| 414 | 418 | }); |
| 415 | 419 | assert_eq!(terminal(&e).outcome, Outcome::Completed, "{e:?}"); |
| 416 | 420 | let bytes = fs::read(s.0.join("result.zip")).unwrap(); |
| 417 | 삭제: let parsed = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())).unwrap(); | |
| 421 | 추가: let parsed = worker_catalog(&bytes, Limits::default(), true, None).unwrap(); | |
| 418 | 422 | assert_eq!(parsed.members[0].data, PAYLOAD); |
| 419 | 423 | } |
| 420 | 424 | } |
| @@ -723,15 +727,7 @@ fn shallow_zip_parent_metadata_is_bounded() { | ||
| 723 | 727 | .iter() |
| 724 | 728 | .all(|n| n.split('/').count() <= archive::MAX_PATH_DEPTH) |
| 725 | 729 | ); |
| 726 | 삭제: assert!( | |
| 727 | 삭제: archive::parse( | |
| 728 | 삭제: &path_fixture_zip(&names), | |
| 729 | 삭제: Limits::default(), | |
| 730 | 삭제: false, | |
| 731 | 삭제: &mut || Ok(()) | |
| 732 | 삭제: ) | |
| 733 | 삭제: .is_ok() | |
| 734 | 삭제: ); | |
| 730 | 추가: assert!(worker_catalog(&path_fixture_zip(&names), Limits::default(), false, None).is_ok()); | |
| 735 | 731 | assert_metadata_limit(&names); |
| 736 | 732 | } |
| 737 | 733 | |
| @@ -749,7 +745,7 @@ fn aggregate_archive_name_metadata_is_bounded() { | ||
| 749 | 745 | let bytes = path_fixture_zip(&names); |
| 750 | 746 | assert!((bytes.len() as u64) < Limits::default().input); |
| 751 | 747 | assert!(matches!( |
| 752 | 삭제: archive::parse(&bytes, Limits::default(), false, &mut || Ok(())), | |
| 748 | 추가: worker_catalog(&bytes, Limits::default(), false, None), | |
| 753 | 749 | Err(ErrorCode::Limit) |
| 754 | 750 | )); |
| 755 | 751 | assert_metadata_limit(&names); |
| @@ -787,7 +783,7 @@ fn zero_payload_gzip_is_not_tar() { | ||
| 787 | 783 | for length in [512, 1024, 1536] { |
| 788 | 784 | let scratch = Scratch::new(); |
| 789 | 785 | let bytes = zeros_gzip(length); |
| 790 | 삭제: let parsed = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())).unwrap(); | |
| 786 | 추가: let parsed = worker_catalog(&bytes, Limits::default(), true, None).unwrap(); | |
| 791 | 787 | assert_eq!(parsed.format, Format::Gzip); |
| 792 | 788 | let events = extract( |
| 793 | 789 | &scratch, |
| @@ -1160,14 +1156,7 @@ fn empty_aes_members_and_random_salts_round_trip() { | ||
| 1160 | 1156 | ) |
| 1161 | 1157 | .unwrap(); |
| 1162 | 1158 | assert_ne!(a, b); |
| 1163 | 삭제: let decoded = archive::parse_with_password( | |
| 1164 | 삭제: &a, | |
| 1165 | 삭제: Limits::default(), | |
| 1166 | 삭제: true, | |
| 1167 | 삭제: Some(TEST_PASSWORD), | |
| 1168 | 삭제: &mut || Ok(()), | |
| 1169 | 삭제: ) | |
| 1170 | 삭제: .unwrap(); | |
| 1159 | 추가: let decoded = worker_catalog(&a, Limits::default(), true, Some(TEST_PASSWORD)).unwrap(); | |
| 1171 | 1160 | assert!(decoded.members[0].data.is_empty()); |
| 1172 | 1161 | } |
| 1173 | 1162 | |
| @@ -1230,7 +1219,7 @@ fn seven_zip_lzma_lzma2_and_copy_interoperate() { | ||
| 1230 | 1219 | "libarchive-lzma2.7z", |
| 1231 | 1220 | ] { |
| 1232 | 1221 | let bytes = seven_fixture(name); |
| 1233 | 삭제: let catalog = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())) | |
| 1222 | 추가: let catalog = worker_catalog(&bytes, Limits::default(), true, None) | |
| 1234 | 1223 | .unwrap_or_else(|e| panic!("{name}: {e:?}")); |
| 1235 | 1224 | assert_eq!(catalog.format, Format::SevenZip); |
| 1236 | 1225 | assert_eq!(catalog.members[0].data, SEVEN_PAYLOAD.repeat(100), "{name}"); |
| @@ -1251,7 +1240,7 @@ fn seven_zip_lzma_lzma2_and_copy_interoperate() { | ||
| 1251 | 1240 | fn seven_zip_encoded_header_solid_and_empty_entries() { |
| 1252 | 1241 | let s = Scratch::new(); |
| 1253 | 1242 | let bytes = seven_fixture("encoded-solid.7z"); |
| 1254 | 삭제: let catalog = archive::parse(&bytes, Limits::default(), false, &mut || Ok(())).unwrap(); | |
| 1243 | 추가: let catalog = worker_catalog(&bytes, Limits::default(), false, None).unwrap(); | |
| 1255 | 1244 | assert_eq!(catalog.members.len(), 4); |
| 1256 | 1245 | let events = extract(&s, &bytes, None, Selection::All, None, Limits::default()); |
| 1257 | 1246 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| @@ -1268,7 +1257,7 @@ fn seven_zip_encoded_header_solid_and_empty_entries() { | ||
| 1268 | 1257 | fn seven_zip_long_matches_and_lzma2_chunks() { |
| 1269 | 1258 | for name in ["long-match.7z", "lzma2-chunks.7z"] { |
| 1270 | 1259 | let bytes = seven_fixture(name); |
| 1271 | 삭제: let catalog = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())) | |
| 1260 | 추가: let catalog = worker_catalog(&bytes, Limits::default(), true, None) | |
| 1272 | 1261 | .unwrap_or_else(|e| panic!("{name}: {e:?}")); |
| 1273 | 1262 | if name == "long-match.7z" { |
| 1274 | 1263 | assert_eq!(catalog.members[0].data, SEVEN_PAYLOAD.repeat(10000)); |
| @@ -1445,20 +1434,6 @@ fn seven_zip_limits_truncation_and_existing_files_are_preserved() { | ||
| 1445 | 1434 | assert_eq!(fs::read_dir(s.0.join("output")).unwrap().count(), 0); |
| 1446 | 1435 | } |
| 1447 | 1436 | #[test] |
| 1448 | 삭제: fn seven_zip_parser_honors_cancellation_checkpoints() { | |
| 1449 | 삭제: let bytes = seven_fixture("long-match.7z"); | |
| 1450 | 삭제: let mut calls = 0; | |
| 1451 | 삭제: let result = archive::parse(&bytes, Limits::default(), true, &mut || { | |
| 1452 | 삭제: calls += 1; | |
| 1453 | 삭제: if calls >= 5 { | |
| 1454 | 삭제: Err(ErrorCode::None) | |
| 1455 | 삭제: } else { | |
| 1456 | 삭제: Ok(()) | |
| 1457 | 삭제: } | |
| 1458 | 삭제: }); | |
| 1459 | 삭제: assert!(matches!(result, Err(ErrorCode::None))); | |
| 1460 | 삭제: } | |
| 1461 | 삭제: #[test] | |
| 1462 | 1437 | fn pr64_malicious_corpus_is_rejected_without_writes() { |
| 1463 | 1438 | let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 1464 | 1439 | .join("../../tests/malicious-archive-fixtures/fixtures"); |
| @@ -0,0 +1,76 @@ | ||
| 1 | 추가: use super::*; | |
| 2 | 추가: | |
| 3 | 추가: pub(super) fn worker_catalog( | |
| 4 | 추가: bytes: &[u8], | |
| 5 | 추가: limits: Limits, | |
| 6 | 추가: decode: bool, | |
| 7 | 추가: password: Option<&[u8]>, | |
| 8 | 추가: ) -> Result<archive::Catalog, ErrorCode> { | |
| 9 | 추가: let scratch = Scratch::new(); | |
| 10 | 추가: fs::write(scratch.0.join("input"), bytes).unwrap(); | |
| 11 | 추가: let events = execute(Start { | |
| 12 | 추가: action: Action::List { | |
| 13 | 추가: archive: scratch.path("input"), | |
| 14 | 추가: }, | |
| 15 | 추가: limits, | |
| 16 | 추가: }); | |
| 17 | 추가: if terminal(&events).outcome != Outcome::Completed { | |
| 18 | 추가: return Err(terminal(&events).error); | |
| 19 | 추가: } | |
| 20 | 추가: let list_id = events | |
| 21 | 추가: .iter() | |
| 22 | 추가: .find_map(|event| match event { | |
| 23 | 추가: Message::ListEnd { list_id, .. } => Some(list_id.clone()), | |
| 24 | 추가: _ => None, | |
| 25 | 추가: }) | |
| 26 | 추가: .expect("작업자의 목록 식별자가 필요함"); | |
| 27 | 추가: let entries: Vec<_> = events | |
| 28 | 추가: .into_iter() | |
| 29 | 추가: .filter_map(|event| match event { | |
| 30 | 추가: Message::Entry(entry) => Some(entry), | |
| 31 | 추가: _ => None, | |
| 32 | 추가: }) | |
| 33 | 추가: .collect(); | |
| 34 | 추가: if decode { | |
| 35 | 추가: let builder = fs::DirBuilder::new(); | |
| 36 | 추가: #[cfg(unix)] | |
| 37 | 추가: let mut builder = builder; | |
| 38 | 추가: #[cfg(unix)] | |
| 39 | 추가: { | |
| 40 | 추가: use std::os::unix::fs::DirBuilderExt; | |
| 41 | 추가: builder.mode(0o700); | |
| 42 | 추가: } | |
| 43 | 추가: builder.create(scratch.0.join("output")).unwrap(); | |
| 44 | 추가: let action = Action::Extract { | |
| 45 | 추가: archive: scratch.path("input"), | |
| 46 | 추가: destination: scratch.path("output"), | |
| 47 | 추가: selection: Selection::All, | |
| 48 | 추가: gzip_output_name: (list_id.format == Format::Gzip).then(|| entries[0].name.clone()), | |
| 49 | 추가: list_id: Some(list_id.clone()), | |
| 50 | 추가: }; | |
| 51 | 추가: let start = Start { action, limits }; | |
| 52 | 추가: let extracted = if let Some(password) = password { | |
| 53 | 추가: interactive(start, |id, _, _| password_reply(id, password)) | |
| 54 | 추가: } else { | |
| 55 | 추가: execute(start) | |
| 56 | 추가: }; | |
| 57 | 추가: if terminal(&extracted).outcome != Outcome::Completed { | |
| 58 | 추가: return Err(terminal(&extracted).error); | |
| 59 | 추가: } | |
| 60 | 추가: } | |
| 61 | 추가: let members = entries | |
| 62 | 추가: .into_iter() | |
| 63 | 추가: .map(|entry| { | |
| 64 | 추가: let data = if decode && entry.kind == EntryKind::File { | |
| 65 | 추가: fs::read(scratch.0.join("output").join(&entry.name)).unwrap() | |
| 66 | 추가: } else { | |
| 67 | 추가: Vec::new() | |
| 68 | 추가: }; | |
| 69 | 추가: archive::Member { entry, data } | |
| 70 | 추가: }) | |
| 71 | 추가: .collect(); | |
| 72 | 추가: Ok(archive::Catalog { | |
| 73 | 추가: format: list_id.format, | |
| 74 | 추가: members, | |
| 75 | 추가: }) | |
| 76 | 추가: } |
| @@ -141,7 +141,7 @@ fn six_damaged_corpora_preserve_source_and_independent_expected_hashes() { | ||
| 141 | 141 | assert_eq!(t.created_count, 1); |
| 142 | 142 | assert_eq!(t.publication, Publication::Complete); |
| 143 | 143 | let bytes = fs::read(s.0.join("recovered.zip")).unwrap(); |
| 144 | 삭제: let catalog = archive::parse(&bytes, Limits::default(), true, &mut || Ok(())).unwrap(); | |
| 144 | 추가: let catalog = worker_catalog(&bytes, Limits::default(), true, None).unwrap(); | |
| 145 | 145 | assert_eq!(catalog.members.len(), count); |
| 146 | 146 | for member in &catalog.members { |
| 147 | 147 | let (_, sha) = expected |
| @@ -410,3 +410,33 @@ fn central_zip64_fields_and_extra_remain_unsupported() { | ||
| 410 | 410 | Err(ErrorCode::UnsupportedFeature) |
| 411 | 411 | )); |
| 412 | 412 | } |
| 413 | 추가: | |
| 414 | 추가: #[test] | |
| 415 | 추가: fn central_rejects_extra_crossing_only_the_next_local_file() { | |
| 416 | 추가: let mut damaged = include_bytes!("../zip-recovery-fixtures/control.zip").to_vec(); | |
| 417 | 추가: let cd = u32at(&damaged, damaged.len() - 6) as usize; | |
| 418 | 추가: let second = cd | |
| 419 | 추가: + 46 | |
| 420 | 추가: + u16at(&damaged, cd + 28) as usize | |
| 421 | 추가: + u16at(&damaged, cd + 30) as usize | |
| 422 | 추가: + u16at(&damaged, cd + 32) as usize; | |
| 423 | 추가: let next_local = u32at(&damaged, second + 42) as usize; | |
| 424 | 추가: let names_end = 30 + u16at(&damaged, 26) as usize; | |
| 425 | 추가: let extra = next_local - names_end + 1; | |
| 426 | 추가: let data_start = names_end + extra; | |
| 427 | 추가: assert!(data_start > next_local); | |
| 428 | 추가: assert!(data_start + u32at(&damaged, cd + 20) as usize <= cd); | |
| 429 | 추가: damaged[28..30].copy_from_slice(&(extra as u16).to_le_bytes()); | |
| 430 | 추가: let scratch = Scratch::new(&damaged); | |
| 431 | 추가: let mut source = File::open(scratch.path()).unwrap(); | |
| 432 | 추가: assert!(matches!( | |
| 433 | 추가: central( | |
| 434 | 추가: &mut source, | |
| 435 | 추가: damaged.len() as u64, | |
| 436 | 추가: Limits::default(), | |
| 437 | 추가: &mut || Ok(()) | |
| 438 | 추가: ), | |
| 439 | 추가: Err(ErrorCode::Integrity) | |
| 440 | 추가: )); | |
| 441 | 추가: assert_eq!(std::fs::read(scratch.path()).unwrap(), damaged); | |
| 442 | 추가: } |
| @@ -174,9 +174,14 @@ fn granted_entry_policy_keeps_independent_metadata_budget() { | ||
| 174 | 174 | ); |
| 175 | 175 | let mut metadata = crate::archive::MetadataBudget::default(); |
| 176 | 176 | for _ in 0..32768 { |
| 177 | 삭제: metadata.charge("").unwrap(); | |
| 177 | 추가: metadata | |
| 178 | 추가: .charge_with("", crate::archive::MetadataLimits::default()) | |
| 179 | 추가: .unwrap(); | |
| 178 | 180 | } |
| 179 | 삭제: assert_eq!(metadata.charge(""), Err(ErrorCode::Limit)); | |
| 181 | 추가: assert_eq!( | |
| 182 | 추가: metadata.charge_with("", crate::archive::MetadataLimits::default()), | |
| 183 | 추가: Err(ErrorCode::Limit) | |
| 184 | 추가: ); | |
| 180 | 185 | } |
| 181 | 186 | |
| 182 | 187 | #[test] |
| @@ -59,19 +59,31 @@ impl Case { | ||
| 59 | 59 | self.archive_names.sort(); |
| 60 | 60 | (self.scratch.path("archive/split.7z.001"), originals) |
| 61 | 61 | } |
| 62 | 추가: fn library(&self) -> PathBuf { | |
| 63 | 추가: let library = self.scratch.0.join("location.so"); | |
| 64 | 추가: if !library.exists() { | |
| 65 | 추가: assert!( | |
| 66 | 추가: Command::new("cc") | |
| 67 | 추가: .args(["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror"]) | |
| 68 | 추가: .arg( | |
| 69 | 추가: PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 70 | 추가: .join("../../tests/rust-archive-worker/spool_location_fault.c") | |
| 71 | 추가: ) | |
| 72 | 추가: .args(["-ldl", "-o"]) | |
| 73 | 추가: .arg(&library) | |
| 74 | 추가: .status() | |
| 75 | 추가: .unwrap() | |
| 76 | 추가: .success() | |
| 77 | 추가: ); | |
| 78 | 추가: } | |
| 79 | 추가: library | |
| 80 | 추가: } | |
| 62 | 81 | fn run(&self, action: Action, phase: &str, mask: u8, named: bool) -> Vec<Message> { |
| 63 | 82 | let s = &self.scratch; |
| 64 | 83 | let mut command = s.worker(); |
| 65 | 84 | command |
| 66 | 85 | .env("XDG_CACHE_HOME", s.0.join("cache")) |
| 67 | 삭제: .env( | |
| 68 | 삭제: "TMPDIR", | |
| 69 | 삭제: if phase == "real" { | |
| 70 | 삭제: PathBuf::from(std::env::var_os("ZIPELO_SPOOL_REAL_TMP").unwrap()) | |
| 71 | 삭제: } else { | |
| 72 | 삭제: s.0.join("system") | |
| 73 | 삭제: }, | |
| 74 | 삭제: ) | |
| 86 | 추가: .env("TMPDIR", s.0.join("system")) | |
| 75 | 87 | .env("HOME", &s.0); |
| 76 | 88 | if phase == "home" { |
| 77 | 89 | command.env_remove("XDG_CACHE_HOME"); |
| @@ -87,31 +99,14 @@ impl Case { | ||
| 87 | 99 | }, |
| 88 | 100 | ); |
| 89 | 101 | } |
| 90 | 삭제: if phase != "real" { | |
| 91 | 삭제: let library = s.0.join("location.so"); | |
| 92 | 삭제: if !library.exists() { | |
| 93 | 삭제: assert!( | |
| 94 | 삭제: Command::new("cc") | |
| 95 | 삭제: .args(["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror"]) | |
| 96 | 삭제: .arg( | |
| 97 | 삭제: PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 98 | 삭제: .join("../../tests/rust-archive-worker/spool_location_fault.c") | |
| 99 | 삭제: ) | |
| 100 | 삭제: .args(["-ldl", "-o"]) | |
| 101 | 삭제: .arg(&library) | |
| 102 | 삭제: .status() | |
| 103 | 삭제: .unwrap() | |
| 104 | 삭제: .success() | |
| 105 | 삭제: ); | |
| 106 | 삭제: } | |
| 107 | 삭제: command | |
| 108 | 삭제: .env("LD_PRELOAD", library) | |
| 109 | 삭제: .env("ZIPELO_SPOOL_PHASE", phase) | |
| 110 | 삭제: .env("ZIPELO_SPOOL_MASK", mask.to_string()) | |
| 111 | 삭제: .env("ZIPELO_SPOOL_TRACE", s.0.join("trace")); | |
| 112 | 삭제: if named { | |
| 113 | 삭제: command.env("ZIPELO_SPOOL_NAMED", "1"); | |
| 114 | 삭제: } | |
| 102 | 추가: let library = self.library(); | |
| 103 | 추가: command | |
| 104 | 추가: .env("LD_PRELOAD", library) | |
| 105 | 추가: .env("ZIPELO_SPOOL_PHASE", phase) | |
| 106 | 추가: .env("ZIPELO_SPOOL_MASK", mask.to_string()) | |
| 107 | 추가: .env("ZIPELO_SPOOL_TRACE", s.0.join("trace")); | |
| 108 | 추가: if named { | |
| 109 | 추가: command.env("ZIPELO_SPOOL_NAMED", "1"); | |
| 115 | 110 | } |
| 116 | 111 | let mut child = Running( |
| 117 | 112 | command |
| @@ -689,45 +684,117 @@ fn source_change_during_direct_read_prevents_success() { | ||
| 689 | 684 | } |
| 690 | 685 | |
| 691 | 686 | #[test] |
| 692 | 삭제: #[ignore = "별도 VM의 작은 tmpfs 경로를 ZIPELO_SPOOL_REAL_TMP로 지정해야 함"] | |
| 693 | 삭제: fn actual_tmpfs_quota_lists_and_previews_large_zip_and_seven_zip() { | |
| 694 | 삭제: let path = PathBuf::from(std::env::var_os("ZIPELO_SPOOL_REAL_TMP").unwrap()); | |
| 695 | 삭제: let filesystem = Command::new("stat") | |
| 696 | 삭제: .args(["-f", "-c", "%T"]) | |
| 697 | 삭제: .arg(&path) | |
| 687 | 추가: fn small_temporary_quota_counts_closed_files_until_unlinked() { | |
| 688 | 추가: let scratch = Scratch::new(); | |
| 689 | 추가: fs::create_dir(scratch.0.join("system")).unwrap(); | |
| 690 | 추가: let c = Case { | |
| 691 | 추가: scratch, | |
| 692 | 추가: archive_names: Vec::new(), | |
| 693 | 추가: }; | |
| 694 | 추가: let library = c.library(); | |
| 695 | 추가: let probe = Command::new("python3") | |
| 696 | 추가: .arg("-B") | |
| 697 | 추가: .arg("-c") | |
| 698 | 추가: .arg( | |
| 699 | 추가: r#" | |
| 700 | 추가: import errno, os, sys | |
| 701 | 추가: from pathlib import Path | |
| 702 | 추가: root = Path(sys.argv[1]) | |
| 703 | 추가: def assert_full(fd): | |
| 704 | 추가: try: | |
| 705 | 추가: os.write(fd, b"x") | |
| 706 | 추가: except OSError as error: | |
| 707 | 추가: assert error.errno == errno.ENOSPC | |
| 708 | 추가: else: | |
| 709 | 추가: raise AssertionError("임시 용량이 너무 일찍 반환됨") | |
| 710 | 추가: a = os.open(root / "quota-a", os.O_CREAT | os.O_RDWR, 0o600) | |
| 711 | 추가: b = os.open(root / "quota-b", os.O_CREAT | os.O_RDWR, 0o600) | |
| 712 | 추가: duplicate = os.dup(a) | |
| 713 | 추가: try: | |
| 714 | 추가: assert os.write(a, bytes(1024 * 1024)) == 1024 * 1024 | |
| 715 | 추가: os.close(duplicate) | |
| 716 | 추가: duplicate = -1 | |
| 717 | 추가: os.close(a) | |
| 718 | 추가: a = -1 | |
| 719 | 추가: assert os.write(b, bytes(2 * 1024 * 1024)) == 1024 * 1024 | |
| 720 | 추가: try: | |
| 721 | 추가: os.write(b, b"x") | |
| 722 | 추가: except OSError as error: | |
| 723 | 추가: assert error.errno == errno.ENOSPC | |
| 724 | 추가: else: | |
| 725 | 추가: raise AssertionError("닫힌 파일의 용량이 반환됨") | |
| 726 | 추가: assert (root / "quota-a").stat().st_size == 1024 * 1024 | |
| 727 | 추가: os.link(root / "quota-a", root / "quota-alias") | |
| 728 | 추가: (root / "quota-a").unlink() | |
| 729 | 추가: assert_full(b) | |
| 730 | 추가: (root / "quota-alias").unlink() | |
| 731 | 추가: assert os.write(b, bytes(1024 * 1024)) == 1024 * 1024 | |
| 732 | 추가: duplicate = os.dup(b) | |
| 733 | 추가: (root / "quota-b").unlink() | |
| 734 | 추가: assert_full(b) | |
| 735 | 추가: os.close(b) | |
| 736 | 추가: b = -1 | |
| 737 | 추가: assert_full(duplicate) | |
| 738 | 추가: os.close(duplicate) | |
| 739 | 추가: duplicate = -1 | |
| 740 | 추가: b = os.open(root / "quota-b", os.O_CREAT | os.O_RDWR, 0o600) | |
| 741 | 추가: assert os.write(b, bytes(2 * 1024 * 1024)) == 2 * 1024 * 1024 | |
| 742 | 추가: finally: | |
| 743 | 추가: if duplicate >= 0: | |
| 744 | 추가: os.close(duplicate) | |
| 745 | 추가: if a >= 0: | |
| 746 | 추가: os.close(a) | |
| 747 | 추가: if b >= 0: | |
| 748 | 추가: os.close(b) | |
| 749 | 추가: for name in ["quota-a", "quota-b", "quota-alias"]: | |
| 750 | 추가: (root / name).unlink(missing_ok=True) | |
| 751 | 추가: "#, | |
| 752 | 추가: ) | |
| 753 | 추가: .arg(c.scratch.0.join("system")) | |
| 754 | 추가: .env("TMPDIR", c.scratch.0.join("system")) | |
| 755 | 추가: .env("LD_PRELOAD", library) | |
| 756 | 추가: .env("ZIPELO_SPOOL_PHASE", "tiny-tmpfs") | |
| 757 | 추가: .env("ZIPELO_SPOOL_TRACE", c.scratch.0.join("trace")) | |
| 698 | 758 | .output() |
| 699 | 759 | .unwrap(); |
| 700 | 삭제: assert_eq!( | |
| 701 | 삭제: String::from_utf8(filesystem.stdout).unwrap().trim(), | |
| 702 | 삭제: "tmpfs" | |
| 760 | 추가: assert!( | |
| 761 | 추가: probe.status.success(), | |
| 762 | 추가: "{}", | |
| 763 | 추가: String::from_utf8_lossy(&probe.stderr) | |
| 703 | 764 | ); |
| 704 | 삭제: let capacity = Command::new("stat") | |
| 705 | 삭제: .args(["-f", "-c", "%S %b"]) | |
| 706 | 삭제: .arg(&path) | |
| 707 | 삭제: .output() | |
| 708 | 삭제: .unwrap(); | |
| 709 | 삭제: let fields: Vec<u64> = String::from_utf8(capacity.stdout) | |
| 710 | 삭제: .unwrap() | |
| 711 | 삭제: .split_whitespace() | |
| 712 | 삭제: .map(|value| value.parse().unwrap()) | |
| 713 | 삭제: .collect(); | |
| 714 | 삭제: let bytes = fields[0] * fields[1]; | |
| 715 | 삭제: assert!(bytes > 0 && bytes < 16 * 1024 * 1024, "{bytes}"); | |
| 716 | 삭제: let probe = path.join("capacity-probe"); | |
| 717 | 삭제: let error = fs::write(&probe, vec![0; bytes as usize + 4096]).unwrap_err(); | |
| 718 | 삭제: assert_eq!(error.kind(), std::io::ErrorKind::StorageFull); | |
| 719 | 삭제: fs::remove_file(probe).unwrap(); | |
| 720 | 삭제: let c = Case::new(); | |
| 721 | 삭제: for format in ["zip", "7z"] { | |
| 722 | 삭제: c.assert_preview(format, "real", 0, false); | |
| 723 | 삭제: } | |
| 724 | 삭제: assert_eq!(fs::read_dir(path).unwrap().count(), 0); | |
| 725 | 삭제: assert_eq!( | |
| 726 | 삭제: fs::metadata(c.scratch.0.join("cache/zipelo")) | |
| 765 | 추가: assert!( | |
| 766 | 추가: fs::read_to_string(c.scratch.0.join("trace")) | |
| 727 | 767 | .unwrap() |
| 728 | 삭제: .permissions() | |
| 729 | 삭제: .mode() | |
| 730 | 삭제: & 0o777, | |
| 731 | 삭제: 0o700 | |
| 768 | 추가: .contains('F') | |
| 732 | 769 | ); |
| 733 | 770 | } |
| 771 | 추가: | |
| 772 | 추가: #[test] | |
| 773 | 추가: fn small_temporary_quota_lists_and_previews_large_zip_and_seven_zip() { | |
| 774 | 추가: for named in [false, true] { | |
| 775 | 추가: let c = Case::new(); | |
| 776 | 추가: for format in ["zip", "7z"] { | |
| 777 | 추가: c.assert_preview(format, "tiny-tmpfs", 0, named); | |
| 778 | 추가: } | |
| 779 | 추가: assert!( | |
| 780 | 추가: Command::new("python3") | |
| 781 | 추가: .arg("-B") | |
| 782 | 추가: .arg( | |
| 783 | 추가: PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 784 | 추가: .join("../../tests/rust-archive-worker/spool_fallback_cases.py") | |
| 785 | 추가: ) | |
| 786 | 추가: .arg(&c.scratch.0) | |
| 787 | 추가: .arg("non-solid") | |
| 788 | 추가: .status() | |
| 789 | 추가: .unwrap() | |
| 790 | 추가: .success() | |
| 791 | 추가: ); | |
| 792 | 추가: for format in ["zip", "7z"] { | |
| 793 | 추가: c.assert_preview(format, "tiny-tmpfs", 3, named); | |
| 794 | 추가: } | |
| 795 | 추가: let trace = fs::read_to_string(c.scratch.0.join("trace")).unwrap(); | |
| 796 | 추가: assert!(trace.contains('2'), "{trace}"); | |
| 797 | 추가: assert_eq!(fs::read_dir(c.scratch.0.join("system")).unwrap().count(), 0); | |
| 798 | 추가: c.clean(); | |
| 799 | 추가: } | |
| 800 | 추가: } |
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | #define _GNU_SOURCE |
| 2 | 2 | #include <dlfcn.h> |
| 3 | 추가: #include <dirent.h> | |
| 3 | 4 | #include <errno.h> |
| 4 | 5 | #include <fcntl.h> |
| 5 | 6 | #include <stdarg.h> |
| @@ -60,10 +61,83 @@ static void trace(int at) { | ||
| 60 | 61 | close(fd); |
| 61 | 62 | } |
| 62 | 63 | } |
| 64 | 추가: struct usage { | |
| 65 | 추가: struct stat seen[256]; | |
| 66 | 추가: size_t count; | |
| 67 | 추가: unsigned long long bytes; | |
| 68 | 추가: }; | |
| 69 | 추가: static void count_file(struct usage *usage, const struct stat *value) { | |
| 70 | 추가: if (!S_ISREG(value->st_mode)) return; | |
| 71 | 추가: for (size_t i = 0; i < usage->count; i++) { | |
| 72 | 추가: if (usage->seen[i].st_dev == value->st_dev | |
| 73 | 추가: && usage->seen[i].st_ino == value->st_ino) return; | |
| 74 | 추가: } | |
| 75 | 추가: if (usage->count == 256) _exit(97); | |
| 76 | 추가: usage->seen[usage->count++] = *value; | |
| 77 | 추가: usage->bytes += value->st_size; | |
| 78 | 추가: } | |
| 79 | 추가: static void count_directory(DIR *directory, struct usage *usage, unsigned depth) { | |
| 80 | 추가: if (depth > 64) _exit(97); | |
| 81 | 추가: struct dirent *entry; | |
| 82 | 추가: while ((entry = readdir(directory))) { | |
| 83 | 추가: if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) continue; | |
| 84 | 추가: struct stat value; | |
| 85 | 추가: if (fstatat(dirfd(directory), entry->d_name, &value, AT_SYMLINK_NOFOLLOW)) _exit(98); | |
| 86 | 추가: count_file(usage, &value); | |
| 87 | 추가: if (S_ISDIR(value.st_mode)) { | |
| 88 | 추가: int fd = original_openat(dirfd(directory), entry->d_name, | |
| 89 | 추가: O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); | |
| 90 | 추가: if (fd < 0) _exit(98); | |
| 91 | 추가: DIR *child = fdopendir(fd); | |
| 92 | 추가: if (!child) _exit(98); | |
| 93 | 추가: count_directory(child, usage, depth + 1); | |
| 94 | 추가: closedir(child); | |
| 95 | 추가: } | |
| 96 | 추가: } | |
| 97 | 추가: } | |
| 98 | 추가: static unsigned long long temporary_usage(void) { | |
| 99 | 추가: struct usage usage = {0}; | |
| 100 | 추가: const char *root = getenv("TMPDIR"); | |
| 101 | 추가: if (!root) _exit(98); | |
| 102 | 추가: DIR *directory = opendir(root); | |
| 103 | 추가: if (!directory) _exit(98); | |
| 104 | 추가: // 닫혀도 이름이 남은 파일은 계속 용량을 사용함. | |
| 105 | 추가: count_directory(directory, &usage, 0); | |
| 106 | 추가: closedir(directory); | |
| 107 | 추가: directory = opendir("/proc/self/fd"); | |
| 108 | 추가: if (!directory) _exit(98); | |
| 109 | 추가: struct dirent *entry; | |
| 110 | 추가: // 이름이 지워진 열린 파일도 마지막 핸들을 닫기 전까지 집계함. | |
| 111 | 추가: while ((entry = readdir(directory))) { | |
| 112 | 추가: if (entry->d_name[0] == '.') continue; | |
| 113 | 추가: int fd = atoi(entry->d_name); | |
| 114 | 추가: char path[4096]; | |
| 115 | 추가: struct stat value; | |
| 116 | 추가: if (descriptor(fd, path, sizeof(path)) == 2 && !fstat(fd, &value)) { | |
| 117 | 추가: count_file(&usage, &value); | |
| 118 | 추가: } | |
| 119 | 추가: } | |
| 120 | 추가: closedir(directory); | |
| 121 | 추가: return usage.bytes; | |
| 122 | 추가: } | |
| 123 | 추가: static void exhausted(void) { | |
| 124 | 추가: const char *path = getenv("ZIPELO_SPOOL_TRACE"); | |
| 125 | 추가: if (!path) return; | |
| 126 | 추가: int fd = original_openat(AT_FDCWD, path, O_CREAT | O_WRONLY | O_APPEND, 0600); | |
| 127 | 추가: if (fd >= 0) { | |
| 128 | 추가: original_write(fd, "F", 1); | |
| 129 | 추가: close(fd); | |
| 130 | 추가: } | |
| 131 | 추가: } | |
| 132 | 추가: #define TEMPORARY_CAPACITY (2ULL * 1024 * 1024) | |
| 63 | 133 | static int reject(int at, int flags) { |
| 64 | 134 | if ((flags & O_TMPFILE) != O_TMPFILE && !(flags & O_CREAT)) return 0; |
| 65 | 135 | trace(at); |
| 66 | 136 | const char *phase = getenv("ZIPELO_SPOOL_PHASE"); |
| 137 | 추가: if (affected(at) && at < 2 && phase && !strcmp(phase, "tiny-tmpfs")) { | |
| 138 | 추가: errno = EDQUOT; | |
| 139 | 추가: return 1; | |
| 140 | 추가: } | |
| 67 | 141 | if (affected(at) && phase && (!strcmp(phase, "create") || !strcmp(phase, "denied"))) { |
| 68 | 142 | errno = !strcmp(phase, "denied") ? EACCES : EDQUOT; |
| 69 | 143 | return 1; |
| @@ -115,6 +189,20 @@ ssize_t write(int fd, const void *bytes, size_t count) { | ||
| 115 | 189 | char path[4096]; |
| 116 | 190 | int at = descriptor(fd, path, sizeof(path)); |
| 117 | 191 | const char *phase = getenv("ZIPELO_SPOOL_PHASE"); |
| 192 | 추가: if (phase && !strcmp(phase, "tiny-tmpfs") && at == 2) { | |
| 193 | 추가: struct stat value; | |
| 194 | 추가: off_t position = lseek(fd, 0, SEEK_CUR); | |
| 195 | 추가: if (position < 0 || fstat(fd, &value)) _exit(96); | |
| 196 | 추가: unsigned long long used = temporary_usage(); | |
| 197 | 추가: unsigned long long room = used < TEMPORARY_CAPACITY ? TEMPORARY_CAPACITY - used : 0; | |
| 198 | 추가: unsigned long long end = (unsigned long long)value.st_size + room; | |
| 199 | 추가: if ((unsigned long long)position >= end) { | |
| 200 | 추가: exhausted(); | |
| 201 | 추가: errno = ENOSPC; | |
| 202 | 추가: return -1; | |
| 203 | 추가: } | |
| 204 | 추가: if (count > end - position) count = end - position; | |
| 205 | 추가: } | |
| 118 | 206 | int stage = phase && !strcmp(phase, "stage-write") && fd != snapshot_fd; |
| 119 | 207 | if (affected(at) && phase && (stage || !strcmp(phase, "write") |
| 120 | 208 | || !strcmp(phase, "changed") || !strcmp(phase, "io"))) { |
| @@ -140,6 +228,12 @@ int fstatvfs(int fd, struct statvfs *value) { | ||
| 140 | 228 | value->f_frsize = 1; |
| 141 | 229 | value->f_bavail = 65536; |
| 142 | 230 | } |
| 231 | 추가: if (!result && at == 2 && phase && !strcmp(phase, "tiny-tmpfs")) { | |
| 232 | 추가: unsigned long long used = temporary_usage(); | |
| 233 | 추가: value->f_frsize = 1; | |
| 234 | 추가: value->f_blocks = TEMPORARY_CAPACITY; | |
| 235 | 추가: value->f_bavail = value->f_bfree = used < TEMPORARY_CAPACITY ? TEMPORARY_CAPACITY - used : 0; | |
| 236 | 추가: } | |
| 143 | 237 | return result; |
| 144 | 238 | } |
| 145 | 239 |
| @@ -122,3 +122,27 @@ fn descriptor_width_uses_sizes_and_crc() { | ||
| 122 | 122 | ); |
| 123 | 123 | } |
| 124 | 124 | } |
| 125 | 추가: | |
| 126 | 추가: #[test] | |
| 127 | 추가: fn seven_zip_parser_honors_cancellation_checkpoints() { | |
| 128 | 추가: let bytes = include_bytes!("fixtures/7z/long-match.7z"); | |
| 129 | 추가: let mut source = spool::temporary().unwrap(); | |
| 130 | 추가: source.write_all(bytes).unwrap(); | |
| 131 | 추가: let mut calls = 0; | |
| 132 | 추가: let result = Catalog::open( | |
| 133 | 추가: &mut source, | |
| 134 | 추가: bytes.len() as u64, | |
| 135 | 추가: Limits::default(), | |
| 136 | 추가: true, | |
| 137 | 추가: None, | |
| 138 | 추가: &mut || { | |
| 139 | 추가: calls += 1; | |
| 140 | 추가: if calls >= 5 { | |
| 141 | 추가: Err(ErrorCode::None) | |
| 142 | 추가: } else { | |
| 143 | 추가: Ok(()) | |
| 144 | 추가: } | |
| 145 | 추가: }, | |
| 146 | 추가: ); | |
| 147 | 추가: assert!(matches!(result, Err(ErrorCode::None))); | |
| 148 | 추가: } |
| @@ -0,0 +1,113 @@ | ||
| 1 | 추가: use super::*; | |
| 2 | 추가: | |
| 3 | 추가: fn field(tag: u16, value: &[u8]) -> Vec<u8> { | |
| 4 | 추가: [ | |
| 5 | 추가: tag.to_le_bytes().as_slice(), | |
| 6 | 추가: &(value.len() as u16).to_le_bytes(), | |
| 7 | 추가: value, | |
| 8 | 추가: ] | |
| 9 | 추가: .concat() | |
| 10 | 추가: } | |
| 11 | 추가: | |
| 12 | 추가: #[test] | |
| 13 | 추가: fn truncated_headers_and_values_are_rejected_by_every_policy() { | |
| 14 | 추가: for policy in [Policy::Streaming, Policy::Recovery, Policy::Names] { | |
| 15 | 추가: for bytes in [vec![0], vec![0, 0], vec![0, 0, 0], vec![2, 0, 1, 0]] { | |
| 16 | 추가: assert!(matches!(parse(&bytes, policy), Err(ErrorCode::Integrity))); | |
| 17 | 추가: } | |
| 18 | 추가: assert!(parse(&field(2, &[]), policy).is_ok()); | |
| 19 | 추가: } | |
| 20 | 추가: } | |
| 21 | 추가: | |
| 22 | 추가: #[test] | |
| 23 | 추가: fn zip64_is_borrowed_only_for_normal_streaming() { | |
| 24 | 추가: let bytes = field(1, &[0; 16]); | |
| 25 | 추가: assert_eq!( | |
| 26 | 추가: parse(&bytes, Policy::Streaming).unwrap().zip64, | |
| 27 | 추가: Some(&bytes[4..]) | |
| 28 | 추가: ); | |
| 29 | 추가: assert!(matches!( | |
| 30 | 추가: parse(&bytes, Policy::Recovery), | |
| 31 | 추가: Err(ErrorCode::UnsupportedFeature) | |
| 32 | 추가: )); | |
| 33 | 추가: assert!(matches!( | |
| 34 | 추가: parse( | |
| 35 | 추가: &[bytes.as_slice(), bytes.as_slice()].concat(), | |
| 36 | 추가: Policy::Streaming | |
| 37 | 추가: ), | |
| 38 | 추가: Err(ErrorCode::Integrity) | |
| 39 | 추가: )); | |
| 40 | 추가: for tag in [1, 0x0017, 0x0018] { | |
| 41 | 추가: assert!(matches!( | |
| 42 | 추가: parse(&field(tag, &[]), Policy::Recovery), | |
| 43 | 추가: Err(ErrorCode::UnsupportedFeature) | |
| 44 | 추가: )); | |
| 45 | 추가: } | |
| 46 | 추가: } | |
| 47 | 추가: | |
| 48 | 추가: #[test] | |
| 49 | 추가: fn aes_policy_keeps_support_and_duplicate_checks() { | |
| 50 | 추가: let value = [2, 0, b'A', b'E', 3, 8, 0]; | |
| 51 | 추가: let bytes = field(0x9901, &value); | |
| 52 | 추가: let policy = Policy::Streaming; | |
| 53 | 추가: assert_eq!( | |
| 54 | 추가: parse(&bytes, policy).unwrap().aes, | |
| 55 | 추가: Some(AesExtra { | |
| 56 | 추가: version: 2, | |
| 57 | 추가: method: 8 | |
| 58 | 추가: }) | |
| 59 | 추가: ); | |
| 60 | 추가: assert!(matches!( | |
| 61 | 추가: parse(&[bytes.as_slice(), bytes.as_slice()].concat(), policy), | |
| 62 | 추가: Err(ErrorCode::Integrity) | |
| 63 | 추가: )); | |
| 64 | 추가: assert!(matches!( | |
| 65 | 추가: parse(&field(0x9901, &value[..6]), policy), | |
| 66 | 추가: Err(ErrorCode::Integrity) | |
| 67 | 추가: )); | |
| 68 | 추가: let mut unsupported = value; | |
| 69 | 추가: unsupported[4] = 2; | |
| 70 | 추가: assert!(matches!( | |
| 71 | 추가: parse(&field(0x9901, &unsupported), policy), | |
| 72 | 추가: Err(ErrorCode::UnsupportedFeature) | |
| 73 | 추가: )); | |
| 74 | 추가: assert!(parse(&bytes, Policy::Recovery).unwrap().aes.is_none()); | |
| 75 | 추가: } | |
| 76 | 추가: | |
| 77 | 추가: #[test] | |
| 78 | 추가: fn unicode_and_aes_fields_can_share_the_same_extra() { | |
| 79 | 추가: let bytes = [ | |
| 80 | 추가: field(0x7075, &[1, 0, 0, 0, 0]), | |
| 81 | 추가: field(0x9901, &[2, 0, b'A', b'E', 3, 0, 0]), | |
| 82 | 추가: ] | |
| 83 | 추가: .concat(); | |
| 84 | 추가: assert_eq!( | |
| 85 | 추가: parse(&bytes, Policy::Names).unwrap().unicode, | |
| 86 | 추가: Some(&bytes[4..9]) | |
| 87 | 추가: ); | |
| 88 | 추가: assert!(parse(&bytes, Policy::Streaming).unwrap().aes.is_some()); | |
| 89 | 추가: assert!(matches!( | |
| 90 | 추가: parse(&field(0x7075, &[0; 4]), Policy::Names), | |
| 91 | 추가: Err(ErrorCode::Integrity) | |
| 92 | 추가: )); | |
| 93 | 추가: } | |
| 94 | 추가: | |
| 95 | 추가: #[test] | |
| 96 | 추가: fn mixed_invalid_fields_keep_error_precedence() { | |
| 97 | 추가: let unsupported_aes = field(0x9901, &[3, 0, b'A', b'E', 3, 0, 0]); | |
| 98 | 추가: let duplicate_zip64 = [field(1, &[]), field(1, &[])].concat(); | |
| 99 | 추가: let extra = [unsupported_aes.as_slice(), duplicate_zip64.as_slice()].concat(); | |
| 100 | 추가: assert!(matches!( | |
| 101 | 추가: parse(&extra, Policy::Streaming), | |
| 102 | 추가: Err(ErrorCode::Integrity) | |
| 103 | 추가: )); | |
| 104 | 추가: let truncated = [unsupported_aes.as_slice(), &[2, 0, 1, 0]].concat(); | |
| 105 | 추가: assert!(matches!( | |
| 106 | 추가: parse(&truncated, Policy::Streaming), | |
| 107 | 추가: Err(ErrorCode::Integrity) | |
| 108 | 추가: )); | |
| 109 | 추가: assert!(matches!( | |
| 110 | 추가: parse(&[1, 0, 8, 0], Policy::Recovery), | |
| 111 | 추가: Err(ErrorCode::UnsupportedFeature) | |
| 112 | 추가: )); | |
| 113 | 추가: } |
| @@ -107,20 +107,11 @@ fn listed_names(bytes: &[u8]) -> std::result::Result<Vec<String>, ErrorCode> { | ||
| 107 | 107 | .collect()) |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 삭제: /// 스트리밍 목록과 메모리 파서가 같은 이름 또는 같은 오류를 내는지 확인함. | |
| 111 | 삭제: fn names_agree(bytes: &[u8]) -> std::result::Result<Vec<String>, ErrorCode> { | |
| 112 | 삭제: let streamed = listed_names(bytes); | |
| 113 | 삭제: let parsed = archive::parse(bytes, Limits::default(), false, &mut || Ok(())) | |
| 114 | 삭제: .map(|c| c.members.into_iter().map(|m| m.entry.name).collect()); | |
| 115 | 삭제: assert_eq!(streamed, parsed); | |
| 116 | 삭제: streamed | |
| 117 | 삭제: } | |
| 118 | 삭제: | |
| 119 | 110 | #[test] |
| 120 | 111 | fn infozip_utf8_names_without_flag_list_and_extract() { |
| 121 | 삭제: assert_eq!(names_agree(KOREAN).unwrap(), ["README.md", "회의록.txt"]); | |
| 112 | 추가: assert_eq!(listed_names(KOREAN).unwrap(), ["README.md", "회의록.txt"]); | |
| 122 | 113 | assert_eq!( |
| 123 | 삭제: names_agree(KOREAN_TREE).unwrap(), | |
| 114 | 추가: listed_names(KOREAN_TREE).unwrap(), | |
| 124 | 115 | ["README.md", "회의록.txt", "폴더", "폴더/메모.txt"] |
| 125 | 116 | ); |
| 126 | 117 | for bytes in [KOREAN, KOREAN_TREE] { |
| @@ -149,7 +140,7 @@ fn infozip_multi_entry_zipcrypto_trees_list_and_extract() { | ||
| 149 | 140 | (ZIPCRYPTO_ASCII_TREE, "docs/notes.txt"), |
| 150 | 141 | (ZIPCRYPTO_KOREAN_TREE, "폴더/메모.txt"), |
| 151 | 142 | ] { |
| 152 | 삭제: let names = names_agree(bytes).unwrap(); | |
| 143 | 추가: let names = listed_names(bytes).unwrap(); | |
| 153 | 144 | assert!(names.iter().any(|n| n == file), "{names:?}"); |
| 154 | 145 | let s = Scratch::new(); |
| 155 | 146 | let events = interactive(encrypted_start(&s, bytes), |id, _, purpose| { |
| @@ -168,7 +159,7 @@ fn unicode_path_extra_is_used_only_when_its_crc_matches() { | ||
| 168 | 159 | let cp949 = b"\xc8\xb8\xc0\xc7\xb7\xcf.txt"; |
| 169 | 160 | let extra = unicode_path(cp949, "회의록.txt"); |
| 170 | 161 | assert_eq!( |
| 171 | 삭제: names_agree(&single_entry_zip(cp949, 0, &extra, b"hi\n")).unwrap(), | |
| 162 | 추가: listed_names(&single_entry_zip(cp949, 0, &extra, b"hi\n")).unwrap(), | |
| 172 | 163 | ["회의록.txt"] |
| 173 | 164 | ); |
| 174 | 165 | let s = Scratch::new(); |
| @@ -180,24 +171,24 @@ fn unicode_path_extra_is_used_only_when_its_crc_matches() { | ||
| 180 | 171 | // CRC가 다른 확장 필드는 이름이 바뀐 뒤 남은 낡은 값이므로 무시함. |
| 181 | 172 | let stale = unicode_path(b"old.txt", "회의록.txt"); |
| 182 | 173 | assert_eq!( |
| 183 | 삭제: names_agree(&single_entry_zip(b"new.txt", 0, &stale, b"hi\n")).unwrap(), | |
| 174 | 추가: listed_names(&single_entry_zip(b"new.txt", 0, &stale, b"hi\n")).unwrap(), | |
| 184 | 175 | ["new.txt"] |
| 185 | 176 | ); |
| 186 | 177 | let stale = unicode_path(b"other", "회의록.txt"); |
| 187 | 178 | assert_eq!( |
| 188 | 삭제: names_agree(&single_entry_zip(cp949, 0, &stale, b"hi\n")), | |
| 179 | 추가: listed_names(&single_entry_zip(cp949, 0, &stale, b"hi\n")), | |
| 189 | 180 | Err(ErrorCode::UnsupportedFeature) |
| 190 | 181 | ); |
| 191 | 182 | // UTF-8 플래그가 있으면 이름 자체가 UTF-8이므로 확장 필드를 보지 않음. |
| 192 | 183 | let ignored = unicode_path("a.txt".as_bytes(), "b.txt"); |
| 193 | 184 | assert_eq!( |
| 194 | 삭제: names_agree(&single_entry_zip(b"a.txt", 0x0800, &ignored, b"hi\n")).unwrap(), | |
| 185 | 추가: listed_names(&single_entry_zip(b"a.txt", 0x0800, &ignored, b"hi\n")).unwrap(), | |
| 195 | 186 | ["a.txt"] |
| 196 | 187 | ); |
| 197 | 188 | } |
| 198 | 189 | |
| 199 | 190 | fn assert_rejected_without_output(bytes: &[u8], error: ErrorCode) { |
| 200 | 삭제: assert_eq!(names_agree(bytes), Err(error)); | |
| 191 | 추가: assert_eq!(listed_names(bytes), Err(error)); | |
| 201 | 192 | let s = Scratch::new(); |
| 202 | 193 | let events = extract(&s, bytes, None, Selection::All, None, Limits::default()); |
| 203 | 194 | let result = terminal(&events); |
| @@ -223,7 +214,10 @@ fn converted_names_colliding_with_other_paths_are_rejected_before_writes() { | ||
| 223 | 214 | let first = b"first.txt"; |
| 224 | 215 | let second = b"second.txt"; |
| 225 | 216 | let original = stored_entries_zip(&[(first, 0, &[], b"first"), (second, 0, &[], b"second")]); |
| 226 | 삭제: assert_eq!(names_agree(&original).unwrap(), ["first.txt", "second.txt"]); | |
| 217 | 추가: assert_eq!( | |
| 218 | 추가: listed_names(&original).unwrap(), | |
| 219 | 추가: ["first.txt", "second.txt"] | |
| 220 | 추가: ); | |
| 227 | 221 | for (first_name, second_name) in [ |
| 228 | 222 | ("회의록.txt", "회의록.txt"), |
| 229 | 223 | ("폴더/Memo.txt", "폴더/memo.txt"), |
| @@ -236,7 +230,7 @@ fn converted_names_colliding_with_other_paths_are_rejected_before_writes() { | ||
| 236 | 230 | (second.as_slice(), &second_extra, second_name), |
| 237 | 231 | ] { |
| 238 | 232 | let bytes = single_entry_zip(raw, 0, extra, b"hi\n"); |
| 239 | 삭제: assert_eq!(names_agree(&bytes).unwrap(), [name]); | |
| 233 | 추가: assert_eq!(listed_names(&bytes).unwrap(), [name]); | |
| 240 | 234 | } |
| 241 | 235 | let bytes = stored_entries_zip(&[ |
| 242 | 236 | (first, 0, &first_extra, b"first"), |
| @@ -251,26 +245,26 @@ fn legacy_and_hostile_names_stay_rejected() { | ||
| 251 | 245 | let cp949 = b"\xc8\xb8\xc0\xc7\xb7\xcf.txt"; |
| 252 | 246 | // 코드 페이지를 추측하지 않음. |
| 253 | 247 | assert_eq!( |
| 254 | 삭제: names_agree(&single_entry_zip(cp949, 0, &[], b"hi\n")), | |
| 248 | 추가: listed_names(&single_entry_zip(cp949, 0, &[], b"hi\n")), | |
| 255 | 249 | Err(ErrorCode::UnsupportedFeature) |
| 256 | 250 | ); |
| 257 | 251 | // UTF-8 플래그와 맞지 않는 이름은 손상으로 봄. |
| 258 | 252 | assert_eq!( |
| 259 | 삭제: names_agree(&single_entry_zip(cp949, 0x0800, &[], b"hi\n")), | |
| 253 | 추가: listed_names(&single_entry_zip(cp949, 0x0800, &[], b"hi\n")), | |
| 260 | 254 | Err(ErrorCode::Integrity) |
| 261 | 255 | ); |
| 262 | 256 | // 확장 필드로 들어온 이름도 같은 경로 검사를 거침. |
| 263 | 257 | for hostile in ["../escape.txt", "/absolute.txt", "C:drive.txt", "CON.txt"] { |
| 264 | 258 | let extra = unicode_path(b"safe.txt", hostile); |
| 265 | 259 | assert_eq!( |
| 266 | 삭제: names_agree(&single_entry_zip(b"safe.txt", 0, &extra, b"hi\n")), | |
| 260 | 추가: listed_names(&single_entry_zip(b"safe.txt", 0, &extra, b"hi\n")), | |
| 267 | 261 | Err(ErrorCode::UnsafePath), |
| 268 | 262 | "{hostile}" |
| 269 | 263 | ); |
| 270 | 264 | } |
| 271 | 265 | for hostile in ["../회의록.txt", "폴더/../../x"] { |
| 272 | 266 | assert_eq!( |
| 273 | 삭제: names_agree(&single_entry_zip(hostile.as_bytes(), 0, &[], b"hi\n")), | |
| 267 | 추가: listed_names(&single_entry_zip(hostile.as_bytes(), 0, &[], b"hi\n")), | |
| 274 | 268 | Err(ErrorCode::UnsafePath), |
| 275 | 269 | "{hostile}" |
| 276 | 270 | ); |
| @@ -280,20 +274,20 @@ fn legacy_and_hostile_names_stay_rejected() { | ||
| 280 | 274 | let len = broken.len(); |
| 281 | 275 | broken[len - 1] = 0xff; |
| 282 | 276 | assert_eq!( |
| 283 | 삭제: names_agree(&single_entry_zip(b"safe.txt", 0, &broken, b"hi\n")), | |
| 277 | 추가: listed_names(&single_entry_zip(b"safe.txt", 0, &broken, b"hi\n")), | |
| 284 | 278 | Err(ErrorCode::Integrity) |
| 285 | 279 | ); |
| 286 | 280 | let mut short = 0x7075u16.to_le_bytes().to_vec(); |
| 287 | 281 | short.extend([4, 0, 1, 0, 0, 0]); |
| 288 | 282 | assert_eq!( |
| 289 | 삭제: names_agree(&single_entry_zip(b"safe.txt", 0, &short, b"hi\n")), | |
| 283 | 추가: listed_names(&single_entry_zip(b"safe.txt", 0, &short, b"hi\n")), | |
| 290 | 284 | Err(ErrorCode::Integrity) |
| 291 | 285 | ); |
| 292 | 286 | // APPNOTE 4.6.9에 따라 모르는 버전의 확장 필드는 무시함. |
| 293 | 287 | let mut version = unicode_path(b"safe.txt", "x"); |
| 294 | 288 | version[4] = 2; |
| 295 | 289 | assert_eq!( |
| 296 | 삭제: names_agree(&single_entry_zip(b"safe.txt", 0, &version, b"hi\n")).unwrap(), | |
| 290 | 추가: listed_names(&single_entry_zip(b"safe.txt", 0, &version, b"hi\n")).unwrap(), | |
| 297 | 291 | ["safe.txt"] |
| 298 | 292 | ); |
| 299 | 293 | } |