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:
Vinoth Kannan 2024-05-30 06:32:57 +05:30 committed by Nat
parent 95be7f4940
commit a747724cb6
No known key found for this signature in database
GPG Key ID: 4938B35D927EC773
2 changed files with 17 additions and 1 deletions

View File

@ -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] }

View File

@ -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") }