|
| 1 | +/** |
| 2 | + * Unit tests for the API-key authorization wired into `createBoundAuthClient`. |
| 3 | + * These exercise only the in-memory decision branches (API-key allowlist / role |
| 4 | + * bypass), none of which call Better Auth — so a minimal stubbed `auth` is enough. |
| 5 | + */ |
| 6 | +import { describe, expect, it } from "bun:test"; |
| 7 | +import { createBoundAuthClient } from "./context-factory"; |
| 8 | +import type { BetterAuthInstance } from "./studio-context"; |
| 9 | + |
| 10 | +const stubAuth = { api: {} } as unknown as BetterAuthInstance; |
| 11 | + |
| 12 | +describe("createBoundAuthClient — API-key authorization", () => { |
| 13 | + it("authorizes an API key SOLELY by its allowlist, ignoring the owner role", async () => { |
| 14 | + const client = createBoundAuthClient({ |
| 15 | + auth: stubAuth, |
| 16 | + headers: new Headers(), |
| 17 | + role: "admin", // owner is an admin — must NOT widen the key |
| 18 | + permissions: { self: ["ORGANIZATION_GET"] }, |
| 19 | + apiKeyId: "key_1", |
| 20 | + }); |
| 21 | + |
| 22 | + expect(client.isApiKeyPrincipal).toBe(true); |
| 23 | + expect(await client.hasPermission({ self: ["ORGANIZATION_GET"] })).toBe( |
| 24 | + true, |
| 25 | + ); |
| 26 | + // The exact escalation the report found — denied even for an admin owner. |
| 27 | + expect(await client.hasPermission({ self: ["API_KEY_CREATE"] })).toBe( |
| 28 | + false, |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it("treats a wildcard key as full access (explicit full key)", async () => { |
| 33 | + const client = createBoundAuthClient({ |
| 34 | + auth: stubAuth, |
| 35 | + headers: new Headers(), |
| 36 | + role: "user", |
| 37 | + permissions: { "*": ["*"] }, |
| 38 | + apiKeyId: "key_2", |
| 39 | + }); |
| 40 | + |
| 41 | + expect(client.isApiKeyPrincipal).toBe(true); |
| 42 | + expect(await client.hasPermission({ self: ["API_KEY_CREATE"] })).toBe(true); |
| 43 | + expect(await client.hasPermission({ vir_x: ["SEND_MESSAGE"] })).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + it("denies an API key with no allowlist (fail-closed)", async () => { |
| 47 | + const client = createBoundAuthClient({ |
| 48 | + auth: stubAuth, |
| 49 | + headers: new Headers(), |
| 50 | + role: "owner", |
| 51 | + permissions: undefined, |
| 52 | + apiKeyId: "key_3", |
| 53 | + }); |
| 54 | + |
| 55 | + expect(client.isApiKeyPrincipal).toBe(true); |
| 56 | + expect(await client.hasPermission({ self: ["ORGANIZATION_GET"] })).toBe( |
| 57 | + false, |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + it("keeps the admin/owner role bypass for a non-API-key principal", async () => { |
| 62 | + const client = createBoundAuthClient({ |
| 63 | + auth: stubAuth, |
| 64 | + headers: new Headers(), |
| 65 | + role: "admin", |
| 66 | + permissions: { self: ["ORGANIZATION_GET"] }, // e.g. a mesh JWT payload |
| 67 | + // no apiKeyId — not an API key principal |
| 68 | + }); |
| 69 | + |
| 70 | + expect(client.isApiKeyPrincipal).toBe(false); |
| 71 | + expect(await client.hasPermission({ self: ["API_KEY_CREATE"] })).toBe(true); |
| 72 | + }); |
| 73 | +}); |
0 commit comments