Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(server): a wildcard in Trusted Hosts no longer accepts every Host header (#9507)
Thinh Le7e1ea93c48a2
diff --git a/docs/src/pages/docs/desktop/api-server.mdx b/docs/src/pages/docs/desktop/api-server.mdxindex 2611a05d1..0c9b0ddc5 100644--- a/docs/src/pages/docs/desktop/api-server.mdx+++ b/docs/src/pages/docs/desktop/api-server.mdx@@ -76,6 +76,8 @@ A secret key used to authenticate requests.### Trusted HostsA comma-separated list of hostnames allowed to access the server. This provides an additional layer of security when the server is exposed on your network.+- Entries are matched against the `Host` header and against the web page that sends a request. Your own computer (`127.0.0.1`, `localhost`) and private network addresses are always accepted as the `Host`.+- `*` lets a web page from any origin call the API. It does not accept any other `Host`: to reach Jan by a hostname such as `my-mac.local`, list that hostname too.### Request TimeoutHow long (in seconds) the server waits for a response from the local model before giving up. Increase this if you are running large models on slow hardware and see request timeouts.diff --git a/src-tauri/src/core/server/tests.rs b/src-tauri/src/core/server/tests.rsindex a40dde81b..38e907315 100644--- a/src-tauri/src/core/server/tests.rs+++ b/src-tauri/src/core/server/tests.rs@@ -1486,4 +1486,73 @@ mod server_tests {assert!(handle.lock().await.is_none());assert!(std::net::TcpListener::bind(("127.0.0.1", port)).is_ok());}++ /// A `*` in Trusted Hosts opens the API to any web page (CORS), but the+ /// Host header must still name this server: a DNS-rebinding page reaches+ /// it under its own hostname.+ #[tokio::test]+ async fn wildcard_trusted_hosts_still_refuse_a_foreign_host_header() {+ use std::collections::HashMap;+ use std::sync::Arc;+ use tokio::sync::Mutex;++ let port = std::net::TcpListener::bind("127.0.0.1:0")+ .unwrap()+ .local_addr()+ .unwrap()+ .port();+ let handle = Arc::new(Mutex::new(None));+ proxy::start_server(+ handle.clone(),+ Arc::new(tauri_plugin_llamacpp::state::LlamacppState::default()),+ Arc::new(Mutex::new(HashMap::new())),+ "127.0.0.1".to_string(),+ port,+ "/v1".to_string(),+ String::new(),+ vec![vec!["*".to_string()]],+ 30,+ Arc::new(Mutex::new(HashMap::new())),+ Arc::new(Mutex::new(HashMap::new())),+ Arc::new(Mutex::new(HashMap::new())),+ Arc::new(Mutex::new(crate::core::mcp::models::McpSettings::default())),+ std::env::temp_dir().to_string_lossy().into_owned(),+ false,+ true,+ )+ .await+ .expect("server starts");++ let get = |host: String, origin: Option<&'static str>| async move {+ let mut req = reqwest::Client::new()+ .get(format!("http://127.0.0.1:{port}/v1/models"))+ .header("Host", host);+ if let Some(o) = origin {+ req = req.header("Origin", o);+ }+ req.send().await.expect("request sent").status()+ };++ // The rebinding shape: attacker hostname in Host and as Origin.+ assert_eq!(+ get("attacker.example".into(), Some("http://attacker.example")).await,+ reqwest::StatusCode::FORBIDDEN+ );+ assert_eq!(+ get("attacker.example:1337".into(), None).await,+ reqwest::StatusCode::FORBIDDEN+ );+ // `*` keeps its CORS meaning: a page on another origin may call the+ // API through the server's own address.+ assert_ne!(+ get(format!("127.0.0.1:{port}"), Some("https://any-web-app.example")).await,+ reqwest::StatusCode::FORBIDDEN+ );+ assert_ne!(+ get(format!("127.0.0.1:{port}"), None).await,+ reqwest::StatusCode::FORBIDDEN+ );++ proxy::stop_server(handle).await.unwrap();+ }}diff --git a/src-tauri/utils/src/http.rs b/src-tauri/utils/src/http.rsindex cfcd8ef1b..bb9014783 100644--- a/src-tauri/utils/src/http.rs+++ b/src-tauri/utils/src/http.rs@@ -15,12 +15,14 @@ pub fn is_cors_header(header_name: &str) -> bool {header_lower.starts_with("access-control-")}-/// Validates if host is in trusted hosts list+/// Validates that a `Host` header names this server: loopback, a private-range+/// IP literal, `host.docker.internal`, or a hostname the user listed.+///+/// A `*` entry does not widen this. It means "any web page may call the API"+/// and is handled by `is_trusted_origin`; letting it also accept every `Host`+/// would switch off the check that refuses DNS-rebinding pages, which reach+/// the server under their own hostname.pub fn is_valid_host(host: &str, trusted_hosts: &[Vec<String>]) -> bool {- if trusted_hosts.iter().any(|hosts| hosts.contains(&"*".to_string())) {- return true;- }-if host.is_empty() {return false;}@@ -482,4 +484,27 @@ mod tests {assert!(!is_valid_host("obsidian.md", &listed));assert!(!is_valid_host("myapp.local:3000", &listed));}++ // `*` means "any web page may call the API" (see `is_trusted_origin`). It+ // must not also mean "any Host header": a DNS-rebinding page reaches the+ // server under its own hostname, and the Host check is what refuses it.+ #[test]+ fn wildcard_does_not_make_every_host_valid() {+ let wildcard = vec![vec!["*".to_string()]];+ assert!(!is_valid_host("evil-rebind.attacker.com", &wildcard));+ assert!(!is_valid_host("evil-rebind.attacker.com:1337", &wildcard));+ assert!(!is_valid_host("8.8.8.8", &wildcard));+ assert!(!is_valid_host("", &wildcard));+ // The server's own names still pass, as they do with an empty list.+ assert!(is_valid_host("127.0.0.1:1337", &wildcard));+ assert!(is_valid_host("localhost", &wildcard));+ assert!(is_valid_host("192.168.1.20:1337", &wildcard));+ }++ #[test]+ fn wildcard_still_honours_a_hostname_listed_next_to_it() {+ let listed = vec![vec!["*".to_string(), "myserver.local".to_string()]];+ assert!(is_valid_host("myserver.local:1337", &listed));+ assert!(!is_valid_host("evil-rebind.attacker.com", &listed));+ }}