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
Hand the token to tools and move MCP onto the owner
Guido Boykec9fab9a6cf93
diff --git a/app/services/lalabase/tools/base.rb b/app/services/lalabase/tools/base.rbindex 9e8fb84..0c93270 100644--- a/app/services/lalabase/tools/base.rb+++ b/app/services/lalabase/tools/base.rb@@ -411,22 +411,22 @@ module Lalabasecontext.authorization&.record!end- # The tickets of `relation` the actor may read (TicketPolicy::Scope). Every- # tool that addresses ONE ticket looks it up in here, so a ticket without- # read access answers exactly like a missing one. A lookup outside the- # scope followed by authorize! answered "Not authorized." instead, which- # told a caller with access to the project, but not to its tickets, which- # ticket numbers exist; numbers count up and can be tried one by one+ # The tickets of `relation` the caller can find: the actor's read rights and,+ # on an MCP call, the reach of its token (Tickets::ReadableForToken owns both;+ # the assistant has no token and is limited by the rights alone). Every tool+ # that addresses ONE ticket looks it up in here, so a ticket without read+ # access answers exactly like a missing one. A lookup outside the scope+ # followed by authorize! answered "Not authorized." instead, which told a+ # caller with access to the project, but not to its tickets, which ticket+ # numbers exist; numbers count up and can be tried one by one# (spec/requests/api/mcp_ticket_existence_spec.rb).## authorize! stays after the lookup: it marks the record-mode tripwire and- # checks the record once more. `relation` must already be bound to the- # context's organisation (project.tickets, or a where on organisation_id):- # the policy resolves its rights against the ambient tenant, the rows must- # not lean on it as well (Context contract; precedent Scope#- # resolve_code_project_ids).+ # checks the record once more. context.organisation must be set, the rows+ # are bound to it and the rights resolve under it as the tenant.def self.readable_tickets(context, relation)- Pundit.policy_scope!(context.actor, relation)+ Tickets::ReadableForToken.call(user: context.actor, organisation: context.organisation,+ relation: relation, token: context.token)end# Resolves the set of ProjectRepository IDs to search for a tool call.diff --git a/app/services/lalabase/tools/context.rb b/app/services/lalabase/tools/context.rbindex 7233fac..e95c4c2 100644--- a/app/services/lalabase/tools/context.rb+++ b/app/services/lalabase/tools/context.rb@@ -30,7 +30,15 @@ module Lalabase# list_tickets_spec), but `reach` is EXCLUSIVE: a token-wide call with a# project or scope set would let a tool branch on the wrong reach, so the# constructor refuses it right here, not three frames into the tool.- Context = Struct.new(:chat, :organisation, :project, :actor, :authorization, :scope, :reach,+ #+ # `token` is the Personal Access Token of an MCP call, set in both branches of+ # Mcp::ToolCall#context_for, and nil on the chat path: the assistant has none,+ # and Tickets::ReadableForToken takes nil as "no second limit". It is not a+ # fourth reach: it sits next to `project` and next to `reach`, and the rule+ # above does not look at it. A branch of context_for that forgets it would+ # therefore read as the assistant's case and widen the reach without an error+ # (pinned in spec/services/mcp/tool_call_token_context_spec.rb).+ Context = Struct.new(:chat, :organisation, :project, :actor, :authorization, :scope, :reach, :token,keyword_init: true) dodef initialize(**)superdiff --git a/app/services/mcp/tool_call.rb b/app/services/mcp/tool_call.rbindex fea683b..7ee687d 100644--- a/app/services/mcp/tool_call.rb+++ b/app/services/mcp/tool_call.rb@@ -232,12 +232,12 @@ module Mcpif project.nil?Lalabase::Tools::Context.new(actor: @token.user, organisation: nil, project: nil, chat: nil,- authorization: recorder, reach: @token.reach+ authorization: recorder, reach: @token.reach, token: @token)elseLalabase::Tools::Context.new(actor: @token.user, organisation: project.organisation, project: project, chat: nil,- authorization: recorder+ authorization: recorder, token: @token)endenddiff --git a/spec/services/mcp/tool_call_token_context_spec.rb b/spec/services/mcp/tool_call_token_context_spec.rbnew file mode 100644index 0000000..939fc2e--- /dev/null+++ b/spec/services/mcp/tool_call_token_context_spec.rb@@ -0,0 +1,114 @@+# frozen_string_literal: true++require 'rails_helper'++# What: Mcp::ToolCall hands its token to the tool through Context#token, on the project+# path and on the token path, and Base.readable_tickets cuts the rows by that token.+# Why: a Context without a token is the assistant's normal case and means "no second+# limit" in Tickets::ReadableForToken. A branch of context_for that forgets the token+# therefore raises nothing and widens the reach to the owner's rights. These examples+# go through a real ToolCall, because that branch is the thing that can go wrong.+#+# Security spec: tagged :ci_guard and listed in .github/workflows/guards.yml.+RSpec.describe Mcp::ToolCall, :ci_guard do+ let(:organisation) { create(:organisation) }+ let(:owner) { create(:user) }+ let(:project_a) { in_tenant { create(:project, organisation: organisation) } }+ let(:project_b) { in_tenant { create(:project, organisation: organisation) } }+ let!(:ticket_a) { in_tenant { create(:ticket, project: project_a, organisation: organisation, creator: owner) } }+ let!(:ticket_b) { in_tenant { create(:ticket, project: project_b, organisation: organisation, creator: owner) } }+ let(:token) do+ create(:personal_access_token, user: owner, organisations: [organisation],+ project_scopes: { organisation => [project_a] })+ end++ before do+ in_tenant do+ create(:membership, user: owner, organisation: organisation,+ role: create(:admin_role, organisation: organisation), active: true)+ end+ token+ end++ def in_tenant(&)+ ActsAsTenant.with_tenant(organisation, &)+ end++ def stub_tool(mode, &body)+ Class.new(Lalabase::Tools::Base) do+ define_singleton_method(:tool_name) { 'stub_tool' }+ define_singleton_method(:scope_mode) { mode }+ define_singleton_method(:execute) do |args:, context:| # rubocop:disable Lint/UnusedBlockArgument+ Lalabase::Tools::Result.new(content: instance_exec(context, &body))+ end+ end+ end++ def context_seen_by(mode, args)+ seen = nil+ tool = stub_tool(mode) do |context|+ seen = context+ 'ok'+ end+ described_class.new(token: token, tool_class: tool, args: args).response+ seen+ end++ describe 'Context#token' do+ it 'carries the token on the project path' do+ context = context_seen_by(:project, { project_id: project_a.id })++ expect(context.project).to eq(project_a)+ expect(context.token).to eq(token)+ end++ it 'carries the token on the token path' do+ context = context_seen_by(:token, {})++ expect(context.reach).not_to be_nil+ expect(context.token).to eq(token)+ end+ end++ describe 'readable_tickets on the project path' do+ # The relation is wider than the gated project on purpose: the real tools narrow it+ # to context.project first, which would hide what this example is about.+ let(:lookup_tool) do+ stub_tool(:project) do |context|+ relation = Ticket.where(organisation_id: context.organisation.id)+ readable_tickets(context, relation).pluck(:id).join(',')+ end+ end++ it "cuts an account admin's rows to the reach of the token" do+ response = described_class.new(token: token, tool_class: lookup_tool, args: { project_id: project_a.id }).response++ expect(response).not_to be_error+ expect(response.content.first[:text]).to eq(ticket_a.id)+ end++ it 'leaves the same owner every ticket without a token, the assistant path' do+ context = Lalabase::Tools::Context.new(actor: owner, organisation: organisation)++ rows = in_tenant do+ Lalabase::Tools::Base.readable_tickets(context, Ticket.where(organisation_id: organisation.id)).pluck(:id)+ end++ expect(rows).to contain_exactly(ticket_a.id, ticket_b.id)+ end+ end++ describe 'the exclusive reach of Lalabase::Tools::Context' do+ let(:reach) { instance_double(PersonalAccessToken::Reach) }++ it 'still refuses a reach next to a project' do+ expect { Lalabase::Tools::Context.new(reach: reach, project: instance_double(Project)) }+ .to raise_error(ArgumentError)+ end++ it 'takes a reach next to a token' do+ expect { Lalabase::Tools::Context.new(reach: reach, token: instance_double(PersonalAccessToken)) }+ .not_to raise_error+ end+ end+end