Skip to content

feat: 支持基于Go Regex规则和全量的请求体透传 - #2840

Merged
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:feature/header-regex-override
Feb 4, 2026
Merged

Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:feature/header-regex-override

Conversation

@seefs001

@seefs001 seefs001 commented Feb 4, 2026 •

Copy link
Copy Markdown
Collaborator

功能位置:渠道编辑-请求头覆盖

'*': true 透传全部请求体,忽略部分可能引起请求异常的请求头和x-api-key/authorization/x-goog-api-key请求头
're:^X-Trace-.*$': true 透传符合'^X-Trace-.*$'表达式的请求头
'X-Foo': '{client_header:X-Foo}' 透传X-Foo请求头

Summary by CodeRabbit

Release Notes

  • New Features
    • Added header passthrough functionality for API request configuration with wildcard and regex-based pattern support.
    • Added template buttons in channel configuration modal to streamline header passthrough setup.
    • Extended multilingual support for passthrough template options across multiple languages.

@coderabbitai

coderabbitai Bot commented Feb 4, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

This PR introduces a header passthrough feature for API request handling. The backend adds wildcard and regex-based passthrough rules with a skip-list for unsafe headers, while the frontend provides UI templates for configuration. Translations are added across six languages to support the new passthrough template labels.

Changes

Cohort / File(s) Summary
Backend Header Passthrough Logic
relay/channel/api_request.go
Introduces header passthrough with wildcard (*) and regex (re:/regex:) support. Adds regex caching, helper functions, and extends processHeaderOverride to apply passthrough headers prior to explicit overrides with proper precedence handling. Includes validation and error handling for invalid regex patterns.
Frontend UI Template
web/src/components/table/channels/modals/EditChannelModal.jsx
Adds clickable template button for header override field and updates the second template option label from "填入模板" to "填入透传模版" with a passthrough template payload including wildcard and regex-based header rules.
Internationalization
web/src/i18n/locales/{en,fr,ja,ru,vi,zh}.json
Adds two new translation keys across six language locales: "填入透传模版" (Fill Passthrough Template) and "填入透传完整模版" (Fill Full Passthrough Template), with localized translations for each language.

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly Related PRs

Poem

🐰 Hops through headers with a gleeful bound,
Wildcards and regexes round and round,
Passthrough magic, no unsafe ones stay,
Templates and translations light the way! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main feature added: header passthrough support using Go Regex rules and full request body passthrough, which aligns with the primary changes across the codebase.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 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.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 3

🤖 Fix all issues with AI agents
In `@relay/channel/api_request.go`:
- Around line 50-74: The passthrough skip list currently omits the cookie
header; update the map variable passthroughSkipHeaderNamesLower in
relay/channel/api_request.go (the map literal containing RFC 7230 hop-by-hop and
other sensitive headers) to include "cookie": {} so request cookies are not
forwarded by wildcard/regex passthrough rules; also add or adjust unit tests
that assert sensitive headers are blocked if such tests exist.

In `@web/src/components/table/channels/modals/EditChannelModal.jsx`:
- Around line 3116-3154: The first clickable Text in EditChannelModal.jsx that
fills the full passthrough template currently uses t('填入模板'); update that label
to the new i18n key t('填入透传完整模版') so the full passthrough template uses the
correct translation; the change should be made on the Text component that calls
handleInputChange('header_override', JSON.stringify({ '*': true,
're:^X-Trace-.*$': true, ... }, null, 2)) to keep the other passthrough Text
(which uses t('填入透传模版')) unchanged.

In `@web/src/i18n/locales/zh.json`:
- Around line 738-739: Update the Chinese display values for the keys "填入透传模版"
and "填入透传完整模版" in zh.json to use the consistent character "模板" instead of "模版"
(keys can remain unchanged); change the value strings to "填入透传模板" and "填入透传完整模板"
respectively so they match nearby UI text.
🧹 Nitpick comments (2)
web/src/i18n/locales/ru.json (1)

753-754: Align “passthrough” terminology with existing Russian translations.

These two strings use English “passthrough,” while other “透传” entries are translated into Russian (e.g., “прозрачная/прямая передача”). Consider standardizing terminology for consistency across the UI.

