A platform for community discussion. Free, open, simple.
SECURITY: Escape watched word in error message (#14434)
Bianca Nenciu1f57b29147f5
diff --git a/lib/new_post_manager.rb b/lib/new_post_manager.rbindex 1dc38fcc7f7..777d93bdb0d 100644--- a/lib/new_post_manager.rb+++ b/lib/new_post_manager.rb@@ -201,10 +201,10 @@ class NewPostManagerresult = NewPostResult.new(:created_post, false)if matches.size == 1key = 'contains_blocked_word'- translation_args = { word: matches[0] }+ translation_args = { word: CGI.escapeHTML(matches[0]) }elsekey = 'contains_blocked_words'- translation_args = { words: matches.join(', ') }+ translation_args = { words: CGI.escapeHTML(matches.join(', ')) }endresult.errors.add(:base, I18n.t(key, translation_args))return resultdiff --git a/lib/validators/watched_words_validator.rb b/lib/validators/watched_words_validator.rbindex cfa7e5a6b66..4174e514af1 100644--- a/lib/validators/watched_words_validator.rb+++ b/lib/validators/watched_words_validator.rb@@ -5,10 +5,10 @@ class WatchedWordsValidator < ActiveModel::EachValidatorif matches = WordWatcher.new(value).should_block?.presenceif matches.size == 1key = 'contains_blocked_word'- translation_args = { word: matches[0] }+ translation_args = { word: CGI.escapeHTML(matches[0]) }elsekey = 'contains_blocked_words'- translation_args = { words: matches.join(', ') }+ translation_args = { words: CGI.escapeHTML(matches.join(', ')) }endrecord.errors.add(:base, I18n.t(key, translation_args))enddiff --git a/spec/integration/watched_words_spec.rb b/spec/integration/watched_words_spec.rbindex b8f5fff39b5..7fb3c04708e 100644--- a/spec/integration/watched_words_spec.rb+++ b/spec/integration/watched_words_spec.rb@@ -32,6 +32,14 @@ describe WatchedWord do}.to_not change { Post.count }end+ it "escapes the blocked word in error message" do+ block_word = Fabricate(:watched_word, action: WatchedWord.actions[:block], word: "<a>")+ manager = NewPostManager.new(tl2_user, raw: "Want some #{block_word.word} for cheap?", topic_id: topic.id)+ result = manager.perform+ expect(result).to_not be_success+ expect(result.errors[:base]&.first).to eq(I18n.t('contains_blocked_word', word: "<a>"))+ end+it "should prevent the post from being created" domanager = NewPostManager.new(tl2_user, raw: "Want some #{block_word.word} for cheap?", topic_id: topic.id)should_block_post(manager)