Skip to content

feat: header passthrough - #2735

Merged
Calcium-Ion merged 3 commits into
QuantumNous:mainfrom
seefs001:feature/header-throughpass
Jan 26, 2026
Merged

feat: header passthrough#2735
Calcium-Ion merged 3 commits into
QuantumNous:mainfrom
seefs001:feature/header-throughpass

Conversation

@seefs001

@seefs001 seefs001 commented Jan 24, 2026

Copy link
Copy Markdown
Collaborator

请求头透传
使用方式 :
{
anthropic-version: {client_header:anthropic-version}
}

先在后端进行更改,后续会采用 #2365 的部分前端变更作为UI编辑页

Summary by CodeRabbit

  • New Features

    • Header override values now support dynamic placeholders (API key and incoming request header references) and are resolved at request time for more flexible header customization.
  • Bug Fixes / Reliability

    • Placeholder processing now validates syntax, skips empty replacements to avoid injecting empty headers, and surfaces clearer errors for invalid templates.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 24, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

Adds runtime placeholder resolution for header overrides ({api_key}, {client_header:}) via a new helper. processHeaderOverride now accepts a *gin.Context; DoApiRequest, DoFormRequest, and DoWssRequest pass the context so client request headers can be used during override resolution.

Changes

Cohort / File(s) Summary
Header override processing
relay/channel/api_request.go
Added clientHeaderPlaceholderPrefix constant and applyHeaderOverridePlaceholders(template string, c *gin.Context, apiKey string) (string, bool, error). Modified processHeaderOverride(info *common.RelayInfo, c *gin.Context) to use the helper and treat empty results as omitted. Updated DoApiRequest, DoFormRequest, and DoWssRequest to pass gin.Context into processHeaderOverride. Errors from placeholder processing are wrapped as ChannelHeaderOverrideInvalid.

Sequence Diagram

sequenceDiagram
    participant Client
    participant Handler as API Handler (DoApiRequest / DoFormRequest / DoWssRequest)
    participant Processor as processHeaderOverride
    participant Resolver as applyHeaderOverridePlaceholders
    participant OutReq as Outgoing Request

    Client->>Handler: Incoming request + gin.Context
    Handler->>Processor: processHeaderOverride(info, gin.Context)
    Processor->>Resolver: applyHeaderOverridePlaceholders(template, gin.Context, apiKey)
    
    rect rgba(100, 150, 200, 0.5)
        Note over Resolver: Parse {api_key} and {client_header:<name>}<br/>Validate syntax & context, then substitute runtime values
    end

    Resolver-->>Processor: resolvedValue + includeFlag
    alt includeFlag == true
        Processor->>OutReq: add header with resolvedValue
    else
        Processor->>OutReq: omit header
    end

    OutReq-->>Client: response
Loading

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly Related PRs

Poem

🐰 I hopped into code with a curious cheer,
Found {api_key} and client headers near,
I stitched them at runtime with gin in my paw,
Now outgoing requests carry what they saw,
Hop, hop—placeholders resolved, hooray! 🎉

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% 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 'feat: header passthrough' is directly related to the changeset, which implements header passthrough functionality with placeholder processing for client headers and API keys.

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

✨ Finishing touches
  • 📝 Generate docstrings

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: 1

🤖 Fix all issues with AI agents
In `@relay/channel/api_request.go`:
- Around line 41-73: The applyHeaderOverridePlaceholders function currently
performs {api_key} interpolation after handling a {client_header:...}
placeholder, allowing a client-supplied header to include "{api_key}" and leak
the key; modify applyHeaderOverridePlaceholders so that when a client header
placeholder is detected (strings.HasPrefix(trimmed,
clientHeaderPlaceholderPrefix)) and a non-empty clientHeaderValue is obtained,
the function returns immediately with that value (return template, true, nil)
instead of continuing to the {api_key} replacement logic; update the branch that
sets template = clientHeaderValue in applyHeaderOverridePlaceholders to return
early and keep existing error/empty-value behavior unchanged.

Comment thread relay/channel/api_request.go
@Calcium-Ion
Calcium-Ion merged commit ac8f17c into QuantumNous:main Jan 26, 2026
1 check passed
@coderabbitai coderabbitai Bot mentioned this pull request Feb 21, 2026
Closed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
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