Skip to content

Commit

Permalink
Merge pull request #137 from initia-labs/fix/validate-big-integer
Browse files Browse the repository at this point in the history
fix: validate bit integer
  • Loading branch information
traviolus authored Feb 5, 2025
2 parents ce82e16 + 344995e commit 19e87d2
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 6 deletions.
23 changes: 20 additions & 3 deletions common/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func ValidateTarLz4Header(dest string) error {
return nil
}

func ValidateBigInt(s string) error {
func ValidatePositiveBigIntOrZero(s string) error {
if s == "" {
return errors.New("empty string is not a valid integer")
}
Expand All @@ -389,16 +389,33 @@ func ValidateBigInt(s string) error {
return fmt.Errorf("failed to parse '%s' as integer", s)
}

// Check if number is negative
if n.Sign() < 0 {
return fmt.Errorf("'%s' is a negative integer", s)
}

// Check if number is within uint256 range (2^256 - 1)
maxUint256 := new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))
if n.Cmp(maxUint256) > 0 {
return fmt.Errorf("number exceeds maximum value (2^256 - 1)")
}

// For format validation
expected := n.String()
if s != expected {
return fmt.Errorf("invalid integer format: '%s'", s)
}

return nil
}

func ValidatePositiveBigInt(s string) error {
err := ValidateBigInt(s)
err := ValidatePositiveBigIntOrZero(s)
if err != nil {
return err
}

if s == "0" || s[0] == '-' {
if s == "0" {
return fmt.Errorf("'%s' is not a positive integer", s)
}

Expand Down
100 changes: 99 additions & 1 deletion common/validate_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package common

import "testing"
import (
"strings"
"testing"
)

func TestValidateURL(t *testing.T) {
failTests := []struct {
Expand Down Expand Up @@ -64,3 +67,98 @@ func TestValidateWSURL(t *testing.T) {
}
}
}

func TestValidatePositiveBigIntOrZero(t *testing.T) {
maxUint256 := strings.Repeat("9", 78) // approximate max uint256 value

tests := []struct {
name string
input string
wantErr bool
}{
{
name: "valid positive integer",
input: "123",
wantErr: false,
},
{
name: "zero",
input: "0",
wantErr: false,
},
{
name: "negative integer",
input: "-123",
wantErr: true,
},
{
name: "empty string",
input: "",
wantErr: true,
},
{
name: "decimal number",
input: "12.3",
wantErr: true,
},
{
name: "exceeds uint256",
input: maxUint256 + "0",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidatePositiveBigIntOrZero(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ValidatePositiveBigIntOrZero() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

func TestValidatePositiveBigInt(t *testing.T) {
maxUint256 := strings.Repeat("9", 78) // approximate max uint256 value

tests := []struct {
name string
input string
wantErr bool
}{
{
name: "valid positive integer",
input: "123",
wantErr: false,
},
{
name: "zero",
input: "0",
wantErr: true,
},
{
name: "negative integer",
input: "-123",
wantErr: true,
},
{
name: "empty string",
input: "",
wantErr: true,
},
{
name: "exceeds uint256",
input: maxUint256 + "0",
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidatePositiveBigInt(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ValidatePositiveBigInt() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
4 changes: 2 additions & 2 deletions models/relayer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ func NewFundManuallyL1BalanceInput(ctx context.Context) (*FundManuallyL1BalanceI
question: fmt.Sprintf("Specify the amount that would be transferred to Relayer account on L1 (%s)", l1GasDenom),
}
model.WithPlaceholder("Enter the amount (or 0 to skip)")
model.WithValidatorFn(common.ValidateBigInt)
model.WithValidatorFn(common.ValidatePositiveBigIntOrZero)
return model, nil
}

Expand Down Expand Up @@ -1456,7 +1456,7 @@ func NewFundManuallyL2BalanceInput(ctx context.Context) (*FundManuallyL2BalanceI
question: fmt.Sprintf("Specify the amount that would be transferred to Relayer account on rollup (%s)", l2GasDenom),
}
model.WithPlaceholder("Enter the amount (or 0 to skip)")
model.WithValidatorFn(common.ValidateBigInt)
model.WithValidatorFn(common.ValidatePositiveBigIntOrZero)
return model, nil
}

Expand Down

0 comments on commit 19e87d2

Please sign in to comment.