Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules/core/02-client/types: fix clientID validation regex to conform closer to spec #2270

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/core/02-client/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func FormatClientIdentifier(clientType string, sequence uint64) string {

// IsClientIDFormat checks if a clientID is in the format required on the SDK for
// parsing client identifiers. The client identifier must be in the form: `{client-type}-{N}
var IsClientIDFormat = regexp.MustCompile(`^.*[^\n-]-[0-9]{1,20}$`).MatchString
var IsClientIDFormat = regexp.MustCompile(`^\w+([\w-]+\w)?-[0-9]{1,20}$`).MatchString
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you document what this regex is doing in the godocs? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated it in the PR #2510, thank you for the request @AdityaSripal!


// IsValidClientID checks if the clientID is valid and can be parsed into the client
// identifier format.
Expand Down
35 changes: 22 additions & 13 deletions modules/core/02-client/types/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,31 @@ func TestParseClientIdentifier(t *testing.T) {
{"negative sequence", "tendermint--1", "tendermint", 0, false},
{"invalid format", "tendermint-tm", "tendermint", 0, false},
{"empty clientype", " -100", "tendermint", 0, false},
{"with in the middle tabs", "a\t\t\t-100", "tendermint", 0, false},
{"leading tabs", "\t\t\ta-100", "tendermint", 0, false},
{"with whitespace", " a-100", "tendermint", 0, false},
{"leading hyphens", "-----a-100", "tendermint", 0, false},
{"with slash", "tendermint/-100", "tendermint", 0, false},
{"non-ASCII:: emoji", "🚨😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎😎-100", "tendermint", 0, false},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotta love this test case :)

{"non-ASCII:: others", "世界-100", "tendermint", 0, false},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
clientType, seq, err := types.ParseClientIdentifier(tc.clientID)
valid := types.IsValidClientID(tc.clientID)
require.Equal(t, tc.expSeq, seq, tc.clientID)

clientType, seq, err := types.ParseClientIdentifier(tc.clientID)
valid := types.IsValidClientID(tc.clientID)
require.Equal(t, tc.expSeq, seq, tc.clientID)

if tc.expPass {
require.NoError(t, err, tc.name)
require.True(t, valid)
require.Equal(t, tc.clientType, clientType)
} else {
require.Error(t, err, tc.name, tc.clientID)
require.False(t, valid)
require.Equal(t, "", clientType)
}
if tc.expPass {
require.NoError(t, err, tc.name)
require.True(t, valid)
require.Equal(t, tc.clientType, clientType)
} else {
require.Error(t, err, tc.name, tc.clientID)
require.False(t, valid)
require.Equal(t, "", clientType)
}
})
}
}