username_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package auth
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "net/url"
  6. "strings"
  7. "testing"
  8. )
  9. func TestValidateUsername(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. input string
  13. wantErr string
  14. }{
  15. {name: "plain", input: "alice"},
  16. {name: "digits and symbols", input: "bob_2.smith-jr"},
  17. {name: "email style", input: "carol@example.com"},
  18. {name: "starts with a digit", input: "2fa"},
  19. {name: "chinese", input: "陳大文"},
  20. {name: "japanese", input: "やまだ"},
  21. {name: "hindi with combining marks", input: "प्रिया"},
  22. {name: "longest allowed", input: strings.Repeat("a", MaxUsernameLength)},
  23. {name: "empty", input: "", wantErr: "empty"},
  24. {name: "too long", input: strings.Repeat("a", MaxUsernameLength+1), wantErr: "too long"},
  25. {name: "slash", input: "a/b", wantErr: `"/"`},
  26. {name: "backslash", input: `a\b`, wantErr: `"\\"`},
  27. {name: "parent folder", input: "..", wantErr: "start with"},
  28. {name: "dots inside", input: "a..b", wantErr: `".."`},
  29. {name: "leading dot", input: ".hidden", wantErr: "start with"},
  30. {name: "leading dash", input: "-rf", wantErr: "start with"},
  31. {name: "space", input: "john doe", wantErr: "spaces"},
  32. {name: "colon", input: "user:x", wantErr: `":"`},
  33. {name: "control character", input: "a\nb", wantErr: "control"},
  34. {name: "zero width space", input: "a\u200bb", wantErr: "control"},
  35. {name: "windows device", input: "con", wantErr: "reserved by Windows"},
  36. {name: "windows device with extension", input: "LPT1.backup", wantErr: "reserved by Windows"},
  37. {name: "device name inside a longer name", input: "console"},
  38. {name: "invalid utf8", input: "a\xffb", wantErr: "not valid text"},
  39. }
  40. for _, tt := range tests {
  41. t.Run(tt.name, func(t *testing.T) {
  42. err := ValidateUsername(tt.input)
  43. if tt.wantErr == "" {
  44. if err != nil {
  45. t.Fatalf("ValidateUsername(%q) unexpected error: %v", tt.input, err)
  46. }
  47. return
  48. }
  49. if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
  50. t.Fatalf("ValidateUsername(%q) error = %v, want it to contain %q", tt.input, err, tt.wantErr)
  51. }
  52. })
  53. }
  54. }
  55. // Every way of creating an account goes through CreateUserAccount, so a bad
  56. // name never reaches the database
  57. func TestCreateUserAccountRefusesUnsafeNames(t *testing.T) {
  58. a := newHashOnlyAgent(t)
  59. for _, name := range []string{"../admin", "a/b", "nul", " alice"} {
  60. if err := a.CreateUserAccount(name, "password", []string{"user"}); err == nil {
  61. t.Errorf("CreateUserAccount(%q) accepted an unsafe name", name)
  62. }
  63. if a.UserExists(name) {
  64. t.Errorf("CreateUserAccount(%q) stored the account anyway", name)
  65. }
  66. }
  67. if err := a.CreateUserAccount("alice", "password", []string{"user"}); err != nil {
  68. t.Fatalf("CreateUserAccount(alice): %v", err)
  69. }
  70. if !a.UserExists("alice") {
  71. t.Error("alice was not stored")
  72. }
  73. }
  74. // The register endpoint passes the reason on to the page
  75. func TestHandleRegisterRefusesUnsafeNames(t *testing.T) {
  76. a := newTestAuthAgent(t, map[string]bool{"root": true})
  77. if err := a.CreateUserAccount("root", "rootpass", []string{"administrator"}); err != nil {
  78. t.Fatalf("CreateUserAccount(root): %v", err)
  79. }
  80. cookies := sessionCookies(t, a, "root")
  81. body := postRegister(a, cookies, "../root2", "secret", "user")
  82. if !strings.Contains(body, "start with a letter") || a.UserExists("../root2") {
  83. t.Errorf("register reply %s, stored %v", body, a.UserExists("../root2"))
  84. }
  85. }
  86. // The batch import reports the names it skipped instead of failing silently
  87. func TestCSVImportReportsInvalidNames(t *testing.T) {
  88. a := newTestAuthAgent(t, nil)
  89. form := url.Values{"csv": {"dave,pass1234,user\n../evil,pass1234,user\nerin smith,pass1234,user"}}
  90. req := httptest.NewRequest(http.MethodPost, "/system/auth/csvimport", strings.NewReader(form.Encode()))
  91. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  92. rec := httptest.NewRecorder()
  93. a.HandleCreateUserAccountsFromCSV(rec, req)
  94. body := rec.Body.String()
  95. if !strings.Contains(body, "../evil skipped") || !strings.Contains(body, "erin smith skipped") {
  96. t.Errorf("reply %s should name both skipped users", body)
  97. }
  98. if !a.UserExists("dave") || a.UserExists("../evil") || a.UserExists("erin smith") {
  99. t.Errorf("wrong accounts stored: dave=%v evil=%v erin=%v", a.UserExists("dave"), a.UserExists("../evil"), a.UserExists("erin smith"))
  100. }
  101. }