Skip to content

Commit 64e0054

Browse files
authored
refactor(core): do not panic on invalid window labels,#3544 (#3596)
1 parent 4d0e2ec commit 64e0054

File tree

8 files changed

+60
-31
lines changed

8 files changed

+60
-31
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
**Breaking change:** The `Builder#create_window` API now returns a Result validating the window label.

.changes/label-validation.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Return an error when creating a window with an invalid label instead of panicking.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-runtime": patch
3+
---
4+
5+
The `PendingWindow::new` and `PendingWindow::with_config` functions now return `Result<Self>` validating the window label.

core/tauri-runtime/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ pub enum Error {
102102
/// Failed to create window.
103103
#[error("failed to create window")]
104104
CreateWindow,
105+
/// The given window label is invalid.
106+
#[error("Window labels must only include alphanumeric characters, `-`, `/`, `:` and `_`.")]
107+
InvalidWindowLabel,
105108
/// Failed to send message to webview.
106109
#[error("failed to send message to the webview")]
107110
FailedToSendMessage,

core/tauri-runtime/src/window.rs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,26 @@ impl<R: Runtime> PendingWindow<R> {
128128
window_builder: <R::Dispatcher as Dispatch>::WindowBuilder,
129129
webview_attributes: WebviewAttributes,
130130
label: impl Into<String>,
131-
) -> Self {
131+
) -> crate::Result<Self> {
132132
let mut menu_ids = HashMap::new();
133133
if let Some(menu) = window_builder.get_menu() {
134134
get_menu_ids(&mut menu_ids, menu);
135135
}
136136
let label = label.into();
137-
assert_label_is_valid(&label);
138-
Self {
139-
window_builder,
140-
webview_attributes,
141-
uri_scheme_protocols: Default::default(),
142-
label,
143-
ipc_handler: None,
144-
file_drop_handler: None,
145-
url: "tauri://localhost".to_string(),
146-
menu_ids: Arc::new(Mutex::new(menu_ids)),
147-
js_event_listeners: Default::default(),
137+
if !is_label_valid(&label) {
138+
Err(crate::Error::InvalidWindowLabel)
139+
} else {
140+
Ok(Self {
141+
window_builder,
142+
webview_attributes,
143+
uri_scheme_protocols: Default::default(),
144+
label,
145+
ipc_handler: None,
146+
file_drop_handler: None,
147+
url: "tauri://localhost".to_string(),
148+
menu_ids: Arc::new(Mutex::new(menu_ids)),
149+
js_event_listeners: Default::default(),
150+
})
148151
}
149152
}
150153

@@ -153,24 +156,27 @@ impl<R: Runtime> PendingWindow<R> {
153156
window_config: WindowConfig,
154157
webview_attributes: WebviewAttributes,
155158
label: impl Into<String>,
156-
) -> Self {
159+
) -> crate::Result<Self> {
157160
let window_builder = <<R::Dispatcher as Dispatch>::WindowBuilder>::with_config(window_config);
158161
let mut menu_ids = HashMap::new();
159162
if let Some(menu) = window_builder.get_menu() {
160163
get_menu_ids(&mut menu_ids, menu);
161164
}
162165
let label = label.into();
163-
assert_label_is_valid(&label);
164-
Self {
165-
window_builder,
166-
webview_attributes,
167-
uri_scheme_protocols: Default::default(),
168-
label,
169-
ipc_handler: None,
170-
file_drop_handler: None,
171-
url: "tauri://localhost".to_string(),
172-
menu_ids: Arc::new(Mutex::new(menu_ids)),
173-
js_event_listeners: Default::default(),
166+
if !is_label_valid(&label) {
167+
Err(crate::Error::InvalidWindowLabel)
168+
} else {
169+
Ok(Self {
170+
window_builder,
171+
webview_attributes,
172+
uri_scheme_protocols: Default::default(),
173+
label,
174+
ipc_handler: None,
175+
file_drop_handler: None,
176+
url: "tauri://localhost".to_string(),
177+
menu_ids: Arc::new(Mutex::new(menu_ids)),
178+
js_event_listeners: Default::default(),
179+
})
174180
}
175181
}
176182

core/tauri/src/app.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ macro_rules! shared_app_impl {
382382
window_builder,
383383
webview_attributes,
384384
label,
385-
))
385+
)?)
386386
}
387387

388388
#[cfg(feature = "system-tray")]
@@ -869,8 +869,12 @@ impl<R: Runtime> Builder<R> {
869869
/// return (win, webview);
870870
/// });
871871
/// ```
872-
#[must_use]
873-
pub fn create_window<F>(mut self, label: impl Into<String>, url: WindowUrl, setup: F) -> Self
872+
pub fn create_window<F>(
873+
mut self,
874+
label: impl Into<String>,
875+
url: WindowUrl,
876+
setup: F,
877+
) -> crate::Result<Self>
874878
where
875879
F: FnOnce(
876880
<R::Dispatcher as Dispatch>::WindowBuilder,
@@ -888,8 +892,8 @@ impl<R: Runtime> Builder<R> {
888892
window_builder,
889893
webview_attributes,
890894
label,
891-
));
892-
self
895+
)?);
896+
Ok(self)
893897
}
894898

895899
/// 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> {
11171121
config,
11181122
webview_attributes,
11191123
label,
1120-
));
1124+
)?);
11211125
}
11221126

11231127
#[cfg(any(windows, target_os = "linux"))]

core/tauri/src/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<R: Runtime> Window<R> {
204204
window_builder,
205205
webview_attributes,
206206
label,
207-
))
207+
)?)
208208
}
209209

210210
pub(crate) fn invoke_responder(&self) -> Arc<InvokeResponder<R>> {

examples/multiwindow/src-tauri/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn main() {
2424
(window_builder.title("Tauri - Rust"), webview_attributes)
2525
},
2626
)
27+
.unwrap() // safe to unwrap: window label is valid
2728
.run(tauri::generate_context!(
2829
"../../examples/multiwindow/src-tauri/tauri.conf.json"
2930
))

0 commit comments

Comments
 (0)