fix(images): reject oversized multipart uploads instead of truncating them

Multipart image uploads were read through io.LimitReader(part, 20MB), so any
image larger than 20MB was silently cut short. The OAuth path (and the Grok
media path) then base64-encoded the truncated bytes and sent them upstream,
which answered 400 invalid_image_file. The API key path forwards the raw
multipart body and was not affected, so the same request worked or failed
depending on which account served it.

- Raise the per-part limit to 50MB, OpenAI's documented per-image limit for
  GPT image edits, and return 400 when a part exceeds it.
- Read Grok media parts whole; the body is already bounded by the gateway
  request body limit.
- Log each upload's content type and size on the images routing line.
This commit is contained in:
SipengXie2024 committed 2026-10-26 03:10:46 +00:00
1 parent ba81c7fd75
commit cde6ddfd8f
4 files changed
+80 -4

No files matched your search

+3 -1
View File
@@ -231,7 +231,9 @@ func parseGrokMediaMultipartRequest(contentType string, body []byte, info *GrokM
_ = part.Close()
continue
}
data, err := io.ReadAll(io.LimitReader(part, openAIImageMaxUploadPartSize))
// The body is already bounded by the request body limit; truncating a part
// here would forward a corrupt image upstream.
data, err := io.ReadAll(part)
_ = part.Close()
if err != nil {
return
+7 -2
View File
@@ -39,7 +39,7 @@ const (
openAIChatGPTFilesURL = "https://chatgpt.com/backend-api/files"
openAIImageBackendUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
openAIImageMaxDownloadBytes = 20 << 20 // 20MB per image download
openAIImageMaxUploadPartSize = 20 << 20 // 20MB per multipart upload part
openAIImageMaxUploadPartSize = 50 << 20 // OpenAI's documented per-image limit for GPT image edits
openAIImagesResponsesMainModel = "gpt-5.6-luna"
openAIImagesVerbatimPromptInstructions = "When invoking the image_generation tool, use the user's image prompt verbatim. Do not rewrite, expand, summarize, embellish, translate, normalize punctuation, or add or remove visual details or constraints. Preserve the original language, wording, capitalization, quotes, and punctuation exactly."
)
@@ -352,11 +352,16 @@ func parseOpenAIImagesMultipartRequest(body []byte, contentType string, req *Ope
continue
}
data, err := io.ReadAll(io.LimitReader(part, openAIImageMaxUploadPartSize))
// Read one byte past the limit so an oversized part is rejected instead of
// being cut short and forwarded upstream as a corrupt image.
data, err := io.ReadAll(io.LimitReader(part, openAIImageMaxUploadPartSize+1))
_ = part.Close()
if err != nil {
return fmt.Errorf("read multipart field %s: %w", name, err)
}
if len(data) > openAIImageMaxUploadPartSize {
return fmt.Errorf("multipart field %q exceeds the %d MB per-part limit", name, openAIImageMaxUploadPartSize>>20)
}
fileName := strings.TrimSpace(part.FileName())
if fileName != "" {
@@ -1806,13 +1806,18 @@ func (s *OpenAIGatewayService) forwardOpenAIImagesOAuth(
direct := usesCodexDirectImages(upstreamModel) && !isOpenAIImagesForceResponses(ctx)
beginUpstreamResponseModelObservation(c)
SetOpsUpstreamModel(c, upstreamModel)
uploadParts := make([]string, 0, len(parsed.Uploads))
for _, upload := range parsed.Uploads {
uploadParts = append(uploadParts, fmt.Sprintf("%s:%d", upload.ContentType, len(upload.Data)))
}
logger.LegacyPrintf(
"service.openai_gateway",
"[OpenAI] Images request routing request_model=%s endpoint=%s account_type=%s uploads=%d",
"[OpenAI] Images request routing request_model=%s endpoint=%s account_type=%s uploads=%d upload_parts=%v",
requestModel,
parsed.Endpoint,
account.Type,
len(parsed.Uploads),
uploadParts,
)
upstreamCtx, releaseUpstreamCtx := detachUpstreamContext(ctx)
defer releaseUpstreamCtx()
@@ -0,0 +1,64 @@
package service
import (
"bytes"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/require"
)
func buildImagesEditMultipartBody(t *testing.T, imageSize int) ([]byte, string, []byte) {
t.Helper()
image := bytes.Repeat([]byte{0xAB}, imageSize)
var body bytes.Buffer
writer := multipart.NewWriter(&body)
require.NoError(t, writer.WriteField("model", "gpt-image-2"))
require.NoError(t, writer.WriteField("prompt", "replace background"))
part, err := writer.CreateFormFile("image", "source.png")
require.NoError(t, err)
_, err = part.Write(image)
require.NoError(t, err)
require.NoError(t, writer.Close())
return body.Bytes(), writer.FormDataContentType(), image
}
func parseImagesEditMultipart(t *testing.T, body []byte, contentType string) (*OpenAIImagesRequest, error) {
t.Helper()
gin.SetMode(gin.TestMode)
req := httptest.NewRequest(http.MethodPost, "/v1/images/edits", bytes.NewReader(body))
req.Header.Set("Content-Type", contentType)
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request = req
svc := &OpenAIGatewayService{}
return svc.ParseOpenAIImagesRequest(c, body)
}
func TestParseOpenAIImagesRequest_MultipartKeepsUploadAbove20MB(t *testing.T) {
body, contentType, image := buildImagesEditMultipartBody(t, 21<<20)
parsed, err := parseImagesEditMultipart(t, body, contentType)
require.NoError(t, err)
require.Len(t, parsed.Uploads, 1)
require.Equal(t, len(image), len(parsed.Uploads[0].Data))
require.True(t, bytes.Equal(image, parsed.Uploads[0].Data))
}
func TestParseOpenAIImagesRequest_MultipartRejectsUploadAbovePartLimit(t *testing.T) {
body, contentType, _ := buildImagesEditMultipartBody(t, openAIImageMaxUploadPartSize+1)
parsed, err := parseImagesEditMultipart(t, body, contentType)
require.Nil(t, parsed)
require.ErrorContains(t, err, `multipart field "image" exceeds the 50 MB per-part limit`)
}
func TestParseGrokMediaRequest_MultipartKeepsUploadAbove20MB(t *testing.T) {
body, contentType, image := buildImagesEditMultipartBody(t, 21<<20)
info := ParseGrokMediaRequest(contentType, body)
require.Len(t, info.Uploads, 1)
require.Equal(t, len(image), len(info.Uploads[0].Data))
}