mirror of
https://github.com/bitbaum/orangecat.git
synced 2026-12-04 03:57:16 +01:00
Today every integration key has full authority over its bound actor's
entities — a leaked product-only key can still fund-grab. This commit
lands least-privilege scopes end-to-end on the server. UI for picking
scopes at mint time is a follow-up; existing keys + new keys without
explicit scopes default to ['*'] (wildcard) for back-compat.
Schema (20260604000001, applied to remote):
ALTER integration_keys ADD COLUMN scopes TEXT[] NOT NULL DEFAULT '{*}'
Token format '<entity>.<read|write>' (e.g. 'products.write').
Service:
- createIntegrationKey gains optional `scopes` param; empty/missing
stores ['*'].
- verifyIntegrationKey returns scopes alongside userId/actorId/keyId.
- listIntegrationKeys returns them too (via the new KEY_PUBLIC_COLUMNS
constant, deduped across mint/list selects).
- rotateIntegrationKey preserves the source scopes — rotation is a
pure "new secret, same authority" operation.
Middleware:
- ResolvedRequestAuth.scopes — session auth always ['*'], integration
key inherits the stored array (defaulting to ['*']).
- hasScope(authScopes, required) — wildcard '*' passes anything;
otherwise exact-token match. No prefix expansion ('products.*' is
NOT treated as a wildcard) to keep the rule trivially auditable.
Enforcement:
- entityPostHandler runs the scope check BEFORE idempotency claim +
rate limit so a forbidden request can't burn a slot or quota.
- entityListHandler enforces on integration-key auth only; anonymous
public reads continue to pass through.
- Both return 403 with a human-readable "this key is not allowed to
<verb> <entity>" message.
POST /api/integration-keys now accepts an optional `scopes` array;
curl-mintable today, UI follow-up next.
Tests:
- 6 new contract tests on hasScope: wildcard, exact match, denial,
empty allowlist, no prefix expansion, mixed wildcard precedence.
47 suites · 610 tests · 0 failures. tsc + eslint clean.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>