Skip to content

fix(platform): let admins cap any member's API key in the budget editor - #3780

Merged
yannickmonney merged 8 commits into
mainfrom
fix/budget-editor-member-keys
Sep 29, 2026
Merged

yannickmonney merged 8 commits into
mainfrom
fix/budget-editor-member-keys

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A budget rule under Settings > Governance > Policies & limits can cap what one API key spends (scope: apiKey). But the rule dialog's key picker read the auth store's own listing (authClient.apiKey.list()), which answers the caller's keys alone. An admin could therefore cap only their own keys, never the key a member's script or coding tool uses, which is what a per-key cap is for. This came up while answering customer feedback about API keys and opencode.

  • New admin-only route GET /api/app/governance/api-keys (backend/domains/governance/api-keys.ts). It lists every active key held by a member of this organization, masked: name, the first characters the owner saw (start), owner name and e-mail, created and expiry. It never includes the secret column, and never a key of someone outside the organization (a key belongs to a person and works in each of their organizations; the cap applies to its spend here). Disabled and expired keys are left out. The listing is bounded at 1000 keys; reaching the bound logs a server warning.
  • The picker and the rules table read it through a new contract entry (governance/api_keys:listOrgApiKeys) and name each key with its owner, e.g. "CI Key · Dana".
  • The picker stays live. A new api_key hint entity (lib/shared/hint-entities.ts) is emitted by the Better Auth after-hook on key create, update and revoke, to every organization the key's holder belongs to. It is non-fatal and runs outside the audit transaction. The listing is keyed under that entity, and the create and revoke mutations also invalidate it in the acting tab. Before this, a key created in another tab or by another member stayed missing from the picker for the five-minute stale window.
  • Docs (EN/DE/FR policies-and-limits.md) say the list covers every member's active key.

The API-key settings page itself still lists only your own keys, as before.

Checklist

  • bun run check passes: not run repo-wide (the machine is shared and loaded); CI runs it. Platform typecheck and oxlint on the touched files are clean.
  • bun run lint:sast passes: the scoped Opengrep hook ran on the staged files.
  • Translations: N/A (the label joins existing data; no new strings).
  • Docs updated in docs/{en,de,fr}/platform/admin/governance/policies-and-limits.md.
  • README: N/A.

Test plan

  • routes.api-keys.test.ts:
    • an Admin and an Owner get the masked listing, including a non-null expiry mapped to epoch ms;
    • the SQL joins member on this organization, filters enabled and expiresAt > now(), and never reads k."key";
    • a Member, Editor or Developer gets 403 with no query run.
  • auth.api-key-audit.test.ts: key create and revoke write one api_key outbox hint per organization of the holder. The hint is still written when the audit rows fail. A refused call writes none.
  • use-api-keys.test.tsx: creating or revoking a key invalidates the organization listing's query.
  • The adapter test in settings-governance.test.ts reads /api/app/governance/api-keys?orgId=….
  • budget-editor.test.tsx: the table resolves a rule's key to "CI Key · Dana" from the organization listing.
  • The new tests failed before the fix commits and pass after. Also passing: test:ui for the api-keys and governance components (7 files, 41 tests), server lib/shared (812 tests), docs tests (171), lint:manual, and check-guide.ts on governance.md.
  • Manual: GOV-F4b now expects the owner in the Target cell. New GOV-F36: an Admin caps a Developer's key, a key the Developer creates joins the open picker without a reload, and a non-admin is refused the listing. The governance preconditions now name the organization listing as the picker's source.
  • Integration check: a new checkOrgApiKeyListing lane mints a live key and an expired key for a member, plus a key for a non-member. It then reads the listing as the owner and expects only the live member key, with its owner and no secret. As the member, it expects 403.

Not verified:

  • The query has not run against real Postgres. Local Docker was unresponsive, so the new integration-check lane was not run either, and CI does not run backend:integration. The column names (referenceId, start, enabled, expiresAt) come from the installed @better-auth/api-key 1.7.5 schema, the one the plugin's own queries use.
  • CI's E2E does not cover the listing: governance.spec.ts never selects a key, and a failed read would only leave the picker empty.
  • The live refresh across sessions was not watched in a browser.

A budget rule can cap what one API key spends, but the rule dialog's key
picker read the auth store's own listing, which answers the caller's keys
alone. An admin could therefore cap only their own keys — never the key a
member's script or coding tool uses, which is exactly what a per-key cap is
for.

A new admin-only route, GET /api/app/governance/api-keys, lists every key held
by a member of this organization, masked: name, the first characters the owner
saw, owner name and e-mail, created and expiry — never the secret, and never a
key of someone outside the organization. The picker and the rules table read
it and name each key with its owner ("CI Key · Dana").
The budget editor's per-key picker reads the organization's key listing
under its own query key, which no signal reached: the api-key plugin's
create, update and revoke only wrote audit rows, and the settings
mutations invalidated only the caller's own key list. An admin who
created a key, or whose member did, kept the old picker for the whole
five-minute stale window.

Declare an `api_key` hint entity shared by both ends, emit it from the
after-hook to every organization the key's holder belongs to (non-fatal,
outside the audit transaction), key the listing under it, and have the
create and revoke mutations invalidate it in the acting tab as well.
The organization key listing filtered disabled keys but still offered
expired ones, which can no longer spend. Filter them in the query, warn
in the server log when the listing reaches its 1000-key bound (a key past
it is missing from the picker), and say "active key" in the docs.

The route test now covers the owner role, the enabled and expiry filters
and a non-null expiry's epoch-ms mapping, and the budget editor's comment
describes the one source it reads instead of two contradicting ones.
The governance preconditions still said the API-key select lists only
the current admin's keys through the personal key hook. It now lists
every member's active key from the organization listing, and GOV-F36
checks that a key a member creates joins an open picker without reload.
The route test drives the listing with a substring-matching sql double,
so a wrong column name would only surface as an empty picker. The
integration check now mints a live and an expired key for a member and a
key for a non-member, then reads the listing as the owner (only the live
member key, owner named, no secret) and as the member (403).
…ber-keys

# Conflicts:
#	services/platform/tests/manual/readme.md
…ber-keys

# Conflicts:
#	services/platform/tests/manual/readme.md
@yannickmonney
yannickmonney merged commit ad06462 into main Sep 29, 2026
57 checks passed
@yannickmonney
yannickmonney deleted the fix/budget-editor-member-keys branch September 29, 2026 02:39
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