⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.
TypeScript agenticagileai-agentsbug-reportingbug-trackerganttissue-trackerjirajira-alternativekanbanlinearlinear-alternativeopen-sourceproduct-managementproject-managementroadmapself-hostedsprint-planningteam-collaborationtrello-alternative
fix(auth): refuse API keys on the account endpoints (#627)
Croffe81a49fa946c
diff --git a/apps/api/src/__tests__/integration/api-keys.test.ts b/apps/api/src/__tests__/integration/api-keys.test.tsindex 823d1fa8..40567c12 100644--- a/apps/api/src/__tests__/integration/api-keys.test.ts+++ b/apps/api/src/__tests__/integration/api-keys.test.ts@@ -95,6 +95,20 @@ describe('api keys', () => {expect(await db.$count(apikey, eq(apikey.referenceId, user.userId))).toBe(1);});+ it('refuses enrolling a passkey with a key', async () => {+ const user = await signUpTestUser();+ const created = await auth.api.createApiKey({ body: { userId: user.userId, name: 'ci' } });+ const registerOptions = (headers: Record<string, string>) =>+ app.handle(+ new Request('http://localhost/api/auth/passkey/generate-register-options', {+ headers: { origin, ...headers },+ }),+ );++ expect((await registerOptions({ 'x-api-key': created.key })).status).toBe(403);+ expect((await registerOptions({ cookie: user.cookie })).status).toBe(200);+ });+it('refuses changing the expiry of a key, with the key itself included', async () => {const user = await signUpTestUser();const created = await auth.api.createApiKey({ body: { userId: user.userId, name: 'ci' } });diff --git a/packages/auth/AGENTS.md b/packages/auth/AGENTS.mdindex 6e9c5755..287b713c 100644--- a/packages/auth/AGENTS.md+++ b/packages/auth/AGENTS.md@@ -234,12 +234,14 @@ milliseconds but its handler passes the value to `getDate(value, "sec")`, so`API_KEY_DEFAULT_EXPIRES_IN_SEC` is in seconds; `maxExpiresIn` is in days, as the pluginreads it. Keys issued before this stay `expires_at` NULL and keep working.-The expiry only holds if a key cannot renew itself, and a request carrying one resolves to-the owner's session on every key endpoint. So `hooks.before` refuses both ways round it: an-`/api-key/update` that carries `expiresIn`, and an `/api-key/create` sent with an-`x-api-key` header. Keys are issued from a signed-in session; a longer life is a new key.-The refusal reads `ctx.request`, which a server-side `auth.api.createApiKey` does not-carry, so `issueKey` is unaffected.+The plugin resolves a key to the owner's session on every better-auth endpoint, and what+that session sets up there outlives the key: a passkey enrolled with it signs in after the+key is deleted and the password reset, and a new key renews the expiry. So the first check+in `hooks.before` refuses any request that carries an `x-api-key` header — a key is for the+planner API, and the account is managed from a signed-in session. It reads `ctx.request`,+which a server-side `auth.api.*` call does not carry, so `issueKey` and+`getSessionFromHeaders` are unaffected. `hooks.before` also refuses an `/api-key/update`+that carries `expiresIn`: a longer life is a new key.An agent's key is the exception and carries no expiry — an agent replays its stored secretwith nothing that would renew it, and an external agent's operator rotates it throughdiff --git a/packages/auth/src/index.ts b/packages/auth/src/index.tsindex fd95ab9c..868c3cec 100644--- a/packages/auth/src/index.ts+++ b/packages/auth/src/index.ts@@ -673,6 +673,18 @@ function createAuth() {// per request, so god mode can change them without a restart.hooks: {before: createAuthMiddleware(async (ctx) => {+ // A key resolves to its owner's session on every endpoint here, and what such a+ // session sets up outlives the key: a passkey enrolled with it still signs in+ // after the key is deleted and the password reset, and a new key starts a fresh+ // lifetime. A key is for the planner API; the account is managed from a signed-in+ // session. A server-side call, such as the one issuing an agent's key, carries+ // no request and passes.+ if (ctx.request?.headers.get('x-api-key')) {+ throw new APIError('FORBIDDEN', {+ message: 'An API key cannot manage the account. Sign in to do this.',+ });+ }+// Read here so the two sign-in endpoints, which also go through the// verification gate below, make one query instead of two.const passwordSettings = PASSWORD_PATHS.has(ctx.path) ? await getAuthSettings() : null;@@ -765,19 +777,7 @@ function createAuth() {return;}- // A key resolves to its owner's session, so a request carrying a leaked key- // reaches the key endpoints. It may not issue another one: a new key starts a- // fresh lifetime, which is the expiry of the leaked key renewed under a- // different row. Issuing a key is left to a signed-in session. The server-side- // call that issues an agent's key carries no request and is unaffected.- if (ctx.path === '/api-key/create' && ctx.request?.headers.get('x-api-key')) {- throw new APIError('FORBIDDEN', {- message: 'An API key cannot create another API key. Sign in to create one.',- });- }-- // The lifetime is fixed at creation for the same reason; a longer one is a new- // key, created from a session.+ // The lifetime of a key is fixed at creation; a longer one is a new key.if (ctx.path === '/api-key/update') {const body = ctx.body as { expiresIn?: number | null } | undefined;if (body && body.expiresIn !== undefined) {