Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(server): let Trusted Hosts opt in Origin null and file://
James Dam60a610e9e187
diff --git a/src-tauri/src/core/server/tests.rs b/src-tauri/src/core/server/tests.rsindex 1b946f800..d2dccec8b 100644--- a/src-tauri/src/core/server/tests.rs+++ b/src-tauri/src/core/server/tests.rs@@ -1082,6 +1082,31 @@ mod server_tests {assert!(!h.contains_key("access-control-allow-credentials"));}+ #[test]+ fn add_cors_headers_reflects_a_listed_opaque_origin() {+ for (origin, entry) in [("null", "null"), ("file://", "file://")] {+ let resp = proxy::add_cors_headers_with_host_and_origin(+ hyper::Response::builder(),+ "127.0.0.1:1337",+ origin,+ &vec![vec![entry.to_string()]],+ )+ .body(http_body_util::Empty::<hyper::body::Bytes>::new())+ .unwrap();+ assert_eq!(resp.headers().get("access-control-allow-origin").unwrap(), origin);++ let resp = proxy::add_cors_headers_with_host_and_origin(+ hyper::Response::builder(),+ "127.0.0.1:1337",+ origin,+ &vec![vec![]],+ )+ .body(http_body_util::Empty::<hyper::body::Bytes>::new())+ .unwrap();+ assert!(!resp.headers().contains_key("access-control-allow-origin"));+ }+ }+#[test]fn add_cors_headers_with_empty_origin_omits_origin() {let trusted = vec![vec!["localhost".to_string()]];@@ -1267,6 +1292,12 @@ mod server_tests {assert!(proxy::origin_allowed("tauri://localhost", h, &t, true));assert!(!proxy::origin_allowed("https://evil.com", h, &t, true));assert!(!proxy::origin_allowed("null", h, &t, true));+ assert!(!proxy::origin_allowed("file://", h, &t, true));+ // Opaque origins only with an explicit entry, and only with CORS on.+ assert!(proxy::origin_allowed("null", h, &trusted(&["null"]), true));+ assert!(proxy::origin_allowed("file://", h, &trusted(&["file://"]), true));+ assert!(!proxy::origin_allowed("file://", h, &trusted(&["null"]), true));+ assert!(!proxy::origin_allowed("null", h, &trusted(&["null"]), false));// Private-IP pages on the LAN are not trusted unless listed.assert!(!proxy::origin_allowed("http://192.168.1.20:3000", h, &t, true));assert!(proxy::origin_allowed(diff --git a/src-tauri/utils/src/http.rs b/src-tauri/utils/src/http.rsindex 1bf36adb1..cfcd8ef1b 100644--- a/src-tauri/utils/src/http.rs+++ b/src-tauri/utils/src/http.rs@@ -70,12 +70,13 @@ fn host_without_port(host: &str) -> &str {/// in the Trusted Hosts list. Entries match on hostname; ports are ignored./// Scheme-qualified entries (e.g. "app://obsidian.md") are skipped: they only/// apply to Origins, where the scheme is known (see `scheme_entry_matches`).+/// The `null` entry is skipped too: it opts in `Origin: null`, not a host.fn host_in_trusted_list(host: &str, trusted_hosts: &[Vec<String>]) -> bool {let host_without_port = host_without_port(host);trusted_hosts.iter().flatten()- .filter(|valid| !valid.contains("://"))+ .filter(|valid| !valid.contains("://") && !valid.trim().eq_ignore_ascii_case("null")).any(|valid| {let host_lower = host.to_lowercase();let valid_lower = valid.to_lowercase();@@ -167,6 +168,19 @@ fn parse_origin(origin: &str) -> Option<(String, String)> {Some((scheme.to_ascii_lowercase(), authority.to_ascii_lowercase()))}+/// The Trusted Hosts entry that opts in an opaque origin: `null` for+/// `Origin: null`, `file://` for a `file://` origin. None for anything else.+fn opaque_origin_entry(origin: &str) -> Option<&'static str> {+ let origin = origin.trim();+ if origin.eq_ignore_ascii_case("null") {+ Some("null")+ } else if origin.eq_ignore_ascii_case("file://") {+ Some("file://")+ } else {+ None+ }+}+/// Whether the app's webview origin is `http(s)://tauri.localhost` on this/// platform. Mirrors Tauri's `tauri_protocol_url` (and `core::navigation`):/// only Windows and Android serve custom schemes from `<scheme>.localhost`;@@ -208,11 +222,19 @@ fn is_same_or_app_origin_on(origin: &str, host_header: &str, tauri_localhost_is_/// `app://obsidian.md` (that scheme and host only), or `*` there.////// A private-range IP origin (another machine on the LAN) is NOT trusted-/// unless listed. `null` and anything malformed are untrusted.+/// unless listed. The opaque origins `null` (sandboxed frames, some Electron+/// apps, local HTML files) and `file://` are trusted only when that exact+/// entry (`null` or `file://`) is listed. Anything malformed is untrusted.pub fn is_trusted_origin(origin: &str, host_header: &str, trusted_hosts: &[Vec<String>]) -> bool {if trusted_hosts.iter().any(|hosts| hosts.iter().any(|h| h == "*")) {return true;}+ if let Some(entry) = opaque_origin_entry(origin) {+ return trusted_hosts+ .iter()+ .flatten()+ .any(|listed| listed.trim().eq_ignore_ascii_case(entry));+ }if is_same_or_app_origin(origin, host_header) {return true;}@@ -419,6 +441,35 @@ mod tests {assert!(!is_trusted_origin("app://app", h, &listed));}+ #[test]+ fn opaque_origins_need_an_explicit_entry() {+ let h = "127.0.0.1:1337";+ assert!(!is_trusted_origin("null", h, &none()));+ assert!(!is_trusted_origin("file://", h, &none()));++ let null_listed = vec![vec!["null".to_string()]];+ assert!(is_trusted_origin("null", h, &null_listed));+ assert!(is_trusted_origin("NULL", h, &null_listed));+ assert!(!is_trusted_origin("file://", h, &null_listed));++ let file_listed = vec![vec![" file:// ".to_string()]];+ assert!(is_trusted_origin("file://", h, &file_listed));+ assert!(is_trusted_origin("FILE://", h, &file_listed));+ assert!(!is_trusted_origin("null", h, &file_listed));+ // Only the bare opaque origin; a host after file:// is not one.+ assert!(!is_trusted_origin("file://evil.com", h, &file_listed));++ // A web origin is never matched by these entries.+ let both = vec![vec!["null".to_string(), "file://".to_string()]];+ assert!(!is_trusted_origin("http://null", h, &both));+ assert!(!is_trusted_origin("https://evil.com", h, &both));++ // `*` still trusts everything.+ let wildcard = vec![vec!["*".to_string()]];+ assert!(is_trusted_origin("null", h, &wildcard));+ assert!(is_trusted_origin("file://", h, &wildcard));+ }+#[test]fn scheme_qualified_entries_do_not_widen_host_check() {let listed = vec![vec![