Skip to content

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

SECURITY: prevent regular users from promoting topics to banners

Keegan George7d565fb932aa
diff --git a/lib/guardian/topic_guardian.rb b/lib/guardian/topic_guardian.rb
index 1ad8655fe61..5f4a9cce2f4 100644
--- a/lib/guardian/topic_guardian.rb
+++ b/lib/guardian/topic_guardian.rb
@@ -323,6 +323,14 @@ module TopicGuardian
topic && authenticated? && !topic.private_message? && is_staff?
end
+ def can_change_archetype?(topic, new_archetype)
+ return true if new_archetype == topic.archetype
+ if new_archetype == Archetype.banner || topic.archetype == Archetype.banner
+ return can_banner_topic?(topic)
+ end
+ true
+ end
+
def can_edit_tags?(topic)
return false unless can_tag_topics?
return false if topic.private_message? && !can_tag_pms?
diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb
index e1f02bba03e..9d957f7f2aa 100644
--- a/lib/post_revisor.rb
+++ b/lib/post_revisor.rb
@@ -118,7 +118,11 @@ class PostRevisor
end
track_topic_field(:archetype) do |topic_changes, attribute|
- track_and_revise topic_changes, :archetype, attribute
+ if topic_changes.guardian.can_change_archetype?(topic_changes.topic, attribute)
+ track_and_revise topic_changes, :archetype, attribute
+ else
+ topic_changes.check_result(false)
+ end
end
track_topic_field(:category_id) do |tc, new_category_id, fields|
diff --git a/spec/lib/guardian_spec.rb b/spec/lib/guardian_spec.rb
index d70430724e8..1f09ab56103 100644
--- a/spec/lib/guardian_spec.rb
+++ b/spec/lib/guardian_spec.rb
@@ -505,6 +505,28 @@ RSpec.describe Guardian do
expect(Guardian.new(admin).can_banner_topic?(topic)).to be_truthy
end
+ describe "#can_change_archetype?" do
+ fab!(:topic)
+
+ it "allows staff to change archetype to banner" do
+ expect(Guardian.new(admin).can_change_archetype?(topic, Archetype.banner)).to eq(true)
+ expect(Guardian.new(moderator).can_change_archetype?(topic, Archetype.banner)).to eq(true)
+ end
+
+ it "does not allow regular users to change archetype to banner" do
+ expect(Guardian.new(user).can_change_archetype?(topic, Archetype.banner)).to eq(false)
+ end
+
+ it "does not allow regular users to change archetype from banner" do
+ topic.update!(archetype: Archetype.banner)
+ expect(Guardian.new(user).can_change_archetype?(topic, Archetype.default)).to eq(false)
+ end
+
+ it "returns true when archetype is unchanged" do
+ expect(Guardian.new(user).can_change_archetype?(topic, topic.archetype)).to eq(true)
+ end
+ end
+
it "respects category group moderator settings" do
group_user = Fabricate(:group_user)
user_gm = group_user.user
diff --git a/spec/lib/post_revisor_spec.rb b/spec/lib/post_revisor_spec.rb
index d6e70e28f09..c6d0ef34551 100644
--- a/spec/lib/post_revisor_spec.rb
+++ b/spec/lib/post_revisor_spec.rb
@@ -1070,11 +1070,11 @@ describe PostRevisor do
expect(post_revisor.raw_changed?).to eq(false)
end
- it "revises and tracks changes of topic archetypes" do
+ it "revises and tracks changes of topic archetypes for staff" do
new_archetype = Archetype.banner
result =
post_revisor.revise!(
- post.user,
+ admin,
{ archetype: new_archetype },
revised_at: post.updated_at + 10.minutes,
)
@@ -1086,6 +1086,19 @@ describe PostRevisor do
expect(post_revisor.raw_changed?).to eq(false)
end
+ it "does not allow regular users to change topic archetype to banner" do
+ result =
+ post_revisor.revise!(
+ post.user,
+ { archetype: Archetype.banner },
+ revised_at: post.updated_at + 10.minutes,
+ )
+
+ expect(result).to eq(false)
+ post.reload
+ expect(post.topic.archetype).to eq(Archetype.default)
+ end
+
it "revises and tracks changes of topic tags" do
post_revisor.revise!(admin, tags: ["new-tag"])
expect(post.post_revisions.last.modifications).to eq("tags" => [[], ["new-tag"]])
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 88a1e2e2b36..58877a01c77 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -2017,6 +2017,13 @@ RSpec.describe TopicsController do
end
end
+ it "does not allow a regular user to change archetype to banner" do
+ put "/t/#{topic.id}.json", params: { archetype: Archetype.banner }
+
+ topic.reload
+ expect(topic.archetype).to eq(Archetype.default)
+ end
+
describe "without permission" do
it "raises an exception when the user doesn't have permission to update the topic" do
topic.update!(archived: true)