fix(runtime): 上游错误体统一走 trace.UpstreamError,正文不进 error
M41 的「LLM / embedding / 视觉 上游错误体」一族(M38 #5 #6 #7 #8 #9 #14): 非 2xx 时把响应体截断后拼进 error,而 error 会被 chat run / loop step / retrieve_fallback 打日志,等于把她的提问、记忆正文、要念的台词写进日志。 - model:openai/anthropic 两处 upstreamError 收口到 upstream.go 的 providerUpstreamError;anthropic 流内 error 事件的 evt.Error.Message (原本无上限)整段丢弃,只保留令牌形状的 type - embedding:readErrorBody 删除,改走 helper(input 就是她的记忆正文) - vision:错误路径不再沿用取图的 50 MiB 上限,改为 4 KiB 有界读且不拼原文 - voice:classifyDashscopeStatus 只解 code/request_id,正文只留在内存里做 分类判据;错误体读取同样改为 4 KiB 有界 - 测试:五个上游各一条「日志/error 里搜不到哨兵正文」用例,且能变异打红
This commit is contained in:
1 parent
46a6da13e0
commit
66dda084cd
16 files changed
+570
-183
No files matched your search
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/trace"
|
||||
)
|
||||
|
||||
// visionMaxTokens 限制 vision/OCR 响应的长度。
|
||||
@@ -170,8 +171,14 @@ func (a *OpenAICompatibleAdapter) ask(ctx context.Context, model string, img Ima
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
snippet, _ := io.ReadAll(io.LimitReader(resp.Body, maxFetchedBody))
|
||||
return "", fmt.Errorf("vision: %w: status %d: %s", ErrUpstream, resp.StatusCode, strings.TrimSpace(string(snippet)))
|
||||
// 错误路径**不再整段读**:这里原来沿用取图的 maxFetchedBody(50 MiB),
|
||||
// 等于「最多 50 MiB 的错误体进内存、再进 error」。错误体是攻击面,因此
|
||||
// 只读 maxErrorBodyBytes,并且一个字节都不进 error —— 只留 status /
|
||||
// contentType / bodyLen / sha256 与 code/type(trace.UpstreamError)。
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, maxErrorBodyBytes))
|
||||
upstream := trace.UpstreamError(trace.HostOf(a.cfg.BaseURL), resp.StatusCode,
|
||||
resp.Header.Get("Content-Type"), body)
|
||||
return "", fmt.Errorf("vision: %w: %w", ErrUpstream, upstream)
|
||||
}
|
||||
return a.decode(resp.Body)
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@ package vision
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
|
||||
@@ -51,3 +53,62 @@ func TestOpenAICompatibleAdapter_NotConfigured(t *testing.T) {
|
||||
t.Fatal("want error for empty base url, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// stubTransport 让测试不经网络直接给出响应。
|
||||
type stubTransport func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f stubTransport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
||||
|
||||
// countingBody 记录被读走的字节数,用来断言错误路径**有界读**。
|
||||
type countingBody struct {
|
||||
r io.Reader
|
||||
n *int64
|
||||
}
|
||||
|
||||
func (b *countingBody) Read(p []byte) (int, error) {
|
||||
n, err := b.r.Read(p)
|
||||
*b.n += int64(n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (b *countingBody) Close() error { return nil }
|
||||
|
||||
// TestOpenAICompatibleAdapter_ErrorBodyKeptOutOfError 覆盖 M41 的视觉一处:
|
||||
// 旧实现沿用取图上限(50 MiB)读错误体,再把它拼进 error —— 既是日志面
|
||||
// (整个 50 MiB 的错误体都能进去),也是 DoS 面(一次超大错误响应就吃掉内存)。
|
||||
//
|
||||
// 两个断言各能单独打红:① 正文出现在 error 里 ⇒ 又回到「拼原文」;
|
||||
// ② 错误路径读走超过 maxErrorBodyBytes ⇒ 又回到按取图上限整段读。
|
||||
// 测试体用 8 MiB(远大于 4 KiB、远小于 50 MiB):足以证伪无界读,又不必在
|
||||
// 测试里分配 50 MiB。
|
||||
func TestOpenAICompatibleAdapter_ErrorBodyKeptOutOfError(t *testing.T) {
|
||||
t.Parallel()
|
||||
const sentinel = "SENTINEL-VISION-ERROR-BODY-7d2a"
|
||||
var read int64
|
||||
payload := sentinel + strings.Repeat("x", 8<<20)
|
||||
ad := newAdapter("https://vision.example.com")
|
||||
ad.client = &http.Client{Transport: stubTransport(func(*http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusInternalServerError,
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
Body: &countingBody{r: strings.NewReader(payload), n: &read},
|
||||
}, nil
|
||||
})}
|
||||
|
||||
_, err := ad.Describe(context.Background(), ImageRef{Source: SourceBase64, Data: "aGVsbG8="}, "")
|
||||
if err == nil {
|
||||
t.Fatal("want error, got nil")
|
||||
}
|
||||
if !errors.Is(err, ErrUpstream) {
|
||||
t.Fatalf("err = %v, want ErrUpstream", err)
|
||||
}
|
||||
if strings.Contains(err.Error(), sentinel) {
|
||||
t.Errorf("error text carries the response body: %v", err)
|
||||
}
|
||||
if read > maxErrorBodyBytes {
|
||||
t.Errorf("error path read %d bytes of the error body, want <= %d", read, maxErrorBodyBytes)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "500") {
|
||||
t.Errorf("err = %v, want the status kept for triage", err)
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,11 @@ import (
|
||||
const (
|
||||
maxImageBytes = 50 << 20
|
||||
maxFetchedBody = 50 << 20
|
||||
// maxErrorBodyBytes 是非 2xx 响应的读取上限。它与上面两个是**不同口径**:
|
||||
// 那两个是数据面(图像字节)的预算,这一条是攻击面的预算 —— 错误体只用来
|
||||
// 数长度 / 取 sha256 前缀 / 抽 error.code,因此 4 KiB 足够,绝不沿用 50 MiB
|
||||
// (那会把「一个超大错误响应」变成一次内存分配与一次日志面暴露)。
|
||||
maxErrorBodyBytes = 4 << 10
|
||||
)
|
||||
|
||||
// imageFetchTimeout 限制一次图像 URL 取回的时长。适配器与消息解析器会
|
||||
|
||||
Reference in new issue
Block a user