Skip to content

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

FIX: Duration-based publish timers bypass destination category authorization (#39816)

Samfc826da5016e
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 28ca8db1191..40db6ff130c 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -644,7 +644,9 @@ class TopicsController < ApplicationController
guardian.ensure_can_delete!(topic) if TopicTimer.destructive_types.values.include?(status_type)
- if status_type == TopicTimer.types[:publish_to_category] && params[:time].present?
+ creating_timer = params[:time].present? || params[:duration_minutes].present?
+
+ if status_type == TopicTimer.types[:publish_to_category] && creating_timer
category = Category.find_by(id: params[:category_id])
raise Discourse::NotFound if !category
raise Discourse::InvalidAccess if !guardian.can_create_topic_on_category?(category)
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 7e7972f92f6..66b28f2afd0 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -6168,6 +6168,32 @@ RSpec.describe TopicsController do
expect(response.parsed_body["error_type"]).to eq("invalid_access")
end
end
+
+ context "when logged in as a moderator" do
+ it "blocks duration-based publishing to a category the moderator cannot create topics in" do
+ sign_in(moderator)
+
+ admin_only_category = Fabricate(:category)
+ admin_only_category.set_permissions(admins: :full)
+ admin_only_category.save!
+
+ moderator_guardian = Guardian.new(moderator)
+ expect(moderator_guardian.can_moderate?(topic)).to eq(true)
+ expect(moderator_guardian.can_create_topic_on_category?(admin_only_category)).to eq(false)
+
+ post "/t/#{topic.id}/timer.json",
+ params: {
+ duration_minutes: 60,
+ based_on_last_post: true,
+ status_type: "publish_to_category",
+ category_id: admin_only_category.id,
+ }
+
+ expect(response.status).to eq(403)
+ expect(response.parsed_body["error_type"]).to eq("invalid_access")
+ expect(topic.reload.public_topic_timer).to eq(nil)
+ end
+ end
end
describe "#set_slow_mode" do