From 0fa0fa4ccf3f7241397b0e01ae4df5989df80cc2 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Wed, 14 Jun 2023 19:39:38 +0300 Subject: [PATCH] fix: check if url local with platform custom protocol, closes #7176 (#7202) * fix: check if url local with platform custom protocol, closes #7176 * Update core/tauri/src/window.rs --- core/tauri/src/manager.rs | 12 ++++++++---- core/tauri/src/window.rs | 11 ++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/core/tauri/src/manager.rs b/core/tauri/src/manager.rs index b7409b5c9..b3b0ffc40 100644 --- a/core/tauri/src/manager.rs +++ b/core/tauri/src/manager.rs @@ -377,13 +377,17 @@ impl WindowManager { pub(crate) fn get_url(&self) -> Cow<'_, Url> { match self.base_path() { AppUrl::Url(WindowUrl::External(url)) => Cow::Borrowed(url), - #[cfg(windows)] - _ => Cow::Owned(Url::parse("https://tauri.localhost").unwrap()), - #[cfg(not(windows))] - _ => Cow::Owned(Url::parse("tauri://localhost").unwrap()), + _ => self.protocol_url(), } } + pub(crate) fn protocol_url(&self) -> Cow<'_, Url> { + #[cfg(any(window, target_os = "android"))] + return Cow::Owned(Url::parse("https://tauri.localhost").unwrap()); + #[cfg(not(any(window, target_os = "android")))] + Cow::Owned(Url::parse("tauri://localhost").unwrap()) + } + fn csp(&self) -> Option { if cfg!(feature = "custom-protocol") { self.inner.config.tauri.security.csp.clone() diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index 383d6f329..e7b87a92a 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -1653,13 +1653,18 @@ impl Window { self.current_url = url; } + fn is_local_url(&self, current_url: &Url) -> bool { + self.manager.get_url().make_relative(current_url).is_some() || { + let protocol_url = self.manager.protocol_url(); + current_url.scheme() == protocol_url.scheme() && current_url.domain() == protocol_url.domain() + } + } + /// Handles this window receiving an [`InvokeMessage`]. pub fn on_message(self, payload: InvokePayload) -> crate::Result<()> { let manager = self.manager.clone(); let current_url = self.url(); - let config_url = manager.get_url(); - let is_local = - config_url.make_relative(¤t_url).is_some() || current_url.scheme() == "tauri"; + let is_local = self.is_local_url(¤t_url); let mut scope_not_found_error_message = ipc_scope_not_found_error_message(&self.window.label, current_url.as_str());