Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

fix: sanitize download filenames

marduc812a5463a84d6e3
diff --git a/src-tauri/src/app/invoke.rs b/src-tauri/src/app/invoke.rs
index 667f2515..1cf668fd 100644
--- a/src-tauri/src/app/invoke.rs
+++ b/src-tauri/src/app/invoke.rs
@@ -1,4 +1,7 @@
-use crate::util::{check_file_or_append, get_download_message_with_lang, show_toast, MessageType};
+use crate::util::{
+ check_file_or_append, get_download_message_with_lang, sanitize_download_filename, show_toast,
+ MessageType,
+};
use std::fs::File;
use std::io::Write;
use std::str::FromStr;
@@ -93,7 +96,7 @@ pub async fn download_file(app: AppHandle, params: DownloadFileParams) -> Result
.download_dir()
.map_err(|e| format!("Failed to get download dir: {}", e))?;
- let output_path = download_dir.join(&params.filename);
+ let output_path = download_dir.join(sanitize_download_filename(&params.filename));
let path_str = output_path.to_str().ok_or("Invalid output path")?;
diff --git a/src-tauri/src/app/window.rs b/src-tauri/src/app/window.rs
index 1d1884b6..eacc6b81 100644
--- a/src-tauri/src/app/window.rs
+++ b/src-tauri/src/app/window.rs
@@ -1,6 +1,7 @@
use crate::app::config::PakeConfig;
use crate::util::{
- check_file_or_append, get_data_dir, get_download_message_with_lang, show_toast, MessageType,
+ check_file_or_append, get_data_dir, get_download_message_with_lang, sanitize_download_filename,
+ show_toast, MessageType,
};
use std::{
path::PathBuf,
@@ -464,7 +465,7 @@ fn build_window(
})
.unwrap_or_else(|| "download".to_string());
- let target = download_dir.join(filename);
+ let target = download_dir.join(sanitize_download_filename(&filename));
if let Some(path_str) = target.to_str() {
*destination = PathBuf::from(check_file_or_append(path_str));
}
diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs
index 3e178572..c5fd7f1f 100644
--- a/src-tauri/src/util.rs
+++ b/src-tauri/src/util.rs
@@ -111,6 +111,22 @@ pub fn get_download_message_with_lang(
.to_string()
}
+pub fn sanitize_download_filename(filename: &str) -> String {
+ let Some(candidate) = filename
+ .rsplit(['/', '\\'])
+ .find(|part| !part.trim().is_empty())
+ .map(str::trim)
+ else {
+ return "download".to_string();
+ };
+
+ if candidate == "." || candidate == ".." {
+ "download".to_string()
+ } else {
+ candidate.to_string()
+ }
+}
+
/// Check if the file exists. If it does, append `-N` to the stem until a free
/// path is found.
///
@@ -218,6 +234,39 @@ mod tests {
let _ = fs::remove_dir_all(path.parent().unwrap());
}
+ #[test]
+ fn sanitize_download_filename_keeps_plain_names() {
+ assert_eq!(sanitize_download_filename("report.pdf"), "report.pdf");
+ assert_eq!(sanitize_download_filename(" report.pdf "), "report.pdf");
+ }
+
+ #[test]
+ fn sanitize_download_filename_takes_the_final_path_segment() {
+ assert_eq!(
+ sanitize_download_filename("../../private/report.pdf"),
+ "report.pdf"
+ );
+ assert_eq!(
+ sanitize_download_filename("..\\private\\report.pdf"),
+ "report.pdf"
+ );
+ assert_eq!(
+ sanitize_download_filename("nested/path/archive.tar.gz"),
+ "archive.tar.gz"
+ );
+ }
+
+ #[test]
+ fn sanitize_download_filename_falls_back_for_empty_or_parent_segments() {
+ for filename in ["", " ", "/", "\\", ".", "..", "../..", "..\\.."] {
+ assert_eq!(
+ sanitize_download_filename(filename),
+ "download",
+ "{filename}"
+ );
+ }
+ }
+
#[test]
fn download_message_falls_back_to_english_for_unknown_locale() {
let msg = get_download_message_with_lang(MessageType::Start, Some("fr-FR".to_string()));