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.
Ruby agplai-assistantdockerdsgvogdprhotwireissue-trackerjira-alternativeknowledge-basellmmcp-servermeeting-notesopen-sourceproject-managementredmine-alternativeruby-on-railsself-hostedticketingtime-tracking
fix(api): keep ticket change responses inside the authorized project
Guido Boykef4abec0fe872
diff --git a/.github/workflows/guards.yml b/.github/workflows/guards.ymlindex 5569782..d13c292 100644--- a/.github/workflows/guards.yml+++ b/.github/workflows/guards.yml@@ -307,6 +307,7 @@ jobs:spec/requests/api/people_parity_spec.rbspec/requests/api/v1/post_comment_format_spec.rbspec/requests/api/v1/ticket_changes_spec.rb+ spec/requests/api/v1/ticket_changes_concurrency_spec.rbspec/requests/api/v1/ticket_changes_side_effects_spec.rbspec/requests/api/v1/knowledge_spec.rbspec/models/codebase_import_mapping_spec.rbdiff --git a/CLAUDE.md b/CLAUDE.mdindex 7ed844a..d022a04 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -937,9 +937,12 @@ text from `services.ticket_change_failure.*` in `errors` and, for a title the momessages behind it. The code is named in the controller: `Api::V1::ErrorBody` alone would read`validation_failed` as invalid input. Nobody is removed by a refused change, and a stale assignmentstays unless it is named. A ticket that moved or was deleted after the rights check answers the same-404; a failure the rule does not name answers 500 without its text. Guards:+404. After the write commits, the response takes a fresh lock in that same project and reads+the whole summary under it, so a concurrent move cannot disclose destination data.+A failure the rule does not name answers 500 without its text. Guards:`spec/requests/api/v1/ticket_changes_spec.rb`, `spec/requests/api/v1/ticket_changes_side_effects_spec.rb`-(the same trail as MCP), `spec/services/tickets/change_spec.rb`.+(the same trail as MCP), `spec/requests/api/v1/ticket_changes_concurrency_spec.rb`+(the response against a concurrent move), `spec/services/tickets/change_spec.rb`.## Admin: Betreiber-Teile kommen nur über Schlitzediff --git a/app/controllers/api/v1/tickets_controller.rb b/app/controllers/api/v1/tickets_controller.rbindex 501dc50..783e0f5 100644--- a/app/controllers/api/v1/tickets_controller.rb+++ b/app/controllers/api/v1/tickets_controller.rb@@ -136,7 +136,13 @@ module Apiraise ActiveRecord::RecordNotFound if outcome.gone?next render_change_failure(outcome) unless outcome.written?- render json: { data: ticket_serializer.summary(ticket.reload) }+ # The write released its lock. Keep the authorized project boundary+ # while reading the response, including the serializer's associations:+ # a move that won the race is a 404, and a later move waits for us.+ data = Ticket.transaction do+ ticket_serializer.summary(project.tickets.lock.find(ticket.id))+ end+ render json: { data: data }endenddiff --git a/spec/requests/api/v1/ticket_changes_concurrency_spec.rb b/spec/requests/api/v1/ticket_changes_concurrency_spec.rbnew file mode 100644index 0000000..e055929--- /dev/null+++ b/spec/requests/api/v1/ticket_changes_concurrency_spec.rb@@ -0,0 +1,66 @@+# frozen_string_literal: true++require 'rails_helper'++# The response reads associations after the write commits. A move must wait+# until that whole snapshot is read, or destination data could reach a caller+# who cannot read the destination project. Use real commits and two connections.+RSpec.describe 'Ticket change response during a concurrent move', :ci_guard, type: :request do+ self.use_transactional_tests = false++ before do+ DatabaseCleaner.clean+ DatabaseCleaner.strategy = :truncation+ DatabaseCleaner.start+ Prosopite.pause+ end++ after { Prosopite.resume }++ it 'keeps a move from committing while the response reads the ticket associations' do+ organisation = create(:organisation)+ user = create(:user)+ ActsAsTenant.current_tenant = organisation+ create(:membership, user: user, organisation: organisation, role: create(:role, organisation: organisation), active: true)+ project = create(:project, organisation: organisation)+ elsewhere = create(:project, organisation: organisation)+ create(:project_permission, user: user, project: project, tickets_access: :read_write)+ ticket = create(:ticket, project: project, organisation: organisation, creator: user)+ pat = build(:personal_access_token, user: user, organisations: [organisation], read_only: false)+ pat.token_hash = nil+ pat.token_prefix = nil+ pat.generate_token!+ worker = nil++ allow_any_instance_of(Tickets::Serializer).to receive(:summary).and_wrap_original do |original, record|+ worker = Thread.new do+ ActiveRecord::Base.connection_pool.with_connection do |connection|+ ActsAsTenant.with_tenant(organisation) do+ Ticket.transaction do+ connection.execute("SET LOCAL lock_timeout = '100ms'")+ Ticket.where(id: ticket.id).update_all(project_id: elsewhere.id, title: 'Private destination title')+ end+ end+ end+ rescue ActiveRecord::LockWaitTimeout => e+ e+ end+ expect(worker.join(10)).to be_present+ expect(worker.value).to be_a(ActiveRecord::LockWaitTimeout)+ original.call(record)+ end++ post "/api/v1/tickets/#{ticket.id}/changes", params: { change: { title: 'Renamed' } }.to_json,+ headers: { 'Authorization' => "Bearer #{pat.token}", 'Content-Type' => 'application/json' }++ expect(response).to have_http_status(:ok)+ expect(response.parsed_body.dig('data', 'title')).to eq('Renamed')+ # The response has finished: the move can now acquire the same row.+ ActsAsTenant.with_tenant(organisation) do+ expect(Ticket.where(id: ticket.id).update_all(project_id: elsewhere.id)).to eq(1)+ end+ ensure+ worker&.join(2)+ worker&.kill if worker&.alive?+ end+enddiff --git a/spec/requests/api/v1/ticket_changes_spec.rb b/spec/requests/api/v1/ticket_changes_spec.rbindex 4de51dd..7867eee 100644--- a/spec/requests/api/v1/ticket_changes_spec.rb+++ b/spec/requests/api/v1/ticket_changes_spec.rb@@ -689,6 +689,29 @@ RSpec.describe 'API V1 ticket changes', :ci_guard, type: :request doendend+ describe 'a ticket that moves after the write' do+ { 'title' => { title: 'Renamed' }, 'status' => { status: 'wip' }, 'assignees' => { add_assignee_ids: ['me'] } }.each do |kind, fields|+ it "does not disclose or cache the destination's data after the #{kind} change" do+ elsewhere = Prosopite.pause { in_org { create(:project, organisation: organisation) } }+ allow_any_instance_of(Tickets::Change).to receive(:write).and_wrap_original do |original, **kwargs|+ original.call(**kwargs).tap do |outcome|+ expect(outcome).to be_written+ # The write released its lock. Another request moves the ticket+ # into a project this caller cannot read, then changes its title.+ Ticket.where(id: ticket.id).update_all(project_id: elsewhere.id, title: 'Private destination title')+ end+ end++ send_change(fields, key: 'moved-before-response')++ expect(response).to have_http_status(:not_found)+ expect(response.body).to eq('{"error":"not_found","code":"not_found"}')+ expect(reloaded.title).to eq('Private destination title')+ expect(McpIdempotencyKey.where(personal_access_token: pat)).not_to exist+ end+ end+ end+describe 'a failure the rule does not name' dolet(:secret) { 'PG::Error: secret detail' }