Browse Source

rpc: return err if page is incorrect (less than 0 or greater than tot… (#3825)

* rpc: return err if page is incorrect (less than 0 or greater than total pages)

Fixes #3813

* fix rpc_test
pull/3926/head
Anton Kaliaev 5 years ago
committed by Jack Zampolin
parent
commit
e89991c445
4 changed files with 43 additions and 26 deletions
  1. +2
    -0
      CHANGELOG_PENDING.md
  2. +13
    -7
      rpc/core/pipe.go
  3. +24
    -18
      rpc/core/pipe_test.go
  4. +4
    -1
      rpc/core/tx.go

+ 2
- 0
CHANGELOG_PENDING.md View File

@ -27,3 +27,5 @@ program](https://hackerone.com/tendermint).
- [mempool] \#3826 Make `max_msg_bytes` configurable
### BUG FIXES:
- [rpc] \#3813 Return err if page is incorrect (less than 0 or greater than total pages)

+ 13
- 7
rpc/core/pipe.go View File

@ -1,6 +1,7 @@
package core
import (
"fmt"
"time"
cfg "github.com/tendermint/tendermint/config"
@ -145,19 +146,24 @@ func SetConfig(c cfg.RPCConfig) {
config = c
}
func validatePage(page, perPage, totalCount int) int {
func validatePage(page, perPage, totalCount int) (int, error) {
if perPage < 1 {
return 1
panic(fmt.Sprintf("zero or negative perPage: %d", perPage))
}
if page == 0 {
return 1, nil // default
}
pages := ((totalCount - 1) / perPage) + 1
if page < 1 {
page = 1
} else if page > pages {
page = pages
if pages == 0 {
pages = 1 // one page (even if it's empty)
}
if page < 0 || page > pages {
return 1, fmt.Errorf("page should be within [0, %d] range, given %d", pages, page)
}
return page
return page, nil
}
func validatePerPage(perPage int) int {


+ 24
- 18
rpc/core/pipe_test.go View File

@ -14,33 +14,39 @@ func TestPaginationPage(t *testing.T) {
perPage int
page int
newPage int
expErr bool
}{
{0, 0, 1, 1},
{0, 10, 1, 1, false},
{0, 10, 0, 1},
{0, 10, 1, 1},
{0, 10, 2, 1},
{0, 10, 0, 1, false},
{0, 10, 1, 1, false},
{0, 10, 2, 0, true},
{5, 10, -1, 1},
{5, 10, 0, 1},
{5, 10, 1, 1},
{5, 10, 2, 1},
{5, 10, 2, 1},
{5, 10, -1, 0, true},
{5, 10, 0, 1, false},
{5, 10, 1, 1, false},
{5, 10, 2, 0, true},
{5, 10, 2, 0, true},
{5, 5, 1, 1},
{5, 5, 2, 1},
{5, 5, 3, 1},
{5, 5, 1, 1, false},
{5, 5, 2, 0, true},
{5, 5, 3, 0, true},
{5, 3, 2, 2},
{5, 3, 3, 2},
{5, 3, 2, 2, false},
{5, 3, 3, 0, true},
{5, 2, 2, 2},
{5, 2, 3, 3},
{5, 2, 4, 3},
{5, 2, 2, 2, false},
{5, 2, 3, 3, false},
{5, 2, 4, 0, true},
}
for _, c := range cases {
p := validatePage(c.page, c.perPage, c.totalCount)
p, err := validatePage(c.page, c.perPage, c.totalCount)
if c.expErr {
assert.Error(t, err)
continue
}
assert.Equal(t, c.newPage, p, fmt.Sprintf("%v", c))
}


+ 4
- 1
rpc/core/tx.go View File

@ -202,7 +202,10 @@ func TxSearch(ctx *rpctypes.Context, query string, prove bool, page, perPage int
totalCount := len(results)
perPage = validatePerPage(perPage)
page = validatePage(page, perPage, totalCount)
page, err = validatePage(page, perPage, totalCount)
if err != nil {
return nil, err
}
skipCount := validateSkipCount(page, perPage)
apiResults := make([]*ctypes.ResultTx, cmn.MinInt(perPage, totalCount-skipCount))


Loading…
Cancel
Save