A platform for community discussion. Free, open, simple.
SECURITY: Enforce poll result visibility in grouped poll results endpoint (#42947)
Sam16d59e8d399e
diff --git a/plugins/poll/lib/poll.rb b/plugins/poll/lib/poll.rbindex aeb7c504232..093f856ebb9 100644--- a/plugins/poll/lib/poll.rb+++ b/plugins/poll/lib/poll.rb@@ -337,6 +337,8 @@ class DiscoursePoll::Pollraise DiscoursePoll::Error.new I18n.t("poll.user_cant_post_in_topic")end+ raise Discourse::InvalidParameters.new(:poll_name) if !poll.can_see_results?(user)+if SiteSetting.poll_groupable_user_fields.split("|").exclude?(user_field_name)raise Discourse::InvalidParameters.new(:user_field_name)enddiff --git a/plugins/poll/spec/integration/poll_endpoints_spec.rb b/plugins/poll/spec/integration/poll_endpoints_spec.rbindex ebca79c3541..9b82ff35919 100644--- a/plugins/poll/spec/integration/poll_endpoints_spec.rb+++ b/plugins/poll/spec/integration/poll_endpoints_spec.rb@@ -182,6 +182,7 @@ RSpec.describe "DiscoursePoll endpoints" dofab!(:user2, :user)fab!(:user3, :user)fab!(:user4, :user)+ fab!(:admin)fab!(:post) { Fabricate(:post, raw: <<~SQL) }[poll type=multiple public=true min=1 max=2]@@ -198,6 +199,34 @@ RSpec.describe "DiscoursePoll endpoints" do[/poll]SQL+ fab!(:post_with_staff_only_poll) { Fabricate(:post, raw: <<~SQL) }+ [poll public=true results=staff_only]+ - A+ - B+ [/poll]+ SQL++ fab!(:post_with_on_close_poll) { Fabricate(:post, raw: <<~SQL) }+ [poll public=true results=on_close]+ - A+ - B+ [/poll]+ SQL++ fab!(:post_with_on_vote_poll) { Fabricate(:post, raw: <<~SQL) }+ [poll public=true results=on_vote]+ - A+ - B+ [/poll]+ SQL++ fab!(:post_with_secret_poll) { Fabricate(:post, raw: <<~SQL) }+ [poll public=false results=always]+ - A+ - B+ [/poll]+ SQL+let(:option_a) { "5c24fc1df56d764b550ceae1b9319125" }let(:option_b) { "e89dec30bbd9bf50fabf6a05b4324edf" }@@ -346,6 +375,85 @@ RSpec.describe "DiscoursePoll endpoints" doexpect(response.body).to include("user_field_name")end+ it "only returns grouped tallies to users who can see poll results" do+ SiteSetting.poll_groupable_user_fields = "something"++ [+ post_with_staff_only_poll,+ post_with_on_close_poll,+ post_with_on_vote_poll,+ post_with_secret_poll,+ ].each do |poll_post|+ DiscoursePoll::Poll.vote(user2, poll_post.id, DiscoursePoll::DEFAULT_POLL_NAME, [option_a])+ end++ [+ post_with_staff_only_poll,+ post_with_on_close_poll,+ post_with_on_vote_poll,+ ].each do |poll_post|+ get "/polls/grouped_poll_results.json",+ params: {+ post_id: poll_post.id,+ poll_name: DiscoursePoll::DEFAULT_POLL_NAME,+ user_field_name: "something",+ }++ expect(response).to have_http_status(:bad_request)+ expect(response.parsed_body["error_type"]).to eq("invalid_parameters")+ expect(response.parsed_body).not_to have_key("grouped_results")+ end++ sign_in(admin)+ get "/polls/grouped_poll_results.json",+ params: {+ post_id: post_with_staff_only_poll.id,+ poll_name: DiscoursePoll::DEFAULT_POLL_NAME,+ user_field_name: "something",+ }++ expect(response).to have_http_status(:success)+ expect(response.parsed_body).to have_key("grouped_results")++ sign_in(user1)+ post_with_on_close_poll.polls.first.update!(status: :closed)+ get "/polls/grouped_poll_results.json",+ params: {+ post_id: post_with_on_close_poll.id,+ poll_name: DiscoursePoll::DEFAULT_POLL_NAME,+ user_field_name: "something",+ }++ expect(response).to have_http_status(:success)+ expect(response.parsed_body).to have_key("grouped_results")++ DiscoursePoll::Poll.vote(+ user1,+ post_with_on_vote_poll.id,+ DiscoursePoll::DEFAULT_POLL_NAME,+ [option_b],+ )+ get "/polls/grouped_poll_results.json",+ params: {+ post_id: post_with_on_vote_poll.id,+ poll_name: DiscoursePoll::DEFAULT_POLL_NAME,+ user_field_name: "something",+ }++ expect(response).to have_http_status(:success)+ expect(response.parsed_body).to have_key("grouped_results")++ get "/polls/grouped_poll_results.json",+ params: {+ post_id: post_with_secret_poll.id,+ poll_name: DiscoursePoll::DEFAULT_POLL_NAME,+ user_field_name: "something",+ }++ expect(response).to have_http_status(:success)+ expect(response.parsed_body).to have_key("grouped_results")+ end+context "when topic is in a private category" dofab!(:admin)fab!(:group)