Skip to content

fix: 按 MIME 转换 Anthropic 媒体并安全拒绝非法载荷(部分修复) - #984

Merged
ranxi2001 merged 8 commits into
productionfrom
fix/issue981-anthropic-media-mime-20261121
Nov 21, 2026
Merged

ranxi2001 merged 8 commits into
productionfrom
fix/issue981-anthropic-media-mime-20261121

Conversation

@ranxi2001

@ranxi2001 ranxi2001 commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

合并前验收,当前提交 3209eb3359b31f34d2c8fc4bca82c21148ad0d6f。 本 PR 交付 #981 的 MIME 转换和本地拒绝部分。PNG、JPEG、GIF、WebP、PDF 和纯文本按 MIME 转换,兼容上游原生音视频开关继续由 #981 跟踪。

测试修复 6e10e4043 将工具媒体用例的模拟上游改为 HTTPS,保留生产 URL 校验。当前提交已整合 production 的 #971 和 #986。执行 gh pr checks 984 -R ranxi2001/sub2api 核对 63 项通过、12 项按工作流条件跳过。覆盖 Chat 和 Responses 的媒体转换、流式及非流式工具结果和后端单元测试。CI 结果

本轮维护由 Codex(GPT-6,环境未提供完整模型 ID 和推理强度)定位 CI 失败、修复测试、复核代码和最终检查。以下保留作者的历史记录。

Checkpoint correction: 2d5e70bfea4f44842a25f4e95f83c245ed23e58d additionally applies the exact two-line whitespace patch from neighboring PR971 timezone-race job136812747814. The inherited file blob was identical here; no failure on this PR is inferred from the neighboring job. Parent independently reviewed the full diff and whitespace-insensitive equality. No behavior/assertion change; new-head formatting/race/all-CI pending. No local gofmt or dynamic validation ran.

Current checkpoint: 2d5e70bfea4f44842a25f4e95f83c245ed23e58d — repair published, new CI pending; still Draft. Older SHA results below are historical only.

2026-11-21 exact-head CI repair

  • Ordinary-merged production fix: 加密会话遇到原账号模型短暂冷却时返回重试时间 #983/fix: 在开启结算事务前按用户排队,避免余额行锁占满连接池 #982 without conflicts. Focused repair bd84e0f28: initialize media handler keys with documented AllowLongContext=true default; preserve the real restricted-key production guard and all MIME/upstream/billing assertions. Correct Antigravity Gemini-target assertions to inspect the existing serialized document carrier including exact MIME/data rather than incorrectly expecting inlineData for unsupported document types.
  • Historical RED compile contamination independently fixed by adding only baseline-absent dependent media fixtures to the shared full-historical-RED manifest. Selected business oracles, validators, candidate/GREEN tests and production media policy are unchanged.
  • Independent focused source review LIMITED SOURCE CLEAR for bd84; parent reviewed final ancestry and verified the nine inherited production files match the current production merge exactly. Runtime still pending: three handler roots, Gemini/Antigravity compatibility matrices, unit shards and four historical RED gates require new-head execution. No local dynamic checks ran.
  • Agent disclosure addendum: Codex sole repair writer requested gpt-6.1-sol / ultra / fast, actual full served profile/tier unverified. Independent Hermes reviewer model gpt-6.1-sol per tool metadata, reasoning/tier unknown. Parent personally inspected focused diff and integration.
  • Issue [Bug] OpenAI→Anthropic 转换时视频/音频被当成 image/document 块(media_type 原样照搬),上游 400 #981 full native audio/video opt-in remains incomplete; keep Draft / Refs only, no understanding, merge or closure claim.

当前 head 0290d4ea3cc7571539c71f695cde5cb17dafa9e0:限定源码修复已发布,远端执行待验收,保持 Draft。 原两项 production blocker(Chat工具媒体flatten漏检、strict共享策略跨目标)已在 964eb86ec 修复并独立复审;review发现的Gemini Responses OAuth fixture不可达已由test-only 0290d4ea3 修复,父级亲读该delta确认使用API-key及正确JSON/SSE/request envelope,未放宽生产guard。此fixture delta尚无独立动态通过证据。

新增具名回归(全部本地 NOT RUN):TestGatewayAnthropicChatToolMediaRejectsBeforeFlattening、TestNativeAnthropicChatToolMediaRejectsBeforeFlattening、TestChatAnthropicToolMediaPreservesParallelAndStringOutputs、TestResponsesToAnthropicMediaPolicyDefaultsRemainCompatible、TestGeminiCompatibilityMediaPolicyIsTargetScoped、TestAntigravityCompatibilityMediaPolicyIsTargetScoped、TestGeminiCompatibilityMediaPolicyOAuthControls。前置校验拒绝真实carrier而非普通字符串;支持媒体保留tool/function配对;strict仅四个实际Anthropic出口启用,其余Gemini/Antigravity保留permissive carrier。原生audio/video opt-in未实现,Refs #981,不关闭Issue。

