Skip to content

Commit 07d5c25

Browse files
committed
fix(api): refuse a personal v2 key with a missing owner as invalid, not a 500
1 parent b9dc603 commit 07d5c25

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎apps/sim/lib/api/server/routes/v2-api-key-auth.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,28 @@ describe('v2 API key authentication', () => {
199199
)
200200
})
201201

202+
it('refuses a personal key whose owner row is gone as invalid, expired or not', async () => {
203+
for (const expiresAt of [null, new Date(Date.now() - 1)]) {
204+
queueTableRows(schemaMock.apiKey, [
205+
{
206+
id: 'key-orphan',
207+
userId: 'user-gone',
208+
workspaceId: null,
209+
type: 'personal',
210+
access: 'write',
211+
expiresAt,
212+
userBanned: null,
213+
userSuspendedAt: null,
214+
},
215+
])
216+
const refusal = await authenticateV2ApiKey({ apiKey: 'orphan', bearer: null }).catch(
217+
(error: unknown) => error
218+
)
219+
expect(refusal).toBeInstanceOf(V2ApiKeyUnauthenticatedError)
220+
expect((refusal as V2ApiKeyUnauthenticatedError).message).toBe('Invalid API key')
221+
}
222+
})
223+
202224
it('propagates auth-store failures instead of converting them to invalid credentials', async () => {
203225
const failure = new Error('database unavailable')
204226
dbChainMockFns.limit.mockRejectedValueOnce(failure)

‎apps/sim/lib/api/server/routes/v2-api-key-auth.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ function requireValidRow(row: ApiKeyRow | undefined): ApiKeyRow {
8585
if (!row) throw new V2ApiKeyUnauthenticatedError()
8686
if (row.type === 'personal' && row.workspaceId === null) {
8787
if (row.userBanned === null) {
88-
throw new Error(`Personal API key ${row.id} is missing its credential owner`)
88+
logger.error('Personal API key is missing its credential owner', { keyId: row.id })
89+
throw new V2ApiKeyUnauthenticatedError()
8990
}
9091
if (row.userBanned) throw new V2ApiKeyUnauthenticatedError()
9192
/**

0 commit comments

Comments
 (0)