A platform for community discussion. Free, open, simple.
FIX: Cap the number of [quote] tags allowed per post (#40281)
Rafael dos Santos Silvaad459f7806be
diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 2a5bb6149e7..e1adbbff7df 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -550,6 +550,9 @@ en:too_many_links:one: "Sorry, new users can only put one link in a post."other: "Sorry, new users can only put %{count} links in a post."+ too_many_quotes:+ one: "Sorry, you can only include one quote in a post."+ other: "Sorry, you can only include %{count} quotes in a post."contains_blocked_word: "Sorry, you can't post the word '%{word}'; it's not allowed."contains_blocked_words: "Sorry, you can't post that. Not allowed: %{words}."@@ -2323,6 +2326,7 @@ en:newuser_max_mentions_per_post: "Maximum number of @name notifications a new user can use in a post."newuser_max_replies_per_topic: "Maximum number of replies a new user can make in a single topic until someone replies to them."max_mentions_per_post: "Maximum number of @name notifications anyone can use in a post."+ max_quotes_per_post: "Maximum number of quotes allowed in a single post. Set to 0 to disable the check."max_users_notified_per_group_mention: "Maximum number of users that may receive a notification if a group is mentioned (if threshold is met no notifications will be raised)"enable_mentions: "Permits users to tag or reference each other in their posts using the '@' symbol."here_mention: "Name used for a @mention to allow privileged users to notify up to 'max_here_mentioned' people participating in the topic. Must not be an existing username."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 5fd96e1a733..7368f009a3a 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -1476,6 +1476,10 @@ posting:max_mentions_per_post:default: 10area: "notifications"+ max_quotes_per_post:+ default: 50+ min: 0+ area: "posts_and_topics"max_users_notified_per_group_mention:default: 100client: truediff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rbindex 138af78a53a..6c8e0c12f3d 100644--- a/lib/validators/post_validator.rb+++ b/lib/validators/post_validator.rb@@ -18,6 +18,7 @@ class PostValidator < ActiveModel::Validatormax_embedded_media_validator(record)max_attachments_validator(record)max_links_validator(record)+ max_quotes_validator(record)force_edit_last_validator(record)endend@@ -150,6 +151,14 @@ class PostValidator < ActiveModel::Validatorpost.errors.add(:base, I18n.t(:links_require_trust))end+ def max_quotes_validator(post)+ max = SiteSetting.max_quotes_per_post+ return if max <= 0++ count = post.raw.to_s.scan(/\[quote[=\]]/i).size+ post.errors.add(:base, I18n.t(:too_many_quotes, count: max)) if count > max+ end+def force_edit_last_validator(post)return if post.idreturn if private_message?(post)diff --git a/spec/lib/validators/post_validator_spec.rb b/spec/lib/validators/post_validator_spec.rbindex 19fbb4a46c3..90a20ad70f5 100644--- a/spec/lib/validators/post_validator_spec.rb+++ b/spec/lib/validators/post_validator_spec.rb@@ -299,6 +299,47 @@ RSpec.describe PostValidator doendend+ describe "max_quotes_validator" do+ before { SiteSetting.max_quotes_per_post = 3 }++ it "is invalid when the post has more quote openers than the limit" do+ post.raw = ([%([quote="user, post:1, topic:1"]), "[/quote]"] * 4).join("\n")+ validator.max_quotes_validator(post)+ expect(post.errors[:base]).to be_present+ end++ it "is valid when the post has quote openers at or below the limit" do+ post.raw = ([%([quote="user, post:1, topic:1"]), "[/quote]"] * 3).join("\n")+ validator.max_quotes_validator(post)+ expect(post.errors[:base]).to be_blank+ end++ it "counts unbalanced openers, not balanced quote pairs" do+ post.raw = %([quote="a, post:1, topic:1"]\n) * 4+ validator.max_quotes_validator(post)+ expect(post.errors[:base]).to be_present+ end++ it "counts plain [quote] openers too" do+ post.raw = "[quote]\n" * 4+ validator.max_quotes_validator(post)+ expect(post.errors[:base]).to be_present+ end++ it "is case-insensitive" do+ post.raw = "[QUOTE=foo]\n" * 4+ validator.max_quotes_validator(post)+ expect(post.errors[:base]).to be_present+ end++ it "is disabled when max_quotes_per_post is 0" do+ SiteSetting.max_quotes_per_post = 0+ post.raw = %([quote="a, post:1, topic:1"]\n) * 50+ validator.max_quotes_validator(post)+ expect(post.errors[:base]).to be_blank+ end+ end+describe "max_embedded_media_validator" dofab!(:new_user) { Fabricate(:newuser, refresh_auto_groups: true) }