Validate that FromBlock/ToBlock epoch is indeed a hex value (#10780)

* Validate that FromBlock/ToBlock epoch is indeed a hex value

* Adding tests
This commit is contained in:
Friðrik Ásmundsson 2023-05-09 17:17:23 -04:00 committed by GitHub
parent 5c26a3da1f
commit ceb3f1e41e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 2 deletions

View File

@ -548,12 +548,12 @@ func (h EthSubscriptionID) String() string {
} }
type EthFilterSpec struct { type EthFilterSpec struct {
// Interpreted as an epoch or one of "latest" for last mined block, "earliest" for first, // Interpreted as an epoch (in hex) or one of "latest" for last mined block, "earliest" for first,
// "pending" for not yet committed messages. // "pending" for not yet committed messages.
// Optional, default: "latest". // Optional, default: "latest".
FromBlock *string `json:"fromBlock,omitempty"` FromBlock *string `json:"fromBlock,omitempty"`
// Interpreted as an epoch or one of "latest" for last mined block, "earliest" for first, // Interpreted as an epoch (in hex) or one of "latest" for last mined block, "earliest" for first,
// "pending" for not yet committed messages. // "pending" for not yet committed messages.
// Optional, default: "latest". // Optional, default: "latest".
ToBlock *string `json:"toBlock,omitempty"` ToBlock *string `json:"toBlock,omitempty"`

View File

@ -425,6 +425,11 @@ func TestEthNewFilterDefaultSpec(t *testing.T) {
elogs, err := parseEthLogsFromFilterResult(res) elogs, err := parseEthLogsFromFilterResult(res)
require.NoError(err) require.NoError(err)
AssertEthLogs(t, elogs, expected, received) AssertEthLogs(t, elogs, expected, received)
// giving a number to fromBlock/toBlock needs to be in hex format, so make sure passing in decimal fails
blockNrInDecimal := "2812200"
_, err = client.EthNewFilter(ctx, &ethtypes.EthFilterSpec{FromBlock: &blockNrInDecimal, ToBlock: &blockNrInDecimal})
require.Error(err, "expected error when fromBlock/toBlock is not in hex format")
} }
func TestEthGetLogsBasic(t *testing.T) { func TestEthGetLogsBasic(t *testing.T) {

View File

@ -9,6 +9,7 @@ import (
"os" "os"
"sort" "sort"
"strconv" "strconv"
"strings"
"sync" "sync"
"time" "time"
@ -1180,6 +1181,9 @@ func (e *EthEvent) installEthFilterSpec(ctx context.Context, filterSpec *ethtype
} else if *filterSpec.FromBlock == "pending" { } else if *filterSpec.FromBlock == "pending" {
return nil, api.ErrNotSupported return nil, api.ErrNotSupported
} else { } else {
if !strings.HasPrefix(*filterSpec.FromBlock, "0x") {
return nil, xerrors.Errorf("FromBlock is not a hex")
}
epoch, err := ethtypes.EthUint64FromHex(*filterSpec.FromBlock) epoch, err := ethtypes.EthUint64FromHex(*filterSpec.FromBlock)
if err != nil { if err != nil {
return nil, xerrors.Errorf("invalid epoch") return nil, xerrors.Errorf("invalid epoch")
@ -1195,6 +1199,9 @@ func (e *EthEvent) installEthFilterSpec(ctx context.Context, filterSpec *ethtype
} else if *filterSpec.ToBlock == "pending" { } else if *filterSpec.ToBlock == "pending" {
return nil, api.ErrNotSupported return nil, api.ErrNotSupported
} else { } else {
if !strings.HasPrefix(*filterSpec.ToBlock, "0x") {
return nil, xerrors.Errorf("ToBlock is not a hex")
}
epoch, err := ethtypes.EthUint64FromHex(*filterSpec.ToBlock) epoch, err := ethtypes.EthUint64FromHex(*filterSpec.ToBlock)
if err != nil { if err != nil {
return nil, xerrors.Errorf("invalid epoch") return nil, xerrors.Errorf("invalid epoch")