feat: enhance Authorization header handling with Header Override support - #2738
Conversation
WalkthroughReorder header setup so SetupRequestHeader runs before applying header overrides in API, form, and WSS request paths; remove duplicate SetupRequestHeader calls. Add guards in OpenAI adaptor to avoid overwriting Authorization when an Authorization override is present. Changes
Sequence Diagram(s)(No sequence diagrams generated — changes are focused header ordering and guards, not new multi-component control flow.) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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 |
|
@coderabbitai 原有的自定义渠道有在使用这个api key吗?该更改是否会如PR描述的兼容和不影响现有使用 |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: Length of output: 7424 🏁 Script executed: Length of output: 4613 🏁 Script executed: Length of output: 2780 🏁 Script executed: Length of output: 8781 🏁 Script executed: Length of output: 19 🏁 Script executed: Length of output: 1117 🏁 Script executed: Length of output: 4779 🏁 Script executed: Length of output: 0 🏁 Script executed: Length of output: 3995 🏁 Script executed: Length of output: 1437 🏁 Script executed: Length of output: 1804 🏁 Script executed: Length of output: 995 🏁 Script executed: Length of output: 0
根据代码分析,我发现该PR的更改对某些现有自定义渠道存在兼容性问题,与PR描述的"向后兼容"说法不完全一致。 问题分析更改前的行为(旧代码):func (a *Adaptor) SetupRequestHeader(...) error {
// ... Azure处理 ...
// 所有渠道类型(包括自定义渠道)都会设置默认Authorization
header.Set("Authorization", "Bearer "+info.ApiKey)
return nil
}
// 然后在api_request.go中应用Header Override旧行为:自定义渠道即使没有配置Header Override,也会自动获得 更改后的行为(新代码):func (a *Adaptor) SetupRequestHeader(...) error {
// ... Azure处理 ...
// 自定义渠道完全跳过默认Authorization设置
if info.ChannelType == constant.ChannelTypeCustom {
return nil // 直接返回,不设置任何Authorization
}
// 其他渠道类型才设置默认Authorization
if !hasAuthOverride {
header.Set("Authorization", "Bearer "+info.ApiKey)
}
return nil
}新行为:自定义渠道必须通过Header Override显式配置Authorization,否则不会有任何Authorization请求头。 兼容性影响✅ 不受影响的场景:
|
|
我已经在本地环境进行测试,目前来看应该是没有问题? |
但是看你这个改动看样子会影响使用自定义渠道配置了在标准的key的字段里配置了key的用户 |
|
好像明白了,我修改一下自定义渠道的逻辑 |
|
应该ok了 |
问题描述
在之前的实现中,Header Override 功能存在优先级问题:系统会在
SetupRequestHeader中设置默认的Authorization: Bearer {api_key}header,然后才应用 Header Override,导致用户自定义的 Authorization header 被覆盖。这导致无法使用非标准认证格式的 API(如
Key xxx:yyy格式),因为 Header Override 中的设置会被默认的 Bearer token 覆盖。修复内容
调整 Header Override 应用顺序(
relay/channel/api_request.go)DoApiRequest、DoFormRequest、DoWssRequest三个函数SetupRequestHeader之后优化自定义渠道处理(
relay/channel/openai/adaptor.go)向后兼容
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.