2020-02-19 01:56:22 -08:00
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
2020-05-15 10:24:31 -07:00
|
|
|
"regexp"
|
2020-02-19 01:56:22 -08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
)
|
|
|
|
|
2020-07-22 03:20:41 -07:00
|
|
|
// DefaultMaxCharacterLength defines the default maximum character length used
|
|
|
|
// in validation of identifiers including the client, connection, port and
|
|
|
|
// channel identifiers.
|
|
|
|
//
|
|
|
|
// NOTE: this restriction is specific to this golang implementation of IBC. If
|
|
|
|
// your use case demands a higher limit, please open an issue and we will consider
|
|
|
|
// adjusting this restriction.
|
|
|
|
const DefaultMaxCharacterLength = 64
|
|
|
|
|
2020-05-15 10:24:31 -07:00
|
|
|
// IsValidID defines regular expression to check if the string consist of
|
|
|
|
// characters in one of the following categories only:
|
|
|
|
// - Alphanumeric
|
|
|
|
// - `.`, `_`, `+`, `-`, `#`
|
|
|
|
// - `[`, `]`, `<`, `>`
|
|
|
|
var IsValidID = regexp.MustCompile(`^[a-zA-Z0-9\.\_\+\-\#\[\]\<\>]+$`).MatchString
|
|
|
|
|
2020-02-19 01:56:22 -08:00
|
|
|
// ICS 024 Identifier and Path Validation Implementation
|
|
|
|
//
|
|
|
|
// This file defines ValidateFn to validate identifier and path strings
|
|
|
|
// The spec for ICS 024 can be located here:
|
|
|
|
// https://github.com/cosmos/ics/tree/master/spec/ics-024-host-requirements
|
|
|
|
|
|
|
|
// ValidateFn function type to validate path and identifier bytestrings
|
|
|
|
type ValidateFn func(string) error
|
|
|
|
|
2020-05-02 12:26:59 -07:00
|
|
|
func defaultIdentifierValidator(id string, min, max int) error { //nolint:unparam
|
2020-05-15 10:24:31 -07:00
|
|
|
if strings.TrimSpace(id) == "" {
|
|
|
|
return sdkerrors.Wrap(ErrInvalidID, "identifier cannot be blank")
|
|
|
|
}
|
2020-02-19 01:56:22 -08:00
|
|
|
// valid id MUST NOT contain "/" separator
|
|
|
|
if strings.Contains(id, "/") {
|
|
|
|
return sdkerrors.Wrapf(ErrInvalidID, "identifier %s cannot contain separator '/'", id)
|
|
|
|
}
|
2020-07-22 03:20:41 -07:00
|
|
|
// valid id must fit the length requirements
|
2020-02-19 01:56:22 -08:00
|
|
|
if len(id) < min || len(id) > max {
|
|
|
|
return sdkerrors.Wrapf(ErrInvalidID, "identifier %s has invalid length: %d, must be between %d-%d characters", id, len(id), min, max)
|
|
|
|
}
|
|
|
|
// valid id must contain only lower alphabetic characters
|
2020-05-15 10:24:31 -07:00
|
|
|
if !IsValidID(id) {
|
|
|
|
return sdkerrors.Wrapf(
|
|
|
|
ErrInvalidID,
|
|
|
|
"identifier %s must contain only alphanumeric or the following characters: '.', '_', '+', '-', '#', '[', ']', '<', '>'",
|
|
|
|
id,
|
|
|
|
)
|
2020-02-19 01:56:22 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-14 15:47:11 -07:00
|
|
|
// ClientIdentifierValidator is the default validator function for Client identifiers.
|
2020-11-13 02:33:33 -08:00
|
|
|
// A valid Identifier must be between 9-64 characters and only contain alphanumeric and some allowed
|
|
|
|
// special characters (see IsValidID).
|
2020-05-14 15:47:11 -07:00
|
|
|
func ClientIdentifierValidator(id string) error {
|
2020-07-22 03:20:41 -07:00
|
|
|
return defaultIdentifierValidator(id, 9, DefaultMaxCharacterLength)
|
2020-02-19 01:56:22 -08:00
|
|
|
}
|
|
|
|
|
2020-05-14 15:47:11 -07:00
|
|
|
// ConnectionIdentifierValidator is the default validator function for Connection identifiers.
|
2020-11-13 02:33:33 -08:00
|
|
|
// A valid Identifier must be between 10-64 characters and only contain alphanumeric and some allowed
|
|
|
|
// special characters (see IsValidID).
|
2020-05-14 15:47:11 -07:00
|
|
|
func ConnectionIdentifierValidator(id string) error {
|
2020-07-22 03:20:41 -07:00
|
|
|
return defaultIdentifierValidator(id, 10, DefaultMaxCharacterLength)
|
2020-02-19 01:56:22 -08:00
|
|
|
}
|
|
|
|
|
2020-05-14 15:47:11 -07:00
|
|
|
// ChannelIdentifierValidator is the default validator function for Channel identifiers.
|
2020-11-24 07:28:20 -08:00
|
|
|
// A valid Identifier must be between 8-64 characters and only contain alphanumeric and some allowed
|
2020-11-13 02:33:33 -08:00
|
|
|
// special characters (see IsValidID).
|
2020-05-14 15:47:11 -07:00
|
|
|
func ChannelIdentifierValidator(id string) error {
|
2020-11-24 07:28:20 -08:00
|
|
|
return defaultIdentifierValidator(id, 8, DefaultMaxCharacterLength)
|
2020-02-19 01:56:22 -08:00
|
|
|
}
|
|
|
|
|
2020-05-14 15:47:11 -07:00
|
|
|
// PortIdentifierValidator is the default validator function for Port identifiers.
|
2020-11-13 02:33:33 -08:00
|
|
|
// A valid Identifier must be between 2-64 characters and only contain alphanumeric and some allowed
|
|
|
|
// special characters (see IsValidID).
|
2020-05-14 15:47:11 -07:00
|
|
|
func PortIdentifierValidator(id string) error {
|
2020-07-22 03:20:41 -07:00
|
|
|
return defaultIdentifierValidator(id, 2, DefaultMaxCharacterLength)
|
2020-02-19 01:56:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPathValidator takes in a Identifier Validator function and returns
|
2020-11-13 02:33:33 -08:00
|
|
|
// a Path Validator function which requires path to consist of `/`-separated valid identifiers,
|
|
|
|
// where a valid identifier is between 1-64 characters, contains only alphanumeric and some allowed
|
|
|
|
// special characters (see IsValidID), and satisfies the custom `idValidator` function.
|
2020-02-19 01:56:22 -08:00
|
|
|
func NewPathValidator(idValidator ValidateFn) ValidateFn {
|
|
|
|
return func(path string) error {
|
|
|
|
pathArr := strings.Split(path, "/")
|
2020-05-15 10:24:31 -07:00
|
|
|
if len(pathArr) > 0 && pathArr[0] == path {
|
|
|
|
return sdkerrors.Wrapf(ErrInvalidPath, "path %s doesn't contain any separator '/'", path)
|
|
|
|
}
|
|
|
|
|
2020-02-19 01:56:22 -08:00
|
|
|
for _, p := range pathArr {
|
2020-05-15 11:58:43 -07:00
|
|
|
// a path beginning or ending in a separator returns empty string elements.
|
2020-05-15 10:24:31 -07:00
|
|
|
if p == "" {
|
2020-05-15 11:58:43 -07:00
|
|
|
return sdkerrors.Wrapf(ErrInvalidPath, "path %s cannot begin or end with '/'", path)
|
2020-05-15 10:24:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := idValidator(p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Each path element must either be a valid identifier or constant number
|
2020-07-22 03:20:41 -07:00
|
|
|
if err := defaultIdentifierValidator(p, 1, DefaultMaxCharacterLength); err != nil {
|
2020-05-15 10:24:31 -07:00
|
|
|
return sdkerrors.Wrapf(err, "path %s contains an invalid identifier: '%s'", path, p)
|
2020-02-19 01:56:22 -08:00
|
|
|
}
|
|
|
|
}
|
2020-05-15 10:24:31 -07:00
|
|
|
|
2020-02-19 01:56:22 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|