users: don't allow users to choose reserved names.

Don't allow users to choose names that conflict with search syntax, like `any` or `none`, or names
that impersonate user levels, like `Admin`, `Moderator`, `Anonymous`, etc.
This commit is contained in:
evazion
2022-11-06 15:46:38 -06:00
parent 8bd60e41a1
commit c133866cb7
6 changed files with 25 additions and 9 deletions

View File

@@ -9,6 +9,13 @@
class UserNameValidator < ActiveModel::EachValidator
ALLOWED_PUNCTUATION = "_.-" # All other punctuation characters are forbidden
RESERVED_NAMES = [
"any", "none", # conflicts with `approver:any` search syntax
"new", "deactivate", "custom_style", # conflicts with user routes (/users/new, /users/deactivate, /users/custom_style)
"mod", "administrator", # mod impersonation
*User::Roles.map(&:to_s) # owner, admin, moderator, anonymous, banned, etc
]
def validate_each(rec, attr, name)
forbidden_characters = name.delete(ALLOWED_PUNCTUATION).chars.grep(/[[:punct:]]/).uniq
@@ -34,6 +41,8 @@ class UserNameValidator < ActiveModel::EachValidator
rec.errors.add(attr, "must contain only basic letters or numbers")
elsif name =~ /\Auser_\d+\z/i
rec.errors.add(attr, "can't be the same as a deleted user")
elsif name.downcase.in?(RESERVED_NAMES)
rec.errors.add(attr, "is a reserved name and can't be used")
elsif name =~ Regexp.union(Danbooru.config.user_name_blacklist)
rec.errors.add(attr, "is not allowed")
end