refactor(core): do not panic on invalid window labels,#3544 (#3596)

This commit is contained in:
Lucas Fernandes Nogueira 2022-03-03 14:47:31 -03:00 committed by GitHub
parent 4d0e2eccd9
commit 64e0054299
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 60 additions and 31 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": patch
---
**Breaking change:** The `Builder#create_window` API now returns a Result validating the window label.

View File

@ -0,0 +1,5 @@
---
"tauri": patch
---
Return an error when creating a window with an invalid label instead of panicking.

View File

@ -0,0 +1,5 @@
---
"tauri-runtime": patch
---
The `PendingWindow::new` and `PendingWindow::with_config` functions now return `Result<Self>` validating the window label.

View File

@ -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,

View File

@ -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(),
})
}
}
@ -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(),
})
}
}

View File

@ -382,7 +382,7 @@ macro_rules! shared_app_impl {
window_builder,
webview_attributes,
label,
))
)?)
}
#[cfg(feature = "system-tray")]
@ -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,
@ -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.
@ -1117,7 +1121,7 @@ impl<R: Runtime> Builder<R> {
config,
webview_attributes,
label,
));
)?);
}
#[cfg(any(windows, target_os = "linux"))]

View File

@ -204,7 +204,7 @@ impl<R: Runtime> Window<R> {
window_builder,
webview_attributes,
label,
))
)?)
}
pub(crate) fn invoke_responder(&self) -> Arc<InvokeResponder<R>> {

View File

@ -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"
))