Skip to content

feat(headers): 用通用兜底规则替代 UA 专用配置 - #979

Open
ihipop wants to merge 1 commit into
tbphp:mainfrom
ihipop:feat/header-rules-default
Open

ihipop wants to merge 1 commit into
tbphp:mainfrom
ihipop:feat/header-rules-default

Conversation

@ihipop

@ihipop ihipop commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

关联 Issue / Related Issue

Related: #963

变更内容 / Change Content

  • Bug 修复 / Bug fix
  • 新功能 / New feature
  • 其他改动 / Other changes

@tbphp 感谢之前在 #963 中的讨论。进一步考虑后,我认可将这类需求统一放在系统/分组请求头规则中的方向,不必为 User-Agent 单独增加一个配置入口。

这个 PR 在现有“设置/移除”旁增加“兜底”,用于只补齐缺失或空值,不覆盖客户端已有的非空请求头。例如 User-Agent 兜底为 Go-http-client/1.1,既保留正常客户端身份,也能覆盖无 UA 的请求以及服务端发起的凭据探测和模型发现。请帮忙审核这种通用方案是否更符合项目的设计。

功能范围

  • API 增加 header_rules.default,系统和分组均可配置;经典/新版界面都可编辑、保存和回读。
  • 在现有显式操作后补齐默认值;同名多值 Header 只要存在非空值就完整保留,不覆盖后面的有效值。
  • 沿用现有名称/值校验、禁止凭据和传输控制 Header、大小写重复检查、${API_KEY} 替换、错误摘要脱敏以及探测配置签名。
  • 分组覆盖仍然替换整套规则,不改成合并。下游响应头规则不增加兜底操作。

替换 #963(有意的 RC 配置变更)

本 PR 完整撤回 #963 中新增的 UPSTREAM_USER_AGENT_FALLBACK 示例、环境变量解析/配置字段、容器注入、执行器及 WebSocket 参数传递、相关专用测试和三语环境变量表说明,不保留兼容分支。

使用旧变量的 RC 用户需要在升级时改用系统或分组的请求头兜底规则。没有自定义兜底规则时,原有内置 GPT-Load/<version> UA 行为不变。此 PR 不撤销 #964 的 OpenAI Audio Speech 支持。

示例:

{
  "header_rules": {
    "set": {},
    "remove": [],
    "default": { "User-Agent": "Go-http-client/1.1" }
  }
}

验证

  • 新增回归覆盖:缺失/空白/客户端非空值、同名多值 Header、模板替换、错误摘要脱敏、非法和重复配置、继承及整套覆盖、设置保存回读、探测签名变化。
  • 完整 make check 已通过(Go 格式/tidy/vet/全量单测、前端 lint/format/类型检查/构建、Go 构建和 diff 检查)。使用项目要求的 Node 24.18.0、pnpm 11.17.0 和标准 Go 缓存。
  • 未执行浏览器 E2E、维护者专用 release-simulate,也未调用付费上游;未部署到现有生产实例。

自查清单 / Checklist

  • 我已运行 make check,或在说明中写明无法运行的原因和未验证范围。 / I ran make check, or documented why it could not run and what remains unverified.
  • 本 PR 范围聚焦,未包含无关改动。 / This PR is focused and contains no unrelated changes.
  • 我已更新必要的公开文档或发布说明。 / I updated any required public documentation or release notes.
  • 我已确认提交、日志和测试数据不包含敏感信息。 / I confirmed that commits, logs, and fixtures contain no sensitive data.
  • 如适用,我已说明兼容性或数据迁移影响。 / Where applicable, I documented compatibility or data-migration impact.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

新增上游请求头 Default 规则。请求头缺失或仅含空白时,系统应用默认值,并支持 ${API_KEY} 替换。后端配置接口、验证逻辑和两个前端均支持该规则。普通通道不再使用 UPSTREAM_USER_AGENT_FALLBACK;缺少非空 User-Agent 时使用内置的 GPT-Load/<version>。

