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.

fix(api): validate structured time booking inputs

Guido Boykeac4a327109ba
diff --git a/app/controllers/api/v1/time_sessions_controller.rb b/app/controllers/api/v1/time_sessions_controller.rb
index 04dbcb5..62f9e08 100644
--- a/app/controllers/api/v1/time_sessions_controller.rb
+++ b/app/controllers/api/v1/time_sessions_controller.rb
@@ -64,7 +64,7 @@ module Api
# token can read and tells nothing the 404 does not.
def create
with_readable_ticket(params[:ticket_id]) do |ticket, _project|
- booking = TimeSessions::Booking.parse(actor: current_user, **session_params.to_h.symbolize_keys)
+ booking = TimeSessions::Booking.parse(actor: current_user, **session_params)
next render_booking_refusal(booking.refusal) if booking.refused?
session = booking.build(ticket: ticket)
@@ -98,10 +98,18 @@ module Api
end
def session_params
- params.require(:time_session).permit(
+ input = params.require(:time_session)
+ permitted = input.permit(
:occurred_on, :duration_minutes, :duration_string,
:description, :customer_description
)
+ # Booking must see supplied arrays/objects too: scalar permit drops them,
+ # turning an invalid date into today or hiding a second duration. Only
+ # these three explicit inputs bypass filtering; Booking validates them.
+ permitted.to_h.symbolize_keys.merge(
+ occurred_on: input[:occurred_on], duration_minutes: input[:duration_minutes],
+ duration_string: input[:duration_string]
+ )
end
def paginate(relation)
diff --git a/spec/requests/api/v1/time_session_booking_spec.rb b/spec/requests/api/v1/time_session_booking_spec.rb
index afb8093..2c4725f 100644
--- a/spec/requests/api/v1/time_session_booking_spec.rb
+++ b/spec/requests/api/v1/time_session_booking_spec.rb
@@ -45,6 +45,11 @@ RSpec.describe 'API V1 time booking', :ci_guard, type: :request do
JSON.parse(response.body)
end
+ def book_json(**time_session)
+ post "/api/v1/tickets/#{ticket.id}/time_sessions", params: { time_session: time_session },
+ headers: { 'Authorization' => "Bearer #{pat.token}" }, as: :json
+ end
+
def bookings
ActsAsTenant.without_tenant { TimeSession.where(ticket_id: ticket.id).count }
end
@@ -107,6 +112,32 @@ RSpec.describe 'API V1 time booking', :ci_guard, type: :request do
end
end
+ describe 'structured booking input' do
+ # Scalar strong parameters used to drop these values before Booking could
+ # refuse them, turning an invalid date into today or hiding a second duration.
+ [[], {}, ['30'], { value: '30' }].each do |value|
+ {
+ 'invalid date' => [{ duration_minutes: 30, occurred_on: value }, :occurred_on_invalid],
+ 'invalid minutes' => [{ duration_minutes: value }, :duration_minutes_invalid],
+ 'second duration as minutes' => [{ duration_string: '30', duration_minutes: value }, :both_durations],
+ 'second duration as text' => [{ duration_minutes: 30, duration_string: value }, :both_durations]
+ }.each do |name, (input, refusal)|
+ it "refuses #{name} #{value.inspect}, writing nothing" do
+ expect { book_json(**input) }.not_to(change { bookings })
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(body).to eq('error' => 'validation_failed', 'code' => 'invalid_input',
+ 'errors' => [I18n.t("services.time_session_booking.#{refusal}", locale: :en)])
+ end
+ end
+ end
+
+ it 'still ignores attributes outside the booking allowlist' do
+ book_json(duration_minutes: 30, creator_id: SecureRandom.uuid, project_id: SecureRandom.uuid, billed: true)
+ expect(response).to have_http_status(:created)
+ expect(booked).to have_attributes(creator_id: user.id, project_id: project.id, billed: false)
+ end
+ end
+
describe 'the day (a calendar day in the profile zone, stored as midnight)' do
it 'defaults to today in the profile zone, also right after midnight there' do
travel_to Time.utc(2026, 8, 19, 22, 30) do # 00:30 on Aug 20 in Berlin