mirror of
https://github.com/bitbaum/orangecat.git
synced 2026-12-04 12:07:11 +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>
46 lines
1.7 KiB
TypeScript
46 lines
1.7 KiB
TypeScript
/**
|
|
* hasScope — least-privilege auth contract.
|
|
*
|
|
* This is the gate between an integration key and a forbidden operation.
|
|
* A silent regression here would either lock out legitimate callers
|
|
* (wildcard stops matching) or fail open (a narrowed key suddenly has
|
|
* full authority). Both are bad enough to test directly.
|
|
*
|
|
* Created: 2026-06-04
|
|
*/
|
|
|
|
import { hasScope } from '@/lib/api/resolveRequestAuth';
|
|
|
|
describe('hasScope', () => {
|
|
it("wildcard ['*'] passes any required scope", () => {
|
|
expect(hasScope(['*'], 'products.write')).toBe(true);
|
|
expect(hasScope(['*'], 'services.read')).toBe(true);
|
|
expect(hasScope(['*'], 'anything.anything')).toBe(true);
|
|
});
|
|
|
|
it('exact match passes', () => {
|
|
expect(hasScope(['products.write'], 'products.write')).toBe(true);
|
|
expect(hasScope(['services.read', 'products.write'], 'products.write')).toBe(true);
|
|
});
|
|
|
|
it('non-listed scope is denied (no prefix expansion)', () => {
|
|
expect(hasScope(['products.read'], 'products.write')).toBe(false);
|
|
expect(hasScope(['products.write'], 'services.write')).toBe(false);
|
|
});
|
|
|
|
it('empty allowlist denies everything', () => {
|
|
expect(hasScope([], 'products.write')).toBe(false);
|
|
});
|
|
|
|
it('does NOT expand `products.*` style wildcards (kept trivially auditable)', () => {
|
|
// If we ever add prefix wildcards this test will need updating; for
|
|
// now we want narrowed keys to mean exactly what they say.
|
|
expect(hasScope(['products.*'], 'products.write')).toBe(false);
|
|
});
|
|
|
|
it('wildcard works when mixed with other tokens', () => {
|
|
// A nonsense state but defensible — any explicit '*' grants all.
|
|
expect(hasScope(['products.read', '*'], 'projects.write')).toBe(true);
|
|
});
|
|
});
|