diff --git a/runtime/src/app_window.rs b/runtime/src/app_window.rs index d33fdc52..d53a2f6d 100644 --- a/runtime/src/app_window.rs +++ b/runtime/src/app_window.rs @@ -203,6 +203,14 @@ fn is_local_host(host: Option<&str>) -> bool { matches!(host, Some("localhost") | Some("127.0.0.1") | Some("::1") | Some("[::1]")) } +fn is_tauri_asset_host(host: Option<&str>) -> bool { + matches!(host, Some("tauri.localhost")) +} + +fn is_tauri_asset_url(url: &tauri::Url) -> bool { + matches!(url.scheme(), "http" | "https") && is_tauri_asset_host(url.host_str()) +} + fn is_local_http_url(url: &tauri::Url) -> bool { matches!(url.scheme(), "http" | "https") && is_local_host(url.host_str()) } @@ -220,6 +228,10 @@ fn should_open_in_system_browser(webview: &tauri::Webview, _ => return false, } + if is_tauri_asset_url(url) { + return false; + } + if let Some(approved_app_url) = APPROVED_APP_URL.lock().unwrap().as_ref() { if same_origin(approved_app_url, url) { return false; @@ -942,6 +954,36 @@ fn validate_shortcut_syntax(shortcut: &str) -> bool { has_key } +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn tauri_localhost_is_tauri_asset_url() { + let https_url = tauri::Url::parse("https://tauri.localhost/index.html").unwrap(); + let http_url = tauri::Url::parse("http://tauri.localhost/index.html").unwrap(); + + assert!(is_tauri_asset_url(&https_url)); + assert!(is_tauri_asset_url(&http_url)); + } + + #[test] + fn localhost_app_url_is_not_tauri_asset_url() { + let url = tauri::Url::parse("http://localhost:12345/").unwrap(); + + assert!(!is_tauri_asset_url(&url)); + assert!(is_local_http_url(&url)); + } + + #[test] + fn external_url_is_not_internal_url() { + let url = tauri::Url::parse("https://example.com/").unwrap(); + + assert!(!is_tauri_asset_url(&url)); + assert!(!is_local_http_url(&url)); + } +} + fn set_pdfium_path(path_resolver: &PathResolver) { let resource_dir = match path_resolver.resource_dir() { Ok(path) => path,