Skip to content

Commit

Permalink
hardened node-api custom validator validation
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Oct 22, 2024
1 parent a659c95 commit 9ced281
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions mod/node-api/engines/echo/vaildator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import (
"errors"
"fmt"
"net/http"
"regexp"
"strconv"

"github.com/berachain/beacon-kit/mod/node-api/handlers/utils"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
)
Expand Down Expand Up @@ -130,11 +131,9 @@ func ValidateUint64(fl validator.FieldLevel) bool {
// validator identifier. It validates against a hex-encoded public key
// or a numeric validator index.
func ValidateValidatorID(fl validator.FieldLevel) bool {
valid, err := validateRegex(fl.Field().String(), `^0x[0-9a-fA-F]{1,96}$`)
if err != nil {
return false
}
if valid {
var key crypto.BLSPubkey
err := key.UnmarshalText([]byte(fl.Field().String()))
if err == nil {
return true
}
if ValidateUint64(fl) {
Expand All @@ -146,11 +145,8 @@ func ValidateValidatorID(fl validator.FieldLevel) bool {
// ValidateRoot checks if the provided field is a valid root.
// It validates against a 32 byte hex-encoded root with "0x" prefix.
func ValidateRoot(value string) bool {
valid, err := validateRegex(value, `^0x[0-9a-fA-F]{64}$`)
if err != nil {
return false
}
return valid
_, err := common.NewRootFromHex(value)
return err == nil
}

func ValidateValidatorStatus(fl validator.FieldLevel) bool {
Expand Down Expand Up @@ -179,17 +175,6 @@ func validateAllowedStrings(
return allowedValues[value]
}

func validateRegex(value string, hexPattern string) (bool, error) {
if value == "" {
return true, nil
}
matched, err := regexp.MatchString(hexPattern, value)
if err != nil {
return false, err
}
return matched, nil
}

func validateStateBlockIDs(value string, allowedValues map[string]bool) bool {
// Check if value is one of the allowed values
if validateAllowedStrings(value, allowedValues) {
Expand Down

0 comments on commit 9ced281

Please sign in to comment.