mirror of
https://github.com/discourse/discourse.git
synced 2025-02-02 20:25:15 +08:00
SECURITY: limit the number of characters in watched word replacements.
The watch words controller creation function, create_or_update_word(), doesn’t validate the size of the replacement parameter, unlike the word parameter, when creating a replace watched word. So anyone with moderator privileges can create watched words with almost unlimited characters.
This commit is contained in:
parent
95be7f4940
commit
a747724cb6
|
@ -3,7 +3,10 @@
|
|||
class WatchedWord < ActiveRecord::Base
|
||||
MAX_WORDS_PER_ACTION = 2000
|
||||
|
||||
before_validation { self.word = WatchedWord.normalize_word(self.word) }
|
||||
before_validation do
|
||||
self.word = WatchedWord.normalize_word(self.word)
|
||||
self.replacement = WatchedWord.normalize_word(self.replacement) if self.replacement.present?
|
||||
end
|
||||
|
||||
before_validation do
|
||||
if self.action == WatchedWord.actions[:link] && self.replacement !~ %r{\Ahttps?://}
|
||||
|
@ -13,6 +16,7 @@ class WatchedWord < ActiveRecord::Base
|
|||
end
|
||||
|
||||
validates :word, presence: true, uniqueness: true, length: { maximum: 100 }
|
||||
validates :replacement, length: { maximum: 100 }
|
||||
validates :action, presence: true
|
||||
validate :replacement_is_url, if: -> { action == WatchedWord.actions[:link] }
|
||||
validate :replacement_is_tag_list, if: -> { action == WatchedWord.actions[:tag] }
|
||||
|
|
|
@ -26,6 +26,18 @@ RSpec.describe WatchedWord do
|
|||
expect(described_class.create(word: "Jest").case_sensitive?).to eq(false)
|
||||
end
|
||||
|
||||
it "limits the number of characters in a word" do
|
||||
w = Fabricate.build(:watched_word, word: "a" * 101)
|
||||
expect(w).to_not be_valid
|
||||
expect(w.errors[:word]).to be_present
|
||||
end
|
||||
|
||||
it "limits the number of characters in a replacement" do
|
||||
w = Fabricate.build(:watched_word, replacement: "a" * 101)
|
||||
expect(w).to_not be_valid
|
||||
expect(w.errors[:replacement]).to be_present
|
||||
end
|
||||
|
||||
describe "action_key=" do
|
||||
let(:w) { WatchedWord.new(word: "troll") }
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user