Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix: accept x-api-key header in place of custom header (#7421)
Louisfc005751a3a8
diff --git a/src-tauri/src/core/server/proxy.rs b/src-tauri/src/core/server/proxy.rsindex 9ada8349b..4b3dfaf28 100644--- a/src-tauri/src/core/server/proxy.rs+++ b/src-tauri/src/core/server/proxy.rs@@ -243,22 +243,24 @@ async fn proxy_request(}if !is_whitelisted_path && !config.proxy_api_key.is_empty() {- if let Some(authorization) = parts.headers.get(hyper::header::AUTHORIZATION) {- let auth_str = authorization.to_str().unwrap_or("");+ // Check Authorization header (Bearer token)+ let auth_valid = parts+ .headers+ .get(hyper::header::AUTHORIZATION)+ .and_then(|v| v.to_str().ok())+ .and_then(|auth_str| auth_str.strip_prefix("Bearer "))+ .map(|token| token == config.proxy_api_key)+ .unwrap_or(false);++ // Check X-Api-Key header+ let api_key_valid = parts+ .headers+ .get("X-Api-Key")+ .and_then(|v| v.to_str().ok())+ .map(|key| key == config.proxy_api_key)+ .unwrap_or(false);- if auth_str.strip_prefix("Bearer ") != Some(config.proxy_api_key.as_str()) {- let mut error_response = Response::builder().status(StatusCode::UNAUTHORIZED);- error_response = add_cors_headers_with_host_and_origin(- error_response,- &host_header,- &origin_header,- &config.trusted_hosts,- );- return Ok(error_response- .body(Body::from("Invalid or missing authorization token"))- .unwrap());- }- } else {+ if !auth_valid && !api_key_valid {let mut error_response = Response::builder().status(StatusCode::UNAUTHORIZED);error_response = add_cors_headers_with_host_and_origin(error_response,@@ -267,7 +269,7 @@ async fn proxy_request(&config.trusted_hosts,);return Ok(error_response- .body(Body::from("Missing authorization header"))+ .body(Body::from("Invalid or missing authorization token")).unwrap());}} else if is_whitelisted_path {diff --git a/src-tauri/src/core/server/tests.rs b/src-tauri/src/core/server/tests.rsindex 25d5c1cf3..ec86fdbf5 100644--- a/src-tauri/src/core/server/tests.rs+++ b/src-tauri/src/core/server/tests.rs@@ -106,8 +106,198 @@ mod tests {"host","origin","user-agent",+ "x-api-key",];assert!(allowed_headers.contains(&"authorization"));assert!(allowed_headers.contains(&"content-type"));+ assert!(allowed_headers.contains(&"x-api-key"));+ }++ // Tests for X-Api-Key header authentication support+ // The proxy now accepts either Authorization: Bearer <token> or X-Api-Key: <token>++ #[test]+ fn test_bearer_token_extraction() {+ let api_key = "test-secret-key";+ let auth_header = "Bearer test-secret-key";++ let auth_valid = auth_header+ .strip_prefix("Bearer ")+ .map(|token| token == api_key)+ .unwrap_or(false);++ assert!(auth_valid);+ }++ #[test]+ fn test_bearer_token_extraction_invalid() {+ let api_key = "test-secret-key";+ let auth_header = "Bearer wrong-key";++ let auth_valid = auth_header+ .strip_prefix("Bearer ")+ .map(|token| token == api_key)+ .unwrap_or(false);++ assert!(!auth_valid);+ }++ #[test]+ fn test_bearer_token_extraction_missing_prefix() {+ let api_key = "test-secret-key";+ let auth_header = "test-secret-key"; // Missing "Bearer " prefix++ let auth_valid = auth_header+ .strip_prefix("Bearer ")+ .map(|token| token == api_key)+ .unwrap_or(false);++ assert!(!auth_valid);+ }++ #[test]+ fn test_x_api_key_validation() {+ let api_key = "test-secret-key";+ let x_api_key_header = "test-secret-key";++ let api_key_valid = x_api_key_header == api_key;++ assert!(api_key_valid);+ }++ #[test]+ fn test_x_api_key_validation_invalid() {+ let api_key = "test-secret-key";+ let x_api_key_header = "wrong-key";++ let api_key_valid = x_api_key_header == api_key;++ assert!(!api_key_valid);+ }++ #[test]+ fn test_auth_either_header_valid_bearer() {+ let api_key = "test-secret-key";+ let auth_header = Some("Bearer test-secret-key");+ let x_api_key_header: Option<&str> = None;++ let auth_valid = auth_header+ .and_then(|auth_str| auth_str.strip_prefix("Bearer "))+ .map(|token| token == api_key)+ .unwrap_or(false);++ let api_key_valid = x_api_key_header+ .map(|key| key == api_key)+ .unwrap_or(false);++ assert!(auth_valid || api_key_valid);+ }++ #[test]+ fn test_auth_either_header_valid_x_api_key() {+ let api_key = "test-secret-key";+ let auth_header: Option<&str> = None;+ let x_api_key_header = Some("test-secret-key");++ let auth_valid = auth_header+ .and_then(|auth_str| auth_str.strip_prefix("Bearer "))+ .map(|token| token == api_key)+ .unwrap_or(false);++ let api_key_valid = x_api_key_header+ .map(|key| key == api_key)+ .unwrap_or(false);++ assert!(auth_valid || api_key_valid);+ }++ #[test]+ fn test_auth_both_headers_missing() {+ let api_key = "test-secret-key";+ let auth_header: Option<&str> = None;+ let x_api_key_header: Option<&str> = None;++ let auth_valid = auth_header+ .and_then(|auth_str| auth_str.strip_prefix("Bearer "))+ .map(|token| token == api_key)+ .unwrap_or(false);++ let api_key_valid = x_api_key_header+ .map(|key| key == api_key)+ .unwrap_or(false);++ assert!(!auth_valid && !api_key_valid);+ }++ #[test]+ fn test_auth_both_headers_invalid() {+ let api_key = "test-secret-key";+ let auth_header = Some("Bearer wrong-key");+ let x_api_key_header = Some("also-wrong-key");++ let auth_valid = auth_header+ .and_then(|auth_str| auth_str.strip_prefix("Bearer "))+ .map(|token| token == api_key)+ .unwrap_or(false);++ let api_key_valid = x_api_key_header+ .map(|key| key == api_key)+ .unwrap_or(false);++ assert!(!auth_valid && !api_key_valid);+ }++ #[test]+ fn test_auth_both_headers_one_valid() {+ let api_key = "test-secret-key";+ // Bearer is wrong but X-Api-Key is correct+ let auth_header = Some("Bearer wrong-key");+ let x_api_key_header = Some("test-secret-key");++ let auth_valid = auth_header+ .and_then(|auth_str| auth_str.strip_prefix("Bearer "))+ .map(|token| token == api_key)+ .unwrap_or(false);++ let api_key_valid = x_api_key_header+ .map(|key| key == api_key)+ .unwrap_or(false);++ // Should pass if either is valid+ assert!(auth_valid || api_key_valid);+ }++ #[test]+ fn test_x_api_key_in_cors_allowed_headers() {+ // Verify x-api-key is in the CORS allowed headers list used by the proxy+ let allowed_headers = [+ "accept",+ "accept-language",+ "authorization",+ "cache-control",+ "connection",+ "content-type",+ "dnt",+ "host",+ "if-modified-since",+ "keep-alive",+ "origin",+ "user-agent",+ "x-api-key",+ "x-csrf-token",+ "x-forwarded-for",+ "x-forwarded-host",+ "x-forwarded-proto",+ "x-requested-with",+ "x-stainless-arch",+ "x-stainless-lang",+ "x-stainless-os",+ "x-stainless-package-version",+ "x-stainless-retry-count",+ "x-stainless-runtime",+ "x-stainless-runtime-version",+ "x-stainless-timeout",+ ];+ assert!(allowed_headers.contains(&"x-api-key"));}}