fix(auth): support x-api-key header for /v1/models endpoint - #2436
fix(auth): support x-api-key header for /v1/models endpoint#2436RedwindA wants to merge 1 commit into
Conversation
WalkthroughThe TokenAuth middleware path check is expanded from only detecting "/v1/messages" to detecting both "/v1/messages" and "/v1/models" endpoints. The Anthropic API key lookup and Authorization header setting now apply to both paths. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
middleware/auth.go (1)
196-202: LGTM! Change correctly extends x-api-key support to /v1/models endpoint.The logic change successfully addresses the PR objective by allowing the
/v1/modelsendpoint to accept Anthropic'sx-api-keyheader. The implementation mirrors the existing/v1/messageshandling.Optional: Consider using
HasPrefixfor more precise matching.The current implementation uses
strings.Contains(), which performs substring matching. This could match unintended paths (e.g.,/api/v1/models,/v1/models_backup). For consistency with the Gemini API handling (lines 204-206) and more precise matching, consider refactoring to usestrings.HasPrefix():- // 检查path包含/v1/messages或/v1/models - if strings.Contains(c.Request.URL.Path, "/v1/messages") || strings.Contains(c.Request.URL.Path, "/v1/models") { + // 检查path以/v1/messages或/v1/models开头 + if strings.HasPrefix(c.Request.URL.Path, "/v1/messages") || strings.HasPrefix(c.Request.URL.Path, "/v1/models") { anthropicKey := c.Request.Header.Get("x-api-key") if anthropicKey != "" { c.Request.Header.Set("Authorization", "Bearer "+anthropicKey) } }
|
与 #2425 重复 |
PR 类型
PR 是否包含破坏性更新?
PR 描述
修复 #2422
问题: 当客户端仅使用 Anthropic 的
x-api-key头部(不带 Bearer Token)请求/v1/models端点时,返回 401 未授权错误。原因:
middleware/auth.go中的TokenAuth()中间件仅对/v1/messages路径提取x-api-key头部,未对/v1/models路径进行相同处理。解决方案: 扩展
TokenAuth()中间件的路径检查条件,使其同时支持/v1/messages和/v1/models路径的x-api-key头部认证。这与
router/relay-router.go中已有的 Anthropic 格式路由适配保持一致。Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.