mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
fix(openai): 修复透传错误脱敏越界并收紧结构化错误字段
- replaceCaseInsensitive 改为在原始字节上做大小写不敏感匹配。原实现用 strings.ToLower 后的下标切原串,遇到折叠后字节长度变化的字符(Ⱥ、Kelvin 符号、非法 UTF-8)会越界 panic 或把上游主机名原样漏给客户端;同时消除 该函数的 errcheck 告警 - error.type/code/param 只接受字符串或数字并限长 256 字节,避免上游把对象 或数组以原始 JSON 形式塞进这三个字段 - 规则命中且未配置自定义文案时,上游消息同样经过凭据与主机名脱敏 - compact 心跳已提交时 response.failed 保留结构化 error.code
This commit is contained in:
1 parent
2b74378321
commit
4339d50615
3 files changed
+288
-22
No files matched your search
@@ -109,10 +109,26 @@ func writeOpenAICompactSSEFailure(c *gin.Context, statusCode int, errorBody []by
|
||||
// 不被识别,会退化为 "stream closed before response.completed" 盲重连)。
|
||||
// 同时标记流内错误,保证挂在 200 流上的失败仍进入 ops 错误看板。
|
||||
func writeOpenAICompactSSEFailureMessage(c *gin.Context, statusCode int, errType, message string) {
|
||||
writeOpenAICompactSSEFailureEvent(c, statusCode, errType, errType, message)
|
||||
}
|
||||
|
||||
// writeOpenAICompactSSEFailureWithCode 同 writeOpenAICompactSSEFailureMessage,
|
||||
// 但 response.error.code 使用调用方给出的结构化错误码(如
|
||||
// invalid_encrypted_content),供客户端区分确定性请求失败;code 为空时回落
|
||||
// upstream_error。ops 侧的错误类型保持 upstream_error 不变。message 由调用方
|
||||
// 负责脱敏。
|
||||
func writeOpenAICompactSSEFailureWithCode(c *gin.Context, statusCode int, code, message string) {
|
||||
if strings.TrimSpace(code) == "" {
|
||||
code = "upstream_error"
|
||||
}
|
||||
writeOpenAICompactSSEFailureEvent(c, statusCode, "upstream_error", code, message)
|
||||
}
|
||||
|
||||
func writeOpenAICompactSSEFailureEvent(c *gin.Context, statusCode int, opsErrType, code, message string) {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
MarkOpsStreamError(c, errType, message, statusCode)
|
||||
MarkOpsStreamError(c, opsErrType, message, statusCode)
|
||||
payload, err := json.Marshal(map[string]any{
|
||||
"type": "response.failed",
|
||||
"sequence_number": 0,
|
||||
@@ -125,7 +141,7 @@ func writeOpenAICompactSSEFailureMessage(c *gin.Context, statusCode int, errType
|
||||
"status": "failed",
|
||||
"output": []any{},
|
||||
"error": map[string]any{
|
||||
"code": errType,
|
||||
"code": code,
|
||||
"message": message,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -892,29 +893,58 @@ func sanitizeOpenAIPassthroughClientErrorText(account *Account, text string) str
|
||||
return replaceCaseInsensitive(sanitized, host, "[upstream]")
|
||||
}
|
||||
|
||||
// replaceCaseInsensitive 把 input 中所有大小写不敏感匹配 target 的片段替换为
|
||||
// replacement。
|
||||
//
|
||||
// 必须直接在原始字节上匹配:strings.ToLower 会改变字节长度("Ⱥ" 2→3 字节、
|
||||
// Kelvin 符号 "K" 3→1 字节、非法 UTF-8 单字节→3 字节 U+FFFD),用小写串的下标
|
||||
// 去切原串会越界 panic 或漏掉 target。正则的 (?i) 在原串上逐 rune 折叠匹配,
|
||||
// 不存在下标错位。
|
||||
//
|
||||
// target 无法编译为模式(如含非法 UTF-8)时返回空串:调用方会回退到通用文案,
|
||||
// 宁可丢弃整段文本,也不把未脱敏的内容写给客户端。
|
||||
func replaceCaseInsensitive(input, target, replacement string) string {
|
||||
if input == "" || target == "" {
|
||||
return input
|
||||
}
|
||||
lowerInput := strings.ToLower(input)
|
||||
lowerTarget := strings.ToLower(target)
|
||||
if !strings.Contains(lowerInput, lowerTarget) {
|
||||
return input
|
||||
pattern, err := regexp.Compile("(?i)" + regexp.QuoteMeta(target))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
var b strings.Builder
|
||||
b.Grow(len(input))
|
||||
start := 0
|
||||
for {
|
||||
idx := strings.Index(lowerInput[start:], lowerTarget)
|
||||
if idx < 0 {
|
||||
b.WriteString(input[start:])
|
||||
break
|
||||
}
|
||||
b.WriteString(input[start : start+idx])
|
||||
b.WriteString(replacement)
|
||||
start += idx + len(lowerTarget)
|
||||
return pattern.ReplaceAllLiteralString(input, replacement)
|
||||
}
|
||||
|
||||
// openAIPassthroughClientErrorFieldMaxBytes 限制透出的 error.type/code/param
|
||||
// 单字段长度。三者都是短标识符,超长只可能是上游在夹带其它内容。
|
||||
const openAIPassthroughClientErrorFieldMaxBytes = 256
|
||||
|
||||
// openAIPassthroughClientErrorField 提取允许写给客户端的标量错误字段。
|
||||
// gjson 对对象/数组的 String() 返回整段原始 JSON,直接透出等于把上游任意
|
||||
// 扩展字段塞进 type/code/param,因此只接受字符串与数字(部分上游的 code 为
|
||||
// 数字),并在脱敏后做长度上限校验,超限即丢弃。
|
||||
func openAIPassthroughClientErrorField(account *Account, value gjson.Result) string {
|
||||
if value.Type != gjson.String && value.Type != gjson.Number {
|
||||
return ""
|
||||
}
|
||||
return b.String()
|
||||
field := sanitizeOpenAIPassthroughClientErrorText(account, value.String())
|
||||
if len(field) > openAIPassthroughClientErrorFieldMaxBytes {
|
||||
return ""
|
||||
}
|
||||
return field
|
||||
}
|
||||
|
||||
// openAIPassthroughClientErrorCode 返回允许写给客户端的 error.code。
|
||||
// error.code 缺失时沿用 extractUpstreamErrorCode 对"message 内嵌 JSON"形态的
|
||||
// 兼容提取;error.code 存在但不是标量时不回退,直接视为无 code。
|
||||
func openAIPassthroughClientErrorCode(account *Account, body []byte) string {
|
||||
if code := gjson.GetBytes(body, "error.code"); code.Exists() && code.Type != gjson.Null {
|
||||
return openAIPassthroughClientErrorField(account, code)
|
||||
}
|
||||
code := sanitizeOpenAIPassthroughClientErrorText(account, extractUpstreamErrorCode(body))
|
||||
if len(code) > openAIPassthroughClientErrorFieldMaxBytes || strings.HasPrefix(code, "{") || strings.HasPrefix(code, "[") {
|
||||
return ""
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
// writeOpenAIPassthroughStructuredErrorEnvelope 在净化后的 OpenAI 错误信封内
|
||||
@@ -933,7 +963,7 @@ func writeOpenAIPassthroughStructuredErrorEnvelope(
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
errType := sanitizeOpenAIPassthroughClientErrorText(account, gjson.GetBytes(body, "error.type").String())
|
||||
errType := openAIPassthroughClientErrorField(account, gjson.GetBytes(body, "error.type"))
|
||||
if errType == "" {
|
||||
errType = fallbackType
|
||||
}
|
||||
@@ -941,10 +971,11 @@ func writeOpenAIPassthroughStructuredErrorEnvelope(
|
||||
errType = "upstream_error"
|
||||
}
|
||||
errorPayload := gin.H{"type": errType}
|
||||
if code := sanitizeOpenAIPassthroughClientErrorText(account, extractUpstreamErrorCode(body)); code != "" {
|
||||
code := openAIPassthroughClientErrorCode(account, body)
|
||||
if code != "" {
|
||||
errorPayload["code"] = code
|
||||
}
|
||||
if param := sanitizeOpenAIPassthroughClientErrorText(account, gjson.GetBytes(body, "error.param").String()); param != "" {
|
||||
if param := openAIPassthroughClientErrorField(account, gjson.GetBytes(body, "error.param")); param != "" {
|
||||
errorPayload["param"] = param
|
||||
}
|
||||
message := sanitizeOpenAIPassthroughClientErrorText(account, upstreamMsg)
|
||||
@@ -956,6 +987,15 @@ func writeOpenAIPassthroughStructuredErrorEnvelope(
|
||||
}
|
||||
errorPayload["message"] = message
|
||||
|
||||
// compact 心跳已把响应头提交为 200 时,错误只能以 response.failed 终止事件
|
||||
// 回传。通用桥接会把 code 固定为 upstream_error,这里直接带上结构化 code,
|
||||
// 否则本函数要保留的 error.code 恰好在 compact 流上丢失。
|
||||
if (downstreamStatus < 200 || downstreamStatus >= 300) &&
|
||||
openAICompactClientWantsStream(c) && StopOpenAICompactSSEKeepaliveCommitted(c) {
|
||||
writeOpenAICompactSSEFailureWithCode(c, downstreamStatus, code, message)
|
||||
return
|
||||
}
|
||||
|
||||
payload, _ := json.Marshal(gin.H{"error": errorPayload})
|
||||
if writeOpenAICompactSSEBridge(c, downstreamStatus, payload) {
|
||||
return
|
||||
@@ -1092,6 +1132,14 @@ func (s *OpenAIGatewayService) handleErrorResponsePassthrough(
|
||||
c, status, resp.Header, body, account, errType, errMsg, "Upstream request failed",
|
||||
)
|
||||
} else {
|
||||
// 管理员自定义文案原样输出;未配置自定义文案时 errMsg 是上游原始消息,
|
||||
// 必须与其它分支一样脱敏(query 凭据、账号凭据、上游主机名)。
|
||||
if rule == nil || rule.CustomMessage == nil {
|
||||
errMsg = sanitizeOpenAIPassthroughClientErrorText(account, errMsg)
|
||||
if errMsg == "" {
|
||||
errMsg = "Upstream request failed"
|
||||
}
|
||||
}
|
||||
writeOpenAIPassthroughErrorEnvelope(c, status, resp.Header, errMsg)
|
||||
}
|
||||
return fmt.Errorf("upstream error: %d (passthrough rule matched)", resp.StatusCode)
|
||||
|
||||
@@ -331,7 +331,209 @@ func TestOpenAIGatewayService_APIKeyPassthrough_ContextWindowPreservesCodeAndCom
|
||||
require.Len(t, events, 1)
|
||||
require.Equal(t, "response.failed", events[0][0])
|
||||
require.Contains(t, gjson.Get(events[0][1], "response.error.message").String(), "could not be verified")
|
||||
// 心跳已提交时 error.code 也必须保留,否则 compact 流上客户端仍无法识别该错误。
|
||||
require.Equal(t, "invalid_encrypted_content", gjson.Get(events[0][1], "response.error.code").String())
|
||||
})
|
||||
}
|
||||
|
||||
var _ = time.Second
|
||||
|
||||
// forwardOpenAIPassthroughErrorForTest 让透传账号收到给定的上游错误响应,返回写给客户端的结果。
|
||||
func forwardOpenAIPassthroughErrorForTest(t *testing.T, c *gin.Context, upstreamStatus int, upstreamBody string, credentials map[string]any) {
|
||||
t.Helper()
|
||||
svc := &OpenAIGatewayService{
|
||||
cfg: &config.Config{Gateway: config.GatewayConfig{ForceCodexCLI: false}},
|
||||
httpUpstream: &httpUpstreamRecorder{resp: &http.Response{
|
||||
StatusCode: upstreamStatus,
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
Body: io.NopCloser(strings.NewReader(upstreamBody)),
|
||||
}},
|
||||
}
|
||||
account := &Account{
|
||||
ID: 301, Name: "openai-pt-hardening", Platform: PlatformOpenAI, Type: AccountTypeAPIKey, Concurrency: 1,
|
||||
Credentials: credentials,
|
||||
Extra: map[string]any{"openai_passthrough": true}, Status: StatusActive, Schedulable: true,
|
||||
}
|
||||
_, err := svc.Forward(context.Background(), c, account, []byte(`{"model":"gpt-5.4","input":"hi"}`))
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func newOpenAIPassthroughErrorTestContext() (*httptest.ResponseRecorder, *gin.Context) {
|
||||
rec := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Request = httptest.NewRequest(http.MethodPost, "/v1/responses", bytes.NewReader(nil))
|
||||
return rec, c
|
||||
}
|
||||
|
||||
// 大小写折叠会改变字节长度的字符(Ⱥ 变长、Kelvin 符号变短、非法 UTF-8 变长)
|
||||
// 曾让主机名脱敏按错位下标切片:要么越界 panic,要么把主机名原样漏给客户端。
|
||||
func TestReplaceCaseInsensitive_ByteLengthChangingCaseFolding(t *testing.T) {
|
||||
const host = "h.example"
|
||||
kelvin := "\u212a"
|
||||
cases := map[string]string{
|
||||
"ascii": "upstream h.example rejected",
|
||||
"mixed case host": "upstream H.Example rejected",
|
||||
"grows when lowered": "\u023a h.example",
|
||||
"grows, host in the middle": strings.Repeat("\u023a", 10) + " h.example tail-padding-text-xxxxxxxx",
|
||||
"shrinks when lowered": kelvin + " h.example",
|
||||
"shrinks, repeated": strings.Repeat(kelvin, 5) + " h.example",
|
||||
"invalid utf8": "\xff h.example",
|
||||
"invalid utf8, host in tail": strings.Repeat("\xff", 10) + " h.example tail-padding-text-xxxxxxxx",
|
||||
"multiple occurrences": "\u023a h.example and " + kelvin + " H.EXAMPLE again h.example",
|
||||
}
|
||||
for name, input := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
var out string
|
||||
require.NotPanics(t, func() { out = replaceCaseInsensitive(input, host, "[upstream]") })
|
||||
require.NotContains(t, strings.ToLower(out), host)
|
||||
require.Contains(t, out, "[upstream]")
|
||||
// 主机名以外的字节必须原样保留。
|
||||
require.Equal(t,
|
||||
strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(input, "h.example", "[upstream]"), "H.Example", "[upstream]"), "H.EXAMPLE", "[upstream]"),
|
||||
out,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
require.Equal(t, "no match here", replaceCaseInsensitive("no match here", host, "[upstream]"))
|
||||
require.Equal(t, "", replaceCaseInsensitive("", host, "[upstream]"))
|
||||
require.Equal(t, "text", replaceCaseInsensitive("text", "", "[upstream]"))
|
||||
// 正则元字符按字面量匹配,"." 不得匹配任意字符。
|
||||
require.Equal(t, "hxexample", replaceCaseInsensitive("hxexample", host, "[upstream]"))
|
||||
// target 无法编译时宁可丢弃整段文本。
|
||||
require.Equal(t, "", replaceCaseInsensitive("text \xff here", "\xff", "[upstream]"))
|
||||
}
|
||||
|
||||
func TestOpenAIGatewayService_APIKeyPassthrough_StructuredErrorHardening(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
t.Run("case-folding characters next to the upstream host neither panic nor leak it", func(t *testing.T) {
|
||||
for name, prefix := range map[string]string{
|
||||
"grows": "\u023a",
|
||||
"shrinks": strings.Repeat("\u212a", 5),
|
||||
"invalid utf8": "\xff",
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
rec, c := newOpenAIPassthroughErrorTestContext()
|
||||
upstreamBody := `{"error":{"message":"` + prefix + ` h.example","type":"invalid_request_error","code":"context_length_exceeded","param":"input"}}`
|
||||
require.NotPanics(t, func() {
|
||||
forwardOpenAIPassthroughErrorForTest(t, c, http.StatusBadRequest, upstreamBody,
|
||||
map[string]any{"api_key": "sk-test", "base_url": "https://h.example/v1"})
|
||||
})
|
||||
require.Equal(t, http.StatusBadRequest, rec.Code)
|
||||
body := rec.Body.String()
|
||||
require.NotContains(t, strings.ToLower(body), "h.example")
|
||||
require.Contains(t, gjson.Get(body, "error.message").String(), "[upstream]")
|
||||
require.Equal(t, "context_length_exceeded", gjson.Get(body, "error.code").String())
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("non-scalar type and param are dropped instead of forwarded as raw JSON", func(t *testing.T) {
|
||||
rec, c := newOpenAIPassthroughErrorTestContext()
|
||||
forwardOpenAIPassthroughErrorForTest(t, c, http.StatusBadRequest,
|
||||
`{"error":{"message":"The encrypted content could not be verified.","type":{"internal_url":"http://10.0.0.1/admin"},"code":"invalid_encrypted_content","param":["internal_token","tok-abcdef"]}}`,
|
||||
map[string]any{"api_key": "sk-test"})
|
||||
require.Equal(t, http.StatusBadRequest, rec.Code)
|
||||
body := rec.Body.String()
|
||||
require.Equal(t, openAIUpstreamClientErrorFallbackType, gjson.Get(body, "error.type").String())
|
||||
require.Equal(t, "invalid_encrypted_content", gjson.Get(body, "error.code").String())
|
||||
require.False(t, gjson.Get(body, "error.param").Exists())
|
||||
require.NotContains(t, body, "10.0.0.1")
|
||||
require.NotContains(t, body, "internal_token")
|
||||
require.NotContains(t, body, "tok-abcdef")
|
||||
})
|
||||
|
||||
t.Run("non-scalar code is not forwarded", func(t *testing.T) {
|
||||
rec, c := newOpenAIPassthroughErrorTestContext()
|
||||
forwardOpenAIPassthroughErrorForTest(t, c, http.StatusBadRequest,
|
||||
`{"error":{"message":"Your input exceeds the context window of this model.","type":"invalid_request_error","code":{"internal_token":"tok-abcdef"}}}`,
|
||||
map[string]any{"api_key": "sk-test"})
|
||||
require.Equal(t, http.StatusBadRequest, rec.Code)
|
||||
body := rec.Body.String()
|
||||
require.False(t, gjson.Get(body, "error.code").Exists())
|
||||
require.NotContains(t, body, "tok-abcdef")
|
||||
require.Contains(t, gjson.Get(body, "error.message").String(), "exceeds the context window")
|
||||
})
|
||||
|
||||
t.Run("oversized fields are dropped", func(t *testing.T) {
|
||||
rec, c := newOpenAIPassthroughErrorTestContext()
|
||||
oversized := strings.Repeat("p", openAIPassthroughClientErrorFieldMaxBytes+1)
|
||||
forwardOpenAIPassthroughErrorForTest(t, c, http.StatusBadRequest,
|
||||
`{"error":{"message":"The encrypted content could not be verified.","type":"`+oversized+`","code":"invalid_encrypted_content","param":"`+oversized+`"}}`,
|
||||
map[string]any{"api_key": "sk-test"})
|
||||
require.Equal(t, http.StatusBadRequest, rec.Code)
|
||||
body := rec.Body.String()
|
||||
require.Equal(t, openAIUpstreamClientErrorFallbackType, gjson.Get(body, "error.type").String())
|
||||
require.False(t, gjson.Get(body, "error.param").Exists())
|
||||
require.NotContains(t, body, oversized)
|
||||
})
|
||||
|
||||
t.Run("numeric code is forwarded as a string", func(t *testing.T) {
|
||||
require.Equal(t, "1234", openAIPassthroughClientErrorCode(nil, []byte(`{"error":{"code":1234}}`)))
|
||||
require.Equal(t, "", openAIPassthroughClientErrorCode(nil, []byte(`{"error":{"code":null}}`)))
|
||||
require.Equal(t, "", openAIPassthroughClientErrorCode(nil, []byte(`{"error":{"code":true}}`)))
|
||||
require.Equal(t, "inner_code", openAIPassthroughClientErrorCode(nil, []byte(`{"error":{"message":"{\"error\":{\"code\":\"inner_code\"}}"}}`)))
|
||||
require.Equal(t, "", openAIPassthroughClientErrorCode(nil, []byte(`{"error":{"message":"{\"error\":{\"code\":{\"k\":\"v\"}}}"}}`)))
|
||||
})
|
||||
|
||||
t.Run("ordinary 400 that merely mentions invalid_encrypted_content stays sanitized", func(t *testing.T) {
|
||||
rec, c := newOpenAIPassthroughErrorTestContext()
|
||||
forwardOpenAIPassthroughErrorForTest(t, c, http.StatusBadRequest,
|
||||
`{"error":{"message":"echoed user text: invalid_encrypted_content at secret-upstream.example","type":"invalid_request_error","code":"invalid_value","param":"input"}}`,
|
||||
map[string]any{"api_key": "sk-test", "base_url": "https://secret-upstream.example/v1"})
|
||||
require.Equal(t, http.StatusBadRequest, rec.Code)
|
||||
body := rec.Body.String()
|
||||
require.Equal(t, "upstream_error", gjson.Get(body, "error.type").String())
|
||||
require.Equal(t, "Upstream request failed", gjson.Get(body, "error.message").String())
|
||||
require.False(t, gjson.Get(body, "error.code").Exists())
|
||||
require.False(t, gjson.Get(body, "error.param").Exists())
|
||||
require.NotContains(t, body, "secret-upstream.example")
|
||||
})
|
||||
}
|
||||
|
||||
func TestOpenAIGatewayService_APIKeyPassthrough_RuleWithoutCustomMessageSanitizesUpstreamMessage(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
bindRule := func(c *gin.Context, customMessage *string) {
|
||||
ruleSvc := &ErrorPassthroughService{}
|
||||
ruleSvc.setLocalCache([]*model.ErrorPassthroughRule{{
|
||||
ID: 1,
|
||||
Name: "openai-400-rule",
|
||||
Enabled: true,
|
||||
Priority: 1,
|
||||
ErrorCodes: []int{http.StatusBadRequest},
|
||||
MatchMode: model.MatchModeAny,
|
||||
Platforms: []string{PlatformOpenAI},
|
||||
PassthroughCode: true,
|
||||
PassthroughBody: false,
|
||||
CustomMessage: customMessage,
|
||||
}})
|
||||
BindErrorPassthroughService(c, ruleSvc)
|
||||
}
|
||||
const upstreamBody = `{"error":{"message":"https://secret-upstream.example/v1?key=secret123 rejected request for sk-test-secret-key","type":"invalid_request_error","code":"invalid_value"}}`
|
||||
credentials := map[string]any{"api_key": "sk-test-secret-key", "base_url": "https://secret-upstream.example/v1"}
|
||||
|
||||
t.Run("upstream message is redacted when the rule has no custom message", func(t *testing.T) {
|
||||
rec, c := newOpenAIPassthroughErrorTestContext()
|
||||
bindRule(c, nil)
|
||||
forwardOpenAIPassthroughErrorForTest(t, c, http.StatusBadRequest, upstreamBody, credentials)
|
||||
require.Equal(t, http.StatusBadRequest, rec.Code)
|
||||
body := rec.Body.String()
|
||||
require.Equal(t, "upstream_error", gjson.Get(body, "error.type").String())
|
||||
require.False(t, gjson.Get(body, "error.code").Exists())
|
||||
require.NotContains(t, body, "secret-upstream.example")
|
||||
require.NotContains(t, body, "secret123")
|
||||
require.NotContains(t, body, "sk-test-secret-key")
|
||||
require.Contains(t, gjson.Get(body, "error.message").String(), "rejected request")
|
||||
})
|
||||
|
||||
t.Run("administrator custom message is written verbatim", func(t *testing.T) {
|
||||
rec, c := newOpenAIPassthroughErrorTestContext()
|
||||
custom := "Contact support at secret-upstream.example"
|
||||
bindRule(c, &custom)
|
||||
forwardOpenAIPassthroughErrorForTest(t, c, http.StatusBadRequest, upstreamBody, credentials)
|
||||
require.Equal(t, http.StatusBadRequest, rec.Code)
|
||||
require.Equal(t, custom, gjson.Get(rec.Body.String(), "error.message").String())
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user