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: review round 4 of #124, name the inactive member's refusal and pin it at the doors

Guido Boykee41cbb503e11
diff --git a/app/controllers/api/v1/terms_controller.rb b/app/controllers/api/v1/terms_controller.rb
index 0ed2539..3660b40 100644
--- a/app/controllers/api/v1/terms_controller.rb
+++ b/app/controllers/api/v1/terms_controller.rb
@@ -17,16 +17,18 @@ module Api
# token the same 404, never ignored.
# * Paging: 50 terms by default, at most 100, with the total; a value outside is a 400.
class TermsController < BaseController
- # Pundit asks the glossary right first; Refused answers the same right taken away
- # between that check and the lookup's own.
+ # Refused comes from two places: Glossary::Lookup.for_token, before the tenant, for an
+ # inactive member of an organisation the token names; and the lookup's own check, for the
+ # right taken away between Pundit's check and it. Both are the same 403.
rescue_from Pundit::NotAuthorizedError, Glossary::Lookup::Refused, with: :glossary_forbidden
rescue_from Glossary::Lookup::InvalidArgument, with: :bad_request
rescue_from Glossary::Lookup::NotFound, with: :not_found
# GET /api/v1/organisations/:organisation_id/terms?project_id=&page=&per_page=
#
- # The reach, then the organisation's tenant, then the glossary right, the order of the
- # people directory: an organisation outside the token tells nothing about itself.
+ # The reach (an inactive member of an organisation the token names is refused there, the
+ # one step the people directory does not have), then the organisation's tenant, then the
+ # glossary right: an organisation outside the token tells nothing about itself.
def index
lookup = Glossary::Lookup.for_token(token: current_token, viewer: current_user,
organisation_id: params[:organisation_id])
diff --git a/spec/requests/api/glossary_parity_spec.rb b/spec/requests/api/glossary_parity_spec.rb
index eab4c68..90df822 100644
--- a/spec/requests/api/glossary_parity_spec.rb
+++ b/spec/requests/api/glossary_parity_spec.rb
@@ -152,6 +152,22 @@ RSpec.describe 'Glossary on REST and MCP', :ci_guard, type: :request do
expect(result.dig('content', 0, 'text')).to eq(body['message'])
end
+ # A token for all organisations has no link rows; it names every organisation of its owner, the
+ # deactivated one included (post-round check of #124: held at the doors, not only in the model).
+ it 'refuses a deactivated member with a token for all organisations on both alike' do
+ every = build(:personal_access_token, :all_organisations, user: anna).tap do |pat|
+ pat.token_hash = nil
+ pat.token_prefix = nil
+ pat.generate_token!
+ end
+ ActsAsTenant.with_tenant(organisation) { Membership.find_by!(user: anna, organisation: organisation).update!(active: false) }
+ status, body = rest(with: every)
+ result = mcp(with: every)
+
+ expect([status, body['reason']]).to eq([403, 'glossary_forbidden'])
+ expect(result['_meta']).to eq('lalabase/code' => 'refused', 'lalabase/reason' => 'glossary_forbidden')
+ end
+
it 'answers an organisation outside the token and a project outside it as not found on both' do
outside_status, = rest(organisation_id: other_organisation.id)
outside = mcp(organisation_id: other_organisation.id)
diff --git a/spec/services/lalabase/tools/get_glossary_spec.rb b/spec/services/lalabase/tools/get_glossary_spec.rb
new file mode 100644
index 0000000..d8e0bc6
--- /dev/null
+++ b/spec/services/lalabase/tools/get_glossary_spec.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+# What: get_glossary refuses to run without the token of an MCP call (post-round check of #124).
+# Why: the tool asks the token itself (Glossary::Lookup.for_token takes it, for the inactive
+# member's refusal). Mcp::ToolCall always sets reach and token together; a caller that set the
+# reach alone would otherwise end in a NoMethodError instead of a plain refusal.
+RSpec.describe Lalabase::Tools::GetGlossary do
+ it 'answers token context missing for a call with a reach but no token' do
+ reach = PersonalAccessToken::Reach.new(project_ids_by_organisation: {})
+ context = Lalabase::Tools::Context.new(actor: create(:user), reach: reach, token: nil)
+
+ result = described_class.execute(args: { organisation_id: SecureRandom.uuid }, context: context)
+
+ expect(result.error).to eq('token context missing')
+ end
+
+ it 'answers token context missing for a call without a reach' do
+ context = Lalabase::Tools::Context.new(actor: create(:user))
+
+ expect(described_class.execute(args: {}, context: context).error).to eq('token context missing')
+ end
+end