enhance: allow '.' and '-' characters in usernames (#42417) (#42588)

related: #42417

- update the isValidUsername function to accept dots and hyphens in
addition to letters, digits, and underscores
- this change improves compatibility with common username formats and
addresses feedback in issue #42417

Signed-off-by: Jean-Francois Weber-Marx <jfwm@hotmail.com>
Signed-off-by: Jean-Francois Weber-Marx <jf.webermarx@criteo.com>
This commit is contained in:
Jean-Francois Weber-Marx 2025-07-24 01:54:54 +00:00 committed by GitHub
parent 990a25e51a
commit 1bd66b09e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 6 deletions

View File

@ -1168,8 +1168,8 @@ func ValidateUsername(username string) error {
usernameSize := len(username)
for i := 1; i < usernameSize; i++ {
c := username[i]
if c != '_' && !isAlpha(c) && !isNumber(c) {
return merr.WrapErrParameterInvalidMsg("invalid user name %s, username must contain only numbers, letters and underscores, but got %s", username, c)
if c != '_' && c != '-' && c != '.' && !isAlpha(c) && !isNumber(c) {
return merr.WrapErrParameterInvalidMsg("invalid user name %s, username must contain only numbers, letters, underscores, dots, and hyphens, but got %s", username, c)
}
}
return nil

View File

@ -791,11 +791,11 @@ func TestValidateUsername(t *testing.T) {
// length gt 32
res = ValidateUsername("aaaaaaaaaabbbbbbbbbbccccccccccddddd")
assert.Error(t, res)
// illegal character which not alphabet, _, or number
res = ValidateUsername("a1^7*).,")
// illegal character which not alphabet, _, ., ., or number
res = ValidateUsername("a1^7*),")
assert.Error(t, res)
// normal username that only contains alphabet, _, and number
res = ValidateUsername("a17_good")
// normal username that only contains alphabet, _, ., -, and number
res = ValidateUsername("a.17_good-")
assert.Nil(t, res)
}