Skip to content

Commit 8de3fbd

Browse files
committed
fix(ai): harden chat request limits, provider-specific thinking params and gemini host migration
- cap the whole chat request body before binding, reject images on non-first messages and redact attachments across every message before logging - inject the provider-documented thinking parameter instead of a global enable_thinking flag (DashScope: enable_thinking, DeepSeek: thinking object, hosts without a documented parameter: none) - add a v2.0.4 migration upgrading only the unchanged legacy Gemini default host, and restore the Anthropic provider entry - preserve reasoning_content end-to-end when rebuilding conversation context for follow-up turns - validate image magic bytes for base64 attachments
1 parent 158516b commit 8de3fbd

11 files changed

Lines changed: 478 additions & 59 deletions

File tree

‎internal/controller/ai_controller.go‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ func (c *AIController) ensureAIChatEnabled(ctx *gin.Context) bool {
102102
return true
103103
}
104104

105+
// maxChatRequestBodyBytes caps the whole chat completion request body before
106+
// it is bound or logged: 4 images × 4MB with base64 expansion (~21.3MB) plus
107+
// text history.
108+
const maxChatRequestBodyBytes = 32 << 20
109+
105110
type ChatCompletionsRequest struct {
106111
Messages []Message `validate:"required,gte=1" json:"messages"`
107112
ConversationID string `json:"conversation_id"`
@@ -175,6 +180,9 @@ func (c *ConversationContext) GetOpenAIMessages() []openai.ChatCompletionMessage
175180
messages[i] = openai.ChatCompletionMessage{
176181
Role: msg.Role,
177182
Content: msg.Content,
183+
// Preserved so tool-bearing follow-up requests can pass prior
184+
// reasoning back to the model (required by e.g. DeepSeek).
185+
ReasoningContent: msg.ReasoningContent,
178186
}
179187
}
180188
return messages
@@ -227,12 +235,30 @@ func (c *AIController) ChatCompletions(ctx *gin.Context) {
227235

228236
aiProvider := aiConfig.GetProvider()
229237

238+
// Cap the complete request body before binding so oversized payloads are
239+
// rejected instead of being buffered or written to logs.
240+
if ctx.Request.ContentLength > maxChatRequestBodyBytes {
241+
handler.HandleResponse(ctx, errors.BadRequest("request body too large"), nil)
242+
return
243+
}
244+
ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, maxChatRequestBodyBytes)
245+
230246
req := &ChatCompletionsRequest{}
231247
if handler.BindAndCheck(ctx, req) {
232248
return
233249
}
234250
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
235251

252+
// Attachments are accepted on the first message only; images on later
253+
// messages are neither validated nor forwarded to the model, so reject
254+
// them outright.
255+
for i, msg := range req.Messages {
256+
if i > 0 && len(msg.Images) > 0 {
257+
handler.HandleResponse(ctx, errors.BadRequest("images are only allowed on the first message"), nil)
258+
return
259+
}
260+
}
261+
236262
// Reject or validate image attachments before the SSE stream starts, so
237263
// clients still receive a proper JSON error with the right status code.
238264
if len(req.Messages) > 0 && len(req.Messages[0].Images) > 0 {
@@ -246,18 +272,18 @@ func (c *AIController) ChatCompletions(ctx *gin.Context) {
246272
}
247273
}
248274

