x/ibc: update path validator to enforce no beginning or ending of the separator (#6233)
* update path validator to enforce no beginning or ending of the separator * modify test slightly
This commit is contained in:
parent
b2ad4d2a23
commit
86a9750508
|
@ -1,7 +1,6 @@
|
|||
package host
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
|
@ -85,15 +84,12 @@ func NewPathValidator(idValidator ValidateFn) ValidateFn {
|
|||
return sdkerrors.Wrapf(ErrInvalidPath, "path %s doesn't contain any separator '/'", path)
|
||||
}
|
||||
|
||||
allEmptyItems := true
|
||||
for _, p := range pathArr {
|
||||
// NOTE: for some reason an empty string is added after Split
|
||||
// a path beginning or ending in a separator returns empty string elements.
|
||||
if p == "" {
|
||||
continue
|
||||
return sdkerrors.Wrapf(ErrInvalidPath, "path %s cannot begin or end with '/'", path)
|
||||
}
|
||||
|
||||
allEmptyItems = false
|
||||
|
||||
if err := idValidator(p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -103,10 +99,6 @@ func NewPathValidator(idValidator ValidateFn) ValidateFn {
|
|||
}
|
||||
}
|
||||
|
||||
if allEmptyItems {
|
||||
return fmt.Errorf("path %s must contain at least one identifier", path)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
@ -119,23 +111,17 @@ func PathValidator(path string) error {
|
|||
return sdkerrors.Wrapf(ErrInvalidPath, "path %s doesn't contain any separator '/'", path)
|
||||
}
|
||||
|
||||
allEmptyItems := true
|
||||
for _, p := range pathArr {
|
||||
// NOTE: for some reason an empty string is added after Split
|
||||
// a path beginning or ending in a separator returns empty string elements.
|
||||
if p == "" {
|
||||
continue
|
||||
return sdkerrors.Wrapf(ErrInvalidPath, "path %s cannot begin or end with '/'", path)
|
||||
}
|
||||
|
||||
allEmptyItems = false
|
||||
|
||||
// Each path element must be a valid identifier or constant number
|
||||
if err := defaultIdentifierValidator(p, 1, 20); err != nil {
|
||||
return sdkerrors.Wrapf(err, "path %s contains an invalid identifier: '%s'", path, p)
|
||||
}
|
||||
}
|
||||
|
||||
if allEmptyItems {
|
||||
return fmt.Errorf("path %s must contain at least one identifier", path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -51,19 +51,24 @@ func TestDefaultIdentifierValidator(t *testing.T) {
|
|||
|
||||
func TestPathValidator(t *testing.T) {
|
||||
testCases := []testCase{
|
||||
{"valid lowercase", "/lowercaseid", true},
|
||||
{"numeric path", "/239123", true},
|
||||
{"valid id special chars", "/._+-#[]<>._+-#[]<>", true},
|
||||
{"valid lowercase", "p/lowercaseid", true},
|
||||
{"numeric path", "p/239123", true},
|
||||
{"valid id special chars", "p/._+-#[]<>._+-#[]<>", true},
|
||||
{"valid id lower and special chars", "lower/._+-#[]<>", true},
|
||||
{"id length out of range", "/l", true},
|
||||
{"uppercase id", "/NOTLOWERCASE", true},
|
||||
{"id length out of range", "p/l", true},
|
||||
{"uppercase id", "p/NOTLOWERCASE", true},
|
||||
{"invalid path", "lowercaseid", false},
|
||||
{"blank id", "/ ", false},
|
||||
{"id length out of range", "/123456789012345678901", false},
|
||||
{"invalid id", "/(clientid)", false},
|
||||
{"blank id", "p/ ", false},
|
||||
{"id length out of range", "p/123456789012345678901", false},
|
||||
{"invalid id", "p/(clientid)", false},
|
||||
{"empty string", "", false},
|
||||
{"separators only", "////", false},
|
||||
{"just separator", "/", false},
|
||||
{"begins with separator", "/id", false},
|
||||
{"blank before separator", " /id", false},
|
||||
{"ends with separator", "id/", false},
|
||||
{"blank after separator", "id/ ", false},
|
||||
{"blanks with separator", " / ", false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
|
@ -87,12 +92,14 @@ func TestCustomPathValidator(t *testing.T) {
|
|||
})
|
||||
|
||||
testCases := []testCase{
|
||||
{"valid custom path", "/id_client/id_one", true},
|
||||
{"valid custom path", "id_client/id_one", true},
|
||||
{"invalid path", "client", false},
|
||||
{"invalid custom path", "/client", false},
|
||||
{"invalid identifier", "/id_client/id_123456789012345678901", false},
|
||||
{"invalid custom path", "id_one/client", false},
|
||||
{"invalid identifier", "id_client/id_123456789012345678901", false},
|
||||
{"separators only", "////", false},
|
||||
{"just separator", "/", false},
|
||||
{"ends with separator", "id_client/id_one/", false},
|
||||
{"beings with separator", "/id_client/id_one", false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
|
|
Loading…
Reference in New Issue