fix: handle missing ENCRYPTION_KEY in API key save routes and fix openrouter provider type
All checks were successful
Deploy to VPS / deploy (push) Successful in 33s

The encrypt() call threw an unhandled error when ENCRYPTION_KEY env var was missing,
causing a 500 that the frontend displayed as "Netzwerkfehler beim Speichern des Schlüssels".
Now returns a clear error message. Also fixed provider type cast that excluded 'openrouter'.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
CTO
2026-04-13 21:58:02 +00:00
parent f4ad3da3ec
commit 622d0bee34
2 changed files with 19 additions and 4 deletions

View File

@@ -50,7 +50,14 @@ export async function PATCH(
auditDetails.isActive = isActive;
}
if (apiKey && typeof apiKey === 'string' && apiKey.length >= 8) {
updates.encryptedKey = encrypt(apiKey);
try {
updates.encryptedKey = encrypt(apiKey);
} catch {
return Response.json(
{ error: 'Serverkonfigurationsfehler: Verschlüsselung nicht verfügbar. Bitte ENCRYPTION_KEY prüfen.' },
{ status: 500 },
);
}
updates.keyHint = keyHint(apiKey);
auditDetails.keyRotated = true;
}

View File

@@ -60,7 +60,7 @@ export async function POST(request: Request) {
.where(
and(
eq(tenantApiKeys.tenantId, ctx.tenantId),
eq(tenantApiKeys.provider, provider as 'anthropic' | 'openai' | 'ollama'),
eq(tenantApiKeys.provider, provider as AIProvider),
label ? eq(tenantApiKeys.label, label) : undefined,
),
)
@@ -73,14 +73,22 @@ export async function POST(request: Request) {
);
}
const encryptedKey = encrypt(apiKey);
let encryptedKey: string;
try {
encryptedKey = encrypt(apiKey);
} catch {
return Response.json(
{ error: 'Serverkonfigurationsfehler: Verschlüsselung nicht verfügbar. Bitte ENCRYPTION_KEY prüfen.' },
{ status: 500 },
);
}
const hint = keyHint(apiKey);
const [created] = await db
.insert(tenantApiKeys)
.values({
tenantId: ctx.tenantId,
provider: provider as 'anthropic' | 'openai' | 'ollama',
provider: provider as AIProvider,
encryptedKey,
keyHint: hint,
label: label || null,