team-ljyzipelo

공개

미사용 메모리 해석 경로를 제거함

작성자
시로사키 하나
작성 시각
커밋
ceea4c17cdd95af12a490381dcccb71017bcad56
부모
dee7d8c
이 시점의 파일 보기

변경된 파일 6개

+35-575
수정experiments/rust-archive-worker/src/archive.rs+1-439
@@ -1,6 +1,6 @@
11use crate::protocol::*;
22use std::collections::{BTreeMap, BTreeSet};
3삭제: use zlib_rs::{Deflate, DeflateFlush, Inflate, InflateFlush, Status, crc32::crc32};
3추가: use zlib_rs::{Deflate, DeflateFlush, Status, crc32::crc32};
44
55type Result<T> = std::result::Result<T, ErrorCode>;
66pub const MAX_PATH_DEPTH: usize = 64;
@@ -56,9 +56,6 @@ impl MetadataBudget {
5656 pub(crate) fn charge_bytes(&mut self, bytes: usize, limits: MetadataLimits) -> Result<()> {
5757 self.charge_cost(bytes, 0, limits)
5858 }
59삭제: pub(crate) fn charge(&mut self, name: &str) -> Result<()> {
60삭제: self.charge_with(name, MetadataLimits::default())
61삭제: }
6259 pub(crate) fn charge_with(&mut self, name: &str, limits: MetadataLimits) -> Result<()> {
6360 // 이름 복제와 인덱스 노드 비용을 할당 전에 계산함.
6461 let cost = name
@@ -90,17 +87,6 @@ pub struct Catalog {
9087 pub format: Format,
9188 pub members: Vec<Member>,
9289}
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삭제:
10490pub fn valid_name(name: &str, directory: bool) -> Result<String> {
10591 let name = if directory {
10692 name.strip_suffix('/').unwrap_or(name)
@@ -218,61 +204,6 @@ fn bounded_append(output: &mut Vec<u8>, data: &[u8], limit: u64) -> Result<()> {
218204 output.extend_from_slice(data);
219205 Ok(())
220206}
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삭제: }
276207pub(crate) fn tar_string(b: &[u8]) -> Result<String> {
277208 let n = b.iter().position(|v| *v == 0).unwrap_or(b.len());
278209 if b[n..].iter().any(|v| *v != 0) {
@@ -294,98 +225,6 @@ pub(crate) fn octal(b: &[u8]) -> Result<u64> {
294225 }
295226 u64::from_str_radix(text, 8).map_err(|_| ErrorCode::Integrity)
296227}
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삭제: pub(crate) use crate::zip_extra::AesExtra;
386삭제: pub(crate) fn aes_extra(extra: &[u8]) -> Result<Option<AesExtra>> {
387삭제: crate::zip_extra::parse(extra, crate::zip_extra::Policy::Classic).map(|extra| extra.aes)
388삭제: }
389228/// ZIP 항목 이름을 해석함.
390229///
391230/// - UTF-8 플래그(비트 11)가 있으면 이름은 UTF-8이어야 하며 아니면 손상으로 봄.
@@ -414,283 +253,6 @@ pub(crate) fn zip_name(raw: &[u8], flags: u16, extra: &[u8]) -> Result<String> {
414253 .map(str::to_owned)
415254 .map_err(|_| ErrorCode::UnsupportedFeature)
416255}
417삭제: fn zip(
418삭제: input: &[u8],
419삭제: limits: Limits,
420삭제: decode: bool,
421삭제: password: Option<&[u8]>,
422삭제: check: &mut impl crate::resources::Monitor,
423삭제: ) -> Result<Vec<Member>> {
424삭제: if input.len() < 22 {
425삭제: return Err(ErrorCode::Integrity);
426삭제: }
427삭제: let end = (input.len().saturating_sub(65557)..=input.len() - 22)
428삭제: .rev()
429삭제: .find(|&i| {
430삭제: input.get(i..i + 4) == Some(b"PK\x05\x06")
431삭제: && u16at(input, i + 20)
432삭제: .ok()
433삭제: .is_some_and(|n| i + 22 + n as usize == input.len())
434삭제: })
435삭제: .ok_or(ErrorCode::Integrity)?;
436삭제: let count = u16at(input, end + 10)? as usize;
437삭제: if u16at(input, end + 4)? != 0
438삭제: || u16at(input, end + 6)? != 0
439삭제: || count != u16at(input, end + 8)? as usize
440삭제: {
441삭제: return Err(ErrorCode::UnsupportedFeature);
442삭제: }
443삭제: if count == 65535 || u32at(input, end + 12)? == u32::MAX || u32at(input, end + 16)? == u32::MAX
444삭제: {
445삭제: return Err(ErrorCode::UnsupportedFeature);
446삭제: }
447삭제: if count > limits.entries as usize {
448삭제: return Err(ErrorCode::Limit);
449삭제: }
450삭제: let central = u32at(input, end + 16)? as usize;
451삭제: if central.checked_add(u32at(input, end + 12)? as usize) != Some(end) {
452삭제: return Err(ErrorCode::Integrity);
453삭제: }
454삭제: let mut at = central;
455삭제: let mut members = Vec::new();
456삭제: let mut spans = Vec::new();
457삭제: let mut total = 0u64;
458삭제: let mut metadata = MetadataBudget::default();
459삭제: for id in 0..count {
460삭제: check.check()?;
461삭제: let h = part(input, at, 46)?;
462삭제: if &h[..4] != b"PK\x01\x02" {
463삭제: return Err(ErrorCode::Integrity);
464삭제: }
465삭제: let flags = u16at(h, 8)?;
466삭제: let method = u16at(h, 10)?;
467삭제: if flags & !0x080f != 0 || !matches!(method, 0 | 8 | 99) || u16at(h, 34)? != 0 {
468삭제: return Err(ErrorCode::UnsupportedFeature);
469삭제: }
470삭제: let crc = u32at(h, 16)?;
471삭제: let compressed = u32at(h, 20)? as usize;
472삭제: let size = u32at(h, 24)? as usize;
473삭제: if compressed == u32::MAX as usize || size == u32::MAX as usize {
474삭제: return Err(ErrorCode::UnsupportedFeature);
475삭제: }
476삭제: total = total
477삭제: .checked_add(size as u64)
478삭제: .filter(|n| *n <= limits.output)
479삭제: .ok_or(ErrorCode::Limit)?;
480삭제: let name_len = u16at(h, 28)? as usize;
481삭제: let extra_len = u16at(h, 30)? as usize;
482삭제: let comment_len = u16at(h, 32)? as usize;
483삭제: let extra = aes_extra(part(input, at + 46 + name_len, extra_len)?)?;
484삭제: let encrypted = flags & 1 != 0;
485삭제: if (method == 99) != extra.is_some() || (method == 99 && !encrypted) {
486삭제: return Err(ErrorCode::Integrity);
487삭제: }
488삭제: if extra.is_some_and(|e| e.version == 2) && crc != 0 {
489삭제: return Err(ErrorCode::Integrity);
490삭제: }
491삭제: let raw = part(input, at + 46, name_len)?;
492삭제: let name = zip_name(raw, flags, part(input, at + 46 + name_len, extra_len)?)?;
493삭제: let name = name.as_str();
494삭제: let mode = u32at(h, 38)? >> 16;
495삭제: let kind = match mode & 0xf000 {
496삭제: 0xa000 => EntryKind::Link,
497삭제: 0x4000 => EntryKind::Directory,
498삭제: 0 | 0x8000 => {
499삭제: if name.ends_with('/') {
500삭제: EntryKind::Directory
501삭제: } else {
502삭제: EntryKind::File
503삭제: }
504삭제: }
505삭제: _ => EntryKind::Other,
506삭제: };
507삭제: metadata.charge(name)?;
508삭제: let name = valid_name(name, kind == EntryKind::Directory)?;
509삭제: let local = u32at(h, 42)? as usize;
510삭제: let lh = part(input, local, 30)?;
511삭제: if &lh[..4] != b"PK\x03\x04" || u16at(lh, 6)? != flags || u16at(lh, 8)? != method {
512삭제: return Err(ErrorCode::Integrity);
513삭제: }
514삭제: let ln = u16at(lh, 26)? as usize;
515삭제: let le = u16at(lh, 28)? as usize;
516삭제: if aes_extra(part(input, local + 30 + ln, le)?)? != extra
517삭제: || u16at(lh, 10)? != u16at(h, 12)?
518삭제: || part(input, local + 30, ln)? != raw
519삭제: {
520삭제: return Err(ErrorCode::Integrity);
521삭제: }
522삭제: if flags & 8 == 0
523삭제: && (u32at(lh, 14)? != crc
524삭제: || u32at(lh, 18)? as usize != compressed
525삭제: || u32at(lh, 22)? as usize != size)
526삭제: {
527삭제: return Err(ErrorCode::Integrity);
528삭제: }
529삭제: let start = local
530삭제: .checked_add(30 + ln + le)
531삭제: .ok_or(ErrorCode::Integrity)?;
532삭제: let packed = part(input, start, compressed)?;
533삭제: let mut stop = start + compressed;
534삭제: if flags & 8 != 0 {
535삭제: if input.get(stop..stop + 4) == Some(b"PK\x07\x08") {
536삭제: stop += 4;
537삭제: }
538삭제: if u32at(input, stop)? != crc
539삭제: || u32at(input, stop + 4)? as usize != compressed
540삭제: || u32at(input, stop + 8)? as usize != size
541삭제: {
542삭제: return Err(ErrorCode::Integrity);
543삭제: }
544삭제: stop += 12;
545삭제: }
546삭제: if stop > central {
547삭제: return Err(ErrorCode::Integrity);
548삭제: }
549삭제: spans.push((local, stop));
550삭제: if encrypted && compressed < if extra.is_some() { 28 } else { 12 } {
551삭제: return Err(ErrorCode::Integrity);
552삭제: }
553삭제: let data = if decode {
554삭제: let decrypted;
555삭제: let packed = if encrypted {
556삭제: let password = password.ok_or(ErrorCode::WrongPassword)?;
557삭제: decrypted = if extra.is_some() {
558삭제: crate::crypto::aes_decrypt(packed, password, check)?
559삭제: } else {
560삭제: let verifier = if flags & 8 != 0 {
561삭제: (u16at(h, 12)? >> 8) as u8
562삭제: } else {
563삭제: (crc >> 24) as u8
564삭제: };
565삭제: crate::crypto::zipcrypto_decrypt(packed, password, verifier, check)?
566삭제: };
567삭제: decrypted.as_slice()
568삭제: } else {
569삭제: packed
570삭제: };
571삭제: let actual_method = extra.map_or(method, |e| e.method);
572삭제: let bytes = if actual_method == 0 {
573삭제: if packed.len() != size {
574삭제: return Err(ErrorCode::Integrity);
575삭제: }
576삭제: packed.to_vec()
577삭제: } else {
578삭제: let (out, used) = inflate(packed, false, size as u64, check)?;
579삭제: if used != packed.len() {
580삭제: return Err(ErrorCode::Integrity);
581삭제: }
582삭제: out
583삭제: };
584삭제: if bytes.len() != size
585삭제: || (extra.is_none_or(|e| e.version == 1) && crc32(0, &bytes) != crc)
586삭제: {
587삭제: return Err(ErrorCode::Integrity);
588삭제: }
589삭제: bytes
590삭제: } else {
591삭제: Vec::new()
592삭제: };
593삭제: if kind == EntryKind::Directory && size != 0 {
594삭제: return Err(ErrorCode::Integrity);
595삭제: }
596삭제: members.push(Member {
597삭제: entry: Entry {
598삭제: entry_id: id as u32,
599삭제: name,
600삭제: kind,
601삭제: size: size as u64,
602삭제: compressed_size: compressed as u64,
603삭제: encrypted,
604삭제: },
605삭제: data,
606삭제: });
607삭제: at = at
608삭제: .checked_add(46 + name_len + extra_len + comment_len)
609삭제: .ok_or(ErrorCode::Integrity)?;
610삭제: }
611삭제: if at != end {
612삭제: return Err(ErrorCode::Integrity);
613삭제: }
614삭제: spans.sort_unstable();
615삭제: let mut next = 0;
616삭제: for (start, stop) in spans {
617삭제: if start != next {
618삭제: return Err(ErrorCode::Integrity);
619삭제: }
620삭제: next = stop;
621삭제: }
622삭제: if next != central {
623삭제: return Err(ErrorCode::Integrity);
624삭제: }
625삭제: validate_members(&members)?;
626삭제: Ok(members)
627삭제: }
628삭제: pub fn parse(
629삭제: input: &[u8],
630삭제: limits: Limits,
631삭제: decode: bool,
632삭제: check: &mut impl crate::resources::Monitor,
633삭제: ) -> Result<Catalog> {
634삭제: parse_with_password(input, limits, decode, None, check)
635삭제: }
636삭제: pub fn parse_with_password(
637삭제: input: &[u8],
638삭제: limits: Limits,
639삭제: decode: bool,
640삭제: password: Option<&[u8]>,
641삭제: check: &mut impl crate::resources::Monitor,
642삭제: ) -> Result<Catalog> {
643삭제: if !limits.valid() {
644삭제: return Err(ErrorCode::Limit);
645삭제: }
646삭제: let limits = limits.legacy();
647삭제: if input.len() as u64 > limits.input {
648삭제: return Err(ErrorCode::Limit);
649삭제: }
650삭제: if input.starts_with(b"7z\xbc\xaf\x27\x1c") {
651삭제: return Ok(Catalog {
652삭제: format: Format::SevenZip,
653삭제: members: crate::seven_zip::parse(input, limits, decode, check)?,
654삭제: });
655삭제: }
656삭제: if input.starts_with(b"PK\x03\x04") || input.starts_with(b"PK\x05\x06") {
657삭제: return Ok(Catalog {
658삭제: format: Format::Zip,
659삭제: members: zip(input, limits, decode, password, check)?,
660삭제: });
661삭제: }
662삭제: if input.starts_with(&[31, 139]) {
663삭제: let bytes = gzip(input, limits, check)?;
664삭제: if bytes.len() >= 512 && matches!(&bytes[257..263], b"ustar\0" | b"ustar ") {
665삭제: return Ok(Catalog {
666삭제: format: Format::TarGzip,
667삭제: members: tar(&bytes, limits, decode, check)?,
668삭제: });
669삭제: }
670삭제: let len = bytes.len() as u64;
671삭제: return Ok(Catalog {
672삭제: format: Format::Gzip,
673삭제: members: vec![Member {
674삭제: entry: Entry {
675삭제: entry_id: 0,
676삭제: name: "GZip stream".to_owned(),
677삭제: kind: EntryKind::File,
678삭제: size: len,
679삭제: compressed_size: input.len() as u64,
680삭제: encrypted: false,
681삭제: },
682삭제: data: if decode { bytes } else { Vec::new() },
683삭제: }],
684삭제: });
685삭제: }
686삭제: if input.len() >= 512 {
687삭제: return Ok(Catalog {
688삭제: format: Format::Tar,
689삭제: members: tar(input, limits, decode, check)?,
690삭제: });
691삭제: }
692삭제: Err(ErrorCode::UnsupportedFormat)
693삭제: }
694256fn deflate(
695257 input: &[u8],
696258 limit: u64,
수정experiments/rust-archive-worker/src/crypto.rs+0-53
@@ -44,28 +44,6 @@ fn authenticate(
4444 }
4545 Ok(mac)
4646}
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삭제: }
6947pub(crate) fn aes_encrypt(
7048 packed: &[u8],
7149 password: &[u8],
@@ -134,37 +112,6 @@ const CRC_TABLE: [u32; 256] = {
134112 }
135113 table
136114};
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삭제:
168115pub(crate) enum Decryptor {
169116 Plain,
170117 Zip(Zeroizing<[u32; 3]>),
수정experiments/rust-archive-worker/src/seven_zip.rs+0-30
@@ -773,33 +773,3 @@ pub(crate) fn open_selected(
773773 data,
774774 })
775775}
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삭제: }
수정experiments/rust-archive-worker/src/zip_extra.rs+0-8
@@ -5,7 +5,6 @@ type Result<T> = std::result::Result<T, ErrorCode>;
55#[derive(Clone, Copy, PartialEq, Eq)]
66pub(crate) enum Policy {
77 Streaming,
8삭제: Classic,
98 Recovery,
109 Names,
1110}
@@ -67,18 +66,11 @@ pub(crate) fn parse(bytes: &[u8], policy: Policy) -> Result<Extra<'_>> {
6766 let end = at.checked_add(4 + length).ok_or(ErrorCode::Integrity)?;
6867 let value = bytes.get(at + 4..end).ok_or(ErrorCode::Integrity)?;
6968 match (policy, tag) {
70삭제: (Policy::Classic, 1) => return Err(ErrorCode::UnsupportedFeature),
7169 (Policy::Streaming, 1) => {
7270 if found.zip64.replace(value).is_some() {
7371 return Err(ErrorCode::Integrity);
7472 }
7573 }
76삭제: (Policy::Classic, 0x9901) => {
77삭제: if found.aes.is_some() {
78삭제: return Err(ErrorCode::Integrity);
79삭제: }
80삭제: found.aes = Some(aes(value)?);
81삭제: }
8274 (Policy::Streaming, 0x9901) => {
8375 if deferred_aes.is_some() {
8476 duplicate_aes = true;
수정tests/rust-archive-worker/resource_budget.rs+7-2
@@ -174,9 +174,14 @@ fn granted_entry_policy_keeps_independent_metadata_budget() {
174174 );
175175 let mut metadata = crate::archive::MetadataBudget::default();
176176 for _ in 0..32768 {
177삭제: metadata.charge("").unwrap();
177추가: metadata
178추가: .charge_with("", crate::archive::MetadataLimits::default())
179추가: .unwrap();
178180 }
179삭제: assert_eq!(metadata.charge(""), Err(ErrorCode::Limit));
181추가: assert_eq!(
182추가: metadata.charge_with("", crate::archive::MetadataLimits::default()),
183추가: Err(ErrorCode::Limit)
184추가: );
180185}
181186
182187#[test]
수정tests/rust-archive-worker/zip_extra.rs+27-43
@@ -11,12 +11,7 @@ fn field(tag: u16, value: &[u8]) -> Vec<u8> {
1111
1212#[test]
1313fn truncated_headers_and_values_are_rejected_by_every_policy() {
14삭제: for policy in [
15삭제: Policy::Streaming,
16삭제: Policy::Classic,
17삭제: Policy::Recovery,
18삭제: Policy::Names,
19삭제: ] {
14추가: for policy in [Policy::Streaming, Policy::Recovery, Policy::Names] {
2015 for bytes in [vec![0], vec![0, 0], vec![0, 0, 0], vec![2, 0, 1, 0]] {
2116 assert!(matches!(parse(&bytes, policy), Err(ErrorCode::Integrity)));
2217 }
@@ -31,12 +26,10 @@ fn zip64_is_borrowed_only_for_normal_streaming() {
3126 parse(&bytes, Policy::Streaming).unwrap().zip64,
3227 Some(&bytes[4..])
3328 );
34삭제: for policy in [Policy::Classic, Policy::Recovery] {
35삭제: assert!(matches!(
36삭제: parse(&bytes, policy),
37삭제: Err(ErrorCode::UnsupportedFeature)
38삭제: ));
39삭제: }
29추가: assert!(matches!(
30추가: parse(&bytes, Policy::Recovery),
31추가: Err(ErrorCode::UnsupportedFeature)
32추가: ));
4033 assert!(matches!(
4134 parse(
4235 &[bytes.as_slice(), bytes.as_slice()].concat(),
@@ -56,29 +49,28 @@ fn zip64_is_borrowed_only_for_normal_streaming() {
5649fn aes_policy_keeps_support_and_duplicate_checks() {
5750 let value = [2, 0, b'A', b'E', 3, 8, 0];
5851 let bytes = field(0x9901, &value);
59삭제: for policy in [Policy::Streaming, Policy::Classic] {
60삭제: assert_eq!(
61삭제: parse(&bytes, policy).unwrap().aes,
62삭제: Some(AesExtra {
63삭제: version: 2,
64삭제: method: 8
65삭제: })
66삭제: );
67삭제: assert!(matches!(
68삭제: parse(&[bytes.as_slice(), bytes.as_slice()].concat(), policy),
69삭제: Err(ErrorCode::Integrity)
70삭제: ));
71삭제: assert!(matches!(
72삭제: parse(&field(0x9901, &value[..6]), policy),
73삭제: Err(ErrorCode::Integrity)
74삭제: ));
75삭제: let mut unsupported = value;
76삭제: unsupported[4] = 2;
77삭제: assert!(matches!(
78삭제: parse(&field(0x9901, &unsupported), policy),
79삭제: Err(ErrorCode::UnsupportedFeature)
80삭제: ));
81삭제: }
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추가: ));
8274 assert!(parse(&bytes, Policy::Recovery).unwrap().aes.is_none());
8375}
8476
@@ -109,19 +101,11 @@ fn mixed_invalid_fields_keep_error_precedence() {
109101 parse(&extra, Policy::Streaming),
110102 Err(ErrorCode::Integrity)
111103 ));
112삭제: assert!(matches!(
113삭제: parse(&extra, Policy::Classic),
114삭제: Err(ErrorCode::UnsupportedFeature)
115삭제: ));
116104 let truncated = [unsupported_aes.as_slice(), &[2, 0, 1, 0]].concat();
117105 assert!(matches!(
118106 parse(&truncated, Policy::Streaming),
119107 Err(ErrorCode::Integrity)
120108 ));
121삭제: assert!(matches!(
122삭제: parse(&truncated, Policy::Classic),
123삭제: Err(ErrorCode::UnsupportedFeature)
124삭제: ));
125109 assert!(matches!(
126110 parse(&[1, 0, 8, 0], Policy::Recovery),
127111 Err(ErrorCode::UnsupportedFeature)