username.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package auth
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "unicode"
  7. "unicode/utf8"
  8. )
  9. /*
  10. Username rules
  11. A username names the user's home folder (files/users/<name>), appears in
  12. virtual paths, share links and the logins of the file servers. Every new
  13. account therefore gets a name that is safe as a folder name on Linux,
  14. macOS and Windows: letters and digits of any script, plus . - _ and @
  15. after the first character. Path separators, spaces, "..", control
  16. characters and the device names Windows reserves are refused.
  17. Only new accounts are checked (CreateUserAccount), so existing users with
  18. older names keep signing in.
  19. */
  20. // MaxUsernameLength is the longest username, in characters
  21. const MaxUsernameLength = 64
  22. // windowsDeviceNames cannot be used as a file or folder name on Windows, not
  23. // even with an extension (con.txt)
  24. var windowsDeviceNames = map[string]bool{
  25. "CON": true, "PRN": true, "AUX": true, "NUL": true,
  26. "COM1": true, "COM2": true, "COM3": true, "COM4": true, "COM5": true, "COM6": true, "COM7": true, "COM8": true, "COM9": true,
  27. "LPT1": true, "LPT2": true, "LPT3": true, "LPT4": true, "LPT5": true, "LPT6": true, "LPT7": true, "LPT8": true, "LPT9": true,
  28. }
  29. // ValidateUsername reports why a name cannot be used for a new account
  30. func ValidateUsername(name string) error {
  31. if name == "" {
  32. return errors.New("Username cannot be empty")
  33. }
  34. if !utf8.ValidString(name) {
  35. return errors.New("Username is not valid text")
  36. }
  37. if utf8.RuneCountInString(name) > MaxUsernameLength {
  38. return fmt.Errorf("Username is too long (at most %d characters)", MaxUsernameLength)
  39. }
  40. for i, r := range name {
  41. if unicode.IsLetter(r) || unicode.IsDigit(r) {
  42. continue
  43. }
  44. if i == 0 {
  45. return errors.New("A username must start with a letter or a number")
  46. }
  47. switch {
  48. case r == '.' || r == '-' || r == '_' || r == '@':
  49. continue
  50. case unicode.IsMark(r):
  51. //Combining marks of scripts such as Devanagari or Thai
  52. continue
  53. case unicode.IsControl(r):
  54. return errors.New("A username cannot contain control or invisible characters")
  55. case unicode.IsSpace(r):
  56. return errors.New("A username cannot contain spaces")
  57. case unicode.IsPrint(r):
  58. return fmt.Errorf("A username cannot contain %q", string(r))
  59. }
  60. return errors.New("A username cannot contain control or invisible characters")
  61. }
  62. if strings.Contains(name, "..") {
  63. return errors.New("A username cannot contain \"..\"")
  64. }
  65. base := strings.ToUpper(strings.SplitN(name, ".", 2)[0])
  66. if windowsDeviceNames[base] {
  67. return errors.New(name + " is reserved by Windows and cannot be used as a username")
  68. }
  69. return nil
  70. }