Skip to content

Commit

Permalink
fix(claimsmanager): properly check index to avoid invalid data in Use…
Browse files Browse the repository at this point in the history
…rClaims,UserLastEpochClaims (#1234)

This change ensures that we correctly check for missing values in
keeper.(UserClaims, UserLastEpochClaims) because otherwise due
to the blind assumption that the key would always be well formed,
+1 and +1+1 were being added respectively which would mean that
the condition "idx >= 0" would ALWAYS pass and send over the wrong
data if the key happened to contain the address bytes.

Fixes #1217
Supersedes PR #1223
  • Loading branch information
odeke-em authored Mar 5, 2024
1 parent dd56067 commit e3864e0
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions x/claimsmanager/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,17 @@ func (k Keeper) UserClaims(c context.Context, q *types.QueryClaimsRequest) (*typ
addrBytes := []byte(q.Address)

k.IterateAllClaims(ctx, func(_ int64, key []byte, claim types.Claim) (stop bool) {
// The assumption is that IterateAllClaims returns non-empty keys.
// check for the presence of the addr bytes in the key.
// first prefix byte is 0x00; so cater for that! Then + 1 to skip the separator.
idx := bytes.Index(key[1:], []byte{0x00}) + 1 + 1
if idx >= 0 && bytes.Equal(key[idx:idx+len(addrBytes)], addrBytes) {
// first prefix byte is 0x00; so cater for that!
idx := bytes.Index(key[1:], []byte{0x00})
if idx < 0 {
return false
}

idx += 1 + 1 // add + 1 to skip the separator.

if bytes.Equal(key[idx:idx+len(addrBytes)], addrBytes) {
out = append(out, claim)
}
return false
Expand All @@ -86,9 +93,15 @@ func (k Keeper) UserLastEpochClaims(c context.Context, q *types.QueryClaimsReque
addrBytes := []byte(q.Address)
k.IterateAllLastEpochClaims(ctx, func(_ int64, key []byte, claim types.Claim) (stop bool) {
// check for the presence of the addr bytes in the key.
// First byte is 0x01 here, so no need to consider it; + 1 to skip the separator.
idx := bytes.Index(key, []byte{0x00}) + 1
if idx >= 0 && bytes.Equal(key[idx:idx+len(addrBytes)], addrBytes) {
idx := bytes.Index(key, []byte{0x00})
if idx < 0 {
return false
}

// First byte was 0x01, so no need to consider it; + 1 to skip the separator.
idx += 1

Check warning on line 102 in x/claimsmanager/keeper/grpc_query.go

View workflow job for this annotation

GitHub Actions / lint

increment-decrement: should replace idx += 1 with idx++ (revive)

if bytes.Equal(key[idx:idx+len(addrBytes)], addrBytes) {
out = append(out, claim)
}
return false
Expand Down

0 comments on commit e3864e0

Please sign in to comment.