mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-04 21:30:06 +08:00
fix(backup): reject entries shorter than their declared length
This commit is contained in:
@@ -10,4 +10,6 @@ pub enum ArchiveError {
|
||||
InvalidPath,
|
||||
#[error("archive contains profile recovery data")]
|
||||
ReservedEntry,
|
||||
#[error("backup entry length differs from its declared size")]
|
||||
SizeMismatch,
|
||||
}
|
||||
|
||||
@@ -61,9 +61,14 @@ impl<R: Read + Seek> ValidatedBackup<R> {
|
||||
return Err(ArchiveError::ReservedEntry);
|
||||
}
|
||||
}
|
||||
// ZIP extraction skips directory payloads.
|
||||
if entry.is_dir() {
|
||||
io::copy(&mut entry, &mut io::sink())?;
|
||||
// Extraction skips directory payloads and permits a decoded file shorter than its declared size.
|
||||
let size = if entry.is_dir() || entry.compression() != CompressionMethod::Stored {
|
||||
io::copy(&mut entry, &mut io::sink())?
|
||||
} else {
|
||||
entry.compressed_size()
|
||||
};
|
||||
if size != entry.size() {
|
||||
return Err(ArchiveError::SizeMismatch);
|
||||
}
|
||||
}
|
||||
Ok(Self { zip })
|
||||
@@ -80,6 +85,27 @@ mod tests {
|
||||
use super::*;
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
fn rejects_entries_shorter_than_their_declared_size() -> Result<(), ArchiveError> {
|
||||
for name in ["profiles/example.yaml", "profiles/"] {
|
||||
for method in [CompressionMethod::Stored, CompressionMethod::Deflated] {
|
||||
let mut writer = ZipWriter::new(Cursor::new(Vec::new()));
|
||||
writer.start_file(name, SimpleFileOptions::default().compression_method(method))?;
|
||||
writer.write_all(b"abc")?;
|
||||
let mut bytes = writer.finish()?.into_inner();
|
||||
let directory = ZipArchive::new(Cursor::new(&bytes))?.central_directory_start() as usize;
|
||||
// Both ZIP headers agree on a size one byte larger than the actual payload.
|
||||
bytes[22..26].copy_from_slice(&4u32.to_le_bytes());
|
||||
bytes[directory + 24..directory + 28].copy_from_slice(&4u32.to_le_bytes());
|
||||
assert!(
|
||||
ValidatedBackup::open(Cursor::new(bytes), &[]).is_err(),
|
||||
"{name} {method:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writes_and_validates_the_backup_layout() -> Result<(), ArchiveError> {
|
||||
let mut writer = BackupWriter::new(Cursor::new(Vec::new()))?;
|
||||
|
||||
Reference in New Issue
Block a user