From e4de98aef98e6e7e10e878883881bbb81287224d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Garamv=C3=B6lgyi?= Date: Wed, 21 Oct 2026 15:00:03 +0200 Subject: [PATCH 1/2] fix(bridge-history-api): limit request size and format --- .../internal/controller/api/txs_by_hashes.go | 12 ++++++++++++ bridge-history-api/internal/types/schema.go | 4 ++-- common/version/version.go | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/bridge-history-api/internal/controller/api/txs_by_hashes.go b/bridge-history-api/internal/controller/api/txs_by_hashes.go index 98440036a7..c633be979d 100644 --- a/bridge-history-api/internal/controller/api/txs_by_hashes.go +++ b/bridge-history-api/internal/controller/api/txs_by_hashes.go @@ -1,6 +1,8 @@ package api import ( + "net/http" + "github.com/gin-gonic/gin" "github.com/go-redis/redis/v8" "gorm.io/gorm" @@ -9,6 +11,9 @@ import ( "scroll-tech/bridge-history-api/internal/types" ) +// maxRequestBodyBytes fits 100 tx hashes with ample headroom. +const maxRequestBodyBytes = 64 * 1024 + // TxsByHashesController the controller of PostQueryTxsByHashes type TxsByHashesController struct { historyLogic *logic.HistoryLogic @@ -23,6 +28,13 @@ func NewTxsByHashesController(db *gorm.DB, redisClient *redis.Client) *TxsByHash // PostQueryTxsByHashes query the txs by hashes func (c *TxsByHashesController) PostQueryTxsByHashes(ctx *gin.Context) { + // Limit request body size + if ctx.Request.ContentLength > maxRequestBodyBytes { + types.RenderFailure(ctx, types.ErrParameterInvalidNo, &http.MaxBytesError{Limit: maxRequestBodyBytes}) + return + } + ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, maxRequestBodyBytes) + var req types.QueryByHashRequest if err := ctx.ShouldBindJSON(&req); err != nil { types.RenderFailure(ctx, types.ErrParameterInvalidNo, err) diff --git a/bridge-history-api/internal/types/schema.go b/bridge-history-api/internal/types/schema.go index 22930aad04..78cbfd7011 100644 --- a/bridge-history-api/internal/types/schema.go +++ b/bridge-history-api/internal/types/schema.go @@ -25,14 +25,14 @@ const ( // QueryByAddressRequest the request parameter of address api type QueryByAddressRequest struct { - Address string `form:"address" binding:"required"` + Address string `form:"address" binding:"required,eth_addr"` Page uint64 `form:"page" binding:"required,min=1"` PageSize uint64 `form:"page_size" binding:"required,min=1,max=100"` } // QueryByHashRequest the request parameter of hash api type QueryByHashRequest struct { - Txs []string `json:"txs" binding:"required,min=1,max=100"` + Txs []string `json:"txs" binding:"required,min=1,max=100,dive,len=66,startswith=0x,hexadecimal"` } // ResultData contains return txs and total diff --git a/common/version/version.go b/common/version/version.go index c9e0acca8a..1e78468a39 100644 --- a/common/version/version.go +++ b/common/version/version.go @@ -5,7 +5,7 @@ import ( "runtime/debug" ) -var tag = "v4.8.0" +var tag = "v4.8.1" var commit = func() string { if info, ok := debug.ReadBuildInfo(); ok { From fa8a16c53fe2e6cd07b8a704df1e6c183138886f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Garamv=C3=B6lgyi?= Date: Wed, 21 Oct 2026 15:24:28 +0200 Subject: [PATCH 2/2] make sure size limit is applied even for chunked requests --- .../internal/controller/api/txs_by_hashes.go | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/bridge-history-api/internal/controller/api/txs_by_hashes.go b/bridge-history-api/internal/controller/api/txs_by_hashes.go index c633be979d..480edf3e6f 100644 --- a/bridge-history-api/internal/controller/api/txs_by_hashes.go +++ b/bridge-history-api/internal/controller/api/txs_by_hashes.go @@ -1,9 +1,11 @@ package api import ( + "io" "net/http" "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" "github.com/go-redis/redis/v8" "gorm.io/gorm" @@ -28,15 +30,14 @@ func NewTxsByHashesController(db *gorm.DB, redisClient *redis.Client) *TxsByHash // PostQueryTxsByHashes query the txs by hashes func (c *TxsByHashesController) PostQueryTxsByHashes(ctx *gin.Context) { - // Limit request body size - if ctx.Request.ContentLength > maxRequestBodyBytes { - types.RenderFailure(ctx, types.ErrParameterInvalidNo, &http.MaxBytesError{Limit: maxRequestBodyBytes}) + body, err := readBodyWithSizeLimit(ctx, maxRequestBodyBytes) + if err != nil { + types.RenderFailure(ctx, types.ErrParameterInvalidNo, err) return } - ctx.Request.Body = http.MaxBytesReader(ctx.Writer, ctx.Request.Body, maxRequestBodyBytes) var req types.QueryByHashRequest - if err := ctx.ShouldBindJSON(&req); err != nil { + if err := binding.JSON.BindBody(body, &req); err != nil { types.RenderFailure(ctx, types.ErrParameterInvalidNo, err) return } @@ -50,3 +51,12 @@ func (c *TxsByHashesController) PostQueryTxsByHashes(ctx *gin.Context) { resultData := &types.ResultData{Results: results, Total: uint64(len(results))} types.RenderSuccess(ctx, resultData) } + +func readBodyWithSizeLimit(ctx *gin.Context, limit int64) ([]byte, error) { + // Reject early if the declared size is too large. + if ctx.Request.ContentLength > limit { + return nil, &http.MaxBytesError{Limit: limit} + } + // Read to EOF to catch trailing data past the limit. + return io.ReadAll(http.MaxBytesReader(ctx.Writer, ctx.Request.Body, limit)) +}