diff --git a/litellm-rust/crates/ai-gateway/src/audio_transcription/hooks.rs b/litellm-rust/crates/ai-gateway/src/audio_transcription/hooks.rs index dbe2d3a325b..55536a7a618 100644 --- a/litellm-rust/crates/ai-gateway/src/audio_transcription/hooks.rs +++ b/litellm-rust/crates/ai-gateway/src/audio_transcription/hooks.rs @@ -271,7 +271,10 @@ fn core_error_kind(error: &Error) -> &'static str { match error { Error::Auth(_) => "AuthError", Error::InvalidProvider(_) => "InvalidProvider", - Error::InvalidRequest(_) => "InvalidRequest", + Error::InvalidRequest(_) + | Error::InvalidHeaderType { .. } + | Error::InvalidHeaderName + | Error::InvalidHeaderValue { .. } => "InvalidRequest", Error::InvalidType { .. } => "InvalidType", Error::MissingField(_) => "MissingField", Error::Http { .. } => "HttpError", diff --git a/litellm-rust/crates/ai-gateway/src/io/responses_ws.rs b/litellm-rust/crates/ai-gateway/src/io/responses_ws.rs index 9393eca06bc..7fd8c58ecc8 100644 --- a/litellm-rust/crates/ai-gateway/src/io/responses_ws.rs +++ b/litellm-rust/crates/ai-gateway/src/io/responses_ws.rs @@ -2,6 +2,8 @@ use std::collections::HashMap; use std::sync::Arc; use std::time::Duration; +use futures_util::stream::{SplitSink, SplitStream}; +use futures_util::{Sink, SinkExt, Stream, StreamExt}; use tokio::net::TcpStream; use tokio::sync::Mutex; use tokio_tungstenite::tungstenite::Message; @@ -9,18 +11,16 @@ use tokio_tungstenite::tungstenite::client::IntoClientRequest; use tokio_tungstenite::tungstenite::http::HeaderValue; use tokio_tungstenite::tungstenite::http::header::{AUTHORIZATION, HeaderName}; use tokio_tungstenite::{MaybeTlsStream, WebSocketStream}; -use futures_util::stream::{SplitSink, SplitStream}; -use futures_util::{Sink, SinkExt, Stream, StreamExt}; use litellm_core::providers::openai::responses::transformation::OPENAI_RESPONSES_WS_CONFIG; use litellm_core::responses::types::ResponsesWsEvent; use litellm_core::responses::websocket::ResponsesWebSocketProviderConfig; use litellm_core::{AuthError, Error}; -use crate::io::tls::connect_upstream; use crate::constants::{ DEFAULT_RESPONSES_WS_CONNECT_TIMEOUT_SECS, DEFAULT_RESPONSES_WS_IDLE_TIMEOUT_SECS, }; +use crate::io::tls::connect_upstream; const OPENAI_API_KEY_ENV: &str = "OPENAI_API_KEY"; const MISSING_KEY_MESSAGE: &str = "Missing OpenAI API Key - a Responses WebSocket call is being made but no key was passed via params or the OPENAI_API_KEY environment variable"; diff --git a/litellm-rust/crates/ai-gateway/src/ocr/common_utils.rs b/litellm-rust/crates/ai-gateway/src/ocr/common_utils.rs index e5cc05d5be8..5393a9dc6ec 100644 --- a/litellm-rust/crates/ai-gateway/src/ocr/common_utils.rs +++ b/litellm-rust/crates/ai-gateway/src/ocr/common_utils.rs @@ -1,6 +1,8 @@ use std::net::IpAddr; use std::time::{Duration, Instant}; +use reqwest::header::HeaderMap; + use base64::Engine; use base64::engine::general_purpose::STANDARD as BASE64_STANDARD; use litellm_core::error::Error; @@ -24,22 +26,8 @@ pub(super) fn truncate_error_body(body: &str) -> String { pub(super) fn string_headers( extra_headers: Option>, -) -> Result, Error> { - extra_headers - .unwrap_or_default() - .into_iter() - .map(|(key, value)| { - value - .as_str() - .map(|value| (key.clone(), value.to_string())) - .ok_or_else(|| { - Error::InvalidRequest(format!( - "OCR extra_headers.{key} must be a string, got {}", - litellm_core::error::json_type_name(&value) - )) - }) - }) - .collect() +) -> Result { + litellm_core::http_utils::header_map("OCR", extra_headers) } fn document_url_field(document: &Value) -> Result, Error> { @@ -306,7 +294,7 @@ fn operation_status(response_json: &Value) -> Result<&str, Error> { pub(super) async fn poll_document_intelligence( operation_url: &str, original_url: &str, - headers: &[(String, String)], + headers: &HeaderMap, timeout: Option, ) -> Result { if !same_origin(operation_url, original_url) { @@ -328,10 +316,8 @@ pub(super) async fn poll_document_intelligence( } let mut request_builder = http_client().get(operation_url); - for (key, value) in headers { - if key.eq_ignore_ascii_case("ocp-apim-subscription-key") { - request_builder = request_builder.header(key, value); - } + if let Some(value) = headers.get("ocp-apim-subscription-key") { + request_builder = request_builder.header("ocp-apim-subscription-key", value); } let response = request_builder .send() @@ -446,8 +432,8 @@ mod tests { .clone(); assert_eq!( - string_headers(Some(headers)).expect("string headers accepted"), - vec![("x-trace-id".to_string(), "trace-1".to_string())] + string_headers(Some(headers)).expect("string headers accepted")["x-trace-id"], + "trace-1" ); } @@ -463,9 +449,11 @@ mod tests { let err = string_headers(Some(headers)).expect_err("non-string header rejected"); assert_eq!( err, - Error::InvalidRequest( - "OCR extra_headers.x-retry-count must be a string, got number".to_string() - ) + Error::InvalidHeaderType { + context: "OCR", + name: "x-retry-count".into(), + actual: "number" + } ); } } diff --git a/litellm-rust/crates/ai-gateway/src/ocr/handler.rs b/litellm-rust/crates/ai-gateway/src/ocr/handler.rs index 6c6e12724cd..253cc07c1e7 100644 --- a/litellm-rust/crates/ai-gateway/src/ocr/handler.rs +++ b/litellm-rust/crates/ai-gateway/src/ocr/handler.rs @@ -14,10 +14,10 @@ pub(crate) async fn execute_ocr_provider_call( hooks: &OcrLifecycleHooks, ) -> Result { let request = hooks.prepare_provider_request(request).await?; - let mut request_builder = http_client().post(&request.url).json(&request.body); - for (key, value) in &request.upstream_headers { - request_builder = request_builder.header(key, value); - } + let mut request_builder = http_client() + .post(&request.url) + .headers(request.upstream_headers.clone()) + .json(&request.body); if let Some(duration) = request.timeout { request_builder = request_builder.timeout(duration); } diff --git a/litellm-rust/crates/ai-gateway/src/ocr/hooks.rs b/litellm-rust/crates/ai-gateway/src/ocr/hooks.rs index 806817abc0a..5c7cfa2b181 100644 --- a/litellm-rust/crates/ai-gateway/src/ocr/hooks.rs +++ b/litellm-rust/crates/ai-gateway/src/ocr/hooks.rs @@ -1,3 +1,5 @@ +use reqwest::header::HeaderMap; + use litellm_core::call_lifecycle::{CallLifecycleContext, CallLifecycleHooks, CallLifecycleTiming}; use litellm_core::error::Error; use litellm_core::ocr::prepare::map_ocr_params; @@ -192,7 +194,7 @@ async fn upload_reducto_document( document: &Value, api_base: Option<&str>, timeout: Option, - upstream_headers: &[(String, String)], + upstream_headers: &HeaderMap, ) -> Result { let source = extract_document_source(document)?; let Some(upload) = build_upload_request(source, api_base) else { @@ -203,14 +205,13 @@ async fn upload_reducto_document( .mime_str(&upload.mime_type) .map_err(|error| Error::InvalidRequest(error.to_string()))?; let form = reqwest::multipart::Form::new().part("file", part); - let mut request_builder = http_client().post(upload.url).multipart(form); - for (name, value) in upstream_headers { - if !name.eq_ignore_ascii_case("content-type") - && !name.eq_ignore_ascii_case("content-length") - { - request_builder = request_builder.header(name, value); - } - } + let mut headers = upstream_headers.clone(); + headers.remove(reqwest::header::CONTENT_TYPE); + headers.remove(reqwest::header::CONTENT_LENGTH); + let mut request_builder = http_client() + .post(upload.url) + .headers(headers) + .multipart(form); if let Some(timeout) = timeout { request_builder = request_builder.timeout(timeout); } @@ -381,7 +382,10 @@ fn core_error_kind(error: &Error) -> &'static str { match error { Error::Auth(_) => "AuthError", Error::InvalidProvider(_) => "InvalidProvider", - Error::InvalidRequest(_) => "InvalidRequest", + Error::InvalidRequest(_) + | Error::InvalidHeaderType { .. } + | Error::InvalidHeaderName + | Error::InvalidHeaderValue { .. } => "InvalidRequest", Error::InvalidType { .. } => "InvalidType", Error::MissingField(_) => "MissingField", Error::Http { .. } => "HttpError", diff --git a/litellm-rust/crates/ai-gateway/src/ocr/prepare.rs b/litellm-rust/crates/ai-gateway/src/ocr/prepare.rs index 47bfa2eb947..aedeea574bf 100644 --- a/litellm-rust/crates/ai-gateway/src/ocr/prepare.rs +++ b/litellm-rust/crates/ai-gateway/src/ocr/prepare.rs @@ -115,8 +115,9 @@ mod tests { let prepared = prepare_ocr_call(request); - assert!( - matches!(prepared.request.config, Err(Error::Auth(AuthError::InvalidConfiguration(message))) if message.contains("tenant_id")) - ); + assert!(matches!( + prepared.request.config, + Err(Error::Auth(AuthError::InvalidAzureConfigType { name })) if name == "tenant_id" + )); } } diff --git a/litellm-rust/crates/ai-gateway/src/ocr/types.rs b/litellm-rust/crates/ai-gateway/src/ocr/types.rs index ba486c56a14..11a9da71f08 100644 --- a/litellm-rust/crates/ai-gateway/src/ocr/types.rs +++ b/litellm-rust/crates/ai-gateway/src/ocr/types.rs @@ -1,6 +1,8 @@ use std::sync::Arc; use std::time::Duration; +use reqwest::header::HeaderMap; + use litellm_core::call_lifecycle::{CallLifecycleContext, CallLifecycleRequest}; use litellm_core::ocr::transformation::OcrProviderConfig; use litellm_core::ocr::types::OcrAuthInputs; @@ -56,6 +58,6 @@ pub(crate) struct ProviderOcrRequest { pub(crate) url: String, pub(crate) body: Value, pub(crate) optional_params: Map, - pub(crate) upstream_headers: Vec<(String, String)>, + pub(crate) upstream_headers: HeaderMap, pub(crate) timeout: Option, } diff --git a/litellm-rust/crates/ai-gateway/src/routes/messages/mod.rs b/litellm-rust/crates/ai-gateway/src/routes/messages/mod.rs index bb9f3851a77..5688cd92888 100644 --- a/litellm-rust/crates/ai-gateway/src/routes/messages/mod.rs +++ b/litellm-rust/crates/ai-gateway/src/routes/messages/mod.rs @@ -113,6 +113,9 @@ impl IntoResponse for MessagesRouteError { | Error::Network(_) | Error::Connect(_) | Error::InvalidResponse(_) + | Error::InvalidHeaderType { .. } + | Error::InvalidHeaderName + | Error::InvalidHeaderValue { .. } | Error::InvalidType { .. } | Error::MissingField(_) => ( StatusCode::BAD_GATEWAY, diff --git a/litellm-rust/crates/core/src/auth/azure/native.rs b/litellm-rust/crates/core/src/auth/azure/native.rs index 2b7a8153623..32f0065657b 100644 --- a/litellm-rust/crates/core/src/auth/azure/native.rs +++ b/litellm-rust/crates/core/src/auth/azure/native.rs @@ -245,7 +245,7 @@ fn build_credential( NativeAzureRequest::DeveloperTools { .. } => DeveloperToolsCredential::new(None) .map(|credential| credential as Arc), } - .map_err(|error| AuthError::InvalidConfiguration(error.to_string())) + .map_err(|error| AuthError::AzureCredentialConfiguration(error.to_string())) } fn client_options( diff --git a/litellm-rust/crates/core/src/auth/azure/resolve.rs b/litellm-rust/crates/core/src/auth/azure/resolve.rs index e9655d9f965..9ee929d0a4b 100644 --- a/litellm-rust/crates/core/src/auth/azure/resolve.rs +++ b/litellm-rust/crates/core/src/auth/azure/resolve.rs @@ -48,9 +48,7 @@ impl AzureAuthService { AzureCredentialPlan::Caller(caller) => { let credential = caller.acquire().await?; if credential.secret().expose().is_empty() { - return Err(AuthError::Caller( - "Azure AD token provider returned an empty token".to_string(), - )); + return Err(AuthError::EmptyAzureAdToken); } Ok(Some(credential)) } @@ -63,11 +61,7 @@ impl AzureAuthService { } => { let assertion = resolve_reference(inputs, env_lookup, &reference) .await? - .ok_or_else(|| { - AuthError::Acquisition( - "Azure OIDC reference did not resolve to a value".to_string(), - ) - })?; + .ok_or(AuthError::UnresolvedOidcReference)?; self.native .acquire(NativeAzureRequest::ClientAssertion { tenant_id, @@ -115,9 +109,7 @@ pub(crate) fn select_auth_plan( let selector = configured_string(&inputs.azure_credential, AZURE_CREDENTIAL_ENV, env_lookup) .map(|value| value.parse::()) .transpose() - .map_err(|_| { - AuthError::InvalidConfiguration("invalid Azure credential selector".to_string()) - })?; + .map_err(|_| AuthError::InvalidAzureCredentialSelector)?; let federated_token_file = configured_string( &inputs.federated_token_file, AZURE_FEDERATED_TOKEN_FILE_ENV, @@ -199,9 +191,7 @@ fn select_native_plan( }); match selected { - AzureCredentialType::ClientSecretCredential => Err(AuthError::InvalidConfiguration( - "ClientSecretCredential requires tenant_id, client_id, and client_secret".to_string(), - )), + AzureCredentialType::ClientSecretCredential => Err(AuthError::MissingClientSecretFields), AzureCredentialType::WorkloadIdentityCredential => Ok(AzureCredentialPlan::Native( workload_request(tenant_id, client_id, federated_token_file, scope, authority)?, )), @@ -273,21 +263,9 @@ fn workload_request( authority: Option, ) -> Result { Ok(NativeAzureRequest::WorkloadIdentity { - tenant_id: tenant_id.ok_or_else(|| { - AuthError::InvalidConfiguration( - "WorkloadIdentityCredential requires tenant_id".to_string(), - ) - })?, - client_id: client_id.ok_or_else(|| { - AuthError::InvalidConfiguration( - "WorkloadIdentityCredential requires client_id".to_string(), - ) - })?, - token_file_path: token_file_path.ok_or_else(|| { - AuthError::InvalidConfiguration( - "WorkloadIdentityCredential requires azure_federated_token_file".to_string(), - ) - })?, + tenant_id: tenant_id.ok_or(AuthError::MissingWorkloadTenantId)?, + client_id: client_id.ok_or(AuthError::MissingWorkloadClientId)?, + token_file_path: token_file_path.ok_or(AuthError::MissingWorkloadTokenFile)?, scope, authority, }) @@ -334,11 +312,10 @@ async fn resolve_reference( .map_or(CredentialLookup::Missing, CredentialLookup::Found), CredentialRef::None => return Ok(None), CredentialRef::File(_) | CredentialRef::Request(_) | CredentialRef::Host(_) => { - let resolver = inputs.credential_resolver.as_ref().ok_or_else(|| { - AuthError::InvalidConfiguration( - "credential reference requires a host credential resolver".to_string(), - ) - })?; + let resolver = inputs + .credential_resolver + .as_ref() + .ok_or(AuthError::MissingCredentialResolver)?; resolver.resolve(reference).await? } }; @@ -353,34 +330,30 @@ fn oidc_reference(token: &Option) -> Result, return Ok(None); }; if let Some(name) = value.strip_prefix("oidc/env/") { - return non_empty_reference(name, "OIDC environment reference") + return non_empty_reference(name, AuthError::EmptyOidcEnvironmentReference) .map(CredentialRef::Env) .map(Some); } if let Some(name) = value.strip_prefix("oidc/env_path/") { - return non_empty_reference(name, "OIDC environment path reference") + return non_empty_reference(name, AuthError::EmptyOidcEnvironmentPathReference) .map(|name| CredentialRef::File(CredentialFileRef::EnvironmentVariable(name))) .map(Some); } if let Some(path) = value.strip_prefix("oidc/file/") { - let path = non_empty_reference(path, "OIDC file reference")?; + let path = non_empty_reference(path, AuthError::EmptyOidcFileReference)?; return Ok(Some(CredentialRef::File(CredentialFileRef::Path( path.into(), )))); } if value.starts_with("oidc/") { - return Err(AuthError::InvalidConfiguration(format!( - "unsupported OIDC reference: {value}" - ))); + return Err(AuthError::UnsupportedOidcReference); } Ok(None) } -fn non_empty_reference(value: &str, kind: &str) -> Result { +fn non_empty_reference(value: &str, error: AuthError) -> Result { if value.is_empty() { - return Err(AuthError::InvalidConfiguration(format!( - "{kind} cannot be empty" - ))); + return Err(error); } Ok(value.to_string()) } @@ -475,7 +448,7 @@ mod tests { let error = oidc_reference(&Some(SecretValue::new("oidc/vault/assertion"))) .expect_err("unsupported backend must fail validation"); - assert!(error.to_string().contains("unsupported OIDC reference")); + assert_eq!(error, crate::AuthError::UnsupportedOidcReference); } #[tokio::test] diff --git a/litellm-rust/crates/core/src/auth/azure/types.rs b/litellm-rust/crates/core/src/auth/azure/types.rs index c0a4c761a1e..790b3720126 100644 --- a/litellm-rust/crates/core/src/auth/azure/types.rs +++ b/litellm-rust/crates/core/src/auth/azure/types.rs @@ -76,9 +76,9 @@ fn string_config( None => Ok(ConfigValue::Absent), Some(Value::Null) => Ok(ConfigValue::ExplicitNone), Some(Value::String(value)) => Ok(ConfigValue::Value(value.clone())), - Some(_) => Err(AuthError::InvalidConfiguration(format!( - "{name} must be a string or null" - ))), + Some(_) => Err(AuthError::InvalidAzureConfigType { + name: name.to_string(), + }), } } diff --git a/litellm-rust/crates/core/src/auth/credential.rs b/litellm-rust/crates/core/src/auth/credential.rs index 9d4105c2500..4bc7569ab8f 100644 --- a/litellm-rust/crates/core/src/auth/credential.rs +++ b/litellm-rust/crates/core/src/auth/credential.rs @@ -88,9 +88,7 @@ impl CredentialPlan { Self::Caller(caller) => { let credential = caller.acquire().await?; if credential.secret().expose().is_empty() { - return Err(AuthError::Caller( - "credential caller returned an empty credential".to_string(), - )); + return Err(AuthError::EmptyCallerCredential); } Ok(CredentialPlanResolution::Resolved(credential)) } diff --git a/litellm-rust/crates/core/src/auth/http.rs b/litellm-rust/crates/core/src/auth/http.rs index 3dafa8a3217..f8c06f9ce52 100644 --- a/litellm-rust/crates/core/src/auth/http.rs +++ b/litellm-rust/crates/core/src/auth/http.rs @@ -1,3 +1,5 @@ +use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; + use crate::AuthError; #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -16,62 +18,109 @@ impl CredentialPlacement { } pub(crate) fn apply_credential( - headers: Vec<(String, String)>, + mut headers: HeaderMap, credential: &str, placement: CredentialPlacement, -) -> Result, AuthError> { +) -> Result { if credential.trim().is_empty() { - return Err(AuthError::InvalidConfiguration( - "credential cannot be empty".to_string(), - )); + return Err(AuthError::EmptyCredential); } - if headers - .iter() - .any(|(name, _)| name.eq_ignore_ascii_case(placement.header_name())) - { - return Err(AuthError::InvalidConfiguration(format!( - "credential header {} already exists", - placement.header_name() - ))); + let name = HeaderName::from_bytes(placement.header_name().as_bytes()).map_err(|_| { + AuthError::InvalidCredentialHeaderName { + name: placement.header_name(), + } + })?; + if headers.contains_key(&name) { + return Err(AuthError::CredentialHeaderAlreadyExists { + name: placement.header_name(), + }); } - let value = match placement { - CredentialPlacement::Bearer => format!("Bearer {credential}"), - CredentialPlacement::Header(_) => credential.to_string(), - }; - Ok( - std::iter::once((placement.header_name().to_string(), value)) - .chain(headers) - .collect(), - ) + let mut value = match placement { + CredentialPlacement::Bearer => HeaderValue::from_str(&format!("Bearer {credential}")), + CredentialPlacement::Header(_) => HeaderValue::from_str(credential), + } + .map_err(|_| AuthError::InvalidCredentialHeaderValue)?; + value.set_sensitive(true); + headers.insert(name, value); + Ok(headers) } #[cfg(test)] mod tests { + use reqwest::header::{AUTHORIZATION, HeaderMap, HeaderValue}; + use super::{CredentialPlacement, apply_credential}; + use crate::AuthError; #[test] - fn bearer_uses_authorization_header() { - let headers = apply_credential(Vec::new(), "key", CredentialPlacement::Bearer) - .expect("credential applies"); + fn credential_headers_are_sensitive_in_built_requests() { + for placement in [ + CredentialPlacement::Bearer, + CredentialPlacement::Header("x-api-key"), + ] { + let headers = apply_credential(HeaderMap::new(), "secret", placement).unwrap(); + let request = reqwest::Client::new() + .get("https://example.com") + .headers(headers) + .build() + .unwrap(); + let value = &request.headers()[placement.header_name()]; + assert!(value.is_sensitive()); + assert!(!format!("{request:?}").contains("secret")); + assert_eq!( + value, + match placement { + CredentialPlacement::Bearer => "Bearer secret", + _ => "secret", + } + ); + } + } + #[test] + fn invalid_credentials_fail_with_typed_errors() { + for placement in [ + CredentialPlacement::Bearer, + CredentialPlacement::Header("x-api-key"), + ] { + for credential in ["", " "] { + assert_eq!( + apply_credential(HeaderMap::new(), credential, placement), + Err(AuthError::EmptyCredential) + ); + } + for credential in ["secret\r\nx-injected: true", "secret\0"] { + assert_eq!( + apply_credential(HeaderMap::new(), credential, placement), + Err(AuthError::InvalidCredentialHeaderValue) + ); + } + } + } + + #[test] + fn duplicate_auth_is_rejected_case_insensitively() { + let headers = + HeaderMap::from_iter([(AUTHORIZATION, HeaderValue::from_static("Bearer caller"))]); assert_eq!( - headers, - vec![("Authorization".to_string(), "Bearer key".to_string())] + apply_credential(headers, "configured", CredentialPlacement::Bearer), + Err(AuthError::CredentialHeaderAlreadyExists { + name: "Authorization" + }) ); } #[test] - fn named_header_rejects_existing_value() { - let error = apply_credential( - vec![( - "ocp-apim-subscription-key".to_string(), - "caller-key".to_string(), - )], - "configured-key", - CredentialPlacement::Header("Ocp-Apim-Subscription-Key"), - ) - .expect_err("provider policy must handle existing credentials"); - - assert!(error.to_string().contains("already exists")); + fn invalid_header_names_fail_with_a_typed_error() { + assert_eq!( + apply_credential( + HeaderMap::new(), + "secret", + CredentialPlacement::Header("invalid name") + ), + Err(AuthError::InvalidCredentialHeaderName { + name: "invalid name" + }) + ); } } diff --git a/litellm-rust/crates/core/src/auth/policy.rs b/litellm-rust/crates/core/src/auth/policy.rs index ea575a5177c..7bdc88fe2ad 100644 --- a/litellm-rust/crates/core/src/auth/policy.rs +++ b/litellm-rust/crates/core/src/auth/policy.rs @@ -1,3 +1,5 @@ +use reqwest::header::HeaderMap; + use crate::AuthError; use super::http::apply_credential; @@ -32,37 +34,29 @@ pub struct ProviderAuthPolicy { } impl ProviderAuthPolicy { - pub fn has_existing_credential(&self, headers: &[(String, String)]) -> bool { - headers.iter().any(|(name, _)| { - self.accepted_existing_headers - .iter() - .any(|accepted| name.eq_ignore_ascii_case(accepted)) - }) + pub fn has_existing_credential(&self, headers: &HeaderMap) -> bool { + self.accepted_existing_headers + .iter() + .any(|name| headers.contains_key(*name)) } pub fn apply( &self, - headers: Vec<(String, String)>, + headers: HeaderMap, kind: CredentialPlanKind, credential: &ResolvedCredential, - ) -> Result, AuthError> { + ) -> Result { if self.has_existing_credential(&headers) { return match self.existing_header_behavior { ExistingHeaderBehavior::Preserve => Ok(headers), - ExistingHeaderBehavior::Reject => Err(AuthError::InvalidConfiguration( - "credential header already exists".to_string(), - )), + ExistingHeaderBehavior::Reject => Err(AuthError::ExistingCredentialHeader), }; } let rule = self .rules .iter() .find(|rule| rule.kind == kind) - .ok_or_else(|| { - AuthError::InvalidConfiguration( - "credential plan is not allowed by the provider auth policy".to_string(), - ) - })?; + .ok_or(AuthError::CredentialPlanNotAllowed)?; apply_credential(headers, credential.secret().expose(), rule.placement) } } @@ -71,6 +65,7 @@ impl ProviderAuthPolicy { mod tests { use super::{CredentialPlanKind, CredentialRule, ExistingHeaderBehavior, ProviderAuthPolicy}; use crate::auth::{CredentialPlacement, ResolvedCredential, SecretValue}; + use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; const RULES: &[CredentialRule] = &[CredentialRule { kind: CredentialPlanKind::Static, @@ -84,11 +79,34 @@ mod tests { audience: None, }; + #[test] + fn existing_credential_policy_preserves_or_rejects_caller_auth() { + let headers = HeaderMap::from_iter([( + HeaderName::from_static("x-api-key"), + HeaderValue::from_static("caller"), + )]); + let credential = ResolvedCredential::Static(SecretValue::new("configured")); + assert_eq!( + POLICY + .apply(headers.clone(), CredentialPlanKind::Static, &credential) + .unwrap()["x-api-key"], + "caller" + ); + let rejecting = ProviderAuthPolicy { + existing_header_behavior: ExistingHeaderBehavior::Reject, + ..POLICY + }; + assert_eq!( + rejecting.apply(headers, CredentialPlanKind::Static, &credential), + Err(crate::AuthError::ExistingCredentialHeader) + ); + } + #[test] fn rules_define_allowed_plans_and_credential_placement() { let headers = POLICY .apply( - Vec::new(), + HeaderMap::new(), CredentialPlanKind::Static, &ResolvedCredential::Static(SecretValue::new("secret")), ) @@ -96,7 +114,10 @@ mod tests { assert_eq!( headers, - vec![("x-api-key".to_string(), "secret".to_string())] + HeaderMap::from_iter([( + HeaderName::from_static("x-api-key"), + HeaderValue::from_static("secret") + )]) ); } @@ -104,12 +125,12 @@ mod tests { fn unsupported_plan_is_rejected() { let error = POLICY .apply( - Vec::new(), + HeaderMap::new(), CredentialPlanKind::Entra, &ResolvedCredential::Static(SecretValue::new("secret")), ) .unwrap_err(); - assert!(error.to_string().contains("not allowed")); + assert_eq!(error, crate::AuthError::CredentialPlanNotAllowed); } } diff --git a/litellm-rust/crates/core/src/error.rs b/litellm-rust/crates/core/src/error.rs index 2150c571e48..1faf9ccf8e1 100644 --- a/litellm-rust/crates/core/src/error.rs +++ b/litellm-rust/crates/core/src/error.rs @@ -2,8 +2,75 @@ use thiserror::Error as ThisError; #[derive(Clone, Debug, ThisError, PartialEq, Eq)] pub enum AuthError { + #[error("invalid authentication configuration: credential cannot be empty")] + EmptyCredential, + #[error("invalid authentication configuration: credential is not a valid HTTP header value")] + InvalidCredentialHeaderValue, + #[error("invalid authentication configuration: invalid credential header name: {name}")] + InvalidCredentialHeaderName { name: &'static str }, + #[error("invalid authentication configuration: credential header {name} already exists")] + CredentialHeaderAlreadyExists { name: &'static str }, + #[error("invalid authentication configuration: credential header already exists")] + ExistingCredentialHeader, + #[error( + "invalid authentication configuration: credential plan is not allowed by the provider auth policy" + )] + CredentialPlanNotAllowed, + #[error( + "invalid authentication configuration: caller credential plan requires provider-specific inputs" + )] + CallerPlanRequiresProviderInputs, + #[error("credential caller failed: credential caller returned an empty credential")] + EmptyCallerCredential, + #[error("credential caller failed: Azure AD token provider returned an empty token")] + EmptyAzureAdToken, + #[error("credential acquisition failed: Azure OIDC reference did not resolve to a value")] + UnresolvedOidcReference, + #[error("invalid authentication configuration: invalid Azure credential selector")] + InvalidAzureCredentialSelector, + #[error( + "invalid authentication configuration: ClientSecretCredential requires tenant_id, client_id, and client_secret" + )] + MissingClientSecretFields, + #[error("invalid authentication configuration: WorkloadIdentityCredential requires tenant_id")] + MissingWorkloadTenantId, + #[error("invalid authentication configuration: WorkloadIdentityCredential requires client_id")] + MissingWorkloadClientId, + #[error( + "invalid authentication configuration: WorkloadIdentityCredential requires azure_federated_token_file" + )] + MissingWorkloadTokenFile, + #[error( + "invalid authentication configuration: credential reference requires a host credential resolver" + )] + MissingCredentialResolver, + #[error("invalid authentication configuration: unsupported OIDC reference")] + UnsupportedOidcReference, + #[error("invalid authentication configuration: OIDC environment reference cannot be empty")] + EmptyOidcEnvironmentReference, + #[error( + "invalid authentication configuration: OIDC environment path reference cannot be empty" + )] + EmptyOidcEnvironmentPathReference, + #[error("invalid authentication configuration: OIDC file reference cannot be empty")] + EmptyOidcFileReference, + #[error("invalid authentication configuration: {name} must be a string or null")] + InvalidAzureConfigType { name: String }, + #[error( + "invalid authentication configuration: Missing Azure AI credentials - set AZURE_AI_API_KEY or configure Entra ID" + )] + MissingAzureAiCredentials, + #[error( + "invalid authentication configuration: Missing Azure Document Intelligence credentials - set AZURE_DOCUMENT_INTELLIGENCE_API_KEY or configure Entra ID" + )] + MissingAzureDocumentIntelligenceCredentials, + #[error( + "Missing REDUCTO_API_KEY - set it in the environment or pass api_key to litellm.ocr()/litellm.aocr()" + )] + MissingReductoApiKey, + #[error("invalid authentication configuration: {0}")] - InvalidConfiguration(String), + AzureCredentialConfiguration(String), #[error("credential acquisition failed: {0}")] Acquisition(String), #[error("credential caller failed: {0}")] @@ -25,6 +92,16 @@ pub enum AuthError { #[derive(Debug, ThisError, PartialEq, Eq)] pub enum Error { + #[error("{context} extra_headers.{name} must be a string, got {actual}")] + InvalidHeaderType { + context: &'static str, + name: String, + actual: &'static str, + }, + #[error("invalid HTTP header name")] + InvalidHeaderName, + #[error("invalid value for HTTP header {name}")] + InvalidHeaderValue { name: String }, #[error("expected {expected}, got {actual}")] InvalidType { expected: &'static str, diff --git a/litellm-rust/crates/core/src/http_utils.rs b/litellm-rust/crates/core/src/http_utils.rs index cb472dd5a57..ef72f99d2ce 100644 --- a/litellm-rust/crates/core/src/http_utils.rs +++ b/litellm-rust/crates/core/src/http_utils.rs @@ -1,5 +1,6 @@ //! Header and upstream-body helpers shared by every route module. +use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; use serde_json::{Map, Value}; use crate::constants::UPSTREAM_ERROR_BODY_MAX_CHARS; @@ -43,6 +44,40 @@ pub fn string_headers( .collect() } +pub fn header_map( + context: &'static str, + extra_headers: Option>, +) -> Result { + extra_headers + .unwrap_or_default() + .into_iter() + .map(|(name, value)| { + let value = value.as_str().ok_or_else(|| Error::InvalidHeaderType { + context, + name: name.clone(), + actual: json_type_name(&value), + })?; + let name = + HeaderName::from_bytes(name.as_bytes()).map_err(|_| Error::InvalidHeaderName)?; + let mut value = + HeaderValue::from_str(value).map_err(|_| Error::InvalidHeaderValue { + name: name.to_string(), + })?; + if matches!( + name.as_str(), + "authorization" + | "proxy-authorization" + | "x-api-key" + | "api-key" + | "ocp-apim-subscription-key" + ) { + value.set_sensitive(true); + } + Ok((name, value)) + }) + .collect() +} + pub fn has_header(headers: &[(String, String)], name: &str) -> bool { headers .iter() @@ -66,6 +101,48 @@ mod tests { use super::*; use serde_json::json; + #[test] + fn header_map_validates_wire_values_without_exposing_credentials() { + for (input, expected) in [ + (json!({"invalid name": "secret"}), Error::InvalidHeaderName), + ( + json!({"Authorization": "secret\r\nx-injected: true"}), + Error::InvalidHeaderValue { + name: "authorization".into(), + }, + ), + ( + json!({"x-retry": 3}), + Error::InvalidHeaderType { + context: "OCR", + name: "x-retry".into(), + actual: "number", + }, + ), + ] { + let error = header_map("OCR", input.as_object().cloned()) + .expect_err("invalid header must fail before sending"); + assert_eq!(error, expected); + assert!(!error.to_string().contains("secret")); + } + let headers = header_map( + "OCR", + json!({"Authorization": "Bearer secret", "X-Api-Key": "secret", "x-trace": "trace"}) + .as_object() + .cloned(), + ) + .unwrap(); + let request = reqwest::Client::new() + .get("https://example.com") + .headers(headers) + .build() + .unwrap(); + assert_eq!(request.headers()["authorization"], "Bearer secret"); + assert_eq!(request.headers()["x-api-key"], "secret"); + assert_eq!(request.headers()["x-trace"], "trace"); + assert!(!format!("{request:?}").contains("secret")); + } + #[test] fn truncate_leaves_short_bodies_untouched() { assert_eq!(truncate_error_body("short"), "short"); diff --git a/litellm-rust/crates/core/src/ocr/transformation.rs b/litellm-rust/crates/core/src/ocr/transformation.rs index 77fbbfb7bba..3e472bf303b 100644 --- a/litellm-rust/crates/core/src/ocr/transformation.rs +++ b/litellm-rust/crates/core/src/ocr/transformation.rs @@ -1,23 +1,24 @@ -use crate::auth::CredentialPlacement; -use crate::auth::http::apply_credential; -use crate::{AuthError, Error}; -use serde_json::{Map, Value}; - use std::future::Future; use std::pin::Pin; +use reqwest::header::HeaderMap; +use serde_json::{Map, Value}; + +use crate::auth::CredentialPlacement; +use crate::auth::http::apply_credential; +use crate::{AuthError, Error}; + use super::types::{OcrAuthInputs, OcrRequestData, OcrResponseData}; -pub type OcrAuthFuture<'a> = - Pin, Error>> + Send + 'a>>; +pub type OcrAuthFuture<'a> = Pin> + Send + 'a>>; pub type OcrRequestSetupFuture<'a> = - Pin), Error>> + Send + 'a>>; + Pin> + Send + 'a>>; pub struct OcrRequestSetup<'a> { pub api_base: Option<&'a str>, pub model: &'a str, pub optional_params: &'a Map, - pub headers: Vec<(String, String)>, + pub headers: HeaderMap, pub api_key: Option<&'a str>, pub auth_inputs: &'a OcrAuthInputs, pub env_lookup: &'a (dyn Fn(&str) -> Option + Sync), @@ -89,12 +90,12 @@ pub trait OcrProviderConfig: Sync { #[tracing::instrument(target = "litellm::function_trace", level = "trace", skip_all)] fn validate_environment( &self, - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&str>, env_lookup: &dyn Fn(&str) -> Option, - ) -> Result, Error> { + ) -> Result { let placement = self.credential_placement(); - if crate::http_utils::has_header(&headers, placement.header_name()) { + if headers.contains_key(placement.header_name()) { return Ok(headers); } let api_key = self.resolve_api_key(api_key, env_lookup)?; @@ -103,7 +104,7 @@ pub trait OcrProviderConfig: Sync { fn authenticate<'a>( &'a self, - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&'a str>, auth_inputs: &'a OcrAuthInputs, env_lookup: &'a (dyn Fn(&str) -> Option + Sync), diff --git a/litellm-rust/crates/core/src/providers/azure_ai/auth.rs b/litellm-rust/crates/core/src/providers/azure_ai/auth.rs index aec715d2d39..69678754358 100644 --- a/litellm-rust/crates/core/src/providers/azure_ai/auth.rs +++ b/litellm-rust/crates/core/src/providers/azure_ai/auth.rs @@ -1,5 +1,7 @@ use std::sync::OnceLock; +use reqwest::header::HeaderMap; + use crate::auth::azure::{AzureAuthInputs, AzureAuthService}; use crate::auth::http::apply_credential; use crate::auth::{ @@ -121,14 +123,11 @@ pub fn resolve_document_intelligence_endpoint( } pub fn validate_environment( - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&str>, env_lookup: &dyn Fn(&str) -> Option, -) -> Result, Error> { - if headers - .iter() - .any(|(name, _)| name.eq_ignore_ascii_case("Authorization")) - { +) -> Result { + if headers.contains_key("Authorization") { return Ok(headers); } let api_key = resolve_api_key(api_key, env_lookup)?; @@ -136,11 +135,11 @@ pub fn validate_environment( } pub async fn authenticate( - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&str>, auth_inputs: Option<&AzureAuthInputs>, env_lookup: &(dyn Fn(&str) -> Option + Sync), -) -> Result, Error> { +) -> Result { authenticate_with_policy( headers, api_key, @@ -148,17 +147,17 @@ pub async fn authenticate( auth_inputs, env_lookup, &AZURE_AI_AUTH_POLICY, - "Missing Azure AI credentials - set AZURE_AI_API_KEY or configure Entra ID", + AuthError::MissingAzureAiCredentials, ) .await } pub async fn authenticate_document_intelligence( - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&str>, auth_inputs: Option<&AzureAuthInputs>, env_lookup: &(dyn Fn(&str) -> Option + Sync), -) -> Result, Error> { +) -> Result { authenticate_with_policy( headers, api_key, @@ -166,20 +165,20 @@ pub async fn authenticate_document_intelligence( auth_inputs, env_lookup, &DOCUMENT_INTELLIGENCE_AUTH_POLICY, - "Missing Azure Document Intelligence credentials - set AZURE_DOCUMENT_INTELLIGENCE_API_KEY or configure Entra ID", + AuthError::MissingAzureDocumentIntelligenceCredentials, ) .await } async fn authenticate_with_policy( - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&str>, api_key_env: &str, auth_inputs: Option<&AzureAuthInputs>, env_lookup: &(dyn Fn(&str) -> Option + Sync), policy: &ProviderAuthPolicy, - missing_message: &'static str, -) -> Result, Error> { + missing_error: AuthError, +) -> Result { if policy.has_existing_credential(&headers) { return Ok(headers); } @@ -190,10 +189,7 @@ async fn authenticate_with_policy( } CredentialPlanKind::Entra => resolve_entra_credential(auth_inputs, env_lookup).await?, CredentialPlanKind::Caller => { - return Err(AuthError::InvalidConfiguration( - "caller credential plan requires provider-specific inputs".to_string(), - ) - .into()); + return Err(AuthError::CallerPlanRequiresProviderInputs.into()); } }; if let Some(credential) = credential { @@ -202,7 +198,7 @@ async fn authenticate_with_policy( .map_err(Error::from); } } - Err(AuthError::InvalidConfiguration(missing_message.to_string()).into()) + Err(missing_error.into()) } async fn resolve_entra_credential( @@ -246,6 +242,7 @@ fn auth_service() -> &'static AzureAuthService { mod tests { use crate::auth::azure::AzureAuthInputs; use crate::error::{AuthError, Error}; + use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; use serde_json::json; use super::{ @@ -258,11 +255,8 @@ mod tests { .expect("valid Azure auth inputs") } - fn header_value<'a>(headers: &'a [(String, String)], name: &str) -> Option<&'a str> { - headers - .iter() - .find(|(header_name, _)| header_name.eq_ignore_ascii_case(name)) - .map(|(_, value)| value.as_str()) + fn header_value<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> { + headers.get(name).and_then(|value| value.to_str().ok()) } #[test] @@ -288,22 +282,25 @@ mod tests { #[test] fn api_key_is_sent_as_bearer() { - let headers = validate_environment(Vec::new(), Some("key"), &|_| None) + let headers = validate_environment(HeaderMap::new(), Some("key"), &|_| None) .expect("api key authenticates"); assert_eq!( headers, - vec![("Authorization".to_string(), "Bearer key".to_string())] + HeaderMap::from_iter([( + HeaderName::from_static("authorization"), + HeaderValue::from_static("Bearer key") + )]) ); } #[test] fn caller_bearer_token_wins_over_environment_key() { let headers = validate_environment( - vec![( - "authorization".to_string(), - "Bearer caller-token".to_string(), - )], + HeaderMap::from_iter([( + HeaderName::from_static("authorization"), + HeaderValue::from_static("Bearer caller-token"), + )]), None, &|name| (name == AZURE_AI_API_KEY_ENV).then(|| "environment-key".to_string()), ) @@ -311,17 +308,17 @@ mod tests { assert_eq!( headers, - vec![( - "authorization".to_string(), - "Bearer caller-token".to_string() - )] + HeaderMap::from_iter([( + HeaderName::from_static("authorization"), + HeaderValue::from_static("Bearer caller-token") + )]) ); } #[tokio::test] async fn azure_ai_uses_supplied_entra_token() { let inputs = azure_inputs(json!({"azure_ad_token": "entra-token"})); - let headers = authenticate(Vec::new(), None, Some(&inputs), &|_| None) + let headers = authenticate(HeaderMap::new(), None, Some(&inputs), &|_| None) .await .expect("Entra token authenticates"); @@ -334,7 +331,7 @@ mod tests { #[tokio::test] async fn document_intelligence_api_key_uses_subscription_header() { let headers = - authenticate_document_intelligence(Vec::new(), Some("my-key"), None, &|_| None) + authenticate_document_intelligence(HeaderMap::new(), Some("my-key"), None, &|_| None) .await .expect("API key authenticates"); @@ -349,7 +346,7 @@ mod tests { async fn document_intelligence_falls_back_to_entra_token() { let inputs = azure_inputs(json!({"azure_ad_token": "entra-token"})); let headers = - authenticate_document_intelligence(Vec::new(), None, Some(&inputs), &|_| None) + authenticate_document_intelligence(HeaderMap::new(), None, Some(&inputs), &|_| None) .await .expect("Entra token authenticates"); @@ -363,12 +360,14 @@ mod tests { #[tokio::test] async fn document_intelligence_api_key_precedes_entra_token() { let inputs = azure_inputs(json!({"azure_ad_token": "entra-token"})); - let headers = - authenticate_document_intelligence(Vec::new(), Some("api-key"), Some(&inputs), &|_| { - None - }) - .await - .expect("API key authenticates"); + let headers = authenticate_document_intelligence( + HeaderMap::new(), + Some("api-key"), + Some(&inputs), + &|_| None, + ) + .await + .expect("API key authenticates"); assert_eq!( header_value(&headers, "Ocp-Apim-Subscription-Key"), diff --git a/litellm-rust/crates/core/src/providers/azure_ai/ocr/transformation.rs b/litellm-rust/crates/core/src/providers/azure_ai/ocr/transformation.rs index a26e030e09b..5fc947ae05b 100644 --- a/litellm-rust/crates/core/src/providers/azure_ai/ocr/transformation.rs +++ b/litellm-rust/crates/core/src/providers/azure_ai/ocr/transformation.rs @@ -1,3 +1,5 @@ +use reqwest::header::HeaderMap; + use serde_json::{Map, Value, json}; use std::collections::BTreeSet; @@ -442,7 +444,7 @@ impl OcrProviderConfig for AzureAiOcrConfig { fn authenticate<'a>( &'a self, - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&'a str>, auth_inputs: &'a OcrAuthInputs, env_lookup: &'a (dyn Fn(&str) -> Option + Sync), @@ -560,7 +562,7 @@ impl OcrProviderConfig for AzureDocumentIntelligenceOcrConfig { fn authenticate<'a>( &'a self, - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&'a str>, auth_inputs: &'a OcrAuthInputs, env_lookup: &'a (dyn Fn(&str) -> Option + Sync), diff --git a/litellm-rust/crates/core/src/providers/mistral/auth.rs b/litellm-rust/crates/core/src/providers/mistral/auth.rs index 4f9ce4f11d1..51bed04c195 100644 --- a/litellm-rust/crates/core/src/providers/mistral/auth.rs +++ b/litellm-rust/crates/core/src/providers/mistral/auth.rs @@ -1,3 +1,5 @@ +use reqwest::header::HeaderMap; + use crate::auth::{ CredentialPlacement, CredentialPlanKind, CredentialRule, ExistingHeaderBehavior, ProviderAuthPolicy, ResolvedCredential, SecretValue, @@ -33,10 +35,10 @@ pub fn resolve_api_key( } pub fn validate_environment( - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&str>, env_lookup: &dyn Fn(&str) -> Option, -) -> Result, Error> { +) -> Result { if AUTH_POLICY.has_existing_credential(&headers) { return Ok(headers); } @@ -50,6 +52,7 @@ pub fn validate_environment( #[cfg(test)] mod tests { use super::*; + use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; #[test] fn explicit_key_precedes_environment_key() { @@ -82,17 +85,23 @@ mod tests { #[test] fn static_key_is_applied_as_bearer() { - let headers = validate_environment(Vec::new(), Some("sk-param"), &|_| None).unwrap(); + let headers = validate_environment(HeaderMap::new(), Some("sk-param"), &|_| None).unwrap(); assert_eq!( headers, - vec![("Authorization".to_string(), "Bearer sk-param".to_string())] + HeaderMap::from_iter([( + HeaderName::from_static("authorization"), + HeaderValue::from_static("Bearer sk-param") + )]) ); } #[test] fn existing_authorization_is_preserved_without_key_lookup() { - let headers = vec![("authorization".to_string(), "Bearer existing".to_string())]; + let headers = HeaderMap::from_iter([( + HeaderName::from_static("authorization"), + HeaderValue::from_static("Bearer existing"), + )]); assert_eq!( validate_environment(headers.clone(), None, &|_| None).unwrap(), diff --git a/litellm-rust/crates/core/src/providers/mistral/ocr/transformation.rs b/litellm-rust/crates/core/src/providers/mistral/ocr/transformation.rs index d1ebbacacf4..6bdd6999265 100644 --- a/litellm-rust/crates/core/src/providers/mistral/ocr/transformation.rs +++ b/litellm-rust/crates/core/src/providers/mistral/ocr/transformation.rs @@ -1,3 +1,5 @@ +use reqwest::header::HeaderMap; + use crate::error::{Error, json_type_name}; use crate::ocr::transformation::OcrProviderConfig; use crate::ocr::types::{OcrRequestData, OcrResponseData}; @@ -136,10 +138,10 @@ impl OcrProviderConfig for MistralOcrConfig { fn validate_environment( &self, - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&str>, env_lookup: &dyn Fn(&str) -> Option, - ) -> Result, Error> { + ) -> Result { auth::validate_environment(headers, api_key, env_lookup) } } diff --git a/litellm-rust/crates/core/src/providers/reducto/auth.rs b/litellm-rust/crates/core/src/providers/reducto/auth.rs index c969e8c70e6..031f9b17287 100644 --- a/litellm-rust/crates/core/src/providers/reducto/auth.rs +++ b/litellm-rust/crates/core/src/providers/reducto/auth.rs @@ -1,3 +1,5 @@ +use reqwest::header::HeaderMap; + use crate::auth::{ CredentialPlacement, CredentialPlanKind, CredentialRule, ExistingHeaderBehavior, ProviderAuthPolicy, ResolvedCredential, SecretValue, @@ -6,7 +8,6 @@ use crate::error::{AuthError, Error}; pub const REDUCTO_API_KEY_ENV: &str = "REDUCTO_API_KEY"; -const MISSING_KEY_MESSAGE: &str = "Missing REDUCTO_API_KEY - set it in the environment or pass api_key to litellm.ocr()/litellm.aocr()"; const AUTH_RULES: &[CredentialRule] = &[CredentialRule { kind: CredentialPlanKind::Static, placement: CredentialPlacement::Bearer, @@ -32,14 +33,14 @@ pub fn resolve_api_key( .map(|key| key.trim().to_string()) .filter(|key| !key.is_empty()) }) - .ok_or_else(|| Error::Auth(AuthError::Message(MISSING_KEY_MESSAGE.to_string()))) + .ok_or(Error::Auth(AuthError::MissingReductoApiKey)) } pub fn validate_environment( - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&str>, env_lookup: &dyn Fn(&str) -> Option, -) -> Result, Error> { +) -> Result { if AUTH_POLICY.has_existing_credential(&headers) { return Ok(headers); } @@ -53,6 +54,7 @@ pub fn validate_environment( #[cfg(test)] mod tests { use super::*; + use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; #[test] fn explicit_key_precedes_environment_key() { @@ -72,17 +74,24 @@ mod tests { #[test] fn static_key_is_applied_as_bearer() { - let headers = validate_environment(Vec::new(), Some("passed-key"), &|_| None).unwrap(); + let headers = + validate_environment(HeaderMap::new(), Some("passed-key"), &|_| None).unwrap(); assert_eq!( headers, - vec![("Authorization".to_string(), "Bearer passed-key".to_string())] + HeaderMap::from_iter([( + HeaderName::from_static("authorization"), + HeaderValue::from_static("Bearer passed-key") + )]) ); } #[test] fn existing_authorization_is_preserved_without_key_lookup() { - let headers = vec![("authorization".to_string(), "Bearer existing".to_string())]; + let headers = HeaderMap::from_iter([( + HeaderName::from_static("authorization"), + HeaderValue::from_static("Bearer existing"), + )]); assert_eq!( validate_environment(headers.clone(), None, &|_| None).unwrap(), diff --git a/litellm-rust/crates/core/src/providers/reducto/ocr/tests.rs b/litellm-rust/crates/core/src/providers/reducto/ocr/tests.rs index 42af123c530..804ee8b846a 100644 --- a/litellm-rust/crates/core/src/providers/reducto/ocr/tests.rs +++ b/litellm-rust/crates/core/src/providers/reducto/ocr/tests.rs @@ -1,3 +1,4 @@ +use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; use rstest::{fixture, rstest}; use serde_json::{Value, json}; @@ -182,12 +183,15 @@ fn test_parse_v3_uses_programmatic_api_key_over_env() { assert_eq!(key, "passed-key"); let headers = REDUCTO_PARSE_V3_CONFIG - .validate_environment(Vec::new(), Some("passed-key"), &|_| { + .validate_environment(HeaderMap::new(), Some("passed-key"), &|_| { Some("env-reducto-key".to_string()) }) .expect("headers should validate"); assert_eq!( headers, - vec![("Authorization".to_string(), "Bearer passed-key".to_string())] + HeaderMap::from_iter([( + HeaderName::from_static("authorization"), + HeaderValue::from_static("Bearer passed-key") + )]) ); } diff --git a/litellm-rust/crates/core/src/providers/reducto/ocr/transformation.rs b/litellm-rust/crates/core/src/providers/reducto/ocr/transformation.rs index d75681fa950..5195c6daf4d 100644 --- a/litellm-rust/crates/core/src/providers/reducto/ocr/transformation.rs +++ b/litellm-rust/crates/core/src/providers/reducto/ocr/transformation.rs @@ -1,3 +1,5 @@ +use reqwest::header::HeaderMap; + use std::collections::BTreeMap; use base64::Engine; @@ -346,10 +348,10 @@ impl OcrProviderConfig for ReductoParseV3Config { fn validate_environment( &self, - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&str>, env_lookup: &dyn Fn(&str) -> Option, - ) -> Result, Error> { + ) -> Result { auth::validate_environment(headers, api_key, env_lookup) } } @@ -397,10 +399,10 @@ impl OcrProviderConfig for ReductoParseLegacyConfig { fn validate_environment( &self, - headers: Vec<(String, String)>, + headers: HeaderMap, api_key: Option<&str>, env_lookup: &dyn Fn(&str) -> Option, - ) -> Result, Error> { + ) -> Result { auth::validate_environment(headers, api_key, env_lookup) } } diff --git a/litellm-rust/crates/python-bridge/src/errors.rs b/litellm-rust/crates/python-bridge/src/errors.rs index 6b4fcd3c238..c05cdbe76db 100644 --- a/litellm-rust/crates/python-bridge/src/errors.rs +++ b/litellm-rust/crates/python-bridge/src/errors.rs @@ -21,6 +21,9 @@ pub(crate) fn core_error_to_pyerr(err: Error) -> PyErr { Error::Auth(message) => PyValueError::new_err(message.to_string()), Error::InvalidProvider(_) | Error::InvalidRequest(_) + | Error::InvalidHeaderType { .. } + | Error::InvalidHeaderName + | Error::InvalidHeaderValue { .. } | Error::InvalidType { .. } | Error::MissingField(_) => PyValueError::new_err(err.to_string()), other => PyRuntimeError::new_err(other.to_string()), @@ -39,6 +42,8 @@ pub(crate) fn chat_completions_error_to_pyerr(err: Error) -> PyErr { | Error::Auth(_) | Error::InvalidProvider(_) | Error::InvalidRequest(_) + | Error::InvalidHeaderType { .. } | Error::InvalidHeaderName + | Error::InvalidHeaderValue { .. } | Error::InvalidType { .. } | Error::MissingField(_) | Error::Routing(_) @@ -83,6 +88,22 @@ mod ocr_error_tests { assert!(mapped.is_instance_of::(py)); assert_eq!(mapped.value(py).to_string(), "Document URL is required"); } + for error in [ + Error::Auth(litellm_core::AuthError::EmptyCredential), + Error::Auth(litellm_core::AuthError::InvalidCredentialHeaderValue), + Error::InvalidHeaderName, + Error::InvalidHeaderValue { + name: "authorization".into(), + }, + Error::InvalidHeaderType { + context: "OCR", + name: "authorization".into(), + actual: "number", + }, + ] { + let mapped = ocr_error_to_pyerr(error); + assert!(mapped.is_instance_of::(py)); + } let mapped = ocr_error_to_pyerr(Error::Http { status: 429, body: r#"{"message":"rate limited"}"#.to_string(), diff --git a/litellm-rust/crates/python-bridge/src/routes/definition.rs b/litellm-rust/crates/python-bridge/src/routes/definition.rs index cdcf7ad0133..76b42f8dc18 100644 --- a/litellm-rust/crates/python-bridge/src/routes/definition.rs +++ b/litellm-rust/crates/python-bridge/src/routes/definition.rs @@ -326,22 +326,20 @@ mod tests { assert!(sync_ocr_error.to_string().contains("invalid OCR request")); assert_eq!(async_ocr_error.to_string(), sync_ocr_error.to_string()); - for (sync_name, async_name) in [("transcription", "atranscription")] { - let sync_error = module - .getattr(sync_name) - .and_then(|function| function.call(("model", &document), Some(&kwargs))) - .expect_err("sync route should reject non-dict extra_headers"); - let async_error = module - .getattr(async_name) - .and_then(|function| function.call(("model", &document), Some(&kwargs))) - .expect_err("async route should reject non-dict extra_headers"); + let sync_error = module + .getattr("transcription") + .and_then(|function| function.call(("model", &document), Some(&kwargs))) + .expect_err("sync route should reject non-dict extra_headers"); + let async_error = module + .getattr("atranscription") + .and_then(|function| function.call(("model", &document), Some(&kwargs))) + .expect_err("async route should reject non-dict extra_headers"); - assert_eq!( - sync_error.to_string(), - "ValueError: extra_headers must be a dict" - ); - assert_eq!(async_error.to_string(), sync_error.to_string()); - } + assert_eq!( + sync_error.to_string(), + "ValueError: extra_headers must be a dict" + ); + assert_eq!(async_error.to_string(), sync_error.to_string()); }); }