Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate usernames upon creation #3018

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions internal/validator/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package validator // import "miniflux.app/v2/internal/validator"
import (
"slices"
"strings"
"unicode"

"miniflux.app/v2/internal/locale"
"miniflux.app/v2/internal/model"
Expand All @@ -22,6 +23,10 @@ func ValidateUserCreationWithPassword(store *storage.Storage, request *model.Use
return locale.NewLocalizedError("error.user_already_exists")
}

if err := validateUsername(request.Username); err != nil {
return err
}

if err := validatePassword(request.Password); err != nil {
return err
}
Expand Down Expand Up @@ -146,6 +151,23 @@ func validatePassword(password string) *locale.LocalizedError {
return nil
}

// validateUsername return an error if the `username` argument contains
// a character that isn't alphanumerical nor `_` and `-`.
func validateUsername(username string) *locale.LocalizedError {
if strings.ContainsFunc(username, func(r rune) bool {
if unicode.IsLetter(r) || unicode.IsNumber(r) {
return false
}
if r == '_' || r == '-' || r == '@' || r == '.' {
return false
}
return true
}) {
return locale.NewLocalizedError("error.invalid_username")
}
return nil
}

func validateTheme(theme string) *locale.LocalizedError {
themes := model.Themes()
if _, found := themes[theme]; !found {
Expand Down
28 changes: 27 additions & 1 deletion internal/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package validator // import "miniflux.app/v2/internal/validator"

import "testing"
import (
"testing"

"miniflux.app/v2/internal/locale"
)

func TestIsValidURL(t *testing.T) {
scenarios := map[string]bool{
Expand Down Expand Up @@ -77,3 +81,25 @@ func TestIsValidDomain(t *testing.T) {
}
}
}

func TestValidateUsername(t *testing.T) {
scenarios := map[string]*locale.LocalizedError{
"jvoisin": nil,
"j.voisin": nil,
"[email protected]": nil,
"invalid username": locale.NewLocalizedError("error.invalid_username"),
}

for username, expected := range scenarios {
result := validateUsername(username)
if expected == nil {
if result != nil {
t.Errorf(`got an unexpected error for %q instead of nil: %v`, username, result)
}
} else {
if result == nil {
t.Errorf(`expected an error, got nil.`)
}
}
}
}
Loading