Skip to content

Commit 31d0c81

Browse files
Merge pull request #1082 from wunter8/fix-username-regex
Fix username regex
2 parents d37f861 + 4111bee commit 31d0c81

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

server/errors.go

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ var (
122122
errHTTPBadRequestTemplateInvalid = &errHTTP{40043, http.StatusBadRequest, "invalid request: could not parse template", "https://ntfy.sh/docs/publish/#message-templating", nil}
123123
errHTTPBadRequestTemplateDisallowedFunctionCalls = &errHTTP{40044, http.StatusBadRequest, "invalid request: template contains disallowed function calls, e.g. template, call, or define", "https://ntfy.sh/docs/publish/#message-templating", nil}
124124
errHTTPBadRequestTemplateExecuteFailed = &errHTTP{40045, http.StatusBadRequest, "invalid request: template execution failed", "https://ntfy.sh/docs/publish/#message-templating", nil}
125+
errHTTPBadRequestInvalidUsername = &errHTTP{40046, http.StatusBadRequest, "invalid request: invalid username", "", nil}
125126
errHTTPNotFound = &errHTTP{40401, http.StatusNotFound, "page not found", "", nil}
126127
errHTTPUnauthorized = &errHTTP{40101, http.StatusUnauthorized, "unauthorized", "https://ntfy.sh/docs/publish/#authentication", nil}
127128
errHTTPForbidden = &errHTTP{40301, http.StatusForbidden, "forbidden", "https://ntfy.sh/docs/publish/#authentication", nil}

server/server_account.go

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package server
22

33
import (
44
"encoding/json"
5+
"errors"
56
"heckel.io/ntfy/v2/log"
67
"heckel.io/ntfy/v2/user"
78
"heckel.io/ntfy/v2/util"
@@ -37,6 +38,9 @@ func (s *Server) handleAccountCreate(w http.ResponseWriter, r *http.Request, v *
3738
}
3839
logvr(v, r).Tag(tagAccount).Field("user_name", newAccount.Username).Info("Creating user %s", newAccount.Username)
3940
if err := s.userManager.AddUser(newAccount.Username, newAccount.Password, user.RoleUser); err != nil {
41+
if errors.Is(err, user.ErrInvalidArgument) {
42+
return errHTTPBadRequestInvalidUsername
43+
}
4044
return err
4145
}
4246
v.AccountCreated()

user/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ const (
241241
)
242242

243243
var (
244-
allowedUsernameRegex = regexp.MustCompile(`^[-_.@a-zA-Z0-9]+$`) // Does not include Everyone (*)
244+
allowedUsernameRegex = regexp.MustCompile(`^[-_.+@a-zA-Z0-9]+$`) // Does not include Everyone (*)
245245
allowedTopicRegex = regexp.MustCompile(`^[-_A-Za-z0-9]{1,64}$`) // No '*'
246246
allowedTopicPatternRegex = regexp.MustCompile(`^[-_*A-Za-z0-9]{1,64}$`) // Adds '*' for wildcards!
247247
allowedTierRegex = regexp.MustCompile(`^[-_A-Za-z0-9]{1,64}$`)

user/types_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,15 @@ func TestTierContext(t *testing.T) {
6161
require.Equal(t, "price_456", context["stripe_yearly_price_id"])
6262

6363
}
64+
65+
func TestUsernameRegex(t *testing.T) {
66+
username := "phil"
67+
usernameEmail := "[email protected]"
68+
usernameEmailAlias := "[email protected]"
69+
usernameInvalid := "phil\rocks"
70+
71+
require.True(t, AllowedUsername(username))
72+
require.True(t, AllowedUsername(usernameEmail))
73+
require.True(t, AllowedUsername(usernameEmailAlias))
74+
require.False(t, AllowedUsername(usernameInvalid))
75+
}

0 commit comments

Comments
 (0)