From 09231c2650f3687285c5300b8b4aa432ebab0b89 Mon Sep 17 00:00:00 2001 From: Bonny07 <111042029+Bonny07@users.noreply.github.com> Date: Sun, 31 May 2026 23:54:26 +0900 Subject: [PATCH] fix(service): validate request body on /translate and /v1/translate 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