Skip to content

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

FIX: Prevent flagging inaccessible topics (#42362)

Isaac Janzen332e23314bd6
diff --git a/app/controllers/post_actions_controller.rb b/app/controllers/post_actions_controller.rb
index a8a1c273692..13be32d0535 100644
--- a/app/controllers/post_actions_controller.rb
+++ b/app/controllers/post_actions_controller.rb
@@ -66,11 +66,13 @@ class PostActionsController < ApplicationController
post_id =
if flag_topic
- begin
- Topic.find(params[:id]).posts.first.id
- rescue StandardError
- raise Discourse::NotFound
- end
+ topic = Topic.find_by(id: params[:id])
+ raise Discourse::NotFound unless guardian.can_see_topic?(topic)
+
+ post = topic.posts.first
+ raise Discourse::NotFound if post.blank?
+
+ post.id
else
params[:id]
end
diff --git a/spec/requests/post_actions_controller_spec.rb b/spec/requests/post_actions_controller_spec.rb
index 54498a98283..ce0bdba0004 100644
--- a/spec/requests/post_actions_controller_spec.rb
+++ b/spec/requests/post_actions_controller_spec.rb
@@ -116,6 +116,26 @@ RSpec.describe PostActionsController do
expect(response.status).to eq(403)
end
+ it "returns 404 when flagging a hidden topic" do
+ sign_in(user)
+ SiteSetting.detailed_404 = false
+ pm = Fabricate(:private_message_post, user: coding_horror)
+
+ get "/t/#{pm.topic.id}.json"
+ expect(response.status).to eq(404)
+ expect(response.parsed_body["errors"].first).to include(I18n.t("not_found"))
+
+ post "/post_actions.json",
+ params: {
+ id: pm.topic.id,
+ flag_topic: "true",
+ post_action_type_id: PostActionType.types[:inappropriate],
+ }
+
+ expect(response.status).to eq(404)
+ expect(response.parsed_body["errors"].first).to include(I18n.t("not_found"))
+ end
+
it "fails when the user tries to notify user that has disabled PM" do
sign_in(user)
user2 = Fabricate(:user)