Skip to content

fix(mcp): harden OAuth and permission flows - #286

Open
NeerajCodz wants to merge 3 commits into
mainfrom
fix/mcp-oauth-issues
Open

NeerajCodz wants to merge 3 commits into
mainfrom
fix/mcp-oauth-issues

Conversation

@NeerajCodz

Copy link
Copy Markdown
Owner

Changes

  • Enforce registered MCP client auth transport and route grant exchange/refresh through server-authenticated internal mutations.
  • Fix MCP permission surfaces and OAuth metadata/client handling.

Issues

Fixes #223
Fixes #224
Fixes #225
Fixes #226
Fixes #227
Fixes #240
Fixes #241
Fixes #251
Fixes #282

- Validate client authentication at the HTTP boundary and keep grant mutations server-internal.
- Fixes #223, #224, #225, #226, #227, #240, #241, #251, and #282.
@vercel

vercel Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
finapp Ready Ready Preview Nov 27, 2026 2:54am UTC

@NeerajCodz

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-27T02:36:25.776571Z
We couldn’t complete this review. Comment @codex review to try again.
e6860e3 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@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: 46c742043e

ℹ️ 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 on lines +144 to +148
const scopes = await client.mutation(api.mcpOAuth.restrictRefreshGrantScopes, {
refreshToken: grant.refreshToken,
clientId,
requestedScopes,
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Narrow private-key refresh scopes before rotating

When a private_key_jwt client supplies scope, the preceding action has already rotated and invalidated the old refresh token before this second mutation runs. If this mutation encounters a transient Convex/network failure, the route returns server_error without revealing the newly issued token, so retrying with the old token produces invalid_grant and permanently disconnects the client. Pass the requested scopes through the assertion action and apply them atomically in the same mutation that rotates the token.

Useful? React with 👍 / 👎.

Comment thread convex/mcpOAuth.ts
});

/** Applies a validated scope subset to a private-key client's live grant after rotation. */
export const restrictRefreshGrantScopes = mutation({

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep refresh-scope restriction off the public API

This is exported as a public Convex mutation and performs no user or client-assertion authentication. Anyone who obtains a refresh token belonging to a private_key_jwt client can therefore call Convex directly and permanently strip scopes from its live grant without possessing the client's private key, bypassing the client authentication enforced by /oauth/token; make this helper internal and invoke it only through the authenticated server flow.

Useful? React with 👍 / 👎.

Comment on lines +220 to +223
let update = scopeUpdates.current.get(grantId);
if (!update) {
update = { scopes: [...grant.scopes], pending: Promise.resolve() };
scopeUpdates.current.set(grantId, update);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Rebase queued changes on the current grant scopes

The map entry is initialized once and never synchronized when the reactive grant.scopes value changes. If another device, a refresh-token scope reduction, or another settings session changes this grant while the page remains mounted, the UI renders the new scopes but the next toggle is computed from the stale cached array and can silently restore permissions that were just removed. Refresh or discard the cached entry once pending work completes and the server value changes.

Useful? React with 👍 / 👎.

Comment on lines +247 to +251
fixture.t.mutation(internal.mcpOAuth.refreshAccessGrant, {
refreshToken: initial.refreshToken,
clientId: fixture.registration.client_id,
requestedScopes: ['finapp:read', 'finapp:write:social'],
}),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Pass the public-client transport in refresh tests

This newly added test invokes refreshAccessGrant without authTransport: 'none', but authenticateClient now rejects a public client unless that exact transport is supplied. Consequently the first assertion receives INVALID_CLIENT instead of the expected INVALID_SCOPE, and the regression test fails before exercising scope narrowing; the subsequent refresh calls in this test need the same transport argument.

Useful? React with 👍 / 👎.

Comment thread convex/mcpOAuth.ts
Comment on lines 222 to +225
const grants = await ctx.db
.query('mcpOAuthGrants')
.withIndex('by_client', (index) => index.eq('clientId', clientId))
.take(500);
.collect();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Bound client deletion instead of collecting every grant

A dashboard client can be authorized repeatedly and there is no cap on grants per client, so replacing the bounded query with collect() makes this mutation perform an unbounded read followed by one write per active grant. Once a shared or repeatedly authorized client accumulates enough grants to exceed Convex mutation read/write limits, deletion rolls back and the owner can no longer delete or revoke that client through this path; process the grants in bounded batches instead.

Useful? React with 👍 / 👎.

Comment on lines +157 to 160
if (message.includes('INVALID_GRANT'))
return Response.json({ error: 'invalid_grant' }, { status: 400 });
return Response.json({ error: 'server_error' }, { status: 503 });
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Align the invalid-grant regression with the new error contract

The existing bounded-form test mocks the mutation failure as new Error('Invalid authorization code') and expects HTTP 400, but this changed catch now recognizes only messages containing the exact uppercase INVALID_GRANT; that test therefore falls through to HTTP 503 and fails. Update the regression mock to use the backend's new sentinel error, or retain compatible classification for the mocked legacy message.

Useful? React with 👍 / 👎.

Comment thread packages/mcp/src/oauth.ts
Comment on lines +43 to +44
const deployKey = process.env.CONVEX_DEPLOY_KEY?.trim();
if (!deployKey) throw new Error('CONVEX_DEPLOY_KEY is required');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Supply the deploy key in the credential-boundary test

The credential-boundary test's environment setup never defines CONVEX_DEPLOY_KEY. After this change, even its retry with a valid signing key throws here before reaching the stubbed Convex fetch, so the test receives another 503 instead of the expected successful token response. Stub a test deploy key in that suite or inject the admin client so the test can exercise the intended boundary.

Useful? React with 👍 / 👎.

- Expose internal mutation typing only on the admin-authenticated server client.
- Guard the SDK internal admin-auth method at runtime.
@NeerajCodz

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

- Model the no-secret token client transport in refresh grant tests.

This branch was successfully deployed

1 active deployment
Preview — 2b0eecf2 Deployed Nov 27, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment