Skip to content

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

SECURITY: Add post visibility check to poll voters endpoint

OsamaSayeghbc1993438452
diff --git a/plugins/poll/app/controllers/discourse_poll/polls_controller.rb b/plugins/poll/app/controllers/discourse_poll/polls_controller.rb
index 1a7a29135eb..fdbf929f2be 100644
--- a/plugins/poll/app/controllers/discourse_poll/polls_controller.rb
+++ b/plugins/poll/app/controllers/discourse_poll/polls_controller.rb
@@ -48,10 +48,16 @@ class DiscoursePoll::PollsController < ::ApplicationController
poll_name = params.require(:poll_name)
opts = params.permit(:limit, :page, :option_id)
- raise Discourse::InvalidParameters.new(:post_id) if !Post.where(id: post_id).exists?
+ poll = Poll.includes(:post).find_by(post_id: post_id, name: poll_name)
- poll = Poll.find_by(post_id: post_id, name: poll_name)
- raise Discourse::InvalidParameters.new(:poll_name) if !poll&.can_see_voters?(current_user)
+ if !poll
+ raise Discourse::InvalidParameters.new(:post_id) if !Post.where(id: post_id).exists?
+ raise Discourse::InvalidParameters.new(:poll_name)
+ end
+
+ guardian.ensure_can_see!(poll.post)
+
+ raise Discourse::InvalidParameters.new(:poll_name) if !poll.can_see_voters?(current_user)
render json: { voters: DiscoursePoll::Poll.serialized_voters(poll, opts) }
end
diff --git a/plugins/poll/spec/controllers/polls_controller_spec.rb b/plugins/poll/spec/controllers/polls_controller_spec.rb
index dfa14025546..a23edaed567 100644
--- a/plugins/poll/spec/controllers/polls_controller_spec.rb
+++ b/plugins/poll/spec/controllers/polls_controller_spec.rb
@@ -494,5 +494,43 @@ RSpec.describe DiscoursePoll::PollsController do
expect(json["voters"][first].size).to eq(1)
end
+
+ context "with private category polls" do
+ fab!(:group)
+ fab!(:private_category) { Fabricate(:private_category, group: group) }
+ fab!(:private_topic) { Fabricate(:topic, category: private_category) }
+ fab!(:poll_creator) { Fabricate(:admin).tap { |u| group.add(u) } }
+ fab!(:private_poll) do
+ Fabricate(
+ :post,
+ topic: private_topic,
+ user: poll_creator,
+ raw: "[poll public=true]\n- A\n- B\n[/poll]",
+ )
+ end
+ fab!(:user_in_group) { Fabricate(:user).tap { |u| group.add(u) } }
+ fab!(:user_outside_group, :user)
+
+ before { DiscoursePoll::Poll.vote(user_in_group, private_poll.id, "poll", [first]) }
+
+ it "prevents users without permission from seeing voters" do
+ log_in_user(user_outside_group)
+
+ get :voters, params: { post_id: private_poll.id, poll_name: "poll" }, format: :json
+
+ expect(response.status).to eq(403)
+ end
+
+ it "allows users with permission to see voters" do
+ log_in_user(user_in_group)
+
+ get :voters, params: { post_id: private_poll.id, poll_name: "poll" }, format: :json
+
+ expect(response.status).to eq(200)
+ json = response.parsed_body
+ expect(json["voters"][first].size).to eq(1)
+ expect(json["voters"][first][0]["id"]).to eq(user_in_group.id)
+ end
+ end
end
end