fix: gate MCP user tokens on owner-scoped personal RBAC by ibetitsmike · Pull Request #27945 · coder/coder · GitHub
Skip to content

fix: gate MCP user tokens on owner-scoped personal RBAC - #27945

Merged
ibetitsmike merged 11 commits into
mainfrom
mike/codagt-806-token-personal-rbac
Aug 19, 2026
Merged

fix: gate MCP user tokens on owner-scoped personal RBAC#27945
ibetitsmike merged 11 commits into
mainfrom
mike/codagt-806-token-personal-rbac

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

Re-gates every mcp_server_user_tokens dbauthz method from rbac.ResourceDeploymentConfig onto owner-scoped personal actions, mirroring the external_auth_links prior art. These are per-user credential rows, not deployment config, and the old gate forced every call site to elevate with dbauthz.AsSystemRestricted.

Summary

  • Reads gate on ActionReadPersonal and writes on ActionUpdatePersonal against the token owner (MCPServerUserToken.RBACObject() derives from user_id). The id-keyed refresh and refresh-failure mutations resolve the owner via a GetMCPServerUserTokenByID prefetch inside dbauthz; the mutation queries cannot retarget a row's owner, so there is no fetch-authorize race.
  • All seven token-path AsSystemRestricted elevations in coderd/mcp.go and all four in chatd are removed along with their //nolint:gocritic markers; handlers use the request actor.
  • The daemon-wide chatd subject keeps only ActionReadPersonal on ResourceUser. Background token refresh and failure persistence use a new per-user AsChatdTokenOwner subject holding only user-level ActionUpdatePersonal scoped to the token owner (both mutations resolve the row via the underlying store, so the subject needs no read grant), and chatd never holds site-wide personal-write access. TestAsChatd pins the complete ResourceUser action set and TestMCPServerUserTokensAuth exercises both subjects against the real authorizer.
  • New strict-authorizer test (rbac.NewAuthorizer against real rows): owner passes get/upsert/delete, a stranger fails, and an org admin of another org fails.

No user-visible behavior change. Stacked on #27944. Completes the MCP org-separation stack.

Closes https://linear.app/codercom/issue/CODAGT-806

UAT: regression pass on a dogfood instance: OAuth2 connect redirect, member list/status with redaction, self-scoped disconnect (cannot touch another user's token), end-to-end chat MCP tool invocation, and lower-stack audit/ACL smoke checks.

Mux (AI agent) authored this PR on Mike's behalf.

@linear-code

linear-code Bot commented Aug 8, 2026

Copy link
Copy Markdown

@ibetitsmike ibetitsmike changed the title mike/codagt 806 token personal rbac fix: gate MCP user tokens on owner-scoped personal RBAC Aug 8, 2026
@ibetitsmike
ibetitsmike force-pushed the mike/codagt-806-token-personal-rbac branch from d1fcb77 to 463b7df Compare August 8, 2026 07:41
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 463b7dfd94

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike force-pushed the mike/codagt-806-token-personal-rbac branch from 463b7df to 733f8fb Compare August 8, 2026 08:05
@ibetitsmike
ibetitsmike force-pushed the mike/codagt-806-token-personal-rbac branch from 733f8fb to 5c286be Compare August 8, 2026 08:23
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5c286be1d8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/database/dbauthz/dbauthz.go Outdated
@ibetitsmike
ibetitsmike force-pushed the mike/codagt-806-token-personal-rbac branch from 5c286be to a5f9914 Compare August 8, 2026 08:51
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: a5f9914ba3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike force-pushed the mike/codagt-806-token-personal-rbac branch from a5f9914 to 9ae868b Compare August 10, 2026 05:25
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 9ae868b6d4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike force-pushed the mike/codagt-806-token-personal-rbac branch from 9ae868b to 30677a1 Compare August 10, 2026 05:37
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 30677a1bea

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/chatd.go
@ibetitsmike
ibetitsmike force-pushed the mike/codagt-806-token-personal-rbac branch from 30677a1 to 5897a6b Compare August 10, 2026 05:55
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 5897a6b088

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 2f244b4b2d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 00365d9d2d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: f94805e353

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: aae8bbef79

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: b1d961124d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 2fc29eaed9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 5ea19be812

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 644b47e0ce

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 767e37f6fb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

mcp_server_user_tokens rows are per-user credentials, but every dbauthz
method was gated on ResourceDeploymentConfig, forcing all call sites to
elevate with AsSystemRestricted. Re-gate them on ActionReadPersonal and
ActionUpdatePersonal against the owning user, mirroring the
external_auth_links pattern, and drop the token-path elevations in
coderd/mcp.go and chatd. The id-keyed refresh mutations resolve the
owner by prefetching the row via a new GetMCPServerUserTokenByID query.
The chatd subject gains ActionUpdatePersonal so background token
refresh and failure persistence pass without elevation.
…l authorizer

The MethodTestSuite entries assert call shape against a fake
authorizer, so they cannot prove real policy outcomes. Add a
strict-authorizer test over a real token row (owner passes get,
upsert, and delete; a stranger and an org admin of another org
fail) and pin the chatd subject's complete ResourceUser action
set so a future broad grant fails the test.
Both production writes fetch the token row through the underlying store
and authorize only ActionUpdatePersonal, so the subject never needs
ActionReadPersonal. Pin the write-only shape in the real-authorizer
test and use test contexts instead of context.Background.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants