fix(headers): mask private names and stabilize empty audit configuration
This commit is contained in:
parent
5feea8d3c3
commit
195540b619
|
|
@ -119,6 +119,7 @@ ai:
|
|||
# 配置自定义头时,仅允许同源重定向,跨主机、端口或协议的重定向会被拒绝。
|
||||
# HITL 审计仅在未显式配置 provider/base_url/api_key 时继承主渠道 headers;
|
||||
# 独立审计身份如需请求头,请在 hitl.audit_model.headers 中单独配置。
|
||||
# 空 headers 与未配置等价;所有自定义头值在设置接口中均按敏感值脱敏。
|
||||
# Eino 路径模型推理:DeepSeek/OpenAI 为 thinking / reasoning_effort;Claude 4.6+ 为 adaptive + output_config.effort(仅显式配置 effort 时下发);3.7 为 enabled+budget_tokens:10000(文档示例),effort 不映射,自定义预算用 extra_request_fields
|
||||
reasoning:
|
||||
mode: on # auto | on | off;off:OpenAI/Claude 不附加推理字段,DeepSeek 发送 thinking.type=disabled(其默认开启思考)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import "strings"
|
|||
// boundary, even when its provider or URL happens to equal the main model's.
|
||||
// A model-name-only override can safely continue using the main identity.
|
||||
func auditModelHeaders(main, audit OpenAIConfig) map[string]string {
|
||||
if audit.Headers != nil {
|
||||
if len(audit.Headers) > 0 {
|
||||
return ExpandHeaderMap(audit.Headers)
|
||||
}
|
||||
if strings.TrimSpace(audit.Provider) != "" || strings.TrimSpace(audit.BaseURL) != "" || strings.TrimSpace(audit.APIKey) != "" {
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ func TestAuditModelHeaderIdentityBoundary(t *testing.T) {
|
|||
{"endpoint", OpenAIConfig{BaseURL: "https://audit.invalid/v1"}, ""},
|
||||
{"key", OpenAIConfig{APIKey: "audit-key"}, ""},
|
||||
{"explicit", OpenAIConfig{BaseURL: "https://audit.invalid", Headers: map[string]string{"Authorization": "${CSAI_AUDIT_TEST_HEADER}"}}, "audit-secret"},
|
||||
{"explicit empty", OpenAIConfig{Headers: map[string]string{}}, ""},
|
||||
{"empty follows inheritance", OpenAIConfig{Headers: map[string]string{}}, "Bearer main"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := (HitlConfig{AuditModel: tc.audit}).AuditModelEffective(main)
|
||||
|
|
|
|||
|
|
@ -167,3 +167,56 @@ func TestAuditHeadersPersistAndRestoreMaskedConfig(t *testing.T) {
|
|||
t.Fatal("audit header lost on save")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrivateHeaderNamesAreMaskedAndRestored(t *testing.T) {
|
||||
cfg := &config.Config{AI: config.AIConfig{Channels: map[string]config.AIChannelConfig{"a": {Headers: map[string]string{"X-Gateway-Token": "private-token", "X-Client-Name": "client-name"}}, "b": {Headers: map[string]string{"X-Gateway-Token": "other-token"}}}}}
|
||||
response, err := maskedConfigResponse(configSecretSnapshot(cfg))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw, _ := json.Marshal(response)
|
||||
if strings.Contains(string(raw), "private-token") || strings.Contains(string(raw), "other-token") || strings.Contains(string(raw), "client-name") {
|
||||
t.Fatal("custom header disclosed")
|
||||
}
|
||||
var req UpdateConfigRequest
|
||||
if err = json.Unmarshal(raw, &req); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// HTTP field casing is not an identity change, but channels must remain isolated.
|
||||
ch := req.AI.Channels["a"]
|
||||
delete(ch.Headers, "X-Gateway-Token")
|
||||
ch.Headers["x-gateway-token"] = maskedSecret
|
||||
req.AI.Channels["a"] = ch
|
||||
if err = restoreConfigRequestSecrets(&req, cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if req.AI.Channels["a"].Headers["x-gateway-token"] != "private-token" || req.AI.Channels["b"].Headers["X-Gateway-Token"] != "other-token" {
|
||||
t.Fatal("wrong channel credential")
|
||||
}
|
||||
if cfg.AI.Channels["a"].Headers["X-Gateway-Token"] != "private-token" {
|
||||
t.Fatal("live config mutated")
|
||||
}
|
||||
for _, headers := range []map[string]interface{}{{"Missing": maskedSecret}, {"X-Gateway-Token": maskedSecret, "x-gateway-token": maskedSecret}} {
|
||||
if transformHeaderSecrets(headers, map[string]interface{}{"X-Gateway-Token": "private-token"}, true) == nil {
|
||||
t.Fatal("ambiguous or missing header accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestEmptyAuditHeadersRoundTripPreservesPolicy(t *testing.T) {
|
||||
main := config.OpenAIConfig{Headers: map[string]string{"Authorization": "Bearer main"}}
|
||||
for _, identity := range []string{"", "https://audit.invalid"} {
|
||||
hitl := config.HitlConfig{AuditModel: config.OpenAIConfig{BaseURL: identity, Headers: map[string]string{}}}
|
||||
before := hitl.AuditModelEffective(main).Headers["Authorization"]
|
||||
var doc yaml.Node
|
||||
yaml.Unmarshal([]byte("hitl: {}\n"), &doc)
|
||||
updateHitlConfig(&doc, hitl)
|
||||
raw, _ := yaml.Marshal(&doc)
|
||||
var saved config.Config
|
||||
if err := yaml.Unmarshal(raw, &saved); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if after := saved.Hitl.AuditModelEffective(main).Headers["Authorization"]; after != before {
|
||||
t.Fatalf("round trip changed header policy: %q -> %q", before, after)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Any private header name may carry a credential. A fixed list such as
|
||||
// Authorization/X-Api-Key cannot safely classify a user-defined header map.
|
||||
func transformHeaderSecrets(headers, stored map[string]interface{}, restoring bool) error {
|
||||
seen := map[string]bool{}
|
||||
for name, value := range headers {
|
||||
key := strings.ToLower(strings.TrimSpace(name))
|
||||
if restoring && seen[key] {
|
||||
return fmt.Errorf("重复的请求头名称")
|
||||
}
|
||||
seen[key] = true
|
||||
text, ok := value.(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if !restoring {
|
||||
if text != "" {
|
||||
headers[name] = maskedSecret
|
||||
}
|
||||
continue
|
||||
}
|
||||
if text != maskedSecret {
|
||||
continue
|
||||
}
|
||||
found := ""
|
||||
count := 0
|
||||
for oldName, oldValue := range stored {
|
||||
if strings.EqualFold(strings.TrimSpace(oldName), strings.TrimSpace(name)) {
|
||||
if oldText, ok := oldValue.(string); ok {
|
||||
found = oldText
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
if count != 1 || found == "" || found == maskedSecret {
|
||||
return fmt.Errorf("没有唯一的已保存请求头凭据,请填写新的值")
|
||||
}
|
||||
headers[name] = found
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -44,6 +44,15 @@ func transformSecretTree(tree, current interface{}, restoring bool) error {
|
|||
case map[string]interface{}:
|
||||
previous, _ := current.(map[string]interface{})
|
||||
for key, value := range node {
|
||||
if key == "headers" {
|
||||
if headers, ok := value.(map[string]interface{}); ok {
|
||||
stored, _ := previous[key].(map[string]interface{})
|
||||
if err := transformHeaderSecrets(headers, stored, restoring); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
text, isString := value.(string)
|
||||
if secretConfigKey(key) && isString {
|
||||
if !restoring && text != "" {
|
||||
|
|
|
|||
|
|
@ -17,3 +17,7 @@ test('audit shares headers only when it inherits model identity',()=>{
|
|||
test('explicit audit headers belong to audit configuration',()=>{
|
||||
assert.deepEqual(collect({'hitl-audit-model-base-url':'https://audit.invalid'},{'X-Api-Key':'********'}).headers,{'X-Api-Key':'********'});
|
||||
});
|
||||
test('empty audit headers follow the same identity policy as omitted headers',()=>{
|
||||
assert.deepEqual(collect({},{}).headers,collect().headers);
|
||||
assert.deepEqual(collect({'hitl-audit-model-api-key':'audit-key'},{}).headers,{});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -3804,13 +3804,14 @@ function collectHitlAuditModelEffectiveConfig() {
|
|||
const overriddenBaseUrl = document.getElementById('hitl-audit-model-base-url')?.value.trim() || '';
|
||||
const mainChannelId = normalizeAIChannelId(selectedAIChannelId || currentConfig?.ai?.default_channel || 'default');
|
||||
const mainHeaders = currentConfig?.ai?.channels?.[mainChannelId]?.headers || {};
|
||||
const auditHeaders = currentConfig?.hitl?.audit_model?.headers || {};
|
||||
return {
|
||||
provider: document.getElementById('hitl-audit-model-provider')?.value || main.provider,
|
||||
base_url: overriddenBaseUrl || main.base_url,
|
||||
api_key: document.getElementById('hitl-audit-model-api-key')?.value.trim() || main.api_key,
|
||||
model: document.getElementById('hitl-audit-model-name')?.value.trim() || main.model,
|
||||
// 审计模型留空即复用主渠道,自定义请求头必须一起跟随,否则私有鉴权头的供应商测试必然失败。
|
||||
headers: currentConfig?.hitl?.audit_model?.headers ?? ((overriddenBaseUrl || auditProvider || auditKey) ? {} : mainHeaders)
|
||||
headers: Object.keys(auditHeaders).length ? auditHeaders : ((overriddenBaseUrl || auditProvider || auditKey) ? {} : mainHeaders)
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue