IsValidPrincipalName checks if the given user/role name would be valid. Valid names must be valid UTF-8, containing at least one alphanumeric (except for the guest user), and no colons, commas, backticks, or slashes.
(name string)
| 132 | // IsValidPrincipalName checks if the given user/role name would be valid. Valid names must be valid UTF-8, containing |
| 133 | // at least one alphanumeric (except for the guest user), and no colons, commas, backticks, or slashes. |
| 134 | func IsValidPrincipalName(name string) bool { |
| 135 | namelen := len(name) |
| 136 | if namelen == 0 { |
| 137 | return true // guest user |
| 138 | } |
| 139 | if namelen > base.MaxPrincipalNameLen { |
| 140 | return false |
| 141 | } |
| 142 | if !utf8.ValidString(name) { |
| 143 | return false |
| 144 | } |
| 145 | seenAnAlphanum := false |
| 146 | for _, char := range name { |
| 147 | // Reasons for forbidding each of these: |
| 148 | // colons: basic authentication uses them to separate usernames from passwords |
| 149 | // commas: fails channels.IsValidChannel, which channels.compileAccessMap uses via SetFromArray |
| 150 | // slashes: would need to make many (possibly breaking) changes to routing |
| 151 | // backticks: MB-50619 |
| 152 | if char == '/' || char == ':' || char == ',' || char == '`' { |
| 153 | return false |
| 154 | } |
| 155 | if !seenAnAlphanum && (unicode.IsLetter(char) || unicode.IsNumber(char)) { |
| 156 | seenAnAlphanum = true |
| 157 | } |
| 158 | } |
| 159 | return seenAnAlphanum |
| 160 | } |
| 161 | |
| 162 | // ValidatePrincipalName performs the same checks as IsValidPrincipalName, but adds length check and returns a more |
| 163 | // verbose error message. This function is slower than IsValidPrincipalName, and should be used only for user |
no outgoing calls