From 64e0054299c95f10ef5a1a9d3f914bbaeff3d73f Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Thu, 3 Mar 2022 14:47:31 -0300 Subject: [PATCH] refactor(core): do not panic on invalid window labels,#3544 (#3596) --- .changes/builder-create-window-result.md | 5 ++ .changes/label-validation.md | 5 ++ .changes/runtime-window-creation-result.md | 5 ++ core/tauri-runtime/src/lib.rs | 3 ++ core/tauri-runtime/src/window.rs | 54 ++++++++++++---------- core/tauri/src/app.rs | 16 ++++--- core/tauri/src/window.rs | 2 +- examples/multiwindow/src-tauri/src/main.rs | 1 + 8 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 .changes/builder-create-window-result.md create mode 100644 .changes/label-validation.md create mode 100644 .changes/runtime-window-creation-result.md diff --git a/.changes/builder-create-window-result.md b/.changes/builder-create-window-result.md new file mode 100644 index 000000000..c8fb7a85f --- /dev/null +++ b/.changes/builder-create-window-result.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +**Breaking change:** The `Builder#create_window` API now returns a Result validating the window label. diff --git a/.changes/label-validation.md b/.changes/label-validation.md new file mode 100644 index 000000000..8d7042407 --- /dev/null +++ b/.changes/label-validation.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Return an error when creating a window with an invalid label instead of panicking. diff --git a/.changes/runtime-window-creation-result.md b/.changes/runtime-window-creation-result.md new file mode 100644 index 000000000..836ee43a4 --- /dev/null +++ b/.changes/runtime-window-creation-result.md @@ -0,0 +1,5 @@ +--- +"tauri-runtime": patch +--- + +The `PendingWindow::new` and `PendingWindow::with_config` functions now return `Result` validating the window label. diff --git a/core/tauri-runtime/src/lib.rs b/core/tauri-runtime/src/lib.rs index 51beccbb8..f18737770 100644 --- a/core/tauri-runtime/src/lib.rs +++ b/core/tauri-runtime/src/lib.rs @@ -102,6 +102,9 @@ pub enum Error { /// Failed to create window. #[error("failed to create window")] CreateWindow, + /// The given window label is invalid. + #[error("Window labels must only include alphanumeric characters, `-`, `/`, `:` and `_`.")] + InvalidWindowLabel, /// Failed to send message to webview. #[error("failed to send message to the webview")] FailedToSendMessage, diff --git a/core/tauri-runtime/src/window.rs b/core/tauri-runtime/src/window.rs index 77a2a4d9d..059cd7339 100644 --- a/core/tauri-runtime/src/window.rs +++ b/core/tauri-runtime/src/window.rs @@ -128,23 +128,26 @@ impl PendingWindow { window_builder: ::WindowBuilder, webview_attributes: WebviewAttributes, label: impl Into, - ) -> Self { + ) -> crate::Result { let mut menu_ids = HashMap::new(); if let Some(menu) = window_builder.get_menu() { get_menu_ids(&mut menu_ids, menu); } let label = label.into(); - assert_label_is_valid(&label); - Self { - window_builder, - webview_attributes, - uri_scheme_protocols: Default::default(), - label, - ipc_handler: None, - file_drop_handler: None, - url: "tauri://localhost".to_string(), - menu_ids: Arc::new(Mutex::new(menu_ids)), - js_event_listeners: Default::default(), + if !is_label_valid(&label) { + Err(crate::Error::InvalidWindowLabel) + } else { + Ok(Self { + window_builder, + webview_attributes, + uri_scheme_protocols: Default::default(), + label, + ipc_handler: None, + file_drop_handler: None, + url: "tauri://localhost".to_string(), + menu_ids: Arc::new(Mutex::new(menu_ids)), + js_event_listeners: Default::default(), + }) } } @@ -153,24 +156,27 @@ impl PendingWindow { window_config: WindowConfig, webview_attributes: WebviewAttributes, label: impl Into, - ) -> Self { + ) -> crate::Result { let window_builder = <::WindowBuilder>::with_config(window_config); let mut menu_ids = HashMap::new(); if let Some(menu) = window_builder.get_menu() { get_menu_ids(&mut menu_ids, menu); } let label = label.into(); - assert_label_is_valid(&label); - Self { - window_builder, - webview_attributes, - uri_scheme_protocols: Default::default(), - label, - ipc_handler: None, - file_drop_handler: None, - url: "tauri://localhost".to_string(), - menu_ids: Arc::new(Mutex::new(menu_ids)), - js_event_listeners: Default::default(), + if !is_label_valid(&label) { + Err(crate::Error::InvalidWindowLabel) + } else { + Ok(Self { + window_builder, + webview_attributes, + uri_scheme_protocols: Default::default(), + label, + ipc_handler: None, + file_drop_handler: None, + url: "tauri://localhost".to_string(), + menu_ids: Arc::new(Mutex::new(menu_ids)), + js_event_listeners: Default::default(), + }) } } diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index ac2499ef3..a484a9c33 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -382,7 +382,7 @@ macro_rules! shared_app_impl { window_builder, webview_attributes, label, - )) + )?) } #[cfg(feature = "system-tray")] @@ -869,8 +869,12 @@ impl Builder { /// return (win, webview); /// }); /// ``` - #[must_use] - pub fn create_window(mut self, label: impl Into, url: WindowUrl, setup: F) -> Self + pub fn create_window( + mut self, + label: impl Into, + url: WindowUrl, + setup: F, + ) -> crate::Result where F: FnOnce( ::WindowBuilder, @@ -888,8 +892,8 @@ impl Builder { window_builder, webview_attributes, label, - )); - self + )?); + Ok(self) } /// Adds the icon configured on `tauri.conf.json` to the system tray with the specified menu items. @@ -1117,7 +1121,7 @@ impl Builder { config, webview_attributes, label, - )); + )?); } #[cfg(any(windows, target_os = "linux"))] diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index b9cb482d4..5a89e321d 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -204,7 +204,7 @@ impl Window { window_builder, webview_attributes, label, - )) + )?) } pub(crate) fn invoke_responder(&self) -> Arc> { diff --git a/examples/multiwindow/src-tauri/src/main.rs b/examples/multiwindow/src-tauri/src/main.rs index d6b5b8ed2..099c6d5a4 100644 --- a/examples/multiwindow/src-tauri/src/main.rs +++ b/examples/multiwindow/src-tauri/src/main.rs @@ -24,6 +24,7 @@ fn main() { (window_builder.title("Tauri - Rust"), webview_attributes) }, ) + .unwrap() // safe to unwrap: window label is valid .run(tauri::generate_context!( "../../examples/multiwindow/src-tauri/tauri.conf.json" ))