249-
// Never dump image data into logs; summarize attachments as a count.
250-
if len(req.Messages) > 0 && len(req.Messages[0].Images) > 0 {
251-
logReq := *req
252-
logReq.Messages = make([]Message, len(req.Messages))
253-
copy(logReq.Messages, req.Messages)
254-
logReq.Messages[0].Images = []string{fmt.Sprintf("<%d images>", len(req.Messages[0].Images))}
255-
data, _ := json.Marshal(logReq)
256-
log.Infof("ai chat request data: %s", string(data))
257-
} else {
258-
data, _ := json.Marshal(req)
259-
log.Infof("ai chat request data: %s", string(data))
275+
// Never dump image data into logs; summarize attachments as a count
276+
// across every message before marshaling.
277+
logReq := *req
278+
logReq.Messages = make([]Message, len(req.Messages))
279+
for i, msg := range req.Messages {
280+
logReq.Messages[i] = msg
281+
if len(msg.Images) > 0 {
282+
logReq.Messages[i].Images = []string{fmt.Sprintf("<%d images>", len(msg.Images))}
283+
}
260284
}
285+
data, _ := json.Marshal(logReq)
286+
log.Infof("ai chat request data: %s", string(data))
261287

262288
ctx.Header("Content-Type", "text/event-stream")
263289
ctx.Header("Cache-Control", "no-cache")
@@ -343,7 +369,9 @@ func (c *AIController) createOpenAIClient() *openai.Client {
343369
config := openai.DefaultConfig(aiProvider.APIKey)
344370
config.BaseURL = schema.NormalizeAPIHost(aiProvider.APIHost)
345371
if aiProvider.ThinkingMode == "on" {
346-
config.HTTPClient = newThinkingHTTPClient()
372+
if param := thinkingParamForHost(aiProvider.APIHost); len(param) > 0 {
373+
config.HTTPClient = newThinkingHTTPClient(param)
374+
}
347375
}
348376
return openai.NewClientWithConfig(config)
349377
}
@@ -426,6 +454,7 @@ func (c *AIController) initializeConversationContext(ctx *gin.Context, model str
426454
ChatCompletionID: record.ChatCompletionID,
427455
Role: record.Role,
428456
Content: record.Content,
457+
ReasoningContent: record.ReasoningContent,
429458
})
430459
}
431460
conversationCtx.Messages = append(conversationCtx.Messages, &ai_conversation.ConversationMessage{

‎internal/controller/ai_thinking_transport.go‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,50 @@ import (
2828
"strings"
2929
)
3030

31-
// ThinkingParamKey is the OpenAI-compatible request flag understood by
32-
// reasoning-capable model gateways (DeepSeek, Qwen/DashScope, SenseNova, ...).
33-
const ThinkingParamKey = "enable_thinking"
31+
// thinkingParamForHost returns the request body patch that enables thinking
32+
// for a provider host, or nil when the host has no documented
33+
// OpenAI-compatible thinking parameter. The flag is gateway-specific rather
34+
// than a neutral OpenAI extension:
35+
// - DashScope/Qwen compatibility mode documents top-level "enable_thinking"
36+
// (https://help.aliyun.com/en/model-studio/qwen-api-via-dashscope).
37+
// - DeepSeek documents an object parameter, thinking: {"type": "enabled"}
38+
// (https://api-docs.deepseek.com/guides/thinking_mode/).
39+
// - Gemini's OpenAI-compatible endpoint accepts neither.
40+
//
41+
// Unknown hosts receive no provider-specific field instead of a global guess.
42+
func thinkingParamForHost(apiHost string) map[string]any {
43+
h := strings.ToLower(strings.TrimSpace(apiHost))
44+
switch {
45+
case strings.Contains(h, "dashscope.aliyuncs.com"):
46+
return map[string]any{"enable_thinking": true}
47+
case strings.Contains(h, "api.deepseek.com"):
48+
return map[string]any{"thinking": map[string]any{"type": "enabled"}}
49+
default:
50+
return nil
51+
}
52+
}
3453

35-
// thinkingTransport merges the thinking flag into chat completion request
54+
// thinkingTransport merges the thinking parameter into chat completion request
3655
// bodies before they leave the process. The openai SDK has no generic extra
3756
// body hook, so an http.Client with this transport is attached to the client
38-
// config when the provider enables thinking mode.
39-
type thinkingTransport struct{ base http.RoundTripper }
57+
// config when the provider enables thinking mode and its host documents a
58+
// thinking parameter.
59+
type thinkingTransport struct {
60+
base http.RoundTripper
61+
param map[string]any
62+
}
4063

4164
func (t *thinkingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
42-
if req.Body != nil && req.ContentLength != 0 &&
65+
if len(t.param) > 0 && req.Body != nil && req.ContentLength != 0 &&
4366
strings.HasSuffix(req.URL.Path, "/chat/completions") {
4467
b, err := io.ReadAll(req.Body)
4568
_ = req.Body.Close()
4669
if err == nil {
4770
var payload map[string]any
4871
if json.Unmarshal(b, &payload) == nil && payload != nil {
49-
payload[ThinkingParamKey] = true
72+
for k, v := range t.param {
73+
payload[k] = v
74+
}
5075
nb, mErr := json.Marshal(payload)
5176
if mErr == nil {
5277
req.Body = io.NopCloser(bytes.NewReader(nb))
@@ -66,6 +91,6 @@ func (t *thinkingTransport) RoundTrip(req *http.Request) (*http.Response, error)
6691
return t.base.RoundTrip(req)
6792
}
6893

69-
func newThinkingHTTPClient() *http.Client {
70-
return &http.Client{Transport: &thinkingTransport{}}
94+
func newThinkingHTTPClient(param map[string]any) *http.Client {
95+
return &http.Client{Transport: &thinkingTransport{param: param}}
7196
}

‎internal/controller/ai_thinking_transport_test.go‎

Lines changed: 84 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,56 +25,109 @@ import (
2525
"io"
2626
"net/http"
2727
"net/http/httptest"
28+
"reflect"
2829
"strings"
2930
"testing"
3031
)
3132

32-
func TestThinkingTransportInjectsFlag(t *testing.T) {
33-
var got map[string]any
33+
const thinkingTestBody = `{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"hi"}],"stream":true}`
34+
35+
// newCaptureServer returns a server that records the last JSON body it
36+
// received and the helper that posts the test request to it.
37+
func newCaptureServer() (*httptest.Server, *map[string]any, func(t *testing.T, client *http.Client, path string)) {
38+
got := &map[string]any{}
3439
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3540
b, _ := io.ReadAll(r.Body)
36-
_ = json.Unmarshal(b, &got)
41+
_ = json.Unmarshal(b, got)
3742
w.WriteHeader(http.StatusOK)
3843
}))
44+
post := func(t *testing.T, client *http.Client, path string) {
45+
t.Helper()
46+
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost,
47+
srv.URL+path, strings.NewReader(thinkingTestBody))
48+
if err != nil {
49+
t.Fatal(err)
50+
}
51+
req.Header.Set("Content-Type", "application/json")
52+
resp, err := client.Do(req)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
_ = resp.Body.Close()
57+
}
58+
return srv, got, post
59+
}
60+
61+
func TestThinkingParamForHost(t *testing.T) {
62+
if !reflect.DeepEqual(thinkingParamForHost("https://dashscope.aliyuncs.com/compatible-mode/v1"),
63+
map[string]any{"enable_thinking": true}) {
64+
t.Fatalf("DashScope host should get enable_thinking")
65+
}
66+
if !reflect.DeepEqual(thinkingParamForHost("https://Api.DeepSeek.com"),
67+
map[string]any{"thinking": map[string]any{"type": "enabled"}}) {
68+
t.Fatalf("DeepSeek host should get the thinking object")
69+
}
70+
for _, host := range []string{
71+
"https://generativelanguage.googleapis.com/v1beta/openai",
72+
"https://example.com",
73+
"",
74+
} {
75+
if thinkingParamForHost(host) != nil {
76+
t.Fatalf("host %q should get no provider-specific parameter", host)
77+
}
78+
}
79+
}
80+
81+
func TestThinkingTransportInjectsDashScopeFlag(t *testing.T) {
82+
srv, got, post := newCaptureServer()
3983
defer srv.Close()
4084

41-
client := newThinkingHTTPClient()
42-
body := `{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"hi"}],"stream":true}`
43-
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost,
44-
srv.URL+"/v1/chat/completions", strings.NewReader(body))
45-
if err != nil {
46-
t.Fatal(err)
85+
client := newThinkingHTTPClient(thinkingParamForHost("https://dashscope.aliyuncs.com/compatible-mode/v1"))
86+
post(t, client, "/v1/chat/completions")
87+
88+
if (*got)["enable_thinking"] != true {
89+
t.Fatalf("enable_thinking not injected: %v", (*got)["enable_thinking"])
4790
}
48-
req.Header.Set("Content-Type", "application/json")
49-
resp, err := client.Do(req)
50-
if err != nil {
51-
t.Fatal(err)
91+
if (*got)["model"] != "deepseek-v4-flash" {
92+
t.Fatalf("original fields lost: %v", *got)
5293
}
53-
resp.Body.Close()
94+
}
95+
96+
func TestThinkingTransportInjectsDeepSeekObject(t *testing.T) {
97+
srv, got, post := newCaptureServer()
98+
defer srv.Close()
99+
100+
client := newThinkingHTTPClient(thinkingParamForHost("https://api.deepseek.com"))
101+
post(t, client, "/v1/chat/completions")
54102

55-
if got["enable_thinking"] != true {
56-
t.Fatalf("enable_thinking not injected: %v", got["enable_thinking"])
103+
thinking, ok := (*got)["thinking"].(map[string]any)
104+
if !ok || thinking["type"] != "enabled" {
105+
t.Fatalf("thinking object not injected: %v", (*got)["thinking"])
57106
}
58-
if got["model"] != "deepseek-v4-flash" {
59-
t.Fatalf("original fields lost: %v", got)
107+
if _, exists := (*got)["enable_thinking"]; exists {
108+
t.Fatalf("gateway-specific flag must not leak to other providers: %v", *got)
60109
}
61110
}
62111

63-
func TestThinkingTransportLeavesOtherPaths(t *testing.T) {
64-
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
65-
w.WriteHeader(http.StatusOK)
66-
}))
112+
func TestThinkingTransportNoParamLeavesBodyUntouched(t *testing.T) {
113+
srv, got, post := newCaptureServer()
67114
defer srv.Close()
68115

69-
client := newThinkingHTTPClient()
70-
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost,
71-
srv.URL+"/v1/models", nil)
72-
if err != nil {
73-
t.Fatal(err)
116+
client := newThinkingHTTPClient(nil)
117+
post(t, client, "/v1/chat/completions")
118+
119+
if _, exists := (*got)["enable_thinking"]; exists {
120+
t.Fatalf("no provider-specific field must be injected: %v", *got)
74121
}
75-
resp, err := client.Do(req)
76-
if err != nil {
77-
t.Fatal(err)
122+
if (*got)["model"] != "deepseek-v4-flash" {
123+
t.Fatalf("body was modified: %v", *got)
78124
}
79-
resp.Body.Close()
125+
}
126+
127+
func TestThinkingTransportLeavesOtherPaths(t *testing.T) {
128+
srv, _, post := newCaptureServer()
129+
defer srv.Close()
130+
131+
client := newThinkingHTTPClient(thinkingParamForHost("https://dashscope.aliyuncs.com/compatible-mode/v1"))
132+
post(t, client, "/v1/models")
80133
}

‎internal/controller/ai_vision.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package controller
2121

2222
import (
23+
"bytes"
2324
"encoding/base64"
2425
"fmt"
2526
"strings"
@@ -41,6 +42,20 @@ var allowedImagePrefixes = []string{
4142
"https://",
4243
}
4344

45+
// hasImageMagicBytes reports whether raw starts with the signature of one of
46+
// the accepted raster formats (PNG/JPEG/WebP). A declared image MIME type plus
47+
// base64 integrity alone does not prove the payload is an image.
48+
func hasImageMagicBytes(raw []byte) bool {
49+
pngSig := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}
50+
if bytes.HasPrefix(raw, pngSig) {
51+
return true
52+
}
53+
if bytes.HasPrefix(raw, []byte{0xFF, 0xD8, 0xFF}) {
54+
return true
55+
}
56+
return len(raw) >= 12 && bytes.Equal(raw[:4], []byte("RIFF")) && bytes.Equal(raw[8:12], []byte("WEBP"))
57+
}
58+
4459
// ValidateAndPrepareImages validates attachments and wraps them together with
4560
// the text into a single MultiContent message. The returned message must be
4661
// used as-is (Content stays empty so it never conflicts with MultiContent).
@@ -74,6 +89,9 @@ func ValidateAndPrepareImages(text string, images []string) (*openai.ChatComplet
7489
if len(raw) > maxImageBytes {
7590
return nil, fmt.Errorf("image too large (max 4MB)")
7691
}
92+
if !hasImageMagicBytes(raw) {
93+
return nil, fmt.Errorf("image content is not a valid PNG/JPEG/WebP file")
94+
}
7795
}
7896
}
7997

0 commit comments

Comments
 (0)