Check ListValidatorBalances response length (#7583)

* Check length

* Add regression test

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
terence tsao 2020-10-20 13:45:38 -07:00 committed by GitHub
parent 78ca8c9265
commit 544dac298a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -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],

View File

@ -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)