Skip to content

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

SECURITY: Fix poll post_id array authorization bypass

Natalie Tay1a6b3cdd8939
diff --git a/plugins/poll/lib/poll.rb b/plugins/poll/lib/poll.rb
index ae32c7cc9c4..aa5826bec9f 100644
--- a/plugins/poll/lib/poll.rb
+++ b/plugins/poll/lib/poll.rb
@@ -163,6 +163,7 @@ class DiscoursePoll::Poll
def self.toggle_status(user, post_id, poll_name, status, raise_errors = true)
Poll.transaction do
post = Post.find_by(id: post_id)
+ post_id = post&.id
guardian = Guardian.new(user)
# post must not be deleted
@@ -187,7 +188,7 @@ class DiscoursePoll::Poll
return
end
- poll = Poll.find_by(post_id: post_id, name: poll_name)
+ poll = Poll.find_by(post_id:, name: poll_name)
if !poll
if raise_errors
@@ -200,7 +201,7 @@ class DiscoursePoll::Poll
poll.save!
serialized_poll = PollSerializer.new(poll, root: false, scope: guardian).as_json
- payload = { post_id: post_id, polls: [serialized_poll] }
+ payload = { post_id:, polls: [serialized_poll] }
post.publish_message!("/polls/#{post.topic_id}", payload)
@@ -487,6 +488,7 @@ class DiscoursePoll::Poll
def self.change_vote(user, post_id, poll_name)
Poll.transaction do
post = Post.find_by(id: post_id)
+ post_id = post&.id
# post must not be deleted
raise DiscoursePoll::Error.new I18n.t("poll.post_is_deleted") if post.nil? || post.trashed?
@@ -502,7 +504,7 @@ class DiscoursePoll::Poll
raise DiscoursePoll::Error.new I18n.t("poll.user_cant_post_in_topic")
end
- poll = Poll.includes(:poll_options).find_by(post_id: post_id, name: poll_name)
+ poll = Poll.includes(:poll_options).find_by(post_id:, name: poll_name)
unless poll
raise DiscoursePoll::Error.new I18n.t("poll.no_poll_with_this_name", name: poll_name)
@@ -522,7 +524,7 @@ class DiscoursePoll::Poll
poll.reload
serialized_poll = PollSerializer.new(poll, root: false, scope: guardian).as_json
- payload = { post_id: post_id, polls: [serialized_poll] }
+ payload = { post_id:, polls: [serialized_poll] }
post.publish_message!("/polls/#{post.topic_id}", payload)
diff --git a/plugins/poll/spec/controllers/polls_controller_spec.rb b/plugins/poll/spec/controllers/polls_controller_spec.rb
index a23edaed567..a0d0774104f 100644
--- a/plugins/poll/spec/controllers/polls_controller_spec.rb
+++ b/plugins/poll/spec/controllers/polls_controller_spec.rb
@@ -533,4 +533,82 @@ RSpec.describe DiscoursePoll::PollsController do
end
end
end
+
+ context "when post_id does not match the poll's post" do
+ it "does not allow voting on another post's poll" do
+ accessible_post = Fabricate(:post, topic: topic, user: user)
+
+ group = Fabricate(:group)
+ private_category = Fabricate(:private_category, group: group)
+ private_topic = Fabricate(:topic, category: private_category)
+ private_poll_post =
+ Fabricate(
+ :post,
+ topic: private_topic,
+ user: Fabricate(:admin).tap { |u| group.add(u) },
+ raw: "[poll]\n- A\n- B\n[/poll]",
+ )
+
+ expect(accessible_post.id).to be < private_poll_post.id
+
+ put :vote,
+ params: {
+ post_id: [accessible_post.id, private_poll_post.id],
+ poll_name: "poll",
+ options: ["5c24fc1df56d764b550ceae1b9319125"],
+ },
+ format: :json
+
+ expect(response.status).not_to eq(200)
+ expect(PollVote.where(user_id: user.id).count).to eq(0)
+ end
+
+ it "does not allow removing a vote from another post's poll" do
+ accessible_post = Fabricate(:post, topic: topic, user: user)
+
+ group = Fabricate(:group)
+ private_category = Fabricate(:private_category, group: group)
+ private_topic = Fabricate(:topic, category: private_category)
+ private_poll_post =
+ Fabricate(
+ :post,
+ topic: private_topic,
+ user: Fabricate(:admin).tap { |u| group.add(u) },
+ raw: "[poll]\n- A\n- B\n[/poll]",
+ )
+
+ expect(accessible_post.id).to be < private_poll_post.id
+
+ delete :remove_vote,
+ params: {
+ post_id: [accessible_post.id, private_poll_post.id],
+ poll_name: "poll",
+ },
+ format: :json
+
+ expect(response.status).not_to eq(200)
+ end
+
+ it "does not allow toggling status of another post's poll" do
+ own_post = Fabricate(:post, topic: topic, user: user)
+
+ other_topic = Fabricate(:topic)
+ other_user = Fabricate(:user, trust_level: TrustLevel[1])
+ victim_poll_post =
+ Fabricate(:post, topic: other_topic, user: other_user, raw: "[poll]\n- A\n- B\n[/poll]")
+
+ expect(own_post.id).to be < victim_poll_post.id
+
+ put :toggle_status,
+ params: {
+ post_id: [own_post.id, victim_poll_post.id],
+ poll_name: "poll",
+ status: "closed",
+ },
+ format: :json
+
+ expect(response.status).not_to eq(200)
+ expect(victim_poll_post.polls.first.reload.status).to eq("open")
+ end
+ end
end