Skip to content

fix(gateway): 补齐辅助与创建入口的重复模型校验 - #7951

Open
lyy0709 wants to merge 2 commits into
Wei-Shaw:mainfrom
lyy0709:codex/duplicate-model-carriers
Open

lyy0709 wants to merge 2 commits into
Wei-Shaw:mainfrom
lyy0709:codex/duplicate-model-carriers

Conversation

@lyy0709

@lyy0709 lyy0709 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Alpha Search、Images 和 Live 创建请求也会先读取模型做准入/路由,再把正文交给另一层解析器。补齐这些入口的模型载体检查:重复 model、重复 session 或 Live 的重复 session.model 在 Composite 改写、调度和计费前返回 400 invalid_request_error;关闭模型白名单时同样生效。

与 #7909 的关系

这是 #7909 的补充。该 PR 尚未合入 main,所以本分支从当前上游 main 5fc0e486c3f6a8a191b8bd140f39b60457f611cf 出发,包含两个独立提交:

行为

  • 将重复键检测放入已有 pkg/requestmodel,原 fix(gateway): 拒绝重复 model 键,阻断解析歧义导致的模型走私 #7909 的入口复用它;比较解码后的键名,涵盖大小写和 Unicode 转义,同值/null 重复同样拒绝。
  • Alpha Search 在读取模型前检查;Images 覆盖 JSON 和 multipart;Live 在解析创建请求前检查外层 session 载体,并在服务校验中检查 session.model。
  • 共享准入中间件在模型白名单关闭时仍检查歧义,沿用 PrereadBody 回填,确保 Composite 路由改写前拒绝。直接 handler/解析器调用也有对应保护。
  • multipart 只检查模型/session 字段,不限制合法的多个图片文件;普通消息、工具参数中的嵌套 model 不递归拒绝。原有缺失模型、JSON 格式、请求大小和白名单规则保留。
  • ReplaceModelInBody 用固定次数线性扫描收敛重复模型键,保留末键位置、其他字段顺序和大整数,避免重复 Delete 在大量同名字段下反复扫描。

变更仅涉及上游网关代码与测试,无配置或数据库迁移。

验证

以下命令均在 backend 执行并通过;网关测试使用本地接收器/桩,并限制外部网络出站:

  • go test -tags=unit ./internal/handler ./internal/server/middleware ./internal/server/routes:完整三包通过(handler 42.105s、middleware 1.454s、routes 12.040s)。
  • go test -tags=unit ./internal/pkg/requestmodel ./internal/service -run 'TestValidateBody|TestHasDuplicateTopLevelKey|TestDuplicateModelKeyParserDiscrepancy|TestReplaceModelInBody|TestParseOpenAIImagesRequest|TestOpenAIGatewayServiceParseOpenAIImagesRequest|TestValidateLiveCallRequest':通过。
  • go build ./...、go vet ./...、golangci-lint run --timeout=30m:通过,lint 为 0 issues。
  • git diff --check:通过。

新增回归覆盖关闭白名单、两个模型均在白名单内、Composite 路由前拦截、直接 handler 拒绝、Live JSON/multipart 重复 session、大小写/转义/null/同值重复、大量重复键,以及合法多图片上传。

aishangwuji and others added 2 commits October 9, 2026 03:59
… smuggling

The gateway extracts the billing/admission model with gjson, which binds
the FIRST occurrence of a duplicated top-level "model" key, while the
conversion paths (encoding/json) and upstream runtimes bind the LAST one.
A body such as {"model":"cheap",...,"model":"expensive"} therefore passes
admission checks and is billed for the cheap model while the expensive
model is what actually executes upstream. The model-mapping rewrite
(sjson) only touches the first key, so the smuggled key survives mapping
too. Verified end-to-end on a live deployment: the executed model always
follows the last key while usage attribution follows the first.

Reject requests whose body contains more than one top-level "model" key
at every forwarding ingress (chat completions x2, responses, OpenAI
compatible messages x2, native Anthropic messages, embeddings), and make
ReplaceModelInBody collapse duplicate keys (keeping the last key in place
and the surrounding field order) so any path that still carries
duplicates cannot smuggle a model past the mapping rewrite. Remaining
non-forwarding endpoints (count-tokens, alpha search, realtime WS
passthrough) can adopt the same check as follow-ups.

(cherry picked from commit c461b1c)
Copilot AI balanced review requested due to automatic review settings October 8, 2026 20:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

riofutab added a commit to riofutab/sub2api that referenced this pull request Oct 9, 2026
- 合入上游 Wei-Shaw#7951 第二个提交的入口覆盖意图,改写为复用本仓 Wei-Shaw#7909 实现,不引入第二套检测
- 重复键检测从 service 移到 pkg/requestmodel(零拷贝、大小写不敏感),新增 ValidateBody 统一检查顶层 model 与 Live 的 session、session.model,multipart 只看非文件字段
- 分组白名单中间件在白名单关闭时也检查模型载体歧义,在合成路由改写与调度前返回 400;同值重复也拒绝
- Alpha Search、图片 multipart、Live 创建(JSON 与 multipart)增加重复载体校验
- 本仓修正:Live 校验使用原始 Content-Type,避免小写化后 multipart boundary 不匹配导致检查被静默跳过

测试:requestmodel、middleware、routes 包 unit 测试与 service、handler 相关定向 unit 测试通过,go vet 通过;未跑 golangci-lint 与全量测试。
riofutab added a commit to riofutab/sub2api that referenced this pull request Oct 9, 2026
- 合入上游已合并的 Wei-Shaw#7618:平台改为数据驱动,接入 Command Code 与 Cline,Go 升级到 1.27.2,golangci-lint 升级到 v2.14.0。
- 合入开放 PR Wei-Shaw#7920 Wei-Shaw#7924 Wei-Shaw#7928 Wei-Shaw#7930 Wei-Shaw#7931 Wei-Shaw#7933 Wei-Shaw#7935 Wei-Shaw#7936 Wei-Shaw#7939 Wei-Shaw#7941 Wei-Shaw#7947 Wei-Shaw#7951 Wei-Shaw#7954 Wei-Shaw#7956 Wei-Shaw#7960。
- 新增 Decisions 决策接口(Wei-Shaw#7922)及其与 TypeSafe System One 的双向互转(Wei-Shaw#7948),上游 404 时切换账号。
- Haiku 5.5 的 top_p 违规请求在网关直接拒绝,OAuth 伪装不再在 top_p 旁补 temperature,工具集 beta 过滤覆盖 Haiku 5.5。
- 修正新增代码的静态检查告警。

全量 unit 测试、golangci-lint、前端全量 vitest 均已通过;集成测试本机无 Docker,待 CI 验证。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants