Skip to content

Commit

Permalink
return error when input limit exceeds the max allowed value
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony committed Jun 21, 2024
1 parent c2f6257 commit a9c8682
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
12 changes: 11 additions & 1 deletion api/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func New(repo *chain.Repository, db *logdb.LogDB, logsLimit uint64) *Events {
// Filter query events with option
func (e *Events) filter(ctx context.Context, ef *EventFilter) ([]*FilteredEvent, error) {
chain := e.repo.NewBestChain()
filter, err := convertEventFilter(chain, ef, e.limit)
filter, err := convertEventFilter(chain, ef)
if err != nil {
return nil, err
}
Expand All @@ -53,6 +53,16 @@ func (e *Events) handleFilter(w http.ResponseWriter, req *http.Request) error {
if err := utils.ParseJSON(req.Body, &filter); err != nil {
return utils.BadRequest(errors.WithMessage(err, "body"))
}
if filter.Options != nil && filter.Options.Limit > e.limit {
return utils.Forbidden(errors.New("options.limit exceeds the maximum allowed value"))
}
if filter.Options == nil {
filter.Options = &logdb.Options{
Offset: 0,
Limit: e.limit,
}
}

fes, err := e.filter(req.Context(), &filter)
if err != nil {
return err
Expand Down
6 changes: 0 additions & 6 deletions api/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,3 @@ func newReceipt() *tx.Receipt {
},
}
}

func TestNormalize(t *testing.T) {
assert.Equal(t, &logdb.Options{Offset: 0, Limit: 10}, events.NormalizeOptions(nil, 10))
assert.Equal(t, &logdb.Options{Offset: 10, Limit: 5}, events.NormalizeOptions(&logdb.Options{Offset: 10, Limit: 5}, 10))
assert.Equal(t, &logdb.Options{Offset: 10, Limit: 10}, events.NormalizeOptions(&logdb.Options{Offset: 10, Limit: 15}, 10))
}
18 changes: 2 additions & 16 deletions api/events/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ type EventFilter struct {
Order logdb.Order `json:"order"`
}

func convertEventFilter(chain *chain.Chain, filter *EventFilter, logsLimit uint64) (*logdb.EventFilter, error) {
func convertEventFilter(chain *chain.Chain, filter *EventFilter) (*logdb.EventFilter, error) {
rng, err := ConvertRange(chain, filter.Range)
if err != nil {
return nil, err
}
f := &logdb.EventFilter{
Range: rng,
Options: NormalizeOptions(filter.Options, logsLimit),
Options: filter.Options,
Order: filter.Order,
}
if len(filter.CriteriaSet) > 0 {
Expand Down Expand Up @@ -187,17 +187,3 @@ func ConvertRange(chain *chain.Chain, r *Range) (*logdb.Range, error) {
To: uint32(r.To),
}, nil
}

func NormalizeOptions(ops *logdb.Options, defaultLimit uint64) *logdb.Options {
if ops == nil {
return &logdb.Options{
Offset: 0,
Limit: defaultLimit,
}
}

if ops.Limit > defaultLimit {
ops.Limit = defaultLimit
}
return ops
}
12 changes: 11 additions & 1 deletion api/transfers/transfers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (t *Transfers) filter(ctx context.Context, filter *TransferFilter) ([]*Filt
transfers, err := t.db.FilterTransfers(ctx, &logdb.TransferFilter{
CriteriaSet: filter.CriteriaSet,
Range: rng,
Options: events.NormalizeOptions(filter.Options, t.limit),
Options: filter.Options,
Order: filter.Order,
})
if err != nil {
Expand All @@ -59,6 +59,16 @@ func (t *Transfers) handleFilterTransferLogs(w http.ResponseWriter, req *http.Re
if err := utils.ParseJSON(req.Body, &filter); err != nil {
return utils.BadRequest(errors.WithMessage(err, "body"))
}
if filter.Options != nil && filter.Options.Limit > t.limit {
return utils.Forbidden(errors.New("options.limit exceeds the maximum allowed value"))
}
if filter.Options == nil {
filter.Options = &logdb.Options{
Offset: 0,
Limit: t.limit,
}
}

tLogs, err := t.filter(req.Context(), &filter)
if err != nil {
return err
Expand Down

0 comments on commit a9c8682

Please sign in to comment.