diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml index ce7054a..85af044 100644 --- a/.github/workflows/golangci-lint.yaml +++ b/.github/workflows/golangci-lint.yaml @@ -9,7 +9,7 @@ on: permissions: contents: read # Optional: allow read access to pull request. Use with `only-new-issues` option. - # pull-requests: read + pull-requests: read jobs: golangci: name: lint @@ -32,7 +32,7 @@ jobs: # args: --issues-exit-code=0 # Optional: show only new issues if it's a pull request. The default value is `false`. - # only-new-issues: true + only-new-issues: true # Optional: if set to true then the all caching functionality will be complete disabled, # takes precedence over all other caching options. diff --git a/pkg/service/eth/block_id.go b/pkg/service/eth/block_id.go index 70e9a62..addd4bc 100644 --- a/pkg/service/eth/block_id.go +++ b/pkg/service/eth/block_id.go @@ -68,7 +68,7 @@ func NewBlockIdentifier(id string) (BlockIdentifier, error) { return newBlockIdentifier(BlockIDRoot, id), nil } - if _, err := strconv.ParseInt(id, 10, 64); err == nil { + if _, err := strconv.ParseUint(id, 10, 64); err == nil { return newBlockIdentifier(BlockIDSlot, id), nil } @@ -83,12 +83,12 @@ func newBlockIdentifier(id BlockIDType, value string) BlockIdentifier { } func NewSlotFromString(id string) (phase0.Slot, error) { - slot, err := strconv.ParseInt(id, 10, 64) + slot, err := strconv.ParseUint(id, 10, 64) if err != nil { return 0, err } - return phase0.Slot(uint64(slot)), nil //nolint:gosec // slot parsed from string is validated + return phase0.Slot(slot), nil } func NewRootFromString(id string) (phase0.Root, error) { diff --git a/pkg/service/eth/block_id_test.go b/pkg/service/eth/block_id_test.go index 986d767..4f7f993 100644 --- a/pkg/service/eth/block_id_test.go +++ b/pkg/service/eth/block_id_test.go @@ -30,3 +30,30 @@ func TestBlockIDMapping(t *testing.T) { }) } } + +func TestBlockIDRejectsNegativeSlot(t *testing.T) { + t.Parallel() + + for _, id := range []string{"-1", "-100"} { + t.Run(id, func(t *testing.T) { + t.Parallel() + + parsed, err := NewBlockIdentifier(id) + if err == nil { + t.Errorf("Expected error for %q, got type %d", id, parsed.Type()) + } + + if parsed.Type() != BlockIDInvalid { + t.Errorf("Expected BlockIDInvalid for %q, got %d", id, parsed.Type()) + } + }) + } +} + +func TestNewSlotFromStringRejectsNegative(t *testing.T) { + t.Parallel() + + if _, err := NewSlotFromString("-1"); err == nil { + t.Error("Expected error for negative slot, got nil") + } +} diff --git a/pkg/service/eth/state_id.go b/pkg/service/eth/state_id.go index fc4e9e1..b72dc0a 100644 --- a/pkg/service/eth/state_id.go +++ b/pkg/service/eth/state_id.go @@ -66,7 +66,7 @@ func NewStateIdentifier(id string) (StateIdentifier, error) { return newStateIdentifier(StateIDRoot, id), nil } - if _, err := strconv.ParseInt(id, 10, 64); err == nil { + if _, err := strconv.ParseUint(id, 10, 64); err == nil { return newStateIdentifier(StateIDSlot, id), nil }