KI-AGENT: Defekte Mailkonto-Verschlüsselung abfangen

This commit is contained in:
2026-09-07 19:31:53 +02:00
parent 49b4c10e10
commit 2f306ca8a2
5 changed files with 87 additions and 27 deletions

View File

@@ -23,27 +23,62 @@ export default async function emailAsUserRoutes(server: FastifyInstance) {
const encryptedValue = (value: unknown) => value ? decrypt(value as any) : null
const accountResponse = (row: any) => ({
id: row.id,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
userId: row.userId,
tenantId: row.tenantId,
type: row.type,
email: encryptedValue(row.emailEncrypted),
smtpHost: encryptedValue(row.smtpHostEncrypted),
smtpPort: row.smtpPort ? Number(row.smtpPort) : null,
smtpSsl: row.smtpSsl,
imapHost: encryptedValue(row.imapHostEncrypted),
imapPort: row.imapPort ? Number(row.imapPort) : null,
imapSsl: row.imapSsl,
hasPassword: Boolean(row.passwordEncrypted),
})
const accountResponse = (row: any) => {
const invalidEncryptedFields: string[] = []
const safeEncryptedValue = (value: unknown, field: string) => {
if (!value) return null
try {
return encryptedValue(value)
} catch {
invalidEncryptedFields.push(field)
return null
}
}
const accountCredentials = (row: any) => ({
...accountResponse(row),
password: encryptedValue(row.passwordEncrypted),
})
const email = safeEncryptedValue(row.emailEncrypted, "email")
const smtpHost = safeEncryptedValue(row.smtpHostEncrypted, "smtpHost")
const imapHost = safeEncryptedValue(row.imapHostEncrypted, "imapHost")
safeEncryptedValue(row.passwordEncrypted, "password")
if (invalidEncryptedFields.length) {
server.log.warn({
accountId: row.id,
invalidEncryptedFields,
}, "E-Mail-Kontodaten können mit dem aktuellen ENCRYPTION_KEY nicht entschlüsselt werden")
}
return {
id: row.id,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
userId: row.userId,
tenantId: row.tenantId,
type: row.type,
email,
displayName: email || `Mailkonto ${String(row.id).slice(0, 8)} muss repariert werden`,
smtpHost,
smtpPort: row.smtpPort ? Number(row.smtpPort) : null,
smtpSsl: row.smtpSsl,
imapHost,
imapPort: row.imapPort ? Number(row.imapPort) : null,
imapSsl: row.imapSsl,
hasPassword: Boolean(row.passwordEncrypted),
credentialsReadable: invalidEncryptedFields.length === 0,
invalidEncryptedFields,
}
}
const accountCredentials = (row: any) => {
const account = accountResponse(row)
if (!account.credentialsReadable) {
throw new Error("Die verschlüsselten Kontodaten sind nicht lesbar. Bitte das E-Mail-Konto in den Einstellungen vollständig neu speichern.")
}
return {
...account,
password: encryptedValue(row.passwordEncrypted),
}
}
const bodyValue = (body: any, camelKey: string, snakeKey: string) => body[camelKey] ?? body[snakeKey]