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.

Refuse people list parameters of the wrong shape without Ruby's insides

Guido Boykebb198ebe839f
diff --git a/app/controllers/api/v1/people_controller.rb b/app/controllers/api/v1/people_controller.rb
index c314202..fcf5ba3 100644
--- a/app/controllers/api/v1/people_controller.rb
+++ b/app/controllers/api/v1/people_controller.rb
@@ -70,12 +70,13 @@ module Api
def query
value = params[:q]
return nil if value.nil?
- raise InvalidParameter, "invalid q #{value.inspect} (expected text)" unless value.is_a?(String)
+ raise InvalidParameter, 'invalid q (expected text)' unless value.is_a?(String)
value.strip
end
- # A whole number from 1 to max; absent or blank is the default.
+ # A whole number from 1 to max; absent or blank is the default. A refused value is quoted
+ # back only when it is text: a list or a nested value would show Ruby's insides.
def whole_number(name, default:, max:)
raw = params[name]
return default if raw.nil? || raw == ''
@@ -83,7 +84,8 @@ module Api
number = Integer(raw, 10) if raw.is_a?(String) && raw.match?(/\A\d+\z/)
return number if number&.between?(1, max)
- raise InvalidParameter, "invalid #{name} #{raw.inspect} (expected a whole number from 1 to #{max})"
+ shown = raw.is_a?(String) ? " #{raw.inspect}" : ''
+ raise InvalidParameter, "invalid #{name}#{shown} (expected a whole number from 1 to #{max})"
end
end
end
diff --git a/spec/requests/api/v1/people_spec.rb b/spec/requests/api/v1/people_spec.rb
index 433c271..e41c1cd 100644
--- a/spec/requests/api/v1/people_spec.rb
+++ b/spec/requests/api/v1/people_spec.rb
@@ -194,6 +194,22 @@ RSpec.describe 'API V1 people', :ci_guard, type: :request do
end
end
end
+
+ # A list or a nested value is no number and no text either: 400, never a 500 from code that
+ # expected a string, and the message names the parameter without Ruby's insides.
+ { page: [['2'], { 'a' => '1' }], per_page: [['2'], { 'a' => '1' }], q: [['doris'], { 'a' => 'x' }] }
+ .each do |name, values|
+ values.each do |value|
+ it "refuses #{name} given as a #{value.class.name.downcase} with invalid_input" do
+ list(name => value)
+
+ expect(response).to have_http_status(:bad_request)
+ expect(body).to include('error' => 'bad_request', 'code' => 'invalid_input')
+ expect(body['message']).to start_with("invalid #{name} ")
+ expect(body['message']).not_to include('ActionController', 'Parameters', '[')
+ end
+ end
+ end
end
describe 'the card' do