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.rbindex 3796c0de7a9..29b55068de1 100644--- a/app/models/discourse_connect.rb+++ b/app/models/discourse_connect.rb@@ -127,6 +127,8 @@ class DiscourseConnect < DiscourseConnectBasesso_record = user.single_sign_on_recordend+ user.skip_username_watched_words_validation = true+# ensure it's not staged anymoreuser.unstage!@@ -265,6 +267,7 @@ class DiscourseConnect < DiscourseConnectBaseenduser = User.new(user_params)+ user.skip_username_watched_words_validation = trueif SiteSetting.must_approve_users && EmailValidator.can_auto_approve_user?(email)ReviewableUser.set_approved_fields!(user, Discourse.system_user)@@ -424,7 +427,7 @@ class DiscourseConnect < DiscourseConnectBasesuggester_input = [username]suggester_input << name if SiteSetting.use_name_for_username_suggestionssuggester_input << email if SiteSetting.use_email_for_username_and_name_suggestions- UserNameSuggester.suggest(*suggester_input)+ UserNameSuggester.suggest(*suggester_input, skip_watched_words: true)enddef resolve_namediff --git a/app/models/group.rb b/app/models/group.rbindex b68b5da0eb2..c894e20e8ef 100644--- a/app/models/group.rb+++ b/app/models/group.rb@@ -561,8 +561,11 @@ class Group < ActiveRecord::Baseenddef 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))enddef self.refresh_automatic_group!(name)@@ -1114,7 +1117,12 @@ class Group < ActiveRecord::Baseself.name = strippedend- UsernameValidator.perform_validation(self, "name", skip_length_validation: automatic) ||+ UsernameValidator.perform_validation(+ self,+ "name",+ skip_length_validation: automatic,+ skip_watched_words: true,+ ) ||beginnormalized_name = User.normalize_username(name)diff --git a/app/models/user.rb b/app/models/user.rbindex c42c8e0b862..2a71ddc6685 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -255,6 +255,9 @@ class User < ActiveRecord::Baseattr_accessor :skip_email_validationattr_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 creationattr_accessor :send_welcome_message@@ -1515,7 +1518,11 @@ class User < ActiveRecord::Baseenddef username_format_validator- UsernameValidator.perform_validation(self, "username")+ UsernameValidator.perform_validation(+ self,+ "username",+ skip_watched_words: skip_username_watched_words_validation,+ )enddef email_confirmed?diff --git a/app/models/username_validator.rb b/app/models/username_validator.rbindex 72645a94a2c..b7fc5f7f225 100644--- a/app/models/username_validator.rb+++ b/app/models/username_validator.rb@@ -28,9 +28,12 @@ class UsernameValidatorendend- 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 UsernameValidatorattr_reader :usernameattr_reader :objectattr_reader :skip_length_validation+ attr_reader :skip_watched_wordsdef valid_format?username_present?@@ -50,6 +54,7 @@ class UsernameValidatorusername_last_char_valid?username_no_double_special?username_does_not_end_with_confusing_suffix?+ username_not_watched_word? if !skip_watched_wordsusername_plugin_validationerrors.empty?end@@ -164,6 +169,19 @@ class UsernameValidatorendend+ 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_validationreturn unless errors.empty?diff --git a/app/services/username_changer.rb b/app/services/username_changer.rbindex 0c6c119cda1..20553c01216 100644--- a/app/services/username_changer.rb+++ b/app/services/username_changer.rb@@ -18,7 +18,12 @@ class UsernameChangerUsernameChanger.change(user, new_username, user)trueelsif 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)trueelsediff --git a/app/services/word_watcher.rb b/app/services/word_watcher.rbindex 9364097f120..f683812816f 100644--- a/app/services/word_watcher.rb+++ b/app/services/word_watcher.rb@@ -242,10 +242,8 @@ class WordWatchermatch_listend- 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.sortend+ 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::IGNORECASERegexp.new(WordWatcher.word_to_regexp(word), options).match?(@raw)diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 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.rbindex c3cebeb40a1..1c32f9e338e 100644--- a/lib/user_name_suggester.rb+++ b/lib/user_name_suggester.rb@@ -4,7 +4,12 @@ module UserNameSuggesterGENERIC_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 UserNameSuggestername = 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:)enddef self.parse_name_from_email(name_or_email)@@ -30,9 +35,13 @@ module UserNameSuggesternameend- 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 = nili = 1+ valid_and_available = ->(candidate) do+ UsernameValidator.new(candidate, skip_watched_words:).valid_format? &&+ User.username_available?(candidate)+ endattempt = namenormalized_attempt = User.normalize_username(attempt)@@ -40,7 +49,7 @@ module UserNameSuggesteroriginal_allowed_username = current_usernamecurrent_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 > 100if offset.nil?normalized = User.normalize_username(name)similar = "#{normalized}(0|1|2|3|4|5|6|7|8|9)+"@@ -89,16 +98,18 @@ module UserNameSuggesteri += 1end- 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 += 1endif current_username == normalized_attemptoriginal_allowed_username- else+ elsif valid_and_available.call(attempt)attempt+ else+ raise Discourse::InvalidParameters, "Unable to suggest a valid username"endenddiff --git a/spec/lib/email/receiver_spec.rb b/spec/lib/email/receiver_spec.rbindex 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 dodescribe "new message to a group" dofab!(: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" doexpect { 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.rbindex 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 dolet(: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" doFabricate(:user, username: "sam")Fabricate(:user, username: "sAm1")diff --git a/spec/models/discourse_connect_spec.rb b/spec/models/discourse_connect_spec.rbindex fdd3623944d..c1273c894b5 100644--- a/spec/models/discourse_connect_spec.rb+++ b/spec/models/discourse_connect_spec.rb@@ -119,6 +119,45 @@ RSpec.describe DiscourseConnect doexpect(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" doSiteSetting.stubs(:normalize_emails).returns(true)diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rbindex fa6337efb36..aed82727656 100644--- a/spec/models/group_spec.rb+++ b/spec/models/group_spec.rb@@ -145,6 +145,17 @@ RSpec.describe Group dodescribe "validation" dolet(: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" dogroup.name = ""expect(group.valid?).to eq falsediff --git a/spec/models/username_validator_spec.rb b/spec/models/username_validator_spec.rbindex 3eac987df60..cc3750e02a9 100644--- a/spec/models/username_validator_spec.rb+++ b/spec/models/username_validator_spec.rb@@ -315,4 +315,147 @@ RSpec.describe UsernameValidator doDiscoursePluginRegistry.unregister_modifier(plugin, modifier, &add_error_block)endend++ 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+ endenddiff --git a/spec/services/word_watcher_spec.rb b/spec/services/word_watcher_spec.rbindex d30e4561556..e1d96e11795 100644--- a/spec/services/word_watcher_spec.rb+++ b/spec/services/word_watcher_spec.rb@@ -412,6 +412,22 @@ RSpec.describe WordWatcher doendend+ 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" dofab!(:censored_word) doFabricate(:watched_word, word: "censored", action: WatchedWord.actions[:censor])