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..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,7 +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" @@ -9,6 +13,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,8 +30,14 @@ func NewTxsByHashesController(db *gorm.DB, redisClient *redis.Client) *TxsByHash // PostQueryTxsByHashes query the txs by hashes func (c *TxsByHashesController) PostQueryTxsByHashes(ctx *gin.Context) { + body, err := readBodyWithSizeLimit(ctx, maxRequestBodyBytes) + if err != nil { + types.RenderFailure(ctx, types.ErrParameterInvalidNo, err) + return + } + 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 } @@ -38,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)) +} 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 {