披露:父级核实Codex原writer rollout gpt-6.1-sol / ultra,fast仅requested;继承的Codex只读子Agent实际ID/强度未知。Hermes父级及两轮只读独审 gpt-6.1-sol,推理强度未知(接口未提供)。独审只读,父级发布与remoteCI验收,未运行本地动态检查/生产/付费请求。以下保留模板与初片范围;其旧候选SHA仅为历史源码检查,不代替当前head执行证据。

解决的问题与行为变化

  • 触发条件 / 原行为:Anthropic 分组的 /v1/chat/completions、/v1/responses 在 image/file 字段收到视频、音频等 data URI 时,仅按字段生成 image/document,MIME 原样照搬,可能收到上游 400;旧模型的转换失败还可能退化为通用服务端错误。
  • 修改后行为:按 MIME 生成正确块;png/jpeg/gif/webp 使用 image,PDF 使用 document/base64,plain text 解码为合法 document/text。未知/音视频 MIME、错误 data URI、空载荷、无效 base64/UTF-8 文本在上游发送前返回明确且不回显载荷的 400 invalid_request_error。
  • 本次范围与仍未解决的问题:这是 [Bug] OpenAI→Anthropic 转换时视频/音频被当成 image/document 块(media_type 原样照搬),上游 400 #981 的严格转换/拒绝部分。未实现兼容上游原生 audio/video opt-in;没有新增原生媒体 schema、账号/渠道开关或 UI,不代表支持视频/音频理解。普通 URL 与 file-ID-only 的既有忽略策略保留,不获取远程内容。共享 Gemini/Antigravity 文档消费者同步保持 plain text 内容。

关联记录与来源

管理员新功能提示

  • 提示 ID 与登记项:不适用,内部协议修复,无管理员新功能。
  • 所有 NewFeatureBadge 展示位置:不适用,无 UI 入口。
  • 关闭行为:不适用,无提示。
  • ID 沿用 / 重新公告说明:不适用,无提示变更。
  • 同一 PR 的必要回归证据:不适用,无提示机制变更;协议回归源代码已添加但尚未运行。
  • A 已关闭后升级到 B,B 的新 ID 仍可见:不适用,无新增提示。
  • 点击 B 任一入口的标签,相关入口同步隐藏:不适用,无新增入口。
  • 刷新/重新登录后 B 仍隐藏:不适用,无提示状态变化。
  • 后续 C 新 ID 正常显示,普通升级不复活 A/B:不适用,无 ID 变更。

复现与验收

  • 环境与基线:ranxi2001/sub2api,base production,基线 04478a2e1a658c03c8632fc79e28255ef354c094;候选 32438a39c47b25b50d7df3d5066ac1cf02c00e54。仅使用虚构数据编写回归,未进行生产复现。
  • 前置条件与操作:
    1. 隔离测试中配置 Anthropic 分组及合成 API-key 账号。
    2. 分别通过两个真实 handler 的 image/file 字段传入支持、跨字段 MIME、未知及格式错误 data URI;覆盖 stream true/false。
  • 修复前观察:静态源码显示块类型仅按字段生成,音视频 MIME 不校验;Chat 的空 base64 image 会先被通用桥丢弃。未执行 RED 复现。
  • 修复后验收标准:合法 MIME 生成正确块/source;不支持或错误 data URI 返回 400,无载荷回显、无上游调用、无 usage/billing 命令;URL 策略与任意普通文本保持;共享文档转换保留内容。未执行 GREEN。
  • 回归测试:backend/internal/handler/gateway_anthropic_media_test.go、backend/internal/pkg/apicompat/responses_to_anthropic_media_test.go、backend/internal/pkg/antigravity/tool_result_images_test.go、backend/internal/service/gateway_anthropic_media_test.go。尚未覆盖真实兼容供应商原生音视频能力。
协议、调度或传输改动补充
  • 真实链路:routes/gateway.go → GatewayHandler.ChatCompletions/Responses → GatewayService.ForwardAsChatCompletions/ForwardAsResponses → Chat→Responses(仅 Chat)→ ResponsesToAnthropicRequest → MIME helper;CN native Anthropic 路径使用同一转换器。
  • 合成输入示例:{"type":"input_image","image_url":"data:video/mp4;base64,c3ludGhldGlj"} → 本地 400;input_file 携带 image/png → image;input_image 携带 PDF → document。
  • 正常完成、上游拒绝、超时、EOF、客户端取消:现有处理保持;新校验失败发生在 token 查询、请求构建、上游发送之前。
  • HTTP/业务错误:400 + invalid_request_error,消息仅包含固定文本,不拼接输入 MIME/URI/正文;新失败不产生上游 request ID 或 usage。
  • 重试/故障转移/幂等:新校验失败不包装为上游 failover 错误,不触发媒体远程重试。
  • 并发/粘性:沿用 handler 的释放流程;新增 handler 回归断言没有上游和用量/计费操作,但尚未执行。

实际验证

