Repository navigation
feat: let org API keys be restricted to MCP or the External API - #3869
Conversation
Org keys get an optional allowedSurfaces list. Absent means both surfaces, so existing keys and PATs keep working; a list restricts the key and an empty list fails closed. A new requireOrgKeySurface middleware enforces it on every /api/v2 route and POST /mcp with a 403. Team Settings gains an opt-in surface restriction on create and an Access column showing All, MCP, or API.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: 60a5e80 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
🔴 Tier 4 — CriticalTouches authentication, tenancy data models, the public API or shipped database config — or substantially changes the query rendering engine, background tasks, the OTel pipeline, image build, or release CI. Why this tier:
Review process: Deep review from a domain expert. Synchronous walkthrough may be required. Stats
|
Duplicate code analysisNo new duplicate code introduced by this PR. jscpd compared this branch against |
PR Review
3 finding(s): 🔴 1 P0/P1 · 🟡 2 P2 · 🔵 0 P3 None could be anchored to changed lines; they are listed below. Findings outside the changed lines
Priority comes from a separate grading pass over the finished review. P3 is folded here instead of posted inline. No finding blocks merge automatically; a maintainer decides. |
E2E Test Results✅ All tests passed • 478 passed • 1 skipped • 1279s
Tests ran across 4 shards in parallel. |
Deep Review✅ No critical issues found. The surface-restriction guard is sound: One recommended (P2) observability regression and a handful of nits follow. 🟡 P2 -- recommended
🔵 P3 nitpicks (5)
Reviewers (8): correctness, security, adversarial, testing, maintainability, api-contract, kieran-typescript, previous-comments. Testing gaps:
No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond |
deniedMessage now has an explicit return type and an exhaustive default, so a new surface fails to compile instead of producing a 403 with no message. surfaceLabel compares the unique surface set against OrgApiKeySurfaceSchema.options instead of counting two values, so a partial list is spelled out, a duplicated value does not read as All, and the stored empty list still reads as None. The mongoose enum and UI labels now derive from the shared schema. Adds label coverage for the explicit-all, empty, and duplicate cases.
The suite registers a user in beforeAll, which creates a team, and never cleared it. Integration test files share one database and run serially in size order, so tasks/checkAlerts/bodyExpressionFallback (which calls createTeam and fails while any team exists) started on a dirty database once v2.int.test.ts grew large enough to run before login.int.test.ts. Passed on main only because v2.int.test.ts ran after login and its afterEach cleared the rows. Clear the database in afterAll so the suite no longer depends on what runs next.
Mount the v2 authentication chain once at the router level instead of spreading it onto each route, so a sub-route added later inherits the surface restriction rather than quietly skipping it. The rate limiter still runs first; only unknown paths change behaviour, now answering 401 instead of 404. Record an org key's last-used time from a middleware that runs after the surface check, so a request refused with 403 no longer shows up as a successful use in Team Settings. Covers the denied path with a test.
orgKeyAuthChain(surface) pairs bearer auth with the surface check and the last-used stamp, so a new mount cannot authenticate org keys without naming its surface. GET /api/v2 goes back to skipping the rate limiter, as on main. The create modal derives its surface options from OrgApiKeySurfaceSchema. Tests: assert the last-use spy directly instead of sleeping, cover a key allowed on both surfaces, and type helpers as OrgApiKeySurface[].
jordan-simonovski
left a comment
There was a problem hiding this comment.
Thanks for jumping on this so quickly!
Summary
Organization API keys can now be limited to a single surface. Before this, a key handed to an MCP client could also call
/api/v2and read connections the MCP tools deliberately hide, so "give the agent a key" meant "give the agent the whole External API". A key is created withallowedSurfaces: ['mcp']or['api'], and an unrestricted key keeps working on both.requireOrgKeySurfacemiddleware runs aftervalidateUserAccessKeyon every/api/v2route (includingGET /) and onPOST /mcp. A denied request gets 403 with a short reason, so a client can tell "wrong surface" from "bad key".allowedSurfacesis optional on create and cannot be changed afterwards. Team Settings has an opt-in "Restrict where this key can be used" checkbox and a new Access column showing All / MCP / API.Design notes
undefinedmeans unrestricted, matching keys created before the field existed. A stored empty list denies every surface (fails closed). The create schema rejects[]with.min(1), so that state is only reachable by direct DB writes.[], which would silently turn every key into "deny all", so the schema setsdefault: undefined.nullfor unrestricted and[]for a stored empty list, so the two remain distinguishable.Screenshots or video
How to reproduce
Starting point:
yarn dev(this needs the API and MongoDB; Team Settings hides the key section in local mode). Open/team.On
main:curl -H "Authorization: Bearer <key>" localhost:3000/api/v2/dashboardsreturns 200.POST /mcp, so there is no way to scope it to one surface.On this branch:
/api/v2/dashboardsrequest now returns 403{"message":"This API key is limited to MCP"}.POST /mcpwith that key still returns 200.MCPin the Access column for the key.How to test on Vercel preview
N/A — non-preview-testable. The key section only renders outside local mode (
TeamPage.tsxgates it onIS_LOCAL_MODE) and the feature requires the API and MongoDB, neither of which the preview runs.References
Note
Medium Risk
Changes authentication on every External API v2 and MCP bearer request; behavior is backward compatible for unrestricted keys but misconfiguration could block automation until keys are recreated.
Overview
Organization API keys can now be scoped to MCP, the External API, or both at create time via optional
allowedSurfaces, closing the bypass where an MCP-intended key could still call/api/v2(e.g. connections).Enforcement: New
orgKeyAuthChain(surface)replaces barevalidateUserAccessKeyonPOST /mcpand all/api/v2routes. Org keys on the wrong surface get 403 with a clear message (not 401). Personal keys and legacy unrestricted org keys are unchanged; emptyallowedSurfacesdenies all surfaces.Data & API:
allowedSurfacesis stored on org-key virtual users, returned asnullwhen unrestricted, and create rejects an empty list. Last-used is recorded only after a successful surface check.UI: Team Settings adds an opt-in surface restrictor on create and an Access column (All / MCP / API).
Reviewed by Cursor Bugbot for commit 60a5e80. Bugbot is set up for automated code reviews on this repo. Configure here.