Skip to content

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

SECURITY: Check topic visibility before allowing poll interactions

Isaac Janzend74ff25db994
diff --git a/plugins/poll/lib/poll.rb b/plugins/poll/lib/poll.rb
index aa5826bec9f..09abd0fa91d 100644
--- a/plugins/poll/lib/poll.rb
+++ b/plugins/poll/lib/poll.rb
@@ -188,6 +188,12 @@ class DiscoursePoll::Poll
return
end
+ # user must be able to see the topic
+ unless guardian.can_see_topic?(post.topic)
+ raise DiscoursePoll::Error.new I18n.t("poll.user_cant_post_in_topic") if raise_errors
+ return
+ end
+
poll = Poll.find_by(post_id:, name: poll_name)
if !poll
diff --git a/plugins/poll/spec/integration/poll_endpoints_spec.rb b/plugins/poll/spec/integration/poll_endpoints_spec.rb
index f642beed2be..baa523017ac 100644
--- a/plugins/poll/spec/integration/poll_endpoints_spec.rb
+++ b/plugins/poll/spec/integration/poll_endpoints_spec.rb
@@ -462,4 +462,50 @@ RSpec.describe "DiscoursePoll endpoints" do
end
end
end
+
+ describe "toggle_status" do
+ fab!(:user) { Fabricate(:user, trust_level: TrustLevel[1]) }
+ fab!(:group)
+ fab!(:private_category) { Fabricate(:private_category, group: group) }
+ fab!(:private_topic) { Fabricate(:topic, category: private_category) }
+
+ before do
+ sign_in(user)
+ group.add(user)
+ Group.refresh_automatic_groups!
+ end
+
+ fab!(:private_poll_post) do
+ Fabricate(:post, topic: private_topic, user: user, raw: "[poll]\n- A\n- B\n[/poll]")
+ end
+
+ it "prevents OP from toggling poll status when they no longer have access to the topic" do
+ put "/polls/toggle_status.json",
+ params: {
+ post_id: private_poll_post.id,
+ poll_name: "poll",
+ status: "closed",
+ }
+ expect(response.status).to eq(200)
+
+ put "/polls/toggle_status.json",
+ params: {
+ post_id: private_poll_post.id,
+ poll_name: "poll",
+ status: "open",
+ }
+ expect(response.status).to eq(200)
+
+ group.remove(user)
+
+ put "/polls/toggle_status.json",
+ params: {
+ post_id: private_poll_post.id,
+ poll_name: "poll",
+ status: "closed",
+ }
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"][0]).to eq(I18n.t("poll.user_cant_post_in_topic"))
+ end
+ end
end