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 里搜不到哨兵正文」用例,且能变异打红
115 lines
3.8 KiB
Go
115 lines
3.8 KiB
Go
package vision
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/config"
|
|
)
|
|
|
|
func TestOpenAICompatibleAdapter_Describe(t *testing.T) {
|
|
t.Parallel()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/chat/completions" {
|
|
t.Errorf("path = %s, want /chat/completions", r.URL.Path)
|
|
}
|
|
var body map[string]any
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
t.Fatalf("decode body: %v", err)
|
|
}
|
|
msgs, _ := body["messages"].([]any)
|
|
if len(msgs) != 1 {
|
|
t.Fatalf("messages = %d, want 1", len(msgs))
|
|
}
|
|
content, _ := msgs[0].(map[string]any)["content"].([]any)
|
|
if len(content) != 2 {
|
|
t.Fatalf("content parts = %d, want 2", len(content))
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = io.WriteString(w, `{"choices":[{"message":{"content":"a red car"}}]}`)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
ad := NewOpenAICompatible(config.LLMConfig{BaseURL: srv.URL, Model: "vision-"})
|
|
text, err := ad.Describe(context.Background(), ImageRef{Source: SourceBase64, Data: "aGVsbG8="}, "")
|
|
if err != nil {
|
|
t.Fatalf("Describe error: %v", err)
|
|
}
|
|
if text != "a red car" {
|
|
t.Errorf("text = %q, want a red car", text)
|
|
}
|
|
}
|
|
|
|
func TestOpenAICompatibleAdapter_NotConfigured(t *testing.T) {
|
|
t.Parallel()
|
|
ad := NewOpenAICompatible(config.LLMConfig{})
|
|
if _, err := ad.Describe(context.Background(), ImageRef{Source: SourceBase64, Data: "aGVsbG8="}, ""); err == nil {
|
|
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)
|
|
}
|
|
}
|