fix(model_fetcher): extract first key from multi-line api_key for HTTP requests#593
Open
haojiajieyangde wants to merge 1 commit into
Open
fix(model_fetcher): extract first key from multi-line api_key for HTTP requests#593haojiajieyangde wants to merge 1 commit into
haojiajieyangde wants to merge 1 commit into
Conversation
When api_key contains multiple keys separated by newlines (the official
multi-key rotation format: 'key1\nkey2'), the model_fetcher endpoints
(fetch-models and {id}/models) would pass the full multi-line string as
an Authorization header value. HTTP clients (reqwest) reject header
values containing newlines, causing a 502 BadGateway.
Fix: add extract_first_key() helper that splits by newline and returns
only the first key. Apply it in both fetch_models_anonymous and
load_provider_config so model fetching only uses a single key.
The frontend's RotatingApiClient handles multi-key rotation for chat
requests independently via environment variables — model fetching only
needs one working key.
Fixes: POST /api/providers/fetch-models 502 with multi-key api_key
Fixes: POST /api/providers/{id}/models 502 with multi-key api_key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
当
api_key包含多个 key(官方多 key 轮询格式key1\nkey2)时,POST /api/providers/fetch-models和POST /api/providers/{id}/models返回 502。根因
FetchConfig.api_key从请求或数据库解密后直接传入 HTTP Authorization header。多行 key 包含\n,导致 reqwest HTTP 客户端拒绝设置 header,返回 "Invalid authorization header" 错误(0ms 延迟,无网络请求)。修复
添加
extract_first_key()辅助函数,按\n分割只取第一个 key。在fetch_models_anonymous和load_provider_config两处调用。为什么只取第一个 key?
RotatingApiClient/ApiKeyManager在 chat 请求层处理(环境变量级轮换)detect-protocol端点已有test_all_keys: true用于多 key 验证测试
新增 7 个测试,全部通过。