Repository navigation
fix(platform): hold API-key creation to one rule, on the server - #3941
Conversation
|
Independent review completed. At original head 6f21551 the server gate, hook wiring, governance access and key-revocation behavior passed 114 focused server tests, 31 client tests and 31 real-Postgres HTTP/SQL checks. The native pass covered all three capabilities, expired/revoked grants, disabled seats, cross-organization eligibility, no-session/userId/API-key-header/bearer refusals and key management after a grant lapses. No authorization bypass found. CI exposed one deterministic introduced failure: API_KEY_CREATE_FORBIDDEN was missing from the existing APP_ONLY_CODES classification. I added only that auth-only classification and its explanatory comment in e60af8d, preserving the public REST error registry and authorization implementation. The existing registry suite now passes 5/5; targeted formatting, lint, commitlint and SAST pass. This is a fix of the failing boundary inventory, not a retry of unchanged CI. Final head e60af8d is MERGEABLE/CLEAN and all 57 reported checks are completed without failure or pending work. Local tests used reused dependencies and isolated synthetic data; no production testing. Accepting the change. |
Summary
Who may create a personal API key was decided by the settings page alone.
@better-auth/api-keylets every signed-in user create one, so anyone could go around the page. The page also admitted only the model-endpoint grant, so a Member granted Export notifications, whom the docs send to create a key for a notification mirror, had nowhere to create it.There is now one rule, which the server enforces and the page reads. An Owner, Admin, or Developer of any organization may create a key, and so may a member with a live grant of a competence that is used with one:
tale:models.api,tale:notifications.export, ortale:rest.act-as. A key belongs to the person and works in every organization they are a member of, so the rule is the person's, not one organization's./api-key/create(auth/api-key-create-gate.ts) refuses anyone else with403 API_KEY_CREATE_FORBIDDENbefore a key exists, and logs the attempt. The platform's own calls and callers without a session pass through as before.GET /api/app/governance/my/api-keysanswers{ mayCreate, holdsKeys }. The REST tab opens to everyone the rule admits. It also stays open, without Create API key, for someone whose right lapsed (a revoked grant, or a Developer moved down) while they hold a key, so they can still see and revoke it. The read is keyed under theapi_keyhint, so the tab follows at once when a key is created or revoked. The denials now say why: the whole section, a Developer-only tab, and the Models tab (which names its competence).The integration lanes that prove a door refuses a lower role's key now mint that key the way one arises in production: the seat is made a Developer for the create and set back afterwards. A new lane covers the gate against real Postgres: refused, then granted, then revoked, and a Developer who needs no grant.
Checklist
bun run check: CI runs the full gate. Locally, the platform'stsc --noEmitand oxlint on every changed file pass, and so do the suites below.bun run lint:sast: the commit hook ran Opengrep on the staged files.accessDenied.apiKeys(reworded),accessDenied.apiTab,accessDenied.apiModels,emptyStates.apiKeys.descriptionNoCreatein EN/DE/FR.SET-B27,SET-B26updated, and a newerror-codes.mdrow forAPI_KEY_CREATE_FORBIDDEN.bun run lint:manualpasses.README.md/README.de.md/README.fr.md: N/A.Test plan
api-key-create-gate.test.ts: Owner, Admin and Developer (any organization) pass without a grant read. A Member or Editor without a grant is refused. A key-using grant admits them. A grant in a disabled seat doesn't count. The competence list is exactly the three.auth.api-key-create.test.ts(the hook wiring): a refused Member gets 403API_KEY_CREATE_FORBIDDENand a warning. A Developer passes. A caller with no session is left to the endpoint's 401. The server's own call is never judged. With the gate call disabled, the refusal test fails.governance/routes.test.ts:/my/api-keysanswers both facts for the session's user.use-api-settings-access.test.ts,settings-rail.test.tsx,use-settings-menu-groups.test.tsxandapi-keys-table.test.tsxcover the REST tab for a key grant, the lapsed holder without Create, and the no-create empty state. All pass, along withbackend/auth/(262) andlib/i18n/(185).mayCreate: false,POST /api/auth/api-key/createanswers 403API_KEY_CREATE_FORBIDDEN, and no key exists.POST /api/app/governance/competences. The read now answers true, the create answers 200, and API shows REST alone. The key'sGET /api/v1/meanswersnotificationExport: true.notificationExport: false. REST stays open, listing both keys without Create API key./settings/api/modelsnames its competence, and/settings/api/mcpanswers "This page is for Owners, Admins and Developers."backend:integration. CI doesn't run it, and locally it stops at an earlier lane that needs an embedding key. The new lane and the five adapted mint sites passed typecheck only.