From 07b02c9f962312f4d90c001d21d3d72ca9daa03a Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Tue, 4 Jun 2024 17:02:18 -0300 Subject: [PATCH] fix(core): test invoke key, closes #9960 (#9972) --- .changes/fix-test-invoke-key.md | 5 +++++ core/tauri/src/app.rs | 6 +++++- core/tauri/src/test/mod.rs | 8 ++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 .changes/fix-test-invoke-key.md diff --git a/.changes/fix-test-invoke-key.md b/.changes/fix-test-invoke-key.md new file mode 100644 index 000000000..2e57e3f33 --- /dev/null +++ b/.changes/fix-test-invoke-key.md @@ -0,0 +1,5 @@ +--- +"tauri": patch:bug +--- + +Added `test::INVOKE_KEY` to be used in `tauri::InvokePayload` when testing using `test::assert_ipc_response` and `test::get_ipc_response`. diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index 4283d8ac4..088f0256e 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -989,6 +989,9 @@ pub struct Builder { /// The script that initializes the `window.__TAURI_POST_MESSAGE__` function. invoke_initialization_script: String, + /// Invoke key. Used to secure IPC calls. + pub(crate) invoke_key: String, + /// The setup hook. setup: SetupHook, @@ -1047,6 +1050,7 @@ impl Builder { invoke_responder: Arc::new(window_invoke_responder), invoke_initialization_script: format!("Object.defineProperty(window, '__TAURI_POST_MESSAGE__', {{ value: (message) => window.ipc.postMessage({}(message)) }})", crate::manager::STRINGIFY_IPC_MESSAGE_FN), + invoke_key: crate::generate_invoke_key().expect("failed to generate invoke key"), on_page_load: Box::new(|_, _| ()), pending_windows: Default::default(), plugins: PluginStore::default(), @@ -1569,7 +1573,7 @@ impl Builder { self.window_event_listeners, (self.menu, self.menu_event_listeners), (self.invoke_responder, self.invoke_initialization_script), - crate::generate_invoke_key()?, + self.invoke_key, ); let http_scheme = manager.config().tauri.security.dangerous_use_http_scheme; diff --git a/core/tauri/src/test/mod.rs b/core/tauri/src/test/mod.rs index 38c651231..206442238 100644 --- a/core/tauri/src/test/mod.rs +++ b/core/tauri/src/test/mod.rs @@ -50,6 +50,7 @@ //! callback: tauri::api::ipc::CallbackFn(0), //! error: tauri::api::ipc::CallbackFn(1), //! inner: serde_json::Value::Null, +//! invoke_key: Some(tauri::test::INVOKE_KEY.into()), //! }, //! Ok(()) //! ); @@ -85,6 +86,9 @@ use tauri_utils::{ config::{CliConfig, Config, PatternKind, TauriConfig}, }; +/// The invoke key used for tests. +pub const INVOKE_KEY: &str = "__invoke-key__"; + /// A key for an [`Ipc`] call. #[derive(Eq, PartialEq)] struct IpcKey { @@ -197,6 +201,8 @@ pub fn mock_context(assets: A) -> crate::Context { pub fn mock_builder() -> Builder { let mut builder = Builder::::new().manage(Ipc(Default::default())); + builder.invoke_key = INVOKE_KEY.to_string(); + builder.invoke_responder = Arc::new(|window, response, callback, error| { let window_ = window.clone(); let ipc = window_.state::(); @@ -250,6 +256,7 @@ pub fn mock_app() -> App { /// callback: tauri::api::ipc::CallbackFn(0), /// error: tauri::api::ipc::CallbackFn(1), /// inner: serde_json::Value::Null, +/// invoke_key: Some(tauri::test::INVOKE_KEY.into()), /// }, /// // the expected response is a success with the "pong" payload /// // we could also use Err("error message") here to ensure the command failed @@ -303,6 +310,7 @@ pub fn assert_ipc_response( /// callback: tauri::api::ipc::CallbackFn(0), /// error: tauri::api::ipc::CallbackFn(1), /// inner: serde_json::Value::Null, +/// invoke_key: Some(tauri::test::INVOKE_KEY.into()), /// }); /// assert_eq!(res, Ok("pong".into())) /// ```