From 83324bfc4fa7ef3f7e1d5688fb0d5cf850ca339c Mon Sep 17 00:00:00 2001 From: Haam Date: Sat, 14 Nov 2026 14:10:24 +0900 Subject: [PATCH] fix(app): bound memory and entry count when extracting zips --- apps/app/src/api/files.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/apps/app/src/api/files.rs b/apps/app/src/api/files.rs index 279e3c6cdc5..3b811962f7c 100644 --- a/apps/app/src/api/files.rs +++ b/apps/app/src/api/files.rs @@ -13,6 +13,10 @@ use theseus::ErrorKind; use tokio::io::AsyncWriteExt; use tokio_util::sync::CancellationToken; +const MAX_EXTRACT_ENTRY_BYTES: u64 = 512 * 1024 * 1024; +const MAX_EXTRACT_TOTAL_BYTES: u64 = 2 * 1024 * 1024 * 1024; +const MAX_EXTRACT_ENTRIES: usize = 20_000; + pub fn init() -> tauri::plugin::TauriPlugin { tauri::plugin::Builder::new("files") .setup(|app, _| { @@ -221,6 +225,7 @@ pub async fn file_extract_zip( let mut archive = zip::ZipArchive::new(Cursor::new(file_bytes)) .context("failed to read zip file")?; let mut names = Vec::new(); + let mut declared_total: u64 = 0; for index in 0..archive.len() { let entry = archive .by_index_raw(index) @@ -231,8 +236,19 @@ pub async fn file_extract_zip( "invalid archive entry name".to_string(), ))? .into_owned(); + declared_total = declared_total.saturating_add(entry.size()); + if declared_total > MAX_EXTRACT_TOTAL_BYTES { + return Err(anyhow!( + "archive expands to more than {MAX_EXTRACT_TOTAL_BYTES} bytes" + )); + } names.push((index, name)); } + if names.len() > MAX_EXTRACT_ENTRIES { + return Err(anyhow!( + "archive contains more than {MAX_EXTRACT_ENTRIES} entries" + )); + } Ok::<_, anyhow::Error>((archive, names)) }) .await @@ -280,13 +296,24 @@ pub async fn file_extract_zip( let (archive, bytes) = tokio::task::spawn_blocking(move || { let mut bytes = Vec::new(); { - let mut reader = + let reader = zip_reader.by_index(index).with_context(|| { format!("failed to read archive entry {entry_path}") })?; - reader.read_to_end(&mut bytes).with_context(|| { + if reader.size() > MAX_EXTRACT_ENTRY_BYTES { + return Err(anyhow!( + "archive entry {entry_path} expands to more than {MAX_EXTRACT_ENTRY_BYTES} bytes" + )); + } + let mut limited = reader.take(MAX_EXTRACT_ENTRY_BYTES + 1); + limited.read_to_end(&mut bytes).with_context(|| { format!("failed to read archive entry {entry_path}") })?; + if bytes.len() as u64 > MAX_EXTRACT_ENTRY_BYTES { + return Err(anyhow!( + "archive entry {entry_path} expands to more than {MAX_EXTRACT_ENTRY_BYTES} bytes" + )); + } } Ok::<_, anyhow::Error>((zip_reader, bytes)) })