A platform for community discussion. Free, open, simple.
FIX: wildcard watched word and regexps (#35217)
Régis Hanolc3efcc99e3e1
diff --git a/app/services/word_watcher.rb b/app/services/word_watcher.rbindex 87514aac598..ab453bce8a7 100644--- a/app/services/word_watcher.rb+++ b/app/services/word_watcher.rb@@ -86,6 +86,8 @@ class WordWatcher.select { |r| r.present? }.join("|")+ next if regexp.blank?+# Add word boundaries to the regexp for regular watched wordsregexp =match_word_regexp(@@ -96,6 +98,7 @@ class WordWatcher# Add case insensitive flag if neededRegexp.new(regexp, group_key == :case_sensitive ? nil : Regexp::IGNORECASE)end+ .compactenddef self.serialized_regexps_for_action(action, engine: :ruby)diff --git a/spec/services/word_watcher_spec.rb b/spec/services/word_watcher_spec.rbindex f7be8d5fe91..6ae66ec4a32 100644--- a/spec/services/word_watcher_spec.rb+++ b/spec/services/word_watcher_spec.rb@@ -115,6 +115,28 @@ RSpec.describe WordWatcher do}.to raise_error(RegexpError)endend++ context "when there's a wildcard watched word" do+ before do+ SiteSetting.watched_words_regular_expressions = false+ WatchedWord.where(action: WatchedWord.actions[:block]).delete_all+ Fabricate(:watched_word, word: "*abc", action: WatchedWord.actions[:block])+ end++ it "works correctly when regular expressions are disabled" do+ regexps = described_class.compiled_regexps_for_action(:block)+ expect(regexps).to be_an(Array)+ expect(regexps).to contain_exactly(/(?:[^[:word:]]|^)(\S*abc)(?=[^[:word:]]|$)/i)+ end++ it "skips invalid watched words when regular expression are enabled" do+ SiteSetting.watched_words_regular_expressions = true++ regexps = described_class.compiled_regexps_for_action(:block)+ expect(regexps).to be_an(Array)+ expect(regexps).to be_empty+ end+ endenddescribe "#word_matches_for_action?" do