Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(webview): keep app windows on app origins
James Dam1a8e05af7810
diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rsindex ebf4cbed8..823de1e41 100644--- a/src-tauri/src/core/mod.rs+++ b/src-tauri/src/core/mod.rs@@ -9,6 +9,9 @@ pub mod downloads;#[cfg(not(feature = "cli"))]pub mod filesystem;pub mod mcp;+// Desktop-only: the webview navigation guard.+#[cfg(not(feature = "cli"))]+pub mod navigation;pub mod openai_schema;pub mod server;// Desktop-only app setup (tray, theme, window wiring); pulls in Tauri GUI typesdiff --git a/src-tauri/src/core/navigation.rs b/src-tauri/src/core/navigation.rsnew file mode 100644index 000000000..34640a0cb--- /dev/null+++ b/src-tauri/src/core/navigation.rs@@ -0,0 +1,201 @@+//! Keeps every app webview on the app's own pages.+//!+//! The windows render content the app does not control (model output, tool+//! results, previews), so a link or a script can ask a frame to leave the+//! bundled frontend. Any navigation to a URL that is not one of the app's own+//! origins is cancelled; an `http(s)` one is handed to the system browser+//! instead, which is what a user clicking such a link expects.+//!+//! Installed as a plugin so the hook covers every webview, including the ones+//! the frontend creates at runtime (log viewer, system monitor), not just the+//! main window from the config.++use std::sync::Mutex;+use std::time::{Duration, Instant};++use tauri::plugin::{Builder, TauriPlugin};+use tauri::{Manager, Runtime, Url};+use tauri_plugin_opener::OpenerExt;++/// Hosts the app's own content is served from on Windows and Android, where+/// custom schemes are spelled `http(s)://<scheme>.localhost`.+const APP_HOSTS: &[&str] = &["tauri.localhost", "preview.localhost"];++/// Custom schemes the app's frames may load on macOS, Linux and iOS. `asset:`+/// and `ipc:` are deliberately absent: they serve files and IPC responses, and+/// no window or frame is ever meant to *navigate* to them.+const APP_SCHEMES: &[&str] = &["tauri", "preview"];++/// Whether a navigation to `url` stays on the app's own content.+///+/// `dev_url` is the configured `devUrl`, and only passed in dev builds, where+/// the frontend is served from it.+///+/// The webview asks for subframe navigations too on macOS and Linux, so the+/// HTML preview frames (`about:srcdoc`, the `preview:` scheme) must pass.+pub(crate) fn is_app_url(url: &Url, dev_url: Option<&Url>) -> bool {+ match url.scheme() {+ s if APP_SCHEMES.contains(&s) => url.host_str() == Some("localhost"),+ "about" => matches!(url.path(), "blank" | "srcdoc"),+ // A blob the app itself minted (e.g. a CSV export) carries the+ // creator's origin inside; one from an opaque-origin preview frame says+ // `null`, and only that frame can navigate to it.+ "blob" => {+ let inner = url.path();+ inner.starts_with("null/")+ || Url::parse(inner)+ .map(|inner| inner.scheme() != "blob" && is_app_url(&inner, dev_url))+ .unwrap_or(false)+ }+ "http" | "https" => {+ url.host_str().is_some_and(|h| APP_HOSTS.contains(&h))+ || dev_url.is_some_and(|dev| same_origin(url, dev))+ }+ _ => false,+ }+}++fn same_origin(a: &Url, b: &Url) -> bool {+ a.scheme() == b.scheme()+ && a.host_str() == b.host_str()+ && a.port_or_known_default() == b.port_or_known_default()+}++/// Whether a refused navigation should open in the system browser.+pub(crate) fn opens_externally(url: &Url) -> bool {+ matches!(url.scheme(), "http" | "https")+}++/// At most one hand-off per this window. A refused navigation is normally a+/// click, but a script in a preview frame can retry one in a loop, and each try+/// would otherwise open another browser tab.+const HANDOFF_INTERVAL: Duration = Duration::from_secs(1);++fn handoff_allowed(last: &Mutex<Option<Instant>>, now: Instant) -> bool {+ let mut last = last.lock().unwrap_or_else(|e| e.into_inner());+ match *last {+ Some(prev) if now.saturating_duration_since(prev) < HANDOFF_INTERVAL => false,+ _ => {+ *last = Some(now);+ true+ }+ }+}++pub fn init<R: Runtime>() -> TauriPlugin<R> {+ static LAST_HANDOFF: Mutex<Option<Instant>> = Mutex::new(None);++ Builder::new("navigation-guard")+ .on_navigation(|webview, url| {+ let dev_url = if tauri::is_dev() {+ webview.config().build.dev_url.clone()+ } else {+ None+ };+ if is_app_url(url, dev_url.as_ref()) {+ return true;+ }+ log::warn!(+ "Blocked navigation of webview '{}' to {}",+ webview.label(),+ url+ );+ if opens_externally(url) && handoff_allowed(&LAST_HANDOFF, Instant::now()) {+ if let Err(e) = webview.opener().open_url(url.as_str(), None::<&str>) {+ log::error!("Failed to open {url} in the system browser: {e}");+ }+ }+ false+ })+ .build()+}++#[cfg(test)]+mod tests {+ use super::*;++ fn u(s: &str) -> Url {+ Url::parse(s).unwrap()+ }++ #[test]+ fn app_origins_are_allowed() {+ for ok in [+ "tauri://localhost/",+ "tauri://localhost/threads/abc?x=1#y",+ "http://tauri.localhost/",+ "https://tauri.localhost/settings",+ "preview://localhost/%2FUsers%2Fme%2Fdeck.html",+ "http://preview.localhost/C%3A%2Fdeck.html",+ "about:blank",+ "about:srcdoc",+ "blob:tauri://localhost/2b6f0cc9-1f0a-4c55-a0b8-6e4f1a0b5a4e",+ "blob:http://tauri.localhost/2b6f0cc9-1f0a-4c55-a0b8-6e4f1a0b5a4e",+ "blob:null/2b6f0cc9-1f0a-4c55-a0b8-6e4f1a0b5a4e",+ ] {+ assert!(is_app_url(&u(ok), None), "{ok} should be allowed");+ }+ }++ #[test]+ fn everything_else_is_refused() {+ for bad in [+ "http://evil.example/",+ "https://evil.example/",+ "http://localhost:1420/",+ "http://127.0.0.1:1337/",+ "http://tauri.localhost.evil.example/",+ "http://evil.example/?h=tauri.localhost",+ "tauri://evil.example/",+ "preview://evil.example/",+ "asset://localhost/%2Ftmp%2Fx.html",+ "http://asset.localhost/%2Ftmp%2Fx.html",+ "ipc://localhost/plugin%3Aopener%7Copen_url",+ "http://ipc.localhost/",+ "file:///etc/passwd",+ "data:text/html,<script>1</script>",+ "javascript:alert(1)",+ "about:config",+ "blob:https://evil.example/2b6f0cc9",+ "blob:asset://localhost/x",+ "jan://open",+ "mailto:[email protected]",+ ] {+ assert!(!is_app_url(&u(bad), None), "{bad} should be refused");+ }+ }++ #[test]+ fn dev_url_is_allowed_only_when_passed_and_only_its_origin() {+ let dev = u("http://localhost:1420");+ assert!(is_app_url(&u("http://localhost:1420/"), Some(&dev)));+ assert!(is_app_url(+ &u("http://localhost:1420/threads/1"),+ Some(&dev)+ ));+ assert!(is_app_url(&u("blob:http://localhost:1420/abc"), Some(&dev)));+ assert!(!is_app_url(&u("http://localhost:1421/"), Some(&dev)));+ assert!(!is_app_url(&u("https://localhost:1420/"), Some(&dev)));+ assert!(!is_app_url(&u("http://127.0.0.1:1420/"), Some(&dev)));+ assert!(!is_app_url(&u("http://localhost:1420/"), None));+ }++ #[test]+ fn only_web_urls_are_handed_to_the_browser() {+ assert!(opens_externally(&u("https://jan.ai/docs")));+ assert!(opens_externally(&u("http://example.com/")));+ assert!(!opens_externally(&u("file:///etc/passwd")));+ assert!(!opens_externally(&u("asset://localhost/x")));+ assert!(!opens_externally(&u("jan://open")));+ assert!(!opens_externally(&u("javascript:alert(1)")));+ }++ #[test]+ fn handoffs_are_throttled() {+ let last = Mutex::new(None);+ let t0 = Instant::now();+ assert!(handoff_allowed(&last, t0));+ assert!(!handoff_allowed(&last, t0 + Duration::from_millis(200)));+ assert!(handoff_allowed(&last, t0 + HANDOFF_INTERVAL));+ }+}diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rsindex dd9efe0df..84338e80c 100644--- a/src-tauri/src/lib.rs+++ b/src-tauri/src/lib.rs@@ -284,6 +284,8 @@ pub fn run() {}));let mut app_builder = builder+ // First, so no other plugin's hook sees a navigation it refuses.+ .plugin(core::navigation::init()).plugin(tauri_plugin_os::init()).plugin(tauri_plugin_opener::init()).plugin(tauri_plugin_http::init())