Repository navigation
fix(platform): let admins cap any member's API key in the budget editor - #3780
Merged
Merged
Conversation
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).
This was referenced Sep 29, 2026
…ber-keys # Conflicts: # services/platform/tests/manual/readme.md
…ber-keys # Conflicts: # services/platform/tests/manual/readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.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.governance/api_keys:listOrgApiKeys) and name each key with its owner, e.g. "CI Key · Dana".api_keyhint 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.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 checkpasses: 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:sastpasses: the scoped Opengrep hook ran on the staged files.docs/{en,de,fr}/platform/admin/governance/policies-and-limits.md.Test plan
routes.api-keys.test.ts:memberon this organization, filtersenabledandexpiresAt > now(), and never readsk."key";auth.api-key-audit.test.ts: key create and revoke write oneapi_keyoutbox 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.settings-governance.test.tsreads/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.test:uifor the api-keys and governance components (7 files, 41 tests), serverlib/shared(812 tests), docs tests (171),lint:manual, andcheck-guide.tsongovernance.md.GOV-F4bnow expects the owner in the Target cell. NewGOV-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.checkOrgApiKeyListinglane 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:
backend:integration. The column names (referenceId,start,enabled,expiresAt) come from the installed@better-auth/api-key1.7.5 schema, the one the plugin's own queries use.governance.spec.tsnever selects a key, and a failed read would only leave the picker empty.