From d19a3902df9ff0a0abb0211ee2de5599c16d829a Mon Sep 17 00:00:00 2001 From: Bonny Lin <111042029+Bonny07@users.noreply.github.com> Date: Tue, 2 Jun 2026 15:32:22 +0900 Subject: [PATCH] fix(service): validate request body on /translate and /v1/translate (#225) The two handlers ignored the error from c.BindJSON, so a malformed JSON body was silently processed as an empty payload. Check the error and return 400, consistent with /v2/translate. --- service/service.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/service/service.go b/service/service.go index e45b088..9d0456f 100644 --- a/service/service.go +++ b/service/service.go @@ -108,7 +108,13 @@ func Router(cfg *Config) *gin.Engine { // Free API endpoint, No Pro Account required r.POST("/translate", authMiddleware(cfg), func(c *gin.Context) { req := PayloadFree{} - c.BindJSON(&req) + if err := c.BindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "message": "Invalid request payload", + }) + return + } sourceLang := req.SourceLang targetLang := req.TargetLang @@ -156,7 +162,13 @@ func Router(cfg *Config) *gin.Engine { // Pro API endpoint, Pro Account required r.POST("/v1/translate", authMiddleware(cfg), func(c *gin.Context) { req := PayloadFree{} - c.BindJSON(&req) + if err := c.BindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "message": "Invalid request payload", + }) + return + } sourceLang := req.SourceLang targetLang := req.TargetLang