fix(api): 渠道请求头 - 补充自定义 Header 配置并应用到上游请求

Refs #39
This commit is contained in:
ddCat committed 2026-07-29 20:57:24 +08:00
1 parent 412399a473
commit 6de81049bf
21 files changed
+535 -75

No files matched your search

+23 -8
View File
@@ -75,14 +75,15 @@ type AdminReferenceData struct {
}
type ChannelRequest struct {
Name string `json:"name"`
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey"`
SecretKey string `json:"secretKey"`
ConcurrencyLimit *int `json:"concurrencyLimit"`
UseGlobalConcurrency *bool `json:"useGlobalConcurrency"`
Models []string `json:"models"`
Enabled *bool `json:"enabled"`
Name string `json:"name"`
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey"`
SecretKey string `json:"secretKey"`
ConcurrencyLimit *int `json:"concurrencyLimit"`
UseGlobalConcurrency *bool `json:"useGlobalConcurrency"`
Models []string `json:"models"`
Headers []OutboundHeader `json:"headers"`
Enabled *bool `json:"enabled"`
}
type PublicModelChannel struct {
@@ -97,6 +98,7 @@ type PublicModelChannel struct {
ConcurrencyLimit int `json:"concurrencyLimit"`
Models []string `json:"models"`
ModelCosts []PublicChannelModelPrice `json:"modelCosts"`
Headers []OutboundHeader `json:"headers,omitempty"`
HasAPIKey bool `json:"hasApiKey"`
HasSecretKey bool `json:"hasSecretKey"`
CreatedAt time.Time `json:"createdAt"`
@@ -595,6 +597,10 @@ func channelFromRequest(req ChannelRequest, channel model.ModelChannel) (model.M
}
models := uniqueNonEmpty(req.Models)
modelsJSON, _ := json.Marshal(models)
headersJSON, err := EncodeOutboundHeadersJSON(req.Headers)
if err != nil {
return channel, err
}
channel.Name = name
channel.BaseURL = strings.TrimRight(baseURL, "/")
if req.APIKey != "" {
@@ -616,6 +622,7 @@ func channelFromRequest(req ChannelRequest, channel model.ModelChannel) (model.M
return channel, BadAuthRequest("请填写渠道最大并发数")
}
channel.ModelsJSON = string(modelsJSON)
channel.HeadersJSON = headersJSON
if req.Enabled != nil {
channel.Enabled = *req.Enabled
}
@@ -632,6 +639,9 @@ func mergeChannelRequest(req ChannelRequest, channel model.ModelChannel) Channel
if req.Models == nil {
req.Models = channelModelNames(channel)
}
if req.Headers == nil {
req.Headers, _ = ParseOutboundHeadersJSON(channel.HeadersJSON)
}
return req
}
@@ -661,11 +671,15 @@ func publicChannel(channel model.ModelChannel, admin bool, channelModels []model
}
apiKey := ""
baseURL := channel.BaseURL
var headers []OutboundHeader
if channel.Scope == model.ChannelScopeSystem {
if !admin {
apiKey = "system"
baseURL = "/api/ai/system/" + channel.ID
}
if admin {
headers, _ = ParseOutboundHeadersJSON(channel.HeadersJSON)
}
} else if admin {
apiKey = channel.APIKey
}
@@ -681,6 +695,7 @@ func publicChannel(channel model.ModelChannel, admin bool, channelModels []model
ConcurrencyLimit: channel.ConcurrencyLimit,
Models: models,
ModelCosts: modelCosts,
Headers: headers,
HasAPIKey: strings.TrimSpace(channel.APIKey) != "",
HasSecretKey: strings.TrimSpace(channel.SecretKey) != "",
CreatedAt: channel.CreatedAt,
+38 -5
View File
@@ -38,16 +38,49 @@ func TestChannelFromRequestStoresConnectionWithoutDefaultProtocol(t *testing.T)
func TestMergeChannelRequestSupportsEnabledOnlyPatch(t *testing.T) {
enabled := false
req := mergeChannelRequest(ChannelRequest{Enabled: &enabled}, model.ModelChannel{
Name: "Video",
BaseURL: "https://example.com/v1",
APIFormat: "openai",
ModelsJSON: `["custom-video"]`,
Name: "Video",
BaseURL: "https://example.com/v1",
APIFormat: "openai",
ModelsJSON: `["custom-video"]`,
HeadersJSON: `[{"name":"User-Agent","value":"Stored Agent"}]`,
})
if req.Name != "Video" || req.BaseURL != "https://example.com/v1" || len(req.Models) != 1 {
if req.Name != "Video" || req.BaseURL != "https://example.com/v1" || len(req.Models) != 1 || len(req.Headers) != 1 {
t.Fatalf("mergeChannelRequest() = %#v", req)
}
}
func TestChannelFromRequestStoresAndClearsHeaders(t *testing.T) {
request := ChannelRequest{Name: "Headers", BaseURL: "https://example.com/v1", Headers: []OutboundHeader{{Name: "User-Agent", Value: "Custom Agent"}}}
channel, err := channelFromRequest(request, model.ModelChannel{})
if err != nil {
t.Fatal(err)
}
if channel.HeadersJSON != `[{"name":"User-Agent","value":"Custom Agent"}]` {
t.Fatalf("HeadersJSON = %q", channel.HeadersJSON)
}
request.Headers = []OutboundHeader{}
channel, err = channelFromRequest(request, channel)
if err != nil {
t.Fatal(err)
}
if channel.HeadersJSON != `[]` {
t.Fatalf("cleared HeadersJSON = %q", channel.HeadersJSON)
}
}
func TestPublicChannelOnlyReturnsSystemHeadersToAdmin(t *testing.T) {
channel := model.ModelChannel{ID: "system-1", Scope: model.ChannelScopeSystem, BaseURL: "https://example.com/v1", HeadersJSON: `[{"name":"X-Gateway-Tenant","value":"tenant-a"}]`}
adminView := publicChannel(channel, true, nil)
if len(adminView.Headers) != 1 || adminView.Headers[0].Name != "X-Gateway-Tenant" {
t.Fatalf("admin headers = %#v", adminView.Headers)
}
userView := publicChannel(channel, false, nil)
if len(userView.Headers) != 0 {
t.Fatalf("user headers = %#v", userView.Headers)
}
}
func TestChannelFromRequestRejectsInvalidConcurrencyLimit(t *testing.T) {
for _, limit := range []int{0, 1000} {
_, err := channelFromRequest(ChannelRequest{Name: "Bad", BaseURL: "https://example.com/v1", ConcurrencyLimit: &limit}, model.ModelChannel{})
@@ -13,9 +13,10 @@ import (
)
type ChannelModelsRequest struct {
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey"`
APIFormat string `json:"apiFormat"`
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey"`
APIFormat string `json:"apiFormat"`
Headers []OutboundHeader `json:"headers"`
}
type channelModelsPayload struct {
@@ -50,6 +51,10 @@ func (s *Service) FetchChannelModels(ctx context.Context, actor *model.User, inp
if apiFormat != "openai" && apiFormat != "gemini" {
return nil, BadAuthRequest("接口协议不支持拉取模型")
}
headers, err := NormalizeOutboundHeaders(input.Headers)
if err != nil {
return nil, err
}
target := apiURL(baseURL, "/models")
if apiFormat == "gemini" {
@@ -71,6 +76,7 @@ func (s *Service) FetchChannelModels(ctx context.Context, actor *model.User, inp
} else {
request.Header.Set("Authorization", "Bearer "+apiKey)
}
ApplyOutboundHeaders(request, headers)
// 只代理固定的模型目录 GET;用户密钥仅用于本次请求,不写入数据库或日志。
data, _, err := doBinary(request)
+11 -2
View File
@@ -75,8 +75,12 @@ func (s *Service) FetchAdminChannelModels(ctx context.Context, actor *model.User
if err != nil {
return nil, err
}
// 使用服务端保存的渠道密钥请求上游,避免密钥为了拉目录再次经过浏览器。
models, err := s.FetchChannelModels(ctx, actor, ChannelModelsRequest{BaseURL: channel.BaseURL, APIKey: channel.APIKey, APIFormat: channel.APIFormat})
headers, err := ParseOutboundHeadersJSON(channel.HeadersJSON)
if err != nil {
return nil, err
}
// 使用服务端保存的渠道密钥和请求头访问上游,避免敏感配置再次经过浏览器。
models, err := s.FetchChannelModels(ctx, actor, ChannelModelsRequest{BaseURL: channel.BaseURL, APIKey: channel.APIKey, APIFormat: channel.APIFormat, Headers: headers})
if err != nil {
return nil, err
}
@@ -174,6 +178,10 @@ func (s *Service) TestAdminChannelModel(ctx context.Context, actor *model.User,
if strings.TrimSpace(channel.BaseURL) == "" || strings.TrimSpace(channel.APIKey) == "" {
return nil, BadAuthRequest("请先在渠道中配置 Base URL 和 API Key")
}
headers, err := ParseOutboundHeadersJSON(channel.HeadersJSON)
if err != nil {
return nil, err
}
prompt := map[string]string{
"text": "Reply with OK.",
@@ -197,6 +205,7 @@ func (s *Service) TestAdminChannelModel(ctx context.Context, actor *model.User,
BaseURL: channel.BaseURL,
APIKey: channel.APIKey,
SecretKey: channel.SecretKey,
Headers: headers,
Model: modelKey,
Size: map[string]string{"image": "1024x1024", "video": "16:9"}[capability],
Quality: "auto",
+133
View File
@@ -2,7 +2,10 @@ package service
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/netip"
@@ -14,9 +17,17 @@ import (
const (
maxOutboundRedirects = 5
maxOutboundHeaderCount = 32
maxOutboundHeaderBytes = 16 << 10
CustomRelayHeadersHeader = "X-Canvas-Upstream-Headers"
DefaultOutboundUserAgent = "InfiniteCanvas/1.0 (+https://github.com/ddcat-ai/open-ai-canvas)"
)
type OutboundHeader struct {
Name string `json:"name"`
Value string `json:"value"`
}
var (
outboundTransport = newOutboundTransport(resolveOutboundHost)
customRelayTransport = newOutboundTransport(resolveCustomRelayHost)
@@ -108,6 +119,128 @@ func ApplyDefaultOutboundHeaders(req *http.Request) {
}
}
// NormalizeOutboundHeaders 是自定义 Header 的安全边界:允许业务/WAF 校验头和 User-Agent,
// 但禁止覆盖鉴权、请求体协议、代理内部头及 hop-by-hop 头。
func NormalizeOutboundHeaders(headers []OutboundHeader) ([]OutboundHeader, error) {
if len(headers) > maxOutboundHeaderCount {
return nil, BadAuthRequest(fmt.Sprintf("自定义请求头最多支持 %d 项", maxOutboundHeaderCount))
}
seen := make(map[string]struct{}, len(headers))
normalized := make([]OutboundHeader, 0, len(headers))
totalBytes := 0
for _, item := range headers {
name := strings.TrimSpace(item.Name)
value := strings.TrimSpace(item.Value)
if name == "" && value == "" {
continue
}
if name == "" || !validOutboundHeaderName(name) {
return nil, BadAuthRequest("自定义请求头名称无效,请使用标准 HTTP Header 名称")
}
name = http.CanonicalHeaderKey(name)
lowerName := strings.ToLower(name)
if blockedOutboundHeader(lowerName) {
return nil, BadAuthRequest("请求头 " + name + " 由系统管理,不允许自定义")
}
if _, exists := seen[lowerName]; exists {
return nil, BadAuthRequest("自定义请求头不能重复:" + name)
}
if !validOutboundHeaderValue(value) {
return nil, BadAuthRequest("请求头 " + name + " 的值包含非法控制字符")
}
if len(name) > 128 || len(value) > 4096 {
return nil, BadAuthRequest("单个自定义请求头名称或值过长")
}
totalBytes += len(name) + len(value)
if totalBytes > maxOutboundHeaderBytes {
return nil, BadAuthRequest("自定义请求头总大小不能超过 16KB")
}
seen[lowerName] = struct{}{}
normalized = append(normalized, OutboundHeader{Name: name, Value: value})
}
return normalized, nil
}
func ApplyOutboundHeaders(req *http.Request, headers []OutboundHeader) {
if req == nil {
return
}
for _, item := range headers {
req.Header.Set(item.Name, item.Value)
}
}
func EncodeOutboundHeadersJSON(headers []OutboundHeader) (string, error) {
normalized, err := NormalizeOutboundHeaders(headers)
if err != nil {
return "", err
}
data, err := json.Marshal(normalized)
return string(data), err
}
func ParseOutboundHeadersJSON(raw string) ([]OutboundHeader, error) {
if strings.TrimSpace(raw) == "" {
return []OutboundHeader{}, nil
}
var headers []OutboundHeader
if err := json.Unmarshal([]byte(raw), &headers); err != nil {
return nil, errors.New("渠道自定义请求头配置损坏")
}
return NormalizeOutboundHeaders(headers)
}
func DecodeRelayOutboundHeaders(encoded string) ([]OutboundHeader, error) {
if strings.TrimSpace(encoded) == "" {
return []OutboundHeader{}, nil
}
if len(encoded) > (maxOutboundHeaderBytes*2)+1024 {
return nil, BadAuthRequest("自定义请求头配置过大")
}
data, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
return nil, BadAuthRequest("自定义请求头编码无效")
}
var headers []OutboundHeader
if err := json.Unmarshal(data, &headers); err != nil {
return nil, BadAuthRequest("自定义请求头格式无效")
}
return NormalizeOutboundHeaders(headers)
}
func validOutboundHeaderName(name string) bool {
if name == "" {
return false
}
for index := 0; index < len(name); index++ {
char := name[index]
if (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') || (char >= '0' && char <= '9') || strings.ContainsRune("!#$%&'*+-.^_`|~", rune(char)) {
continue
}
return false
}
return true
}
func validOutboundHeaderValue(value string) bool {
for index := 0; index < len(value); index++ {
char := value[index]
if char == '\t' || char >= 32 && char != 127 {
continue
}
return false
}
return true
}
func blockedOutboundHeader(name string) bool {
switch name {
case "authorization", "proxy-authorization", "cookie", "set-cookie", "host", "content-length", "content-type", "accept", "connection", "proxy-connection", "keep-alive", "transfer-encoding", "te", "trailer", "upgrade", "forwarded", "x-goog-api-key":
return true
}
return strings.HasPrefix(name, "x-canvas-") || strings.HasPrefix(name, "x-forwarded-")
}
func newOutboundTransport(resolveHost func(context.Context, string) ([]net.IP, error)) *http.Transport {
dialer := &net.Dialer{Timeout: 15 * time.Second, KeepAlive: 30 * time.Second}
return &http.Transport{
+41
View File
@@ -1,6 +1,7 @@
package service
import (
"encoding/base64"
"net"
"net/http"
"net/http/httptest"
@@ -8,6 +9,46 @@ import (
"time"
)
func TestNormalizeOutboundHeadersAllowsCustomUserAgent(t *testing.T) {
headers, err := NormalizeOutboundHeaders([]OutboundHeader{{Name: "user-agent", Value: "Custom Gateway/2.0"}, {Name: "X-Gateway-Tenant", Value: "tenant-a"}})
if err != nil {
t.Fatal(err)
}
request, _ := http.NewRequest(http.MethodGet, "https://example.com", nil)
ApplyOutboundHeaders(request, headers)
ApplyDefaultOutboundHeaders(request)
if got := request.Header.Get("User-Agent"); got != "Custom Gateway/2.0" {
t.Fatalf("User-Agent = %q", got)
}
if got := request.Header.Get("X-Gateway-Tenant"); got != "tenant-a" {
t.Fatalf("X-Gateway-Tenant = %q", got)
}
}
func TestNormalizeOutboundHeadersRejectsUnsafeAndDuplicateValues(t *testing.T) {
tests := [][]OutboundHeader{
{{Name: "Authorization", Value: "Bearer attacker"}},
{{Name: "X-Test", Value: "safe\r\ninjected: true"}},
{{Name: "X-Test", Value: "one"}, {Name: "x-test", Value: "two"}},
}
for _, headers := range tests {
if _, err := NormalizeOutboundHeaders(headers); err == nil {
t.Fatalf("NormalizeOutboundHeaders(%#v) should fail", headers)
}
}
}
func TestDecodeRelayOutboundHeaders(t *testing.T) {
raw := base64.StdEncoding.EncodeToString([]byte(`[{"name":"User-Agent","value":"Relay Agent"}]`))
headers, err := DecodeRelayOutboundHeaders(raw)
if err != nil {
t.Fatal(err)
}
if len(headers) != 1 || headers[0].Name != "User-Agent" || headers[0].Value != "Relay Agent" {
t.Fatalf("DecodeRelayOutboundHeaders() = %#v", headers)
}
}
func TestApplyDefaultOutboundHeaders(t *testing.T) {
request, err := http.NewRequest(http.MethodGet, "https://example.com", nil)
if err != nil {
+38 -20
View File
@@ -33,26 +33,27 @@ type canvasGenerationInput struct {
}
type providerConfig struct {
ChannelID string `json:"channelId"`
APIFormat string `json:"apiFormat"`
InterfaceType string `json:"interfaceType"`
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey"`
SecretKey string `json:"secretKey"`
Model string `json:"model"`
Size string `json:"size"`
Quality string `json:"quality"`
TransparentBackground string `json:"transparentBackground"`
Count string `json:"count"`
VideoSeconds string `json:"videoSeconds"`
VQuality string `json:"vquality"`
VideoGenerateAudio string `json:"videoGenerateAudio"`
VideoWatermark string `json:"videoWatermark"`
AudioVoice string `json:"audioVoice"`
AudioFormat string `json:"audioFormat"`
AudioSpeed string `json:"audioSpeed"`
AudioInstructions string `json:"audioInstructions"`
SystemPrompt string `json:"systemPrompt"`
ChannelID string `json:"channelId"`
APIFormat string `json:"apiFormat"`
InterfaceType string `json:"interfaceType"`
BaseURL string `json:"baseUrl"`
APIKey string `json:"apiKey"`
SecretKey string `json:"secretKey"`
Headers []OutboundHeader `json:"headers"`
Model string `json:"model"`
Size string `json:"size"`
Quality string `json:"quality"`
TransparentBackground string `json:"transparentBackground"`
Count string `json:"count"`
VideoSeconds string `json:"videoSeconds"`
VQuality string `json:"vquality"`
VideoGenerateAudio string `json:"videoGenerateAudio"`
VideoWatermark string `json:"videoWatermark"`
AudioVoice string `json:"audioVoice"`
AudioFormat string `json:"audioFormat"`
AudioSpeed string `json:"audioSpeed"`
AudioInstructions string `json:"audioInstructions"`
SystemPrompt string `json:"systemPrompt"`
}
const providerHTTPTimeout = 5 * time.Minute
@@ -278,6 +279,11 @@ func normalizedMediaMimeType(declared string, data []byte) string {
}
func (s *Service) resolveProviderConfig(config providerConfig) (providerConfig, error) {
headers, err := NormalizeOutboundHeaders(config.Headers)
if err != nil {
return providerConfig{}, err
}
config.Headers = headers
channelID := strings.TrimSpace(config.ChannelID)
if channelID == "" {
channelID = systemChannelIDFromBaseURL(config.BaseURL)
@@ -319,6 +325,10 @@ func (s *Service) resolveProviderConfig(config providerConfig) (providerConfig,
config.BaseURL = channel.BaseURL
config.APIKey = channel.APIKey
config.SecretKey = channel.SecretKey
config.Headers, err = ParseOutboundHeadersJSON(channel.HeadersJSON)
if err != nil {
return providerConfig{}, err
}
config.Model = modelName
return config, nil
}
@@ -861,6 +871,7 @@ func postGeminiJSON(ctx context.Context, config providerConfig, path string, bod
}
req.Header.Set("x-goog-api-key", config.APIKey)
req.Header.Set("Content-Type", "application/json")
ApplyOutboundHeaders(req, config.Headers)
return doJSON(req, target)
}
@@ -870,6 +881,7 @@ func getGeminiJSON(ctx context.Context, config providerConfig, path string, targ
return err
}
req.Header.Set("x-goog-api-key", config.APIKey)
ApplyOutboundHeaders(req, config.Headers)
return doJSON(req, target)
}
@@ -879,6 +891,7 @@ func getGeminiBinary(ctx context.Context, config providerConfig, rawURL string)
return nil, "", err
}
req.Header.Set("x-goog-api-key", config.APIKey)
ApplyOutboundHeaders(req, config.Headers)
return doBinary(req)
}
@@ -1466,6 +1479,7 @@ func postJSON(ctx context.Context, config providerConfig, path string, body inte
}
req.Header.Set("Authorization", "Bearer "+config.APIKey)
req.Header.Set("Content-Type", "application/json")
ApplyOutboundHeaders(req, config.Headers)
return doJSON(req, target)
}
@@ -1476,6 +1490,7 @@ func postForm(ctx context.Context, config providerConfig, path string, contentTy
}
req.Header.Set("Authorization", "Bearer "+config.APIKey)
req.Header.Set("Content-Type", contentType)
ApplyOutboundHeaders(req, config.Headers)
return doJSON(req, target)
}
@@ -1485,6 +1500,7 @@ func getJSON(ctx context.Context, config providerConfig, path string, target int
return err
}
req.Header.Set("Authorization", "Bearer "+config.APIKey)
ApplyOutboundHeaders(req, config.Headers)
return doJSON(req, target)
}
@@ -1496,6 +1512,7 @@ func postBinary(ctx context.Context, config providerConfig, path string, body in
}
req.Header.Set("Authorization", "Bearer "+config.APIKey)
req.Header.Set("Content-Type", "application/json")
ApplyOutboundHeaders(req, config.Headers)
return doBinary(req)
}
@@ -1505,6 +1522,7 @@ func getBinary(ctx context.Context, config providerConfig, path string) ([]byte,
return nil, "", err
}
req.Header.Set("Authorization", "Bearer "+config.APIKey)
ApplyOutboundHeaders(req, config.Headers)
return doBinary(req)
}
@@ -197,6 +197,7 @@ func postJiMengJSON(ctx context.Context, config providerConfig, action string, b
return err
}
req.Header.Set("Content-Type", "application/json")
ApplyOutboundHeaders(req, config.Headers)
ApplyDefaultOutboundHeaders(req)
credentials := base.Credentials{
AccessKeyID: strings.TrimSpace(config.APIKey),