A platform for community discussion. Free, open, simple.
FIX: Handle all UTF-8 characters (#21344)
Bianca Nenciu9a2780397faa
diff --git a/app/assets/javascripts/discourse-common/addon/utils/watched-words.js b/app/assets/javascripts/discourse-common/addon/utils/watched-words.jsindex d864b807352..8ff622ce550 100644--- a/app/assets/javascripts/discourse-common/addon/utils/watched-words.js+++ b/app/assets/javascripts/discourse-common/addon/utils/watched-words.js@@ -1,6 +1,6 @@export function createWatchedWordRegExp(word) {const caseFlag = word.case_sensitive ? "" : "i";- return new RegExp(word.regexp, `${caseFlag}g`);+ return new RegExp(word.regexp, `${caseFlag}gu`);}export function toWatchedWord(regexp) {diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rbindex 7bafdb66976..140d7657c71 100644--- a/app/serializers/site_serializer.rb+++ b/app/serializers/site_serializer.rb@@ -205,7 +205,7 @@ class SiteSerializer < ApplicationSerializerenddef censored_regexp- WordWatcher.serializable_word_matcher_regexp(:censor)+ WordWatcher.serializable_word_matcher_regexp(:censor, engine: :js)enddef custom_emoji_translation@@ -221,11 +221,11 @@ class SiteSerializer < ApplicationSerializerenddef watched_words_replace- WordWatcher.word_matcher_regexps(:replace)+ WordWatcher.word_matcher_regexps(:replace, engine: :js)enddef watched_words_link- WordWatcher.word_matcher_regexps(:link)+ WordWatcher.word_matcher_regexps(:link, engine: :js)enddef categoriesdiff --git a/app/serializers/watched_word_serializer.rb b/app/serializers/watched_word_serializer.rbindex a798091f282..135aca292b3 100644--- a/app/serializers/watched_word_serializer.rb+++ b/app/serializers/watched_word_serializer.rb@@ -4,7 +4,7 @@ class WatchedWordSerializer < ApplicationSerializerattributes :id, :word, :regexp, :replacement, :action, :case_sensitivedef regexp- WordWatcher.word_to_regexp(word, whole: true)+ WordWatcher.word_to_regexp(word)enddef actiondiff --git a/app/services/word_watcher.rb b/app/services/word_watcher.rbindex 80cf414d7b8..1899e963d27 100644--- a/app/services/word_watcher.rb+++ b/app/services/word_watcher.rb@@ -44,23 +44,23 @@ class WordWatcherendend- def self.serializable_word_matcher_regexp(action)- word_matcher_regexp_list(action).map { |r| { r.source => { case_sensitive: !r.casefold? } } }+ def self.serializable_word_matcher_regexp(action, engine: :ruby)+ word_matcher_regexp_list(action, engine: engine).map do |r|+ { r.source => { case_sensitive: !r.casefold? } }+ endend# This regexp is run in miniracer, and the client JS app# Make sure it is compatible with major browsers when changing# hint: non-chrome browsers do not support 'lookbehind'- def self.word_matcher_regexp_list(action, raise_errors: false)+ def self.word_matcher_regexp_list(action, engine: :ruby, raise_errors: false)words = get_cached_words(action)return [] if words.blank?grouped_words = { case_sensitive: [], case_insensitive: [] }- words.each do |w, attrs|- word = word_to_regexp(w)- word = "(#{word})" if SiteSetting.watched_words_regular_expressions?-+ words.each do |word, attrs|+ word = word_to_regexp(word, whole: SiteSetting.watched_words_regular_expressions?)group_key = attrs[:case_sensitive] ? :case_sensitive : :case_insensitivegrouped_words[group_key] << wordend@@ -68,10 +68,7 @@ class WordWatcherregexps = grouped_words.select { |_, w| w.present? }.transform_values { |w| w.join("|") }if !SiteSetting.watched_words_regular_expressions?- regexps.transform_values! do |regexp|- regexp = "(#{regexp})"- "(?:\\W|^)#{regexp}(?=\\W|$)"- end+ regexps.transform_values! { |regexp| wrap_regexp(regexp, engine: engine) }endregexps.map { |c, regexp| Regexp.new(regexp, c == :case_sensitive ? nil : Regexp::IGNORECASE) }@@ -80,29 +77,42 @@ class WordWatcher[] # Admin will be alerted via admin_dashboard_data.rbend- def self.word_matcher_regexps(action)+ def self.word_matcher_regexps(action, engine: :ruby)if words = get_cached_words(action)- words.map { |w, opts| [word_to_regexp(w, whole: true), opts] }.to_h+ words.map { |word, attrs| [word_to_regexp(word, engine: engine), attrs] }.to_hendend- def self.word_to_regexp(word, whole: false)+ def self.word_to_regexp(word, engine: :ruby, whole: true)if SiteSetting.watched_words_regular_expressions?- # Strip ruby regexp format if present+ # Strip Ruby regexp format if presentregexp = word.start_with?("(?-mix:") ? word[7..-2] : wordregexp = "(#{regexp})" if wholereturn regexpend- regexp = Regexp.escape(word).gsub("\\*", '\S*')+ # Escape regular expression. Avoid using Regexp.escape because it escapes+ # more characters than it should (for example, whitespaces)+ regexp = word.gsub(/([.*+?^${}()|\[\]\\])/, '\\\\\1')- if whole && !SiteSetting.watched_words_regular_expressions?- regexp = "(?:\\W|^)(#{regexp})(?=\\W|$)"- end+ # Handle wildcards+ regexp = regexp.gsub("\\*", '\S*')++ regexp = wrap_regexp(regexp, engine: engine) if wholeregexpend+ def self.wrap_regexp(regexp, engine: :ruby)+ if engine == :js+ "(?:\\P{L}|^)(#{regexp})(?=\\P{L}|$)"+ elsif engine == :ruby+ "(?:[^[:word:]]|^)(#{regexp})(?=[^[:word:]]|$)"+ else+ "(?:\\W|^)(#{regexp})(?=\\W|$)"+ end+ end+def self.word_matcher_regexp_key(action)"watched-words-list:v#{CACHE_VERSION}:#{action}"end@@ -212,10 +222,8 @@ class WordWatcherenddef word_matches?(word, case_sensitive: false)- Regexp.new(- WordWatcher.word_to_regexp(word, whole: true),- case_sensitive ? nil : Regexp::IGNORECASE,- ).match?(@raw)+ options = case_sensitive ? nil : Regexp::IGNORECASE+ Regexp.new(WordWatcher.word_to_regexp(word), options).match?(@raw)enddef self.replace_text_with_regexp(text, regexp, replacement)diff --git a/lib/pretty_text.rb b/lib/pretty_text.rbindex 986dbccecd0..871a9f431cc 100644--- a/lib/pretty_text.rb+++ b/lib/pretty_text.rb@@ -204,9 +204,9 @@ module PrettyText__optInput.emojiUnicodeReplacer = __emojiUnicodeReplacer;__optInput.emojiDenyList = #{Emoji.denied.to_json};__optInput.lookupUploadUrls = __lookupUploadUrls;- __optInput.censoredRegexp = #{WordWatcher.serializable_word_matcher_regexp(:censor).to_json};- __optInput.watchedWordsReplace = #{WordWatcher.word_matcher_regexps(:replace).to_json};- __optInput.watchedWordsLink = #{WordWatcher.word_matcher_regexps(:link).to_json};+ __optInput.censoredRegexp = #{WordWatcher.serializable_word_matcher_regexp(:censor, engine: :js).to_json};+ __optInput.watchedWordsReplace = #{WordWatcher.word_matcher_regexps(:replace, engine: :js).to_json};+ __optInput.watchedWordsLink = #{WordWatcher.word_matcher_regexps(:link, engine: :js).to_json};__optInput.additionalOptions = #{Site.markdown_additional_options.to_json};JSdiff --git a/spec/integration/watched_words_spec.rb b/spec/integration/watched_words_spec.rbindex 2304fb6bce0..a7d6902fbdd 100644--- a/spec/integration/watched_words_spec.rb+++ b/spec/integration/watched_words_spec.rb@@ -64,6 +64,14 @@ RSpec.describe WatchedWord doshould_block_post(manager)end+ it "should handle UTF-8 characters" do+ block_word = Fabricate(:watched_word, action: WatchedWord.actions[:block], word: "abc")+ manager =+ NewPostManager.new(tl2_user, title: "Hello world", raw: "abcódef", topic_id: topic.id)++ expect(manager.perform).to be_success+ end+it "should block the post from admin" domanager =NewPostManager.new(diff --git a/spec/services/word_watcher_spec.rb b/spec/services/word_watcher_spec.rbindex f7b8881b2a5..4d55d6ab0eb 100644--- a/spec/services/word_watcher_spec.rb+++ b/spec/services/word_watcher_spec.rb@@ -79,8 +79,8 @@ RSpec.describe WordWatcher doexpect(regexps).to be_an(Array)expect(regexps.map(&:inspect)).to contain_exactly(- "/(?:\\W|^)(#{word1}|#{word2})(?=\\W|$)/i",- "/(?:\\W|^)(#{word3}|#{word4})(?=\\W|$)/",+ "/(?:[^[:word:]]|^)(#{word1}|#{word2})(?=[^[:word:]]|$)/i",+ "/(?:[^[:word:]]|^)(#{word3}|#{word4})(?=[^[:word:]]|$)/",)end