Skip to content

Commit

Permalink
refactor(core): do not panic on invalid window labels,#3544 (#3596)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Mar 3, 2022
1 parent 4d0e2ec commit 64e0054
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .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.
5 changes: 5 additions & 0 deletions .changes/label-validation.md
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Return an error when creating a window with an invalid label instead of panicking.
5 changes: 5 additions & 0 deletions .changes/runtime-window-creation-result.md
@@ -0,0 +1,5 @@
---
"tauri-runtime": patch
---

The `PendingWindow::new` and `PendingWindow::with_config` functions now return `Result<Self>` validating the window label.
3 changes: 3 additions & 0 deletions core/tauri-runtime/src/lib.rs
Expand Up @@ -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,
Expand Down
54 changes: 30 additions & 24 deletions core/tauri-runtime/src/window.rs
Expand Up @@ -128,23 +128,26 @@ impl<R: Runtime> PendingWindow<R> {
window_builder: <R::Dispatcher as Dispatch>::WindowBuilder,
webview_attributes: WebviewAttributes,
label: impl Into<String>,
) -> Self {
) -> crate::Result<Self> {
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(),
})
}
}

Expand All @@ -153,24 +156,27 @@ impl<R: Runtime> PendingWindow<R> {
window_config: WindowConfig,
webview_attributes: WebviewAttributes,
label: impl Into<String>,
) -> Self {
) -> crate::Result<Self> {
let window_builder = <<R::Dispatcher as Dispatch>::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(),
})
}
}

Expand Down
16 changes: 10 additions & 6 deletions core/tauri/src/app.rs
Expand Up @@ -382,7 +382,7 @@ macro_rules! shared_app_impl {
window_builder,
webview_attributes,
label,
))
)?)
}

#[cfg(feature = "system-tray")]
Expand Down Expand Up @@ -869,8 +869,12 @@ impl<R: Runtime> Builder<R> {
/// return (win, webview);
/// });
/// ```
#[must_use]
pub fn create_window<F>(mut self, label: impl Into<String>, url: WindowUrl, setup: F) -> Self
pub fn create_window<F>(
mut self,
label: impl Into<String>,
url: WindowUrl,
setup: F,
) -> crate::Result<Self>
where
F: FnOnce(
<R::Dispatcher as Dispatch>::WindowBuilder,
Expand All @@ -888,8 +892,8 @@ impl<R: Runtime> Builder<R> {
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.
Expand Down Expand Up @@ -1117,7 +1121,7 @@ impl<R: Runtime> Builder<R> {
config,
webview_attributes,
label,
));
)?);
}

#[cfg(any(windows, target_os = "linux"))]
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/window.rs
Expand Up @@ -204,7 +204,7 @@ impl<R: Runtime> Window<R> {
window_builder,
webview_attributes,
label,
))
)?)
}

pub(crate) fn invoke_responder(&self) -> Arc<InvokeResponder<R>> {
Expand Down
1 change: 1 addition & 0 deletions examples/multiwindow/src-tauri/src/main.rs
Expand Up @@ -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"
))
Expand Down

0 comments on commit 64e0054

Please sign in to comment.