feat: validate window label [TRI-021] (#13)

This commit is contained in:
Lucas Fernandes Nogueira 2021-10-23 10:25:57 -03:00 committed by Lucas Nogueira
parent d42ccfb34f
commit 680554de3e
No known key found for this signature in database
GPG Key ID: 2714B66BCFB01F7F
5 changed files with 26 additions and 4 deletions

View File

@ -0,0 +1,6 @@
---
"tauri": patch
"tauri-runtime": patch
---
The window label is now validated and must be alphanumeric, resulting in a panic if it isn't.

View File

@ -109,6 +109,13 @@ pub struct PendingWindow<R: Runtime> {
pub js_event_listeners: Arc<Mutex<HashMap<String, HashSet<u64>>>>,
}
fn validate_label(label: &str) {
assert!(
label.chars().all(char::is_alphanumeric),
"Window label must be alphanumeric"
);
}
impl<R: Runtime> PendingWindow<R> {
/// Create a new [`PendingWindow`] with a label and starting url.
pub fn new(
@ -120,11 +127,13 @@ impl<R: Runtime> PendingWindow<R> {
if let Some(menu) = window_builder.get_menu() {
get_menu_ids(&mut menu_ids, menu);
}
let label = label.into();
validate_label(&label);
Self {
window_builder,
webview_attributes,
uri_scheme_protocols: Default::default(),
label: label.into(),
label,
ipc_handler: None,
file_drop_handler: None,
url: "tauri://localhost".to_string(),
@ -144,11 +153,13 @@ impl<R: Runtime> PendingWindow<R> {
if let Some(menu) = window_builder.get_menu() {
get_menu_ids(&mut menu_ids, menu);
}
let label = label.into();
validate_label(&label);
Self {
window_builder,
webview_attributes,
uri_scheme_protocols: Default::default(),
label: label.into(),
label,
ipc_handler: None,
file_drop_handler: None,
url: "tauri://localhost".to_string(),

View File

@ -403,7 +403,7 @@ impl CliConfig {
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct WindowConfig {
/// The window identifier.
/// The window identifier. It must be alphanumeric.
#[serde(default = "default_window_label")]
pub label: String,
/// The window webview URL.

View File

@ -1094,6 +1094,11 @@ class WindowManager extends WebviewWindowHandle {
* ```
*/
class WebviewWindow extends WindowManager {
/**
* Creates a new WebviewWindow.
* * @param label The webview window label. It must be alphanumeric.
* @returns The WebviewWindow instance to communicate with the webview.
*/
constructor(
label: WindowLabel | null | undefined,
options: WindowOptions = {}

View File

@ -1405,7 +1405,7 @@
"format": "double"
},
"label": {
"description": "The window identifier.",
"description": "The window identifier. It must be alphanumeric.",
"default": "main",
"type": "string"
},