Skip to content

fix: skip Accept-Encoding during header passthrough (#2214) - #2997

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
Caisin:fix/issue-2214-accept-encoding-passthrough
Feb 24, 2026
Merged

fix: skip Accept-Encoding during header passthrough (#2214)#2997
seefs001 merged 1 commit into
QuantumNous:mainfrom
Caisin:fix/issue-2214-accept-encoding-passthrough

Conversation

@Caisin

@Caisin Caisin commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

背景

开启请求头透传后,使用 curl 请求可能报错:
invalid character '\x1f' looking for beginning of valuebad_response_body)。

根因

透传规则(* / re:)把客户端 Accept-Encoding 也转发到了上游。
在这种情况下,上游可能返回 gzip 压缩响应体,而当前链路会把该二进制内容按 JSON 解析,导致首字节 0x1f 触发解析失败。

修复内容

  • 在请求头透传黑名单中新增 accept-encoding,避免通过通配/正则透传该头。
  • 新增测试 TestProcessHeaderOverride_PassthroughSkipsAcceptEncoding,验证透传时会跳过 Accept-Encoding

验证

  • go test ./relay/channel -run TestProcessHeaderOverride -count=1
  • go test ./relay/channel -count=1

影响范围

  • 仅影响“按名称透传”的头部规则(* / re: / regex:)。
  • 不影响其他头透传逻辑。
  • 显式 header override 仍可按配置覆盖。

Summary by CodeRabbit

  • Bug Fixes
    • Resolved incorrect header forwarding behavior in API requests to properly exclude accept-encoding headers while maintaining request traceability.

@coderabbitai

coderabbitai Bot commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

Added "accept-encoding" to the passthrough skip header set in the API request handler to prevent this header from being forwarded by passthrough rules. A corresponding test validates that the Accept-Encoding header is properly omitted while preserving other headers like X-Trace-Id.

Changes

Cohort / File(s) Summary
Header Skip Configuration
relay/channel/api_request.go
Extended passthroughSkipHeaderNamesLower to include "accept-encoding", preventing this header from being forwarded in passthrough header rules.
Validation Test
relay/channel/api_request_test.go
Added TestProcessHeaderOverride_PassthroughSkipsAcceptEncoding to verify that Accept-Encoding is omitted when passthrough headers are configured, while preserving X-Trace-Id.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A header's been skipped from the flow,
Accept-Encoding won't steal the show,
Clean passthrough rules, no escape in sight,
With tests in place, everything's right!
hop hop

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding Accept-Encoding to the header passthrough skip list to fix JSON parsing errors.
Linked Issues check ✅ Passed The pull request directly addresses issue #2214 by preventing gzip-compressed responses from being misinterpreted as JSON through header passthrough blacklisting.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the Accept-Encoding header passthrough issue: modifications to skip header set and corresponding test coverage.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@seefs001

seefs001 commented Feb 24, 2026

Copy link
Copy Markdown
Collaborator

有你说的issues之前还没请求头透传功能呢。。。
可以看你遇到的情况来调整,但是没必要挑着以前的issues来猜测吧

@Caisin

Caisin commented Feb 24, 2026

Copy link
Copy Markdown
Contributor Author

我用了请求头透传,出现了这个问题

{"error":{"message":"invalid character '\x1f' looking for beginning of value","type":"bad_response_body","param":"","code":"bad_response_body"}}
然后修复了后
搜索看到了这个issue,顺手关闭下

@seefs001

Copy link
Copy Markdown
Collaborator

我用了请求头透传,出现了这个问题

{"error":{"message":"invalid character '\x1f' looking for beginning of value","type":"bad_response_body","param":"","code":"bad_response_body"}} 然后修复了后 搜索看到了这个issue,顺手关闭下

是你自己遇到的那可以,这个Issue关联我就去掉了,他和这个毫无关系。

@seefs001
seefs001 merged commit a7c35cd into QuantumNous:main Feb 24, 2026
1 check passed
@Caisin
Caisin deleted the fix/issue-2214-accept-encoding-passthrough branch February 24, 2026 04:47
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
…-encoding-passthrough

fix: skip Accept-Encoding during header passthrough (QuantumNous#2214)
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.

2 participants