Skip to content

A platform for community discussion. Free, open, simple.

FIX: Apply watched words validation to usernames

Jake Goldsborougha3c292a9b97c
diff --git a/app/models/discourse_connect.rb b/app/models/discourse_connect.rb
index 3796c0de7a9..29b55068de1 100644
--- a/app/models/discourse_connect.rb
+++ b/app/models/discourse_connect.rb
@@ -127,6 +127,8 @@ class DiscourseConnect < DiscourseConnectBase
sso_record = user.single_sign_on_record
end
+ user.skip_username_watched_words_validation = true
+
# ensure it's not staged anymore
user.unstage!
@@ -265,6 +267,7 @@ class DiscourseConnect < DiscourseConnectBase
end
user = User.new(user_params)
+ user.skip_username_watched_words_validation = true
if SiteSetting.must_approve_users && EmailValidator.can_auto_approve_user?(email)
ReviewableUser.set_approved_fields!(user, Discourse.system_user)
@@ -424,7 +427,7 @@ class DiscourseConnect < DiscourseConnectBase
suggester_input = [username]
suggester_input << name if SiteSetting.use_name_for_username_suggestions
suggester_input << email if SiteSetting.use_email_for_username_and_name_suggestions
- UserNameSuggester.suggest(*suggester_input)
+ UserNameSuggester.suggest(*suggester_input, skip_watched_words: true)
end
def resolve_name
diff --git a/app/models/group.rb b/app/models/group.rb
index b68b5da0eb2..c894e20e8ef 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -561,8 +561,11 @@ class Group < ActiveRecord::Base
end
def self.can_use_name?(name, group)
- UsernameValidator.new(name, skip_length_validation: group.automatic).valid_format? &&
- (group.name == name || !User.username_exists?(name))
+ UsernameValidator.new(
+ name,
+ skip_length_validation: group.automatic,
+ skip_watched_words: true,
+ ).valid_format? && (group.name == name || !User.username_exists?(name))
end
def self.refresh_automatic_group!(name)
@@ -1114,7 +1117,12 @@ class Group < ActiveRecord::Base
self.name = stripped
end
- UsernameValidator.perform_validation(self, "name", skip_length_validation: automatic) ||
+ UsernameValidator.perform_validation(
+ self,
+ "name",
+ skip_length_validation: automatic,
+ skip_watched_words: true,
+ ) ||
begin
normalized_name = User.normalize_username(name)
diff --git a/app/models/user.rb b/app/models/user.rb
index c42c8e0b862..2a71ddc6685 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -255,6 +255,9 @@ class User < ActiveRecord::Base
attr_accessor :skip_email_validation
attr_accessor :enforce_username_restrictions
+ # DiscourseConnect is authoritative for usernames, so watched words must not block SSO users.
+ attr_accessor :skip_username_watched_words_validation
+
# Whether we need to be sending a system message after creation
attr_accessor :send_welcome_message
@@ -1515,7 +1518,11 @@ class User < ActiveRecord::Base
end
def username_format_validator
- UsernameValidator.perform_validation(self, "username")
+ UsernameValidator.perform_validation(
+ self,
+ "username",
+ skip_watched_words: skip_username_watched_words_validation,
+ )
end
def email_confirmed?
diff --git a/app/models/username_validator.rb b/app/models/username_validator.rb
index 72645a94a2c..b7fc5f7f225 100644
--- a/app/models/username_validator.rb
+++ b/app/models/username_validator.rb
@@ -28,9 +28,12 @@ class UsernameValidator
end
end
- def initialize(username, skip_length_validation: false, object: nil)
+ WATCHED_WORD_ACTIONS = %i[block censor require_approval flag silence].freeze
+
+ def initialize(username, skip_length_validation: false, skip_watched_words: false, object: nil)
@username = username&.unicode_normalize
@skip_length_validation = skip_length_validation
+ @skip_watched_words = skip_watched_words
@object = object
@errors = []
end
@@ -39,6 +42,7 @@ class UsernameValidator
attr_reader :username
attr_reader :object
attr_reader :skip_length_validation
+ attr_reader :skip_watched_words
def valid_format?
username_present?
@@ -50,6 +54,7 @@ class UsernameValidator
username_last_char_valid?
username_no_double_special?
username_does_not_end_with_confusing_suffix?
+ username_not_watched_word? if !skip_watched_words
username_plugin_validation
errors.empty?
end
@@ -164,6 +169,19 @@ class UsernameValidator
end
end
+ def username_not_watched_word?
+ return unless errors.empty?
+
+ matches = WordWatcher.new(username).word_matches_across_actions(WATCHED_WORD_ACTIONS)
+ return if matches.blank?
+
+ errors << I18n.t(
+ :"user.username.contains_blocked_words",
+ count: matches.size,
+ words: matches.join(", "),
+ )
+ end
+
def username_plugin_validation
return unless errors.empty?
diff --git a/app/services/username_changer.rb b/app/services/username_changer.rb
index 0c6c119cda1..20553c01216 100644
--- a/app/services/username_changer.rb
+++ b/app/services/username_changer.rb
@@ -18,7 +18,12 @@ class UsernameChanger
UsernameChanger.change(user, new_username, user)
true
elsif user.username != UserNameSuggester.fix_username(new_username)
- suggested_username = UserNameSuggester.suggest(new_username, current_username: user.username)
+ suggested_username =
+ UserNameSuggester.suggest(
+ new_username,
+ current_username: user.username,
+ skip_watched_words: user.skip_username_watched_words_validation,
+ )
UsernameChanger.change(user, suggested_username, user)
true
else
diff --git a/app/services/word_watcher.rb b/app/services/word_watcher.rb
index 9364097f120..f683812816f 100644
--- a/app/services/word_watcher.rb
+++ b/app/services/word_watcher.rb
@@ -242,10 +242,8 @@ class WordWatcher
match_list
end
- def word_matches_across_all_actions
- WatchedWord
- .actions
- .keys
+ def word_matches_across_actions(actions)
+ actions
.filter_map { |action| word_matches_for_action?(action, all_matches: true) }
.flatten
.compact
@@ -253,6 +251,10 @@ class WordWatcher
.sort
end
+ def word_matches_across_all_actions
+ word_matches_across_actions(WatchedWord.actions.keys)
+ end
+
def word_matches?(word, case_sensitive: false)
options = case_sensitive ? nil : Regexp::IGNORECASE
Regexp.new(WordWatcher.word_to_regexp(word), options).match?(@raw)
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index cacaf53b017..7b91045817b 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -3708,6 +3708,9 @@ en:
must_end_with_alphanumeric: "must end with a letter or a number"
must_not_contain_two_special_chars_in_seq: "must not contain a sequence of 2 or more special chars (.-_)"
must_not_end_with_confusing_suffix: "must not end with a confusing suffix like .json or .png etc."
+ contains_blocked_words:
+ one: "contains the blocked word '%{words}'"
+ other: "contains blocked words: %{words}"
email:
blank: "can't be blank."
invalid: "is invalid."
diff --git a/lib/user_name_suggester.rb b/lib/user_name_suggester.rb
index c3cebeb40a1..1c32f9e338e 100644
--- a/lib/user_name_suggester.rb
+++ b/lib/user_name_suggester.rb
@@ -4,7 +4,12 @@ module UserNameSuggester
GENERIC_NAMES = %w[i me info support admin webmaster hello mail office contact team]
LAST_RESORT_USERNAME = "user"
- def self.suggest(*input, current_username: nil, allow_generic_fallback: true)
+ def self.suggest(
+ *input,
+ current_username: nil,
+ allow_generic_fallback: true,
+ skip_watched_words: false
+ )
name =
input.find do |item|
if !SiteSetting.use_email_for_username_and_name_suggestions
@@ -16,7 +21,7 @@ module UserNameSuggester
name = fix_username(name, allow_generic_fallback:)
return nil if name.blank?
- find_available_username_based_on(name, current_username)
+ find_available_username_based_on(name, current_username, skip_watched_words:)
end
def self.parse_name_from_email(name_or_email)
@@ -30,9 +35,13 @@ module UserNameSuggester
name
end
- def self.find_available_username_based_on(name, current_username = nil)
+ def self.find_available_username_based_on(name, current_username = nil, skip_watched_words: false)
offset = nil
i = 1
+ valid_and_available = ->(candidate) do
+ UsernameValidator.new(candidate, skip_watched_words:).valid_format? &&
+ User.username_available?(candidate)
+ end
attempt = name
normalized_attempt = User.normalize_username(attempt)
@@ -40,7 +49,7 @@ module UserNameSuggester
original_allowed_username = current_username
current_username = User.normalize_username(current_username) if current_username
- until normalized_attempt == current_username || User.username_available?(attempt) || i > 100
+ until normalized_attempt == current_username || valid_and_available.call(attempt) || i > 100
if offset.nil?
normalized = User.normalize_username(name)
similar = "#{normalized}(0|1|2|3|4|5|6|7|8|9)+"
@@ -89,16 +98,18 @@ module UserNameSuggester
i += 1
end
- until normalized_attempt == current_username || User.username_available?(attempt) || i > 200
- attempt = SecureRandom.hex[1..SiteSetting.max_username_length]
+ until normalized_attempt == current_username || valid_and_available.call(attempt) || i > 200
+ attempt = rightsize_username(SecureRandom.hex)
normalized_attempt = User.normalize_username(attempt)
i += 1
end
if current_username == normalized_attempt
original_allowed_username
- else
+ elsif valid_and_available.call(attempt)
attempt
+ else
+ raise Discourse::InvalidParameters, "Unable to suggest a valid username"
end
end
diff --git a/spec/lib/email/receiver_spec.rb b/spec/lib/email/receiver_spec.rb
index d0388eaa0ea..f699fdfb50d 100644
--- a/spec/lib/email/receiver_spec.rb
+++ b/spec/lib/email/receiver_spec.rb
@@ -179,6 +179,21 @@ RSpec.describe Email::Receiver do
describe "new message to a group" do
fab!(:group) { Fabricate(:group, incoming_email: "[email protected]|[email protected]") }
+ it "delivers email when the sender's name matches a watched word" do
+ SiteSetting.enable_staged_users = true
+ Fabricate(:watched_word, word: "*bad*", action: WatchedWord.actions[:block])
+ mail = Mail.read_from_string(email(:encoded_display_name))
+ mail.from = "Badger <[email protected]>"
+
+ post = Email::Receiver.new(mail.to_s).process!
+
+ expect(post).to be_persisted
+ expect(post.topic.allowed_groups).to include(group)
+ expect(post.user).to be_staged
+ expect(post.user.email).to eq("[email protected]")
+ expect(UsernameValidator.new(post.user.username).valid_format?).to eq(true)
+ end
+
it "handles encoded display names" do
expect { process(:encoded_display_name) }.to change(Topic, :count)
diff --git a/spec/lib/user_name_suggester_spec.rb b/spec/lib/user_name_suggester_spec.rb
index fd79e663a8e..63aa8f8d83f 100644
--- a/spec/lib/user_name_suggester_spec.rb
+++ b/spec/lib/user_name_suggester_spec.rb
@@ -12,6 +12,48 @@ RSpec.describe UserNameSuggester do
let(:fallback_username) { I18n.t("fallback_username") + "1" }
+ it "suggests a valid username when the input matches a watched word" do
+ Fabricate(:watched_word, word: "*bad*", action: WatchedWord.actions[:block])
+
+ suggestion = described_class.suggest("Badger")
+
+ expect(UsernameValidator.new(suggestion).valid_format?).to eq(true)
+ expect(User.username_available?(suggestion)).to eq(true)
+ end
+
+ it "checks watched words on numbered candidates" do
+ Fabricate(:user, username: "sam")
+ Fabricate(:watched_word, word: "sam1", action: WatchedWord.actions[:block])
+
+ expect(described_class.suggest("sam")).to eq("sam2")
+ end
+
+ it "uses a valid random name when named candidates are blocked" do
+ Fabricate(:watched_word, word: "*bad*", action: WatchedWord.actions[:block])
+ Fabricate(:watched_word, word: "*user*", action: WatchedWord.actions[:block])
+
+ suggestion = described_class.suggest("Badger")
+
+ expect(UsernameValidator.new(suggestion).valid_format?).to eq(true)
+ expect(User.username_available?(suggestion)).to eq(true)
+ end
+
+ it "fails explicitly when watched words block every candidate" do
+ Fabricate(:watched_word, word: "*", action: WatchedWord.actions[:block])
+
+ expect { described_class.suggest("Badger") }.to raise_error(
+ Discourse::InvalidParameters,
+ "Unable to suggest a valid username",
+ )
+ end
+
+ it "preserves an existing username that now matches a watched word" do
+ Fabricate(:user, username: "Badger")
+ Fabricate(:watched_word, word: "*bad*", action: WatchedWord.actions[:block])
+
+ expect(described_class.suggest("badger", current_username: "Badger")).to eq("Badger")
+ end
+
it "keeps adding numbers to the username" do
Fabricate(:user, username: "sam")
Fabricate(:user, username: "sAm1")
diff --git a/spec/models/discourse_connect_spec.rb b/spec/models/discourse_connect_spec.rb
index fdd3623944d..c1273c894b5 100644
--- a/spec/models/discourse_connect_spec.rb
+++ b/spec/models/discourse_connect_spec.rb
@@ -119,6 +119,45 @@ RSpec.describe DiscourseConnect do
expect(user.persisted?).to eq(true)
end
+ it "allows users with watched-word usernames to be created and log in" do
+ WatchedWord.create!(word: "blockedword", action: WatchedWord.actions[:block])
+
+ sso = new_discourse_sso
+ sso.username = "blockedword"
+ sso.name = ""
+ sso.email = "[email protected]"
+ sso.external_id = "C"
+ sso.suppress_welcome_message = true
+
+ user = sso.lookup_or_create_user(ip_address)
+ returning_user = sso.lookup_or_create_user(ip_address)
+
+ expect(user).to be_persisted
+ expect(user.username).to eq("blockedword")
+ expect(returning_user).to eq(user)
+ end
+
+ it "preserves an authoritative watched-word username across repeated overrides" do
+ SiteSetting.auth_overrides_username = true
+ user = Fabricate(:user, username: "alice")
+ Fabricate(:watched_word, word: "*bad*", action: WatchedWord.actions[:block])
+
+ sso = new_discourse_sso
+ sso.username = "Badger"
+ sso.email = user.email
+ sso.external_id = "watched-word-override"
+ sso.suppress_welcome_message = true
+
+ usernames =
+ 2.times.map do
+ returning_user = sso.lookup_or_create_user(ip_address)
+ expect(returning_user.id).to eq(user.id)
+ user.reload.username
+ end
+
+ expect(usernames).to eq(%w[Badger Badger])
+ end
+
it "always creates new users when using plus addressing" do
SiteSetting.stubs(:normalize_emails).returns(true)
diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb
index fa6337efb36..aed82727656 100644
--- a/spec/models/group_spec.rb
+++ b/spec/models/group_spec.rb
@@ -145,6 +145,17 @@ RSpec.describe Group do
describe "validation" do
let(:group) { build(:group) }
+ it "allows creating and renaming groups whose names match watched words" do
+ Fabricate(:watched_word, word: "*bad*", action: WatchedWord.actions[:block])
+ group.name = "badgers"
+
+ group.save!
+ group.update!(name: "badger_team")
+
+ expect(group.reload.name).to eq("badger_team")
+ expect(Group.can_use_name?("badgers", group)).to eq(true)
+ end
+
it "is invalid for blank" do
group.name = ""
expect(group.valid?).to eq false
diff --git a/spec/models/username_validator_spec.rb b/spec/models/username_validator_spec.rb
index 3eac987df60..cc3750e02a9 100644
--- a/spec/models/username_validator_spec.rb
+++ b/spec/models/username_validator_spec.rb
@@ -315,4 +315,147 @@ RSpec.describe UsernameValidator do
DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &add_error_block)
end
end
+
+ describe "watched words validation" do
+ after { WatchedWord.destroy_all }
+
+ it "is invalid when username contains a blocked watched word" do
+ WatchedWord.create!(word: "blocked", action: WatchedWord.actions[:block])
+
+ expect_invalid(
+ "blocked",
+ error_message: I18n.t(:"user.username.contains_blocked_words", count: 1, words: "blocked"),
+ )
+ end
+
+ it "is invalid when username contains a blocked watched word with wildcard" do
+ WatchedWord.create!(word: "*bad*", action: WatchedWord.actions[:block])
+
+ validator = UsernameValidator.new("bad")
+ expect(validator.valid_format?).to eq(false)
+ expect(validator.errors.first).to include("blocked word")
+
+ validator = UsernameValidator.new("bad_user")
+ expect(validator.valid_format?).to eq(false)
+ expect(validator.errors.first).to include("blocked word")
+
+ validator = UsernameValidator.new("mybadname")
+ expect(validator.valid_format?).to eq(false)
+ expect(validator.errors.first).to include("blocked word")
+ end
+
+ it "is invalid when username contains a censored watched word" do
+ WatchedWord.create!(word: "*censor*", action: WatchedWord.actions[:censor])
+
+ expect_invalid(
+ "censor_user",
+ error_message:
+ I18n.t(:"user.username.contains_blocked_words", count: 1, words: "censor_user"),
+ )
+ end
+
+ it "is invalid when username contains a flagged watched word" do
+ WatchedWord.create!(word: "*flag*", action: WatchedWord.actions[:flag])
+
+ expect_invalid(
+ "flag_user",
+ error_message:
+ I18n.t(:"user.username.contains_blocked_words", count: 1, words: "flag_user"),
+ )
+ end
+
+ it "is invalid when username contains a silenced watched word" do
+ WatchedWord.create!(word: "*silence*", action: WatchedWord.actions[:silence])
+
+ expect_invalid(
+ "silence_user",
+ error_message:
+ I18n.t(:"user.username.contains_blocked_words", count: 1, words: "silence_user"),
+ )
+ end
+
+ it "is invalid when username contains a require approval watched word" do
+ WatchedWord.create!(word: "*approve*", action: WatchedWord.actions[:require_approval])
+
+ expect_invalid(
+ "approve_user",
+ error_message:
+ I18n.t(:"user.username.contains_blocked_words", count: 1, words: "approve_user"),
+ )
+ end
+
+ it "is invalid when username contains multiple watched words" do
+ WatchedWord.create!(word: "*bad*", action: WatchedWord.actions[:block])
+ WatchedWord.create!(word: "*evil*", action: WatchedWord.actions[:block])
+
+ validator = UsernameValidator.new("bad_evil_user")
+ expect(validator.valid_format?).to eq(false)
+ expect(validator.errors.first).to include("blocked word")
+ end
+
+ it "is valid when username does not contain any watched words" do
+ WatchedWord.create!(word: "*bad*", action: WatchedWord.actions[:block])
+
+ expect_valid("gooduser", "nice_name")
+ end
+
+ it "is valid when username matches non-moderation watched word actions" do
+ Fabricate(
+ :watched_word,
+ word: "teh",
+ replacement: "the",
+ action: WatchedWord.actions[:replace],
+ )
+ Fabricate(
+ :watched_word,
+ word: "github",
+ replacement: "https://github.com",
+ action: WatchedWord.actions[:link],
+ )
+ Fabricate(:tag, name: "supporttag")
+ Fabricate(
+ :watched_word,
+ word: "support",
+ replacement: "supporttag",
+ action: WatchedWord.actions[:tag],
+ )
+
+ expect_valid("teh", "github", "support")
+ end
+
+ it "is invalid when username matches watched word with word boundaries" do
+ WatchedWord.create!(word: "test", action: WatchedWord.actions[:block])
+
+ expect_invalid(
+ "test",
+ error_message: I18n.t(:"user.username.contains_blocked_words", count: 1, words: "test"),
+ )
+ end
+
+ it "is valid when username contains watched word but not as complete word" do
+ WatchedWord.create!(word: "test", action: WatchedWord.actions[:block])
+
+ expect_valid("testing", "mytest", "protest")
+ end
+
+ it "is invalid when username contains a watched word from a moderation action" do
+ WatchedWord.create!(word: "*block*", action: WatchedWord.actions[:block])
+ WatchedWord.create!(word: "*censor*", action: WatchedWord.actions[:censor])
+ WatchedWord.create!(word: "*flag*", action: WatchedWord.actions[:flag])
+ WatchedWord.create!(word: "*silence*", action: WatchedWord.actions[:silence])
+ WatchedWord.create!(word: "*approve*", action: WatchedWord.actions[:require_approval])
+
+ %w[
+ user_with_block
+ user_with_censor
+ user_with_flag
+ user_with_silence
+ user_with_approve
+ ].each do |username|
+ validator = UsernameValidator.new(username)
+ expect(validator.valid_format?).to eq(false), "Expected #{username} to be invalid"
+ expect(validator.errors.first).to include("blocked word")
+ end
+ end
+ end
end
diff --git a/spec/services/word_watcher_spec.rb b/spec/services/word_watcher_spec.rb
index d30e4561556..e1d96e11795 100644
--- a/spec/services/word_watcher_spec.rb
+++ b/spec/services/word_watcher_spec.rb
@@ -412,6 +412,22 @@ RSpec.describe WordWatcher do
end
end
+ describe "#word_matches_across_actions" do
+ it "only returns words for the specified actions" do
+ Fabricate(:watched_word, action: WatchedWord.actions[:block], word: "blocked")
+ Fabricate(
+ :watched_word,
+ action: WatchedWord.actions[:replace],
+ word: "replaced",
+ replacement: "replacement",
+ )
+
+ watcher = described_class.new("blocked and replaced")
+
+ expect(watcher.word_matches_across_actions(%i[block flag])).to contain_exactly("blocked")
+ end
+ end
+
describe "word replacement" do
fab!(:censored_word) do
Fabricate(:watched_word, word: "censored", action: WatchedWord.actions[:censor])