Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(navigation): let screenshot windows load their file: page

James Dam9a39e66b3eec
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/lib.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/lib.rs
index 7765e0650..194523468 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/lib.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/lib.rs
@@ -27,6 +27,11 @@ mod commands;
))]
mod webview_shot;
+/// Label prefix of the hidden windows the webview `screenshot` renderer opens.
+/// The app's navigation guard keys on it: those windows load their target as a
+/// `file:` URL, which no other webview may.
+pub const SCREENSHOT_WINDOW_LABEL_PREFIX: &str = "jan-shot-";
+
/// Runs the confined-spawn helper and exits, when this process was re-exec'd as
/// one by the Windows sandbox backend. A no-op on every other platform and on a
/// normal launch, but it must be called before anything else in `main`: the
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/webview_shot.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/webview_shot.rs
index 05e4be849..1ca70cf52 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/webview_shot.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/webview_shot.rs
@@ -65,7 +65,11 @@ async fn capture<R: Runtime>(
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
- let label = format!("jan-shot-{}-{nanos}", std::process::id());
+ let label = format!(
+ "{}{}-{nanos}",
+ crate::SCREENSHOT_WINDOW_LABEL_PREFIX,
+ std::process::id()
+ );
let (load_tx, load_rx) = oneshot::channel::<()>();
let load_signal = Arc::new(Mutex::new(Some(load_tx)));
diff --git a/src-tauri/src/core/navigation.rs b/src-tauri/src/core/navigation.rs
index 54923c4b0..62c967891 100644
--- a/src-tauri/src/core/navigation.rs
+++ b/src-tauri/src/core/navigation.rs
@@ -77,6 +77,15 @@ fn is_app_url_on(url: &Url, dev_url: Option<&Url>, localhost_hosts: &[&str]) ->
}
}
+/// Whether the webview labelled `label` may navigate to `url`: an app URL, or,
+/// for the hidden windows the `screenshot` tool renders into, the `file:` page
+/// it loads. Those windows match no capability, so the page gets no IPC.
+pub(crate) fn allows_navigation(label: &str, url: &Url, dev_url: Option<&Url>) -> bool {
+ is_app_url(url, dev_url)
+ || (url.scheme() == "file"
+ && label.starts_with(tauri_plugin_agent_tools::SCREENSHOT_WINDOW_LABEL_PREFIX))
+}
+
fn same_origin(a: &Url, b: &Url) -> bool {
a.scheme() == b.scheme()
&& a.host_str() == b.host_str()
@@ -114,7 +123,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
} else {
None
};
- if is_app_url(url, dev_url.as_ref()) {
+ if allows_navigation(webview.label(), url, dev_url.as_ref()) {
return true;
}
log::warn!(
@@ -248,6 +257,30 @@ mod tests {
assert!(!is_app_url(&u("http://localhost:1420/"), None));
}
+ #[test]
+ fn only_screenshot_windows_may_load_files() {
+ let file = u("file:///tmp/jan-agent-s1/chart.html");
+ let shot = format!(
+ "{}4242-1700000000",
+ tauri_plugin_agent_tools::SCREENSHOT_WINDOW_LABEL_PREFIX
+ );
+ assert!(allows_navigation(&shot, &file, None));
+ for label in [
+ "main",
+ "logs-app-window",
+ "system-monitor-window",
+ "jan-shot",
+ "x-jan-shot-1",
+ ] {
+ assert!(!allows_navigation(label, &file, None), "{label}");
+ }
+ // The exception is `file:` alone: a screenshot window is otherwise
+ // held to the app's origins like any other.
+ assert!(!allows_navigation(&shot, &u("https://evil.example/"), None));
+ assert!(!allows_navigation(&shot, &u("asset://localhost/x"), None));
+ assert!(allows_navigation("main", &u("tauri://localhost/"), None));
+ }
+
#[test]
fn only_web_urls_are_handed_to_the_browser() {
assert!(opens_externally(&u("https://jan.ai/docs")));