Skip to content

fix(platform): hold API-key creation to one rule, on the server - #3941

Merged
yannickmonney merged 4 commits into
mainfrom
fix/api-key-create-rule
Sep 29, 2026
Merged

yannickmonney merged 4 commits into
mainfrom
fix/api-key-create-rule

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

Summary

Who may create a personal API key was decided by the settings page alone. @better-auth/api-key lets 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, or tale: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.

  • Server: a before-hook on /api-key/create (auth/api-key-create-gate.ts) refuses anyone else with 403 API_KEY_CREATE_FORBIDDEN before a key exists, and logs the attempt. The platform's own calls and callers without a session pass through as before.
  • Settings: the new GET /api/app/governance/my/api-keys answers { 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 the api_key hint, 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).
  • Docs (EN/DE/FR): API keys, Use Tale from your editor, the API reference's Authentication section and Competences now state the one rule and what a lapsed right keeps. This also removes the reference's old "Admin or Developer access" wording.

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's tsc --noEmit and 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.
  • Translations: accessDenied.apiKeys (reworded), accessDenied.apiTab, accessDenied.apiModels, emptyStates.apiKeys.descriptionNoCreate in EN/DE/FR.
  • Docs: EN/DE/FR as above. The docs suite passes (200).
  • Manual tests: new SET-B27, SET-B26 updated, and a new error-codes.md row for API_KEY_CREATE_FORBIDDEN. bun run lint:manual passes.
  • 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 403 API_KEY_CREATE_FORBIDDEN and 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-keys answers both facts for the session's user. use-api-settings-access.test.ts, settings-rail.test.tsx, use-settings-menu-groups.test.tsx and api-keys-table.test.tsx cover the REST tab for a key grant, the lapsed holder without Create, and the no-create empty state. All pass, along with backend/auth/ (262) and lib/i18n/ (185).
  • Live, on an isolated stack with real Postgres, signed in as a Member (Marta):
    • With no grant, the read answers mayCreate: false, POST /api/auth/api-key/create answers 403 API_KEY_CREATE_FORBIDDEN, and no key exists.
    • The Owner grants Export notifications through POST /api/app/governance/competences. The read now answers true, the create answers 200, and API shows REST alone. The key's GET /api/v1/me answers notificationExport: true.
    • The Owner revokes the grant. The create is refused again, and the old key still authenticates with notificationExport: false. REST stays open, listing both keys without Create API key. /settings/api/models names its competence, and /settings/api/mcp answers "This page is for Owners, Admins and Developers."
    • Revoking both keys from the table makes the API group disappear without a reload.
    • The Owner still creates and deletes a key.
  • Not run: 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.

@yannickmonney

Copy link
Copy Markdown
Contributor Author

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.

@yannickmonney
yannickmonney merged commit 01eb755 into main Sep 29, 2026
57 checks passed
@yannickmonney
yannickmonney deleted the fix/api-key-create-rule branch September 29, 2026 18:56
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