💡 Possible wording (example)
-    "填入透传模版": "Заполнить шаблон passthrough",
-    "填入透传完整模版": "Заполнить полный шаблон passthrough",
+    "填入透传模版": "Заполнить шаблон сквозной передачи",
+    "填入透传完整模版": "Заполнить полный шаблон сквозной передачи",
relay/channel/api_request.go (1)

76-98: Unbounded regex cache may cause memory growth.

The sync.Map cache stores compiled regexes indefinitely with no eviction. In long-running deployments where channel configurations change frequently or many unique patterns are used, this could lead to gradual memory growth.

For now this is likely acceptable if patterns are admin-controlled and relatively static, but consider adding a bounded cache (e.g., LRU with a size limit) if memory becomes a concern in production.

Comment on lines +50 to +74
var passthroughSkipHeaderNamesLower = map[string]struct{}{
// RFC 7230 hop-by-hop headers.
"connection": {},
"keep-alive": {},
"proxy-authenticate": {},
"proxy-authorization": {},
"te": {},
"trailer": {},
"transfer-encoding": {},
"upgrade": {},

// Additional headers that should not be forwarded by name-matching passthrough rules.
"host": {},
"content-length": {},

// Do not passthrough credentials by wildcard/regex.
"authorization": {},
"x-api-key": {},
"x-goog-api-key": {},

// WebSocket handshake headers are generated by the client/dialer.
"sec-websocket-key": {},
"sec-websocket-version": {},
"sec-websocket-extensions": {},
}

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.

⚠️ Potential issue | 🟡 Minor

Consider adding cookie to the skip list.

The cookie header often contains session tokens and authentication data. With wildcard (*) or broad regex passthrough, cookies could be unintentionally forwarded to upstream services, potentially leaking user sessions.

If cookie passthrough is intentionally supported, this is fine—but please confirm this is the desired behavior.

🛡️ Proposed fix to block cookie passthrough
 	// Do not passthrough credentials by wildcard/regex.
 	"authorization":  {},
 	"x-api-key":      {},
 	"x-goog-api-key": {},
+	"cookie":         {},
 
 	// WebSocket handshake headers are generated by the client/dialer.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
var passthroughSkipHeaderNamesLower = map[string]struct{}{
// RFC 7230 hop-by-hop headers.
"connection": {},
"keep-alive": {},
"proxy-authenticate": {},
"proxy-authorization": {},
"te": {},
"trailer": {},
"transfer-encoding": {},
"upgrade": {},
// Additional headers that should not be forwarded by name-matching passthrough rules.
"host": {},
"content-length": {},
// Do not passthrough credentials by wildcard/regex.
"authorization": {},
"x-api-key": {},
"x-goog-api-key": {},
// WebSocket handshake headers are generated by the client/dialer.
"sec-websocket-key": {},
"sec-websocket-version": {},
"sec-websocket-extensions": {},
}
var passthroughSkipHeaderNamesLower = map[string]struct{}{
// RFC 7230 hop-by-hop headers.
"connection": {},
"keep-alive": {},
"proxy-authenticate": {},
"proxy-authorization": {},
"te": {},
"trailer": {},
"transfer-encoding": {},
"upgrade": {},
// Additional headers that should not be forwarded by name-matching passthrough rules.
"host": {},
"content-length": {},
// Do not passthrough credentials by wildcard/regex.
"authorization": {},
"x-api-key": {},
"x-goog-api-key": {},
"cookie": {},
// WebSocket handshake headers are generated by the client/dialer.
"sec-websocket-key": {},
"sec-websocket-version": {},
"sec-websocket-extensions": {},
}
🤖 Prompt for AI Agents
In `@relay/channel/api_request.go` around lines 50 - 74, The passthrough skip list
currently omits the cookie header; update the map variable
passthroughSkipHeaderNamesLower in relay/channel/api_request.go (the map literal
containing RFC 7230 hop-by-hop and other sensitive headers) to include "cookie":
{} so request cookies are not forwarded by wildcard/regex passthrough rules;
also add or adjust unit tests that assert sensitive headers are blocked if such
tests exist.

Comment on lines +3116 to 3154
<Text
className='!text-semi-color-primary cursor-pointer'
onClick={() =>
handleInputChange(
'header_override',
JSON.stringify(
{
'*': true,
're:^X-Trace-.*$': true,
'X-Foo': '{client_header:X-Foo}',
Authorization: 'Bearer {api_key}',
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0',
},
null,
2,
),
)
}
>
{t('填入模板')}
</Text>
<Text
className='!text-semi-color-primary cursor-pointer'
onClick={() =>
handleInputChange(
'header_override',
JSON.stringify(
{
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0',
Authorization: 'Bearer{api_key}',
'*': true,
},
null,
2,
),
)
}
>
{t('填入模板')}
{t('填入透传模版')}
</Text>

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.

