浏览代码

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

Lucas Fernandes Nogueira 3 年之前
父节点
当前提交
680554de3e

+ 6 - 0
.changes/validate-window-label.md

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

+ 13 - 2
core/tauri-runtime/src/window.rs

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

+ 1 - 1
core/tauri-utils/src/config.rs

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

+ 5 - 0
tooling/api/src/window.ts

@@ -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 = {}

+ 1 - 1
tooling/cli.rs/schema.json

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