Skip to content

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.rb
index 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 words
regexp =
match_word_regexp(
@@ -96,6 +98,7 @@ class WordWatcher
# Add case insensitive flag if needed
Regexp.new(regexp, group_key == :case_sensitive ? nil : Regexp::IGNORECASE)
end
+ .compact
end
def self.serialized_regexps_for_action(action, engine: :ruby)
diff --git a/spec/services/word_watcher_spec.rb b/spec/services/word_watcher_spec.rb
index 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)
end
end
+
+ 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
+ end
end
describe "#word_matches_for_action?" do