Skip to content

Commit 2a0ac54

Browse files
Gargronhiyuki2578
authored andcommitted
Fix poll API not requiring authentication on non-public polls (mastodon#10960)
* Fix poll API not requiring authentication on non-public polls That API does not reveal the content of the status, i.e. the question itself, nor who the author is, nor which status it belongs to, but it does reveal the poll options and how many answers they got Fix mastodon#10959 * Add test
1 parent 290d565 commit 2a0ac54

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
# frozen_string_literal: true
22

33
class Api::V1::PollsController < Api::BaseController
4+
include Authorization
5+
46
before_action -> { authorize_if_got_token! :read, :'read:statuses' }, only: :show
7+
before_action :set_poll
8+
before_action :refresh_poll
59

610
respond_to :json
711

812
def show
13+
render json: @poll, serializer: REST::PollSerializer, include_results: true
14+
end
15+
16+
private
17+
18+
def set_poll
919
@poll = Poll.attached.find(params[:id])
20+
authorize @poll.status, :show?
21+
rescue Mastodon::NotPermittedError
22+
raise ActiveRecord::RecordNotFound
23+
end
24+
25+
def refresh_poll
1026
ActivityPub::FetchRemotePollService.new.call(@poll, current_account) if user_signed_in? && @poll.possibly_stale?
11-
render json: @poll, serializer: REST::PollSerializer, include_results: true
1227
end
1328
end

spec/controllers/api/v1/polls_controller_spec.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,26 @@
1010
before { allow(controller).to receive(:doorkeeper_token) { token } }
1111

1212
describe 'GET #show' do
13-
let(:poll) { Fabricate(:poll) }
13+
let(:poll) { Fabricate(:poll, status: Fabricate(:status, visibility: visibility)) }
1414

1515
before do
1616
get :show, params: { id: poll.id }
1717
end
1818

19-
it 'returns http success' do
20-
expect(response).to have_http_status(200)
19+
context 'when parent status is public' do
20+
let(:visibility) { 'public' }
21+
22+
it 'returns http success' do
23+
expect(response).to have_http_status(200)
24+
end
25+
end
26+
27+
context 'when parent status is private' do
28+
let(:visibility) { 'private' }
29+
30+
it 'returns http not found' do
31+
expect(response).to have_http_status(404)
32+
end
2133
end
2234
end
2335
end

0 commit comments

Comments
 (0)