fix for username length spam

This commit is contained in:
Kyle Drake 2017-05-23 12:39:11 -07:00
parent 4d397df015
commit 0212167b3e

View file

@ -137,8 +137,9 @@ class Site < Sequel::Model
DISPOSABLE_EMAIL_BLACKLIST_PATH = File.join(DIR_ROOT, 'files', 'disposable_email_blacklist.conf') DISPOSABLE_EMAIL_BLACKLIST_PATH = File.join(DIR_ROOT, 'files', 'disposable_email_blacklist.conf')
BLOCK_JERK_THRESHOLD = 2 BLOCK_JERK_THRESHOLD = 2
MAXIMUM_TAGS = 5 MAXIMUM_TAGS = 5
MAX_USERNAME_LENGTH = 25.freeze
MAX_USERNAME_LENGTH_CUTOFF = Time.parse('May 22, 2017')
def self.newsletter_sites def self.newsletter_sites
Site.select(:email). Site.select(:email).
@ -565,6 +566,10 @@ class Site < Sequel::Model
!username.empty? && username.match(/^[a-zA-Z0-9_\-]+$/i) !username.empty? && username.match(/^[a-zA-Z0-9_\-]+$/i)
end end
def username_too_long?
(new? || (created_at && created_at > MAX_USERNAME_LENGTH_CUTOFF)) && values[:username].length > MAX_USERNAME_LENGTH
end
def self.disposable_email_domains def self.disposable_email_domains
File.readlines(DISPOSABLE_EMAIL_BLACKLIST_PATH).collect {|d| d.strip} File.readlines(DISPOSABLE_EMAIL_BLACKLIST_PATH).collect {|d| d.strip}
end end
@ -890,6 +895,10 @@ class Site < Sequel::Model
errors.add :username, 'Usernames can only contain letters, numbers, underscores and hyphens.' errors.add :username, 'Usernames can only contain letters, numbers, underscores and hyphens.'
end end
if username_too_long?
errors.add :username, "Username length cannot be greater than #{MAX_USERNAME_LENGTH} characters."
end
if new? && !values[:username].nil? && !values[:username].empty? if new? && !values[:username].nil? && !values[:username].empty?
# TODO regex fails for usernames <= 2 chars, tempfix for now. # TODO regex fails for usernames <= 2 chars, tempfix for now.
if new? && values[:username].nil? || (values[:username].length > 2 && !values[:username].match(VALID_HOSTNAME)) if new? && values[:username].nil? || (values[:username].length > 2 && !values[:username].match(VALID_HOSTNAME))