Merge Risk: Low · up to fd8c9

Header Default rules and the move from the User-Agent environment variable are implemented consistently. Two narrow edge cases remain: a multi-value User-Agent whose first value is blank, and a default rule named __proto__ in the classic editor. Both are low impact and can be fixed before or shortly after merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed 标题“feat(headers): 用通用兜底规则替代 UA 专用配置”概括了主要变更:新增 header_rules.default 通用兜底操作,并移除 UPSTREAM_USER_AGENT_FALLBACK 这一 UA 专用配置。标题与变更集直接相关,聚焦于核心意图。标题使用 Conventional Commits 格式,属于可接受的风格,不影响判定。
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed PR 描述基本完整:已填写关联 Issue(Related: #963),勾选了变更类型(新功能、其他改动),详细说明了功能范围、对 #963 的替换、兼容性影响(RC 用户需迁移到 header_rules.default),并列出验证范围与未验证项。自查清单已逐项勾选。主要不足:模板要求的 “Closes #” 格式被替换为 “Related: #963”,且 #963 是否应被关闭未明确;另…
Full details: Description check

Explanation

PR 描述基本完整:已填写关联 Issue(Related: #963),勾选了变更类型(新功能、其他改动),详细说明了功能范围、对 #963 的替换、兼容性影响(RC 用户需迁移到 header_rules.default),并列出验证范围与未验证项。自查清单已逐项勾选。主要不足:模板要求的 “Closes #” 格式被替换为 “Related: #963”,且 #963 是否应被关闭未明确;另外需求中的“Bug 修复”未勾选,符合“新功能”定位即可。这些属于非关键项,不影响整体通过。

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.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Quiet mode is enabled, so only the most important comments were posted inline. Other review comments are grouped below.

Other comments: 2

internal/gateway/execution_forward.go: 1

982-982: Minor · Functional Correctness · Quick win

withUserAgent 只检查首个 User-Agent 值,会丢弃后续有效值。

当请求头 User-Agent 包含多个值且首个值为空白时,spec.Header.Get("User-Agent") 返回空字符串。函数随后调用 Set,用内置值覆盖整个字段,客户端的有效值会丢失。应检查全部值,只要存在非空白值就保留原始请求头。execution_forward.go 第 982 行保留了全部值,该行无需修改。

建议修复

-	if strings.TrimSpace(spec.Header.Get("User-Agent")) != "" {
-		return spec
+	for _, ua := range spec.Header.Values("User-Agent") {
+		if strings.TrimSpace(ua) != "" {
+			return spec
+		}
 	}

Source: Learnings

web/src/frontends/classic/components/config/HeaderRulesEditor.vue: 1

102-102: Minor · Data Integrity & Integration · Quick win

避免在编辑后丢失 __proto__ 默认请求头。

__proto__ 是合法请求头名称。读取该默认规则后,如果用户修改另一行,此处会向普通对象的 __proto__ setter 赋字符串,不会创建自有属性。随后发布的规则会静默丢失该请求头。

请使用 Object.create(null) 创建 default 映射,或用 Object.fromEntries 构造映射。

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: QUIET
  • Plan: Advanced
  • Run ID: 26f6d888-36ca-49a5-a6a0-f9bea2127c81
Commits

Reviewing files that changed from the base of the PR and between ae50d0b and fd8c9ce.

Files selected for processing: 47

  • .env.example
  • README.md
  • README_CN.md
  • README_JP.md
  • internal/container/container.go
  • internal/control/discover_executor.go
  • internal/control/group_detail.go
  • internal/control/header_defaults_test.go
  • internal/control/settings.go
  • internal/control/validation.go
  • internal/execution/bifrost/executor.go
  • internal/execution/bifrost/newapi_runtime_test.go
  • internal/execution/bifrost/runtime.go
  • internal/execution/bifrost/test_runtime_test.go
  • internal/execution/bifrost/user_agent.go
  • internal/execution/bifrost/user_agent_test.go
  • internal/execution/bifrost/websocket.go
  • internal/gateway/execution_forward.go
  • internal/gateway/header_defaults_test.go
  • internal/gateway/request_log.go
  • internal/platform/config/config.go
  • internal/platform/config/config_test.go
  • internal/scheduler/scheduler.go
  • internal/state/header_defaults_test.go
  • internal/state/runtime_settings.go
  • internal/state/snapshot.go
  • web/src/frontends/classic/api/control/types.ts
  • web/src/frontends/classic/app/resources/groups.ts
  • web/src/frontends/classic/app/resources/settings.ts
  • web/src/frontends/classic/components/config/HeaderRulesEditor.vue
  • web/src/frontends/classic/components/config/header-rules-validation.ts
  • web/src/frontends/classic/features/groups/settings/GroupSettingsTab.vue
  • web/src/frontends/classic/features/groups/settings/group-settings-patch.ts
  • web/src/frontends/classic/features/settings/BrowserAccessSection.vue
  • web/src/frontends/classic/features/settings/settings-patch.ts
  • web/src/frontends/classic/i18n/locales/en-US/core.ts
  • web/src/frontends/classic/i18n/locales/en-US/group.ts
  • web/src/frontends/classic/i18n/locales/ja-JP/core.ts
  • web/src/frontends/classic/i18n/locales/ja-JP/group.ts
  • web/src/frontends/classic/i18n/locales/zh-CN/core.ts
  • web/src/frontends/classic/i18n/locales/zh-CN/group.ts
  • web/src/frontends/modern/api/group-detail.ts
  • web/src/frontends/modern/api/settings.ts
  • web/src/frontends/modern/features/groups/GroupAdvancedPanel.vue
  • web/src/frontends/modern/features/settings/SettingsHeadersEditor.vue
  • web/src/frontends/modern/features/settings/settings-draft.ts
  • web/src/frontends/modern/i18n/locales/settings-form.ts
Files with no reviewable changes: 3

  • .env.example
  • internal/execution/bifrost/test_runtime_test.go
  • internal/platform/config/config_test.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@ihipop
ihipop force-pushed the feat/header-rules-default branch from fd8c9ce to ddaa9dd Compare November 16, 2026 16:04
@ihipop

ihipop commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

@coderabbitai 感谢审查,两条反馈都已核对,问题确实成立。

  • 多值 User-Agent:withUserAgent() 只检查首个值的逻辑,在 feat(execution): 支持配置上游 User-Agent 兜底值 #963 之前就已存在,并非本 PR 新增。
  • __proto__:原有“设置”操作就使用普通对象按名称赋值,已有相同的丢值问题;新增的“兜底”沿用了该写法,因此也存在这个边界问题。

本次按范围约束,不在这个 PR 中修复这些既有逻辑及其沿用行为,避免扩大改动。两条建议均暂不采纳为本 PR 的修改项;如需处理,适合另开聚焦的修复 PR。当前提交保持不变。

@coderabbitai

coderabbitai Bot commented Nov 17, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

@ihipop 感谢核对。我理解这两项问题暂不在本 PR 中修复,目前仍然存在。

需要我分别创建两个 GitHub Issue,以便后续跟踪吗?

You are interacting with an AI system.

@ihipop

ihipop commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

[!TIP]
For best results, initiate chat on the files or code changes.

@ihipop 感谢核对。我理解这两项问题暂不在本 PR 中修复,目前仍然存在。

需要我分别创建两个 GitHub Issue,以便后续跟踪吗?

You are interacting with an AI system.

@coderabbitai 请咨询仓库owner决定

@tbphp tbphp self-assigned this Nov 18, 2026
@tbphp tbphp added the under-review Currently being reviewed. label Nov 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

under-review Currently being reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants