diff --git a/.changes/dialog-default-path-validation.md b/.changes/dialog-default-path-validation.md new file mode 100644 index 000000000..200e2bd16 --- /dev/null +++ b/.changes/dialog-default-path-validation.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Validate dialog option `defaultPath` - it must exists. diff --git a/core/tauri/src/endpoints/dialog.rs b/core/tauri/src/endpoints/dialog.rs index 02ffe0e9e..066ae3348 100644 --- a/core/tauri/src/endpoints/dialog.rs +++ b/core/tauri/src/endpoints/dialog.rs @@ -144,6 +144,9 @@ fn set_default_path( pub fn open(options: OpenDialogOptions) -> crate::Result { let mut dialog_builder = FileDialogBuilder::new(); if let Some(default_path) = options.default_path { + if !default_path.exists() { + return Err(crate::Error::DialogDefaultPathNotExists(default_path)); + } dialog_builder = set_default_path(dialog_builder, default_path); } for filter in options.filters { @@ -165,6 +168,9 @@ pub fn open(options: OpenDialogOptions) -> crate::Result { pub fn save(options: SaveDialogOptions) -> crate::Result { let mut dialog_builder = FileDialogBuilder::new(); if let Some(default_path) = options.default_path { + if !default_path.exists() { + return Err(crate::Error::DialogDefaultPathNotExists(default_path)); + } dialog_builder = set_default_path(dialog_builder, default_path); } for filter in options.filters { diff --git a/core/tauri/src/error.rs b/core/tauri/src/error.rs index b4e2c08ee..5d630c9ce 100644 --- a/core/tauri/src/error.rs +++ b/core/tauri/src/error.rs @@ -2,6 +2,8 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT +use std::path::PathBuf; + /// Runtime errors that can happen inside a Tauri application. #[derive(Debug, thiserror::Error)] pub enum Error { @@ -60,6 +62,9 @@ pub enum Error { /// Error initializing plugin. #[error("failed to initialize plugin `{0}`: {1}")] PluginInitialization(String, String), + /// `default_path` provided to dialog API doesn't exist. + #[error("failed to setup dialog: provided default path `{0}` doesn't exist")] + DialogDefaultPathNotExists(PathBuf), } impl From for Error {