MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / IsValidPrincipalName

Function IsValidPrincipalName

auth/role.go:134–160  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
134func 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

Callers 11

createUserSessionMethod · 0.92
TestFunkyUsernamesFunction · 0.92
TestFunkyRoleNamesFunction · 0.92
validateAccessMapFunction · 0.92
validateRoleAccessMapFunction · 0.92
validateMethod · 0.85
TestOIDCUsernameFunction · 0.85
validateMethod · 0.85

Calls

no outgoing calls

Tested by 6

TestFunkyUsernamesFunction · 0.74
TestFunkyRoleNamesFunction · 0.74
TestOIDCUsernameFunction · 0.68