Skip to content

Commit

Permalink
fix: limit pagination to protect the node would not be Query DoS (#252)
Browse files Browse the repository at this point in the history
* fix: limit pagination to protect the node would not be Query DoS

* chore: fix lint

* chore: fix testcase
  • Loading branch information
j75689 authored Jul 24, 2023
1 parent ea98b41 commit 9b00f07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
6 changes: 5 additions & 1 deletion types/query/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func ParsePagination(pageReq *PageRequest) (page, limit int, err error) {

if limit < 0 {
return 1, 0, status.Error(codes.InvalidArgument, "limit must greater than 0")
} else if limit == 0 {
} else if limit > DefaultLimit || limit == 0 {
// limit to protect the node would not be Query DoS
limit = DefaultLimit
}

Expand Down Expand Up @@ -74,6 +75,9 @@ func Paginate(

// count total results when the limit is zero/not supplied
countTotal = true
} else if limit > DefaultLimit {
// limit to protect the node would not be Query DoS
limit = DefaultLimit
}

if len(key) != 0 {
Expand Down
13 changes: 11 additions & 2 deletions types/query/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,20 @@ func (s *paginationTestSuite) TestReversePagination() {
s.Require().NotNil(res1.Pagination.NextKey)

s.T().Log("verify paginate with custom limit and countTotal, Reverse false")
pageReq = &query.PageRequest{Limit: 150}
pageReq = &query.PageRequest{Limit: 100}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res1, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res1.Balances.Len(), 150)
s.Require().Equal(res1.Balances.Len(), 100)
s.Require().NotNil(res1.Pagination.NextKey)
s.Require().Equal(res1.Pagination.Total, uint64(0))

s.T().Log("verify paginate with custom limit and countTotal, Reverse false")
pageReq = &query.PageRequest{Limit: 50, Offset: 100}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res1, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res1.Balances.Len(), 50)
s.Require().NotNil(res1.Pagination.NextKey)
s.Require().Equal(res1.Pagination.Total, uint64(0))

Expand Down

0 comments on commit 9b00f07

Please sign in to comment.