Skip to content

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

SECURITY: Add guardian check on PM to topic conversion

David Battersby9e088bc3c78a
diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rb
index c3468a59c24..aa47fcfbdb0 100644
--- a/lib/guardian/post_guardian.rb
+++ b/lib/guardian/post_guardian.rb
@@ -416,18 +416,16 @@ module PostGuardian
private
def can_create_post_in_topic?(topic)
- if !SiteSetting.enable_system_message_replies? && topic.try(:subtype) == "system_message"
+ if !SiteSetting.enable_system_message_replies? && topic&.subtype == "system_message"
return false
end
- (
- !SpamRule::AutoSilence.prevent_posting?(@user) ||
- (!!topic.try(:private_message?) && topic.allowed_users.include?(@user))
- ) &&
- (
- !topic || !topic.category ||
- Category.post_create_allowed(self).where(id: topic.category.id).count == 1
- )
+ is_pm = !!topic&.private_message?
+ return false if is_pm && topic.subtype != "system_message" && !can_see_topic?(topic)
+ return false if !is_pm && SpamRule::AutoSilence.prevent_posting?(@user)
+
+ !topic || !topic.category ||
+ Category.post_create_allowed(self).where(id: topic.category_id).exists?
end
def topic_memoize_key(topic)
diff --git a/spec/fabricators/topic_fabricator.rb b/spec/fabricators/topic_fabricator.rb
index 6bf64bc4082..0f601840109 100644
--- a/spec/fabricators/topic_fabricator.rb
+++ b/spec/fabricators/topic_fabricator.rb
@@ -29,6 +29,10 @@ Fabricator(:private_message_topic, from: :topic) do
end
end
+Fabricator(:system_message_topic, from: :private_message_topic) do
+ subtype TopicSubtype.system_message
+end
+
Fabricator(:group_private_message_topic, from: :topic) do
transient :recipient_group
category_id { nil }
diff --git a/spec/lib/guardian/post_guardian_spec.rb b/spec/lib/guardian/post_guardian_spec.rb
index d2bc853d20e..92c1dbdc6a7 100644
--- a/spec/lib/guardian/post_guardian_spec.rb
+++ b/spec/lib/guardian/post_guardian_spec.rb
@@ -458,14 +458,7 @@ RSpec.describe PostGuardian do
end
context "with system message" do
- fab!(:private_message) do
- Fabricate(
- :topic,
- archetype: Archetype.private_message,
- subtype: "system_message",
- category_id: nil,
- )
- end
+ fab!(:private_message) { Fabricate(:system_message_topic, user: user) }
before { user.save! }
it "allows the user to reply to system messages" do
@@ -476,9 +469,7 @@ RSpec.describe PostGuardian do
end
context "with private message" do
- fab!(:private_message) do
- Fabricate(:topic, archetype: Archetype.private_message, category_id: nil)
- end
+ fab!(:private_message, :private_message_topic)
before { user.save! }
@@ -487,8 +478,16 @@ RSpec.describe PostGuardian do
expect(Guardian.new(user).can_create?(Post, private_message)).to be_truthy
end
- it "doesn't allow new posts by people not invited to the pm" do
- expect(Guardian.new(user).can_create?(Post, private_message)).to be_falsey
+ it "allows new posts by people included via allowed groups" do
+ group = Fabricate(:group)
+ group.add(user)
+ private_message.topic_allowed_groups.create!(group: group)
+
+ expect(Guardian.new(user).can_create_post?(private_message)).to be_truthy
+ end
+
+ it "doesn't allow new posts by people not included in the pm" do
+ expect(Guardian.new(user).can_create_post?(private_message)).to be_falsey
end
it "allows new posts from silenced users included in the pm" do
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index d35b3c398e3..aecda203ff5 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -4967,6 +4967,12 @@ RSpec.describe TopicsController do
expect(response).to be_forbidden
end
+ it "raises an error when a moderator doesn't have permission to convert topic" do
+ sign_in(moderator)
+ put "/t/#{topic.id}/convert-topic/public.json"
+ expect(response).to be_forbidden
+ end
+
context "with success" do
it "returns success and the new url" do
sign_in(admin)