Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: limit pagination to protect the node would not be Query DoS #252

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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