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: keep private comments out of the ticket list search of REST and MCP

Guido Boyke0393ba16d2d6
diff --git a/.github/workflows/guards.yml b/.github/workflows/guards.yml
index 5ad0275..a809ff0 100644
--- a/.github/workflows/guards.yml
+++ b/.github/workflows/guards.yml
@@ -267,6 +267,7 @@ jobs:
spec/requests/api/mcp_get_meeting_spec.rb
spec/requests/api/v1/projects_listing_spec.rb
spec/requests/api/mcp_idempotency_key_bound_spec.rb
+ spec/requests/api/ticket_list_search_private_comments_spec.rb
spec/services/mcp/idempotency_store_spec.rb
spec/services/mcp/tool_call_token_context_spec.rb
spec/services/personal_access_tokens/reach_listing_spec.rb
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dfa012d..87efa1d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,6 +32,14 @@ starts (`db:prepare lalabase:data_backfills`). Without Docker, run
show. An image that cannot be served shows as its alt text, one with a full address
becomes a link. SVG is not shown yet. Limits: 5 MB per image, 200 images per set.
+### Security
+
+- The search of the ticket list in `/api/v1` and in the MCP tool `list_tickets` (`q`) no
+ longer matches the text of comments. It matched every comment, private ones the reader may
+ not read included, so a hit told the searcher what a private comment said. It now searches
+ title and description, as `list_tickets` describes it. The search on the web pages was not
+ affected.
+
### Upgrade notes
- Run `bin/rails documentation:resync_all` once after updating. Until then a page keeps
diff --git a/app/models/ticket.rb b/app/models/ticket.rb
index 41d844b..dadec95 100644
--- a/app/models/ticket.rb
+++ b/app/models/ticket.rb
@@ -137,24 +137,20 @@ class Ticket < ApplicationRecord
validate :parent_unchanged, on: :update
include PgSearch::Model
- pg_search_scope :search_full_text, using: {
- tsearch: { prefix: true }
- }, against: [:title],
- associated_against: {
- posts: [:plain_text_version],
- rich_text_content: [:body]
- }
+ # Title and description, what every reader of the ticket may read. Never add `posts` to
+ # associated_against: a pg_search join cannot ask Post.readable_by, so a hidden private
+ # comment would make its ticket a hit, and prefix matching spells out its words. Until
+ # 2026-11-05 it did, in the ticket list of REST and MCP
+ # (spec/requests/api/ticket_list_search_private_comments_spec.rb). A search over comments
+ # joins the readable ones only, as TicketSearchQuery#readable_posts_join does.
+ pg_search_scope :search_title_and_description, using: { tsearch: { prefix: true } },
+ against: [:title],
+ associated_against: { rich_text_content: [:body] }
# The title alone, same settings. For a picker that lists tickets from several projects
- # (Tickets::Lookup): search_full_text also matches every comment, private ones included.
+ # (Tickets::Lookup).
pg_search_scope :search_title, using: { tsearch: { prefix: true } }, against: [:title]
- # pg_search_scope :keyword_search, against: [:title, :content],
- # associated_against: {posts: :content},
- # :using => {
- # :tsearch => {:any_word => true, :prefix => true}
- # }
-
# Plain text version for embeddings
def plain_text_version
"#{title}\n#{content&.to_plain_text || ''}"
@@ -182,21 +178,12 @@ class Ticket < ApplicationRecord
def self.text_search(query)
if query.present?
- search_full_text(query)
+ search_title_and_description(query)
else
all
end
end
- # def self.text_search_keywords(keyword)
- # if keyword.present?
- # k = keyword.title.gsub(',',' ')
- # keyword_search(k)
- # else
- # all
- # end
- # end
-
# Display title with number prefix
def display_title
"##{number} #{title}"
diff --git a/app/services/CLAUDE.md b/app/services/CLAUDE.md
index 43c019c..62fb367 100644
--- a/app/services/CLAUDE.md
+++ b/app/services/CLAUDE.md
@@ -1,4 +1,4 @@
-# Karte: app/services (Stand: 2026-11-04)
+# Karte: app/services (Stand: 2026-11-05)
**Entry-Point-Konvention für NEUE Services: `Klass.call(**kwargs)`** (Klassenmethode; eine
Instanz-`#call`-Implementierung dahinter ist erlaubt). Pflicht: mindestens 1 Kommentarzeile
@@ -620,7 +620,9 @@ Bekannte Doppel-Verortungen (Altbestand; Boy-Scout-Regel: bei Berührung konsoli
hat dazu `open_ids` (dieselbe Erledigt-Regel für die graue Anzeige), `tickets/relation_section.rb`
liest Abschnitt und Kopf-Hinweis EINES Tickets in fester Abfragezahl (nur sichtbare Beziehungen,
der Hinweis zählt trotzdem alle Blocker), `tickets/lookup.rb` ist die Suche des Pickers (Nummer
- oder Titel über `Ticket.search_title`, NIE `search_full_text`: das trifft auch private Kommentare).
+ oder Titel über `Ticket.search_title`). Die Suche `q` der Ticketliste (REST und MCP, `tickets/filter.rb`) geht über
+ `Ticket.search_title_and_description`: Titel und Beschreibung, keine Kommentare. Bis zum 05.11.2026
+ traf sie jeden Kommentar, private eingeschlossen; ein pg_search-Join kann `readable_by` nicht fragen.
Seit PR 4 (Filter): `tickets/waiting_state_filter.rb` macht aus „wartet“/„hält auf“ eine
Ja/Nein-Bedingung („ist nicht“ = der Rest der Liste, erledigte Tickets eingeschlossen); Leiste
(`AdHocFilterService`) und Ansicht (`SmartFolders::Filters::WaitingFilter`/`BlockingFilter`)
diff --git a/app/services/tickets/lookup.rb b/app/services/tickets/lookup.rb
index eff848f..a2e030a 100644
--- a/app/services/tickets/lookup.rb
+++ b/app/services/tickets/lookup.rb
@@ -4,9 +4,9 @@
# up to LIMIT tickets of `scope` whose number is the term, then those whose title matches it.
#
# The caller hands in what the user may read (policy_scope(Ticket)); relations cross projects,
-# so this search must never be run over anything wider. It searches number and TITLE only:
-# Ticket.search_full_text also matches the text of every comment, private ones included, and a
-# hit there would tell the searcher what a private comment says.
+# so this search must never be run over anything wider. It searches number and TITLE only: a
+# picker term is a title, and a search over comment text would need the readable ones only
+# (Post.readable_by), or a hit would tell the searcher what a private comment says.
#
# No SQL is built from the term here. The number goes in as an integer, the title through
# pg_search (Ticket.search_title), which strips tsquery syntax and quotes the rest; the raw
diff --git a/spec/requests/api/ticket_list_search_private_comments_spec.rb b/spec/requests/api/ticket_list_search_private_comments_spec.rb
new file mode 100644
index 0000000..a07b046
--- /dev/null
+++ b/spec/requests/api/ticket_list_search_private_comments_spec.rb
@@ -0,0 +1,104 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# The search of the ticket list (`q`) must not match the text of a private
+# comment the reader may not read. Both surfaces share Tickets::Filter: REST
+# GET /api/v1/projects/:id/tickets and the MCP tool list_tickets.
+#
+# Until 2026-11-05 the search ran a pg_search scope that joined every
+# comment of the ticket and never asked Post.readable_by. A hidden private
+# comment made its ticket a hit, and with prefix matching its words could be
+# guessed one letter at a time. The post-visibility guard did not see it: the
+# join was declared in a pg_search_scope, with no `.posts` in the code.
+RSpec.describe 'Ticket list search and private comments', :ci_guard, type: :request do
+ let(:organisation) { create(:organisation) }
+ let(:reader) { create(:user) }
+ let(:author) { create(:user) }
+ let(:role) { create(:role, organisation: organisation) }
+ let(:project) { ActsAsTenant.with_tenant(organisation) { create(:project, organisation: organisation) } }
+
+ let(:secret_word) { 'Zebrafinanzgeheimnis' }
+
+ before do
+ ActsAsTenant.with_tenant(organisation) do
+ [reader, author].each do |user|
+ create(:membership, user: user, organisation: organisation, role: role, active: true)
+ create(:project_permission, user: user, project: project, tickets_access: :read_write)
+ end
+ end
+ end
+
+ let(:pat) do
+ token = build(:personal_access_token, user: reader, organisations: [organisation])
+ token.token_hash = nil
+ token.token_prefix = nil
+ token.generate_token!
+ token
+ end
+
+ # A ticket the reader can read, carrying a private comment addressed to its
+ # author only: the reader may read the ticket but not that comment.
+ let!(:ticket) do
+ ActsAsTenant.with_tenant(organisation) do
+ ticket = create(:ticket, project: project, organisation: organisation, creator: author,
+ title: 'Harmloser Titel', content: 'Eine Beschreibung mit Ananaskuchen')
+ comment = create(:post, ticket: ticket, user: author, organisation: organisation, is_private: true,
+ visible_to_user: author, content: secret_word)
+ expect(comment.reload.plain_text_version).to include(secret_word)
+ expect(comment.readable_by?(reader)).to be(false)
+ ticket
+ end
+ end
+
+ def rest_ids(query)
+ get "/api/v1/projects/#{project.id}/tickets", params: { q: query },
+ headers: { 'Authorization' => "Bearer #{pat.token}" }
+ expect(response).to have_http_status(:ok)
+ JSON.parse(response.body).fetch('data').map { |row| row['id'] }
+ end
+
+ def mcp_ids(query)
+ body = { jsonrpc: '2.0', id: 1, method: 'tools/call',
+ params: { name: 'list_tickets', arguments: { project_id: project.id, q: query } } }.to_json
+ post '/api/mcp', params: body,
+ headers: { 'Authorization' => "Bearer #{pat.token}", 'CONTENT_TYPE' => 'application/json' }
+ expect(response).to have_http_status(:ok)
+ result = JSON.parse(response.body).fetch('result')
+ expect(result['isError']).to be_falsey
+ JSON.parse(result.dig('content', 0, 'text')).fetch('data').map { |row| row['id'] }
+ end
+
+ # A pg_search join cannot ask Post.readable_by, so no search scope may join
+ # comments. Read from the declarations, not from one scope: a second scope
+ # with the same join would leak through whichever caller picks it. A search
+ # over readable comments joins them itself (TicketSearchQuery).
+ it 'no pg_search scope in app/models joins comments' do
+ offenders = Rails.root.glob('app/models/**/*.rb').flat_map do |path|
+ source = path.read.lines.reject { |line| line.lstrip.start_with?('#') }.join
+ source.scan(/pg_search_scope\s+:\w+.*?(?=\n\s*\n|\z)/m)
+ .select { |declaration| declaration.match?(/associated_against:\s*\{[^}]*\bposts\b/m) }
+ .map { |declaration| "#{path.relative_path_from(Rails.root)}: #{declaration.lines.first.strip}" }
+ end
+ expect(offenders).to be_empty
+ end
+
+ %i[rest_ids mcp_ids].each do |surface|
+ describe "via #{surface == :rest_ids ? 'REST' : 'MCP'}" do
+ # The search works: the same ticket is a hit by its title and its
+ # description, so an empty answer below is about the comment.
+ it 'finds the ticket by its title and by its description' do
+ expect(send(surface, 'Harmloser')).to eq([ticket.id])
+ expect(send(surface, 'Ananaskuchen')).to eq([ticket.id])
+ end
+
+ it 'does not find the ticket by a word only a hidden private comment holds' do
+ expect(send(surface, secret_word)).to be_empty
+ end
+
+ it 'does not find it by a prefix of that word either' do
+ expect(send(surface, secret_word.first(6))).to be_empty
+ end
+ end
+ end
+end