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.

feat: warn when API server binds a public interface with no API key (#8532)

Isabel Wuf51e0648806f
diff --git a/src-tauri/src/core/server/proxy.rs b/src-tauri/src/core/server/proxy.rs
index ccad30196..81295fc08 100644
--- a/src-tauri/src/core/server/proxy.rs
+++ b/src-tauri/src/core/server/proxy.rs
@@ -2929,6 +2929,13 @@ async fn proxy_request(
.unwrap())
}
+/// True when this bind exposes the API to the network with no authentication:
+/// a non-loopback host and an empty API key.
+fn is_insecure_public_bind(host: &str, api_key: &str) -> bool {
+ let is_loopback = matches!(host, "127.0.0.1" | "localhost" | "::1");
+ !is_loopback && api_key.is_empty()
+}
+
pub(crate) fn add_cors_headers_with_host_and_origin(
builder: hyper::http::response::Builder,
_host: &str,
@@ -3035,6 +3042,10 @@ async fn start_server_internal(
// and send that literal IP as the Host header; is_valid_host now accepts loopback
// and private-range IP literals, which is safe against DNS rebinding (that sends an
// attacker *hostname*, not an IP). Hostnames still require an explicit allowlist entry.
+
+ // Calculate this before proxy_api_key is moved into ProxyConfig.
+ let insecure_public_bind = is_insecure_public_bind(&host, &proxy_api_key);
+
let config = ProxyConfig {
prefix,
proxy_api_key,
@@ -3059,6 +3070,19 @@ async fn start_server_internal(
};
log::info!("Jan API server started on http://{addr}");
+ // Security: binding to a non-loopback interface exposes the OpenAI-compatible
+ // API on the network. With no API key set, any reachable host can call it
+ // unauthenticated (model inference, model enumeration, and MCP tool execution
+ // when enabled). Warn loudly rather than refuse to start, so existing
+ // trusted-LAN setups keep working, but the operator is told.
+ if insecure_public_bind {
+ log::warn!(
+ "Jan API server is bound to a non-loopback address ({host}) with no API key set. \
+ The local API is reachable UNAUTHENTICATED by any host that can reach this machine. \
+ Set an API key in Settings > Local API Server to require authentication."
+ );
+ }
+
let server_task = tokio::spawn(async move {
loop {
let (stream, _) = match listener.accept().await {
@@ -3526,3 +3550,28 @@ async fn forward_non_streaming(
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::is_insecure_public_bind;
+
+ #[test]
+ fn loopback_never_warns() {
+ for host in ["127.0.0.1", "localhost", "::1"] {
+ assert!(!is_insecure_public_bind(host, ""), "{host} should be safe");
+ assert!(!is_insecure_public_bind(host, "secret"));
+ }
+ }
+
+ #[test]
+ fn public_bind_without_key_warns() {
+ assert!(is_insecure_public_bind("0.0.0.0", ""));
+ assert!(is_insecure_public_bind("192.168.1.10", ""));
+ }
+
+ #[test]
+ fn public_bind_with_key_is_ok() {
+ assert!(!is_insecure_public_bind("0.0.0.0", "secret"));
+ assert!(!is_insecure_public_bind("192.168.1.10", "secret"));
+ }
+}