Skip to content

fix(mcp): refuse token changes while impersonating; error on a token no company accepts - #528

Merged
artempartos merged 1 commit into
developfrom
fix/impersonation-mcp-token
Oct 21, 2026
Merged

artempartos merged 1 commit into
developfrom
fix/impersonation-mcp-token

Conversation

@artempartos

Copy link
Copy Markdown
Contributor

Problem

A personal MCP token regenerated while a super admin was impersonating a user authenticated fine, but list_companies and list_projects returned {"companies": []} / {"projects": []} with HTTP 200 and no error. Regenerating it again from the same session did not help. Signing in normally and regenerating did.

Cause

  • impersonate_user starts the impersonation session with sign_in(user, impersonator:) and no provider, so the session holds no sign-in proofs. The web gate lets impersonated requests through (company_auth_policy_satisfied?), so nothing in the UI hints at it.
  • regenerate_mcp_token stores current_user_session.proved_provider_ids, which is [], not NULL. User#mcp_token_proven_provider_ids falls back to the password provider only for NULL.
  • Auth::PolicyResolver.satisfied_company_ids(provider_ids: []) builds where(identity_provider_id: []). Rails treats that as a contradiction and returns [] without running a query. Every personal tool then scopes to where(company_id: []).

Production logs matched this exactly. Each regenerate from the impersonation session wrote an empty proof list. The /mcp calls made with that token ran only the token lookup, with no company or project query at all, so the failure left nothing in the logs. Regenerating also replaced the token the person's own clients were using.

Fix

  • Regenerate and disable are refused while impersonating. The user gets a redirect to the MCP tab with an alert. This follows the existing refuse_impersonation guards on passwords and sign-in methods. The fix deliberately does not make an impersonation-issued token work: that would give the operator a long-lived credential acting as the person, which outlives the impersonation and its audit trail.
  • A token that reaches none of the user's active companies now returns an explicit tool error ("sign in to Aixle, then regenerate the token from your profile") instead of empty lists. This also covers tokens already stored with [], and tokens whose proofs only satisfy companies the user has since left.

Not done: refusing to issue a token with an empty proof list. Through the web, a non-impersonated session without proofs never reaches the profile (enforce_company_auth_policy sends it to step-up, and super admins are redirected to /admin), so that guard would be dead code.

Tests

  • test/integration/web/profile_controller_test.rb: an operator impersonating a user cannot regenerate or disable that user's token. The user's existing token keeps working and its proofs are unchanged.
  • test/integration/personal_mcp_sign_in_policy_test.rb: a token issued with no proofs gets the explicit error from list_companies and list_projects.

Both new tests fail without the fix. Locally (isolated container and DB), every test file that touches personal MCP, MCP tokens or impersonation ran: 394 runs, 0 failures. There were 7 errors, all Vite Ruby can't find entrypoints/admin* from a stale local public/vite-test copy; they render admin pages this change does not touch. rubocop and brakeman are clean. The full suite is left to CI.

🤖 Generated with Claude Code

…no company accepts

An impersonation session proves no sign-in method of the person's, so a
personal MCP token regenerated under it was stored with an empty proof list.
Every company was then filtered out and the token answered list_companies
and list_projects with empty results, HTTP 200 and no error. Regenerating it
also replaced the token the person's own clients were using.

Regenerate and disable are now refused while impersonating, like password
and sign-in method changes. A token whose proofs satisfy none of the user's
active companies now gets an explicit tool error telling them to sign in and
regenerate it, instead of empty lists.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
@artempartos
artempartos merged commit 715fc7c into develop Oct 21, 2026
16 checks passed
artempartos added a commit to Hitin/flow that referenced this pull request Oct 21, 2026
* commit '2ecb0ecb':
  fix(admin): keep form fields dark after a Turbo visit (AixleHQ#533)
  feat(overview): rename Total Spend card to Token Cost (AixleHQ#532)
  fix(admin): preload company session limits on the companies index (AixleHQ#523)
  fix(board): keep the board settings gear on the toolbar row (AixleHQ#524)
  feat(capacity): give every AWS Marketplace company at least 3 workers (AixleHQ#531)
  fix(mcp): refuse token changes while impersonating; error on a token no company accepts (AixleHQ#528)
  feat(feature-flags): registry, resolver and Feature facade (1/7) (AixleHQ#526)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant