Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Pin the 403 for a people right lost between Pundit and the lookup

Guido Boyke7bfe03641230
diff --git a/app/controllers/api/v1/people_controller.rb b/app/controllers/api/v1/people_controller.rb
index 3593108..1a6a14e 100644
--- a/app/controllers/api/v1/people_controller.rb
+++ b/app/controllers/api/v1/people_controller.rb
@@ -26,6 +26,8 @@ module Api
# A list parameter the endpoint cannot take (q, page, per_page).
class InvalidParameter < StandardError; end
+ # Pundit asks the people right first (with_directory); this answers the same right taken
+ # away between that check and the lookup's own.
rescue_from People::Lookup::Refused, with: :forbidden
rescue_from People::Lookup::InvalidArgument, InvalidParameter, with: :bad_request
diff --git a/spec/requests/api/v1/people_spec.rb b/spec/requests/api/v1/people_spec.rb
index 6874461..6cc7722 100644
--- a/spec/requests/api/v1/people_spec.rb
+++ b/spec/requests/api/v1/people_spec.rb
@@ -340,6 +340,30 @@ RSpec.describe 'API V1 people', :ci_guard, type: :request do
expect(body).to eq('error' => 'forbidden', 'code' => 'forbidden')
end
+ # Pundit asks the people right first (authorize Person, :index?) and People::Lookup asks it
+ # again before it reads. A right taken away between the two is a 403 as well, never another
+ # status: the first answer of view_allowed? lets Pundit pass, the second one is the
+ # lookup's, and it refuses before the directory would ask a third time.
+ describe 'a right lost between the two checks' do
+ before { allow(PersonPolicy).to receive(:view_allowed?).and_return(true, false) }
+
+ it 'answers the list with 403 forbidden' do
+ list
+
+ expect(response).to have_http_status(:forbidden)
+ expect(body).to eq('error' => 'forbidden', 'code' => 'forbidden')
+ expect(PersonPolicy).to have_received(:view_allowed?).twice
+ end
+
+ it 'answers the card with 403 forbidden' do
+ show("person:#{contact.id}")
+
+ expect(response).to have_http_status(:forbidden)
+ expect(body).to eq('error' => 'forbidden', 'code' => 'forbidden')
+ expect(PersonPolicy).to have_received(:view_allowed?).twice
+ end
+ end
+
it 'asks the right before it looks at the parameters' do
plain = token(owner: colleague)