A platform for community discussion. Free, open, simple.
SECURITY: Require whisper authorization when changing a post's type to whisper (#43940)
Sam830dd55edd7e
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rbindex d3a75203de3..25da1b05294 100644--- a/app/controllers/posts_controller.rb+++ b/app/controllers/posts_controller.rb@@ -714,6 +714,14 @@ class PostsController < ApplicationControllerraise Discourse::InvalidParameters.new(:post_type)end+ if post_type == Post.types[:whisper] && !guardian.can_create_whisper?+ raise Discourse::InvalidAccess.new(+ "invalid_whisper_access",+ nil,+ custom_message: "invalid_whisper_access",+ )+ end+post.revise(current_user, post_type: post_type)render body: nildiff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rbindex 70accb15b2a..2ad90c5c406 100644--- a/spec/requests/posts_controller_spec.rb+++ b/spec/requests/posts_controller_spec.rb@@ -1426,6 +1426,22 @@ RSpec.describe PostsController doexpect(post.reload.post_type).to eq(Post.types[:regular])end+ it "prevents moderators outside whisper groups from converting public replies to whispers" do+ whisper_group = Fabricate(:group)+ SiteSetting.whispers_allowed_groups = whisper_group.id.to_s+ topic = Fabricate(:topic)+ Fabricate(:post, topic:)+ public_reply = Fabricate(:post, topic:, raw: "public reply that must remain visible")++ put "/posts/#{public_reply.id}/post_type.json", params: { post_type: Post.types[:whisper] }++ aggregate_failures do+ expect(response).to be_forbidden+ expect(response.body).to include(I18n.t("invalid_whisper_access"))+ expect(public_reply.reload.post_type).to eq(Post.types[:regular])+ end+ end+it "rejects changing an opening post to a whisper" doopening_post = Fabricate(:post)