⚠️ Potential issue | 🟡 Minor

Align the full passthrough template label with the new i18n key.
The full template still shows “填入模板”, while translations include “填入透传完整模版”. This makes the UI ambiguous and leaves the new key unused.

💡 Suggested label fix
-                              {t('填入模板')}
+                              {t('填入透传完整模版')}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<Text
className='!text-semi-color-primary cursor-pointer'
onClick={() =>
handleInputChange(
'header_override',
JSON.stringify(
{
'*': true,
're:^X-Trace-.*$': true,
'X-Foo': '{client_header:X-Foo}',
Authorization: 'Bearer {api_key}',
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0',
},
null,
2,
),
)
}
>
{t('填入模板')}
</Text>
<Text
className='!text-semi-color-primary cursor-pointer'
onClick={() =>
handleInputChange(
'header_override',
JSON.stringify(
{
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0',
Authorization: 'Bearer{api_key}',
'*': true,
},
null,
2,
),
)
}
>
{t('填入模板')}
{t('填入透传模版')}
</Text>
<Text
className='!text-semi-color-primary cursor-pointer'
onClick={() =>
handleInputChange(
'header_override',
JSON.stringify(
{
'*': true,
're:^X-Trace-.*$': true,
'X-Foo': '{client_header:X-Foo}',
Authorization: 'Bearer {api_key}',
'User-Agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36 Edg/139.0.0.0',
},
null,
2,
),
)
}
>
{t('填入透传完整模版')}
</Text>
<Text
className='!text-semi-color-primary cursor-pointer'
onClick={() =>
handleInputChange(
'header_override',
JSON.stringify(
{
'*': true,
},
null,
2,
),
)
}
>
{t('填入透传模版')}
</Text>
🤖 Prompt for AI Agents
In `@web/src/components/table/channels/modals/EditChannelModal.jsx` around lines
3116 - 3154, The first clickable Text in EditChannelModal.jsx that fills the
full passthrough template currently uses t('填入模板'); update that label to the new
i18n key t('填入透传完整模版') so the full passthrough template uses the correct
translation; the change should be made on the Text component that calls
handleInputChange('header_override', JSON.stringify({ '*': true,
're:^X-Trace-.*$': true, ... }, null, 2)) to keep the other passthrough Text
(which uses t('填入透传模版')) unchanged.

Comment on lines +738 to +739
"填入透传模版": "填入透传模版",
"填入透传完整模版": "填入透传完整模版",

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.

⚠️ Potential issue | 🟡 Minor

Use “模板” in the Chinese display text for consistency.

These two labels use “模版” while nearby UI uses “模板”. Consider updating the values to “模板” (keys can remain unchanged if already referenced elsewhere).

✏️ Suggested change
-    "填入透传模版": "填入透传模版",
-    "填入透传完整模版": "填入透传完整模版",
+    "填入透传模版": "填入透传模板",
+    "填入透传完整模版": "填入透传完整模板",
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"填入透传模版": "填入透传模版",
"填入透传完整模版": "填入透传完整模版",
"填入透传模版": "填入透传模板",
"填入透传完整模版": "填入透传完整模板",
🤖 Prompt for AI Agents
In `@web/src/i18n/locales/zh.json` around lines 738 - 739, Update the Chinese
display values for the keys "填入透传模版" and "填入透传完整模版" in zh.json to use the
consistent character "模板" instead of "模版" (keys can remain unchanged); change
the value strings to "填入透传模板" and "填入透传完整模板" respectively so they match nearby
UI text.

@Calcium-Ion
Calcium-Ion merged commit a21ee5f into QuantumNous:main Feb 4, 2026
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
…x-override

feat: 支持基于Go Regex规则和全量的请求体透传
@coderabbitai coderabbitai Bot mentioned this pull request May 20, 2026
4 tasks done
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
…x-override

feat: 支持基于Go Regex规则和全量的请求体透传
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
…x-override

feat: 支持基于Go Regex规则和全量的请求体透传
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