From ceb9dec8e4004b325315a3146299c5f80175f18d Mon Sep 17 00:00:00 2001 From: erio Date: Sat, 7 Mar 2026 20:15:09 +0800 Subject: [PATCH] fix: reject non-UTF-8 request body in Sora handler Windows terminals may send request bodies in GBK encoding, causing garbled prompts and upstream failures. Validate UTF-8 encoding in readBody and return 400 for invalid encodings. Co-Authored-By: Claude Opus 4.6 --- backend/internal/handler/sora_videos_handler.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/backend/internal/handler/sora_videos_handler.go b/backend/internal/handler/sora_videos_handler.go index 56fc10937e..fff9041394 100644 --- a/backend/internal/handler/sora_videos_handler.go +++ b/backend/internal/handler/sora_videos_handler.go @@ -2,7 +2,9 @@ package handler import ( "encoding/json" + "fmt" "net/http" + "unicode/utf8" "github.com/Wei-Shaw/sub2api/internal/pkg/logger" middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware" @@ -353,7 +355,11 @@ func readBody(c *gin.Context) ([]byte, error) { } if len(body) == 0 { soraErrorResponse(c, http.StatusBadRequest, "invalid_request_error", "Request body is empty") - return nil, err + return nil, fmt.Errorf("empty body") + } + if !utf8.Valid(body) { + soraErrorResponse(c, http.StatusBadRequest, "invalid_request_error", "Request body must be valid UTF-8") + return nil, fmt.Errorf("invalid utf-8") } return body, nil }