Add support for multiple encryption keys in data handling
Some checks failed
Code Analysis (JS/Vue) / analyze (push) Failing after 1m1s
Some checks failed
Code Analysis (JS/Vue) / analyze (push) Failing after 1m1s
This commit introduces a mechanism to handle multiple possible encryption keys for data decryption across various modules, including auth.js, members.js, newsletter.js, and encryption.js. It adds functions to retrieve potential old keys for migration purposes and updates the decryption logic to attempt decryption with these keys. Additionally, it includes warnings for users when old keys are used and provides guidance for re-encrypting data. This enhancement improves data migration capabilities and ensures backward compatibility with previously encrypted data.
This commit is contained in:
@@ -27,11 +27,51 @@ function getEncryptionKey() {
|
||||
return process.env.ENCRYPTION_KEY || 'local_development_encryption_key_change_in_production'
|
||||
}
|
||||
|
||||
// Check if data is encrypted by trying to parse as JSON first
|
||||
// Liste möglicher alter Verschlüsselungsschlüssel (für Migration)
|
||||
function getPossibleEncryptionKeys() {
|
||||
const currentKey = getEncryptionKey()
|
||||
const oldKeys = [
|
||||
'default-key-change-in-production',
|
||||
'local_development_encryption_key_change_in_production'
|
||||
]
|
||||
|
||||
// Aktueller Schlüssel zuerst, dann alte Schlüssel
|
||||
const keys = [currentKey]
|
||||
for (const oldKey of oldKeys) {
|
||||
if (oldKey !== currentKey) {
|
||||
keys.push(oldKey)
|
||||
}
|
||||
}
|
||||
|
||||
// Optional: Alter Schlüssel aus Environment-Variable
|
||||
if (process.env.OLD_ENCRYPTION_KEY && process.env.OLD_ENCRYPTION_KEY !== currentKey) {
|
||||
keys.push(process.env.OLD_ENCRYPTION_KEY)
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
// Check if data is encrypted by checking format indicators
|
||||
function isEncrypted(data) {
|
||||
if (!data || typeof data !== 'string') {
|
||||
return false
|
||||
}
|
||||
|
||||
const trimmed = data.trim()
|
||||
|
||||
// Empty or whitespace only
|
||||
if (!trimmed) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check for v2 prefix (v2:base64...)
|
||||
if (trimmed.startsWith('v2:')) {
|
||||
return true
|
||||
}
|
||||
|
||||
// Try to parse as JSON - if successful, it's likely unencrypted
|
||||
try {
|
||||
// Try to parse as JSON - if successful and looks like member data, it's unencrypted
|
||||
const parsed = JSON.parse(data.trim())
|
||||
const parsed = JSON.parse(trimmed)
|
||||
// If it's an array (members list) or object with member-like structure, it's unencrypted
|
||||
if (Array.isArray(parsed)) {
|
||||
return false // Unencrypted array
|
||||
@@ -42,8 +82,15 @@ function isEncrypted(data) {
|
||||
}
|
||||
return false
|
||||
} catch (_e) {
|
||||
// JSON parsing failed - likely encrypted base64
|
||||
return true
|
||||
// JSON parsing failed - check if it looks like base64 (encrypted)
|
||||
// Base64 strings typically don't contain spaces and have specific character set
|
||||
const base64Pattern = /^[A-Za-z0-9+/=]+$/
|
||||
if (base64Pattern.test(trimmed) && trimmed.length > 50) {
|
||||
// Looks like base64 and long enough to be encrypted data
|
||||
return true
|
||||
}
|
||||
// If it's not valid JSON and doesn't look like base64, assume unencrypted but malformed
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,29 +99,78 @@ export async function readMembers() {
|
||||
try {
|
||||
const data = await fs.readFile(MEMBERS_FILE, 'utf-8')
|
||||
|
||||
// Check if file is empty or whitespace only
|
||||
if (!data || !data.trim()) {
|
||||
console.warn('Mitgliederdaten-Datei ist leer')
|
||||
return []
|
||||
}
|
||||
|
||||
// Check if data is encrypted or plain JSON
|
||||
const encrypted = isEncrypted(data)
|
||||
|
||||
if (encrypted) {
|
||||
// Decrypt and parse
|
||||
// Versuche mit verschiedenen Schlüsseln zu entschlüsseln (für Migration)
|
||||
const possibleKeys = getPossibleEncryptionKeys()
|
||||
const encryptionKey = getEncryptionKey()
|
||||
try {
|
||||
return decryptObject(data, encryptionKey)
|
||||
} catch (decryptError) {
|
||||
console.error('Fehler beim Entschlüsseln der Mitgliederdaten:', decryptError)
|
||||
// Fallback: try to read as plain JSON (migration scenario)
|
||||
|
||||
// Warn if using default encryption key in production
|
||||
if (process.env.NODE_ENV === 'production' &&
|
||||
encryptionKey === 'local_development_encryption_key_change_in_production') {
|
||||
console.error('WARNUNG: Produktionsumgebung verwendet Standard-Verschlüsselungsschlüssel!')
|
||||
}
|
||||
|
||||
let lastError = null
|
||||
for (let i = 0; i < possibleKeys.length; i++) {
|
||||
const key = possibleKeys[i]
|
||||
try {
|
||||
const plainData = JSON.parse(data)
|
||||
console.warn('Entschlüsselung fehlgeschlagen, versuche als unverschlüsseltes Format zu lesen')
|
||||
return plainData
|
||||
} catch (_parseError) {
|
||||
console.error('Konnte Mitgliederdaten weder entschlüsseln noch als JSON lesen')
|
||||
return []
|
||||
const decrypted = decryptObject(data, key)
|
||||
|
||||
// Wenn mit altem Schlüssel entschlüsselt wurde, warnen und neu verschlüsseln
|
||||
if (i > 0 && key !== encryptionKey) {
|
||||
console.warn(`⚠️ Mitgliederdaten wurden mit altem Schlüssel entschlüsselt. Automatische Neuverschlüsselung...`)
|
||||
try {
|
||||
await writeMembers(decrypted)
|
||||
console.log('✅ Mitgliederdaten erfolgreich mit neuem Schlüssel neu verschlüsselt')
|
||||
} catch (writeError) {
|
||||
console.error('❌ Fehler beim Neuverschlüsseln:', writeError.message)
|
||||
console.error(' Bitte führen Sie manuell aus: node scripts/re-encrypt-data.js')
|
||||
}
|
||||
}
|
||||
|
||||
return decrypted
|
||||
} catch (decryptError) {
|
||||
lastError = decryptError
|
||||
// Versuche nächsten Schlüssel
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Alle Schlüssel fehlgeschlagen
|
||||
console.error('Fehler beim Entschlüsseln der Mitgliederdaten:')
|
||||
console.error(' Versuchte Schlüssel:', possibleKeys.length)
|
||||
console.error(' Letzter Fehler:', lastError?.message || 'Unbekannter Fehler')
|
||||
console.error(' Daten-Länge:', data.length)
|
||||
console.error(' Daten-Präfix (erste 50 Zeichen):', data.substring(0, 50))
|
||||
console.error('')
|
||||
console.error('💡 Lösung: Führen Sie das Re-Encrypt-Skript aus:')
|
||||
console.error(' node scripts/re-encrypt-data.js --old-key="<alter-schlüssel>"')
|
||||
console.error(' Oder setzen Sie OLD_ENCRYPTION_KEY als Environment-Variable')
|
||||
|
||||
// Fallback: try to read as plain JSON (migration scenario)
|
||||
try {
|
||||
const plainData = JSON.parse(data.trim())
|
||||
console.warn('Entschlüsselung fehlgeschlagen, versuche als unverschlüsseltes Format zu lesen')
|
||||
// Don't auto-migrate if decryption failed - might be wrong key
|
||||
return plainData
|
||||
} catch (parseError) {
|
||||
console.error('Konnte Mitgliederdaten weder entschlüsseln noch als JSON lesen')
|
||||
console.error('JSON-Parse-Fehler:', parseError.message)
|
||||
// Return empty array to prevent complete failure
|
||||
return []
|
||||
}
|
||||
} else {
|
||||
// Plain JSON - migrate to encrypted format
|
||||
const members = JSON.parse(data)
|
||||
const members = JSON.parse(data.trim())
|
||||
console.log('Migriere unverschlüsselte Mitgliederdaten zu verschlüsselter Speicherung...')
|
||||
|
||||
// Write back encrypted
|
||||
|
||||
Reference in New Issue
Block a user