检查与执行目录 / 命令 结果 覆盖范围、证据或未运行原因
工作区 git diff --check、git diff --cached --check 通过,exit 0 提交前静态空白检查
工作区 git diff HEAD^ HEAD --check 通过,exit 0 候选提交静态空白检查;提交后工作区干净
TestGatewayAnthropicMediaHandlers 未运行 按本次指令禁止本地动态检查,待远程 CI;两个真实 handler、stream 模式、MIME/格式错误/零上游
TestResponsesToAnthropicMediaMIMEBlocks、TestResponsesToAnthropicMediaRejectsSafely、TestResponsesToAnthropicMediaURLPolicy、TestResponsesToAnthropicToolMediaRejectsSafely、TestResponsesToAnthropicMediaLeavesTextUntouched 未运行 转换矩阵、错误脱敏、工具结果及普通文本;待远程执行
TestResponsesPlainDocumentSurvivesAnthropicBridge、TestGeminiDocumentsSurviveAnthropicMIMEBridge 未运行 共享文档消费者;待远程执行
gofmt / lint / typecheck / build / RED-GREEN / 生产验证 未运行 用户限制本地动态检查;未把静态分析当作编译或测试通过

首次未满足的验收门槛为当前 head 远程定向回归执行及候选编译;具名新回归尚未运行。当前仅交付可审查源码增量;Draft 可先发布,不以等待大套件阻塞。

兼容性与运行影响

  • 配置 / API / 默认行为:无新配置;保留导出转换器签名。新 typed media error 在 Anthropic 分组稳定映射为本地 400。只支持既有无参数 base64 data URI 形式,不扩展 URL 下载/MIME 推断。
  • 数据库迁移、数据兼容与二进制回滚限制:不适用,无迁移或持久化格式变化。
  • 权限、凭据脱敏、日志或采集内容:固定错误消息不引用调用者提供的 URI、MIME header、base64 或正文;无新增日志/采集。
  • 计费、资源开销、并发或性能:无发票/计费功能修改;拒绝请求不发送上游。base64 校验对二进制流式丢弃解码结果,plain text 解码为 UTF-8;未测量性能。
  • 灰度 / 回滚方式与残余风险:父任务独立审查、远程验证后决定发布;按提交回退。本转换器也用于 Gemini/Antigravity,后续修复已将 strict policy 限定到实际 Anthropic 出口;其他目标保留既有 MIME carrier,文档下游仍需远程回归。二进制文件内容/大小、损坏图片/PDF 不由本次 base64 语法检查完全验证。

文档与用户可见变化

客户端得到明确的本地媒体格式错误;受支持媒体按 MIME 生成合法块。本次不新增设置页或音视频理解能力。原生兼容上游 opt-in 需明确供应商版本、schema、能力边界及隔离验证,仍属于 #981 后续范围;不关闭 Issue。提交、PR、Release 与生产部署状态分别披露。

Agent 使用声明

  • Codex /root:唯一源码与提交 writer;问题分析、回归源码、实现及本正文。运行环境标识 GPT-6;完整部署模型 ID、推理强度不可获取,均为未知。brief 请求 gpt-6.1-sol / ultra / fast,不能据此声称实际交付配置一致。
  • Codex /root/media_readonly_review:只读链路/调用者/原生能力审计,无修改和动态检查;继承父模型设置,完整 ID/强度未知。后续复核请求因工具载荷缺失未执行。
  • Codex /root/static_review:未完成实质复核,工具载荷缺失;无修改或检查。继承父模型设置,完整 ID/强度未知。

提交前自查

  • base 分支和改动范围正确,没有夹带无关修改。
  • 正文、提交、测试数据和附件不含凭据、ticket/state 或真实用户敏感信息。
  • 实际验证结果已列出,失败、未运行和未覆盖的部分已说明。
  • 已检查配置默认值、权限、兼容性及相关文档;不适用项已说明。
  • Bug 修复有针对性回归源代码;执行证据待远程 CI。
  • 已核对管理员新功能提示要求:本次不涉及,相关字段逐项说明不适用。
  • 新功能提示必要回归:不适用,未新增提示或功能入口,已逐项说明。
  • 已填写所有参与 Agent 的实际使用声明,未知模型设置未猜测。

Choose image/document blocks from supported data URI MIME types, decode plain text documents, and return safe local 400 errors for unsupported or malformed media on OpenAI compatibility routes. Preserve shared document consumers and legacy URL handling.

Refs #981
Validate Anthropic-target Chat media before generic tool/function flattening and retain supported structured tool output. Keep the existing converter default permissive for Gemini and Antigravity, opting actual Anthropic forwarding into strict MIME checks.

Refs #981
Use API-key Gemini accounts for both media compatibility entry points, matching native request roots and JSON/SSE responses. Keep Antigravity wrapped upstream SSE and add Chat OAuth plus Responses OAuth rejection controls without changing production guards or media assertions.

Refs #981
…lation

Initialize media handler keys with the documented long-context default, assert the shared Gemini document carrier for Antigravity Gemini targets, and exclude only the candidate-only dependent media fixtures from complete historical RED overlays.

Refs #981
@ranxi2001
ranxi2001 marked this pull request as ready for review November 21, 2026 13:35
@ranxi2001
ranxi2001 merged commit b176b26 into production Nov 21, 2026
75 checks passed
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.

1 participant