@@ -99,33 +99,38 @@ def resolve_channel_legacy_api_keys(channel_name: str, base_url: Optional[str])
9999 """
100100 normalized_name = canonicalize_llm_channel_protocol (channel_name )
101101 raw_name = (channel_name or "" ).strip ().lower ()
102- host = (urlparse (base_url or "" ).hostname or "" ).lower ()
102+ parsed_url = urlparse (base_url or "" )
103+ host = (parsed_url .hostname or "" ).lower ()
104+ scheme = (parsed_url .scheme or "" ).lower ()
103105 # Name-based fallback is only safe when no custom base_url is configured,
104106 # because requests will go to the provider's default endpoint. When a
105107 # base_url IS present, only trust the strict host check to prevent legacy
106108 # secrets from leaking to attacker-controlled endpoints.
107109 has_custom_url = bool (base_url and base_url .strip ())
110+ # Reject non-HTTPS custom URLs for host-based fallback to prevent
111+ # credential exposure over plaintext HTTP connections.
112+ host_fallback_ok = has_custom_url and scheme == "https"
108113
109- if _is_provider_host (host , "aihubmix.com" ) or (not has_custom_url and raw_name == "aihubmix" ):
114+ if ( host_fallback_ok and _is_provider_host (host , "aihubmix.com" ) ) or (not has_custom_url and raw_name == "aihubmix" ):
110115 aihubmix_key = os .getenv ('AIHUBMIX_KEY' , '' ).strip ()
111116 if aihubmix_key :
112117 return [aihubmix_key ], 'AIHUBMIX_KEY'
113118 return read_env_key_list ('OPENAI_API_KEYS' , 'OPENAI_API_KEY' )
114119
115- if _is_provider_host (host , "deepseek.com" ) or (not has_custom_url and normalized_name == "deepseek" ):
120+ if ( host_fallback_ok and _is_provider_host (host , "deepseek.com" ) ) or (not has_custom_url and normalized_name == "deepseek" ):
116121 return read_env_key_list ('DEEPSEEK_API_KEYS' , 'DEEPSEEK_API_KEY' )
117122
118123 if (
119- _is_provider_host (host , "generativelanguage.googleapis.com" )
120- or _is_provider_host (host , "aiplatform.googleapis.com" )
124+ ( host_fallback_ok and _is_provider_host (host , "generativelanguage.googleapis.com" ) )
125+ or ( host_fallback_ok and _is_provider_host (host , "aiplatform.googleapis.com" ) )
121126 or (not has_custom_url and normalized_name in {"gemini" , "vertex_ai" })
122127 ):
123128 return read_env_key_list ('GEMINI_API_KEYS' , 'GEMINI_API_KEY' )
124129
125- if _is_provider_host (host , "anthropic.com" ) or (not has_custom_url and normalized_name == "anthropic" ):
130+ if ( host_fallback_ok and _is_provider_host (host , "anthropic.com" ) ) or (not has_custom_url and normalized_name == "anthropic" ):
126131 return read_env_key_list ('ANTHROPIC_API_KEYS' , 'ANTHROPIC_API_KEY' )
127132
128- if _is_provider_host (host , "openai.com" ) or (not has_custom_url and normalized_name == "openai" ):
133+ if ( host_fallback_ok and _is_provider_host (host , "openai.com" ) ) or (not has_custom_url and normalized_name == "openai" ):
129134 return read_env_key_list ('OPENAI_API_KEYS' , 'OPENAI_API_KEY' )
130135
131136 return [], None
0 commit comments