From 71e578eac9dd78324aa9d8e94c72afa767b86dc3 Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Thu, 4 Dec 2025 22:57:39 -0500 Subject: [PATCH] ignore: fix provider credentials query for BYOK Provider credentials field was being selected from ProviderTable even when the table wasn't joined (when byokProvider was undefined). Now the join is conditional - when byokProvider exists, we join and get the credentials; when it doesn't, the join condition is always false so provider remains null. --- .../app/src/routes/zen/util/handler.ts | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/console/app/src/routes/zen/util/handler.ts b/packages/console/app/src/routes/zen/util/handler.ts index a7f025bab..e34704f98 100644 --- a/packages/console/app/src/routes/zen/util/handler.ts +++ b/packages/console/app/src/routes/zen/util/handler.ts @@ -362,8 +362,8 @@ export async function handler( throw new AuthError("Missing API key.") } - const data = await Database.use((tx) => { - const query = tx + const data = await Database.use((tx) => + tx .select({ apiKey: KeyTable.id, workspaceID: KeyTable.workspaceID, @@ -391,15 +391,18 @@ export async function handler( .innerJoin(BillingTable, eq(BillingTable.workspaceID, KeyTable.workspaceID)) .innerJoin(UserTable, and(eq(UserTable.workspaceID, KeyTable.workspaceID), eq(UserTable.id, KeyTable.userID))) .leftJoin(ModelTable, and(eq(ModelTable.workspaceID, KeyTable.workspaceID), eq(ModelTable.model, modelInfo.id))) - - if (modelInfo.byokProvider) { - query.leftJoin( + .leftJoin( ProviderTable, - and(eq(ProviderTable.workspaceID, KeyTable.workspaceID), eq(ProviderTable.provider, modelInfo.byokProvider)), + modelInfo.byokProvider + ? and( + eq(ProviderTable.workspaceID, KeyTable.workspaceID), + eq(ProviderTable.provider, modelInfo.byokProvider), + ) + : sql`false`, ) - } - return query.where(and(eq(KeyTable.key, apiKey), isNull(KeyTable.timeDeleted))).then((rows) => rows[0]) - }) + .where(and(eq(KeyTable.key, apiKey), isNull(KeyTable.timeDeleted))) + .then((rows) => rows[0]), + ) if (!data) throw new AuthError("Invalid API key.") logger.metric({