Skip to content

Commit

Permalink
Check ListValidatorBalances response length (#7583)
Browse files Browse the repository at this point in the history
* Check length

* Add regression test

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
terencechain and prylabs-bulldozer[bot] committed Oct 20, 2020
1 parent 78ca8c9 commit 544dac2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions beacon-chain/rpc/beacon/validators.go
Expand Up @@ -162,6 +162,10 @@ func (bs *Server) ListValidatorBalances(
}, nil
}

if end > len(res) || end < start {
return nil, status.Error(codes.OutOfRange, "Request exceeds response length")
}

return &ethpb.ValidatorBalances{
Epoch: requestedEpoch,
Balances: res[start:end],
Expand Down
27 changes: 27 additions & 0 deletions beacon-chain/rpc/beacon/validators_test.go
Expand Up @@ -372,6 +372,33 @@ func TestServer_ListValidatorBalances_Pagination_CustomPageSizes(t *testing.T) {
}
}

func TestServer_ListValidatorBalances_ResponseOutOfBound(t *testing.T) {
db, sc := dbTest.SetupDB(t)
ctx := context.Background()

count := 10
setupValidators(t, db, count)
headState, err := db.HeadState(context.Background())
require.NoError(t, err)
b := testutil.NewBeaconBlock()
gRoot, err := b.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, db.SaveGenesisBlockRoot(ctx, gRoot))
require.NoError(t, db.SaveState(ctx, headState, gRoot))

bs := &Server{
GenesisTimeFetcher: &mock.ChainService{},
StateGen: stategen.New(db, sc),
HeadFetcher: &mock.ChainService{
State: headState,
},
}

req := &ethpb.ListValidatorBalancesRequest{PageSize: 250, QueryFilter: &ethpb.ListValidatorBalancesRequest_Epoch{Epoch: 0}, PublicKeys: [][]byte{{'a'}}}
_, err = bs.ListValidatorBalances(context.Background(), req)
require.ErrorContains(t, "Request exceeds response length", err)
}

func TestServer_ListValidatorBalances_OutOfRange(t *testing.T) {
db, sc := dbTest.SetupDB(t)

Expand Down

0 comments on commit 544dac2

Please sign in to comment.