Skip to content

A platform for community discussion. Free, open, simple.

SECURITY: Respect max length in bot-human PMs

Jarek Radoszaa6d0bb6853b
diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb
index 3114f28246b..6b6b6dda8fc 100644
--- a/app/models/topic_link.rb
+++ b/app/models/topic_link.rb
@@ -288,7 +288,7 @@ class TopicLink < ActiveRecord::Base
internal = Discourse.store.internal?
# Store the same URL that will be used in the cooked version of the post
url = UrlHelper.cook_url(upload.url, secure: upload.secure?)
- elsif route = Discourse.route_for(parsed)
+ elsif route = Discourse.route_for(parsed.to_s[...TopicLink.max_url_length])
# this is a special case for the silent flag
# in internal links
return nil if url && (url.split("?")[1] == "silent=true")
diff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rb
index b488eec4ebc..138af78a53a 100644
--- a/lib/validators/post_validator.rb
+++ b/lib/validators/post_validator.rb
@@ -28,15 +28,20 @@ class PostValidator < ActiveModel::Validator
end
def post_body_validator(post)
- return if options[:skip_post_body] || post.topic&.pm_with_non_human_user?
+ return if options[:skip_post_body]
+
stripped_length(post)
+ return if post.topic&.pm_with_non_human_user?
+
raw_quality(post)
WatchedWordsValidator.new(attributes: [:raw]).validate(post) if !post.acting_user&.staged
end
def stripped_length(post)
range =
- if private_message?(post)
+ if post.topic&.pm_with_non_human_user?
+ (0..SiteSetting.max_post_length)
+ elsif private_message?(post)
SiteSetting.private_message_post_length
elsif post.is_first_post? || (post.topic.present? && post.topic.posts_count == 0)
if post.topic&.featured_link.present?
diff --git a/lib/validators/stripped_length_validator.rb b/lib/validators/stripped_length_validator.rb
index df9fd5628c6..116d0bb52b2 100644
--- a/lib/validators/stripped_length_validator.rb
+++ b/lib/validators/stripped_length_validator.rb
@@ -2,7 +2,7 @@
class StrippedLengthValidator < ActiveModel::EachValidator
def self.validate(record, attribute, value, range, strip_uploads: false)
- if value.blank?
+ if value.blank? && range.begin > 0
record.errors.add attribute, I18n.t("errors.messages.blank")
elsif value.length > range.end
record.errors.add attribute,
@@ -11,12 +11,8 @@ class StrippedLengthValidator < ActiveModel::EachValidator
count: range.end,
length: value.length,
)
- else
- value = get_sanitized_value(value, strip_uploads:)
-
- if value.length < range.begin
- record.errors.add attribute, I18n.t("errors.messages.too_short", count: range.begin)
- end
+ elsif get_sanitized_value(value, strip_uploads:).length < range.begin
+ record.errors.add attribute, I18n.t("errors.messages.too_short", count: range.begin)
end
end
diff --git a/spec/lib/validators/post_validator_spec.rb b/spec/lib/validators/post_validator_spec.rb
index 5f5033fb1ee..87e5fc8ffb8 100644
--- a/spec/lib/validators/post_validator_spec.rb
+++ b/spec/lib/validators/post_validator_spec.rb
@@ -43,6 +43,18 @@ RSpec.describe PostValidator do
expect(post.errors).to be_empty
end
+
+ it "respects the max body length" do
+ SiteSetting.max_post_length = 5
+ post = Fabricate.build(:post, topic: topic)
+ post.raw = "that's too much mate"
+ validator.post_body_validator(post)
+
+ expect(post.errors.count).to eq(1)
+ expect(post.errors[:raw]).to contain_exactly(
+ I18n.t("errors.messages.too_long_validation", count: 5, length: 20),
+ )
+ end
end
end
diff --git a/spec/models/topic_link_spec.rb b/spec/models/topic_link_spec.rb
index fe2e25b3f59..4c1dfcfe1f3 100644
--- a/spec/models/topic_link_spec.rb
+++ b/spec/models/topic_link_spec.rb
@@ -584,4 +584,12 @@ RSpec.describe TopicLink do
expect { TopicLink.extract_from(post) }.to_not raise_error
end
end
+
+ it "extract_from performs well" do
+ raw = "[a](#{"/a" * 25_000})"
+ post = Fabricate.build(:post, user:, raw:)
+ post.cooked = post.cook(raw)
+
+ expect { Timeout.timeout(2) { TopicLink.extract_from(post) } }.not_to raise_error
+ end
end