diff --git a/src/backend/starter.ts b/src/backend/starter.ts index f8b4c6ae..5da0ed37 100644 --- a/src/backend/starter.ts +++ b/src/backend/starter.ts @@ -19,9 +19,20 @@ import { systemLogger, versionLogger } from "./utils/logger.js"; const envPath = path.join(dataDir, ".env"); try { await fs.access(envPath); - dotenv.config({ path: envPath }); + // Load the persistent .env file and override process.env + const persistentConfig = dotenv.config({ path: envPath }); + if (persistentConfig.parsed) { + // Override process.env with values from persistent config + Object.assign(process.env, persistentConfig.parsed); + } systemLogger.info(`Loaded persistent configuration from ${envPath}`, { - operation: "config_load" + operation: "config_load", + hasDatabaseKey: !!persistentConfig.parsed?.DATABASE_KEY, + databaseKeyLength: persistentConfig.parsed?.DATABASE_KEY?.length || 0, + hasJwtSecret: !!persistentConfig.parsed?.JWT_SECRET, + jwtSecretLength: persistentConfig.parsed?.JWT_SECRET?.length || 0, + hasInternalAuthToken: !!persistentConfig.parsed?.INTERNAL_AUTH_TOKEN, + internalAuthTokenLength: persistentConfig.parsed?.INTERNAL_AUTH_TOKEN?.length || 0 }); } catch { // Config file doesn't exist yet, will be created on first run diff --git a/src/backend/utils/system-crypto.ts b/src/backend/utils/system-crypto.ts index 9283a553..5895dc6f 100644 --- a/src/backend/utils/system-crypto.ts +++ b/src/backend/utils/system-crypto.ts @@ -76,8 +76,19 @@ class SystemCrypto { // Check environment variable const envKey = process.env.DATABASE_KEY; + databaseLogger.info("Checking DATABASE_KEY from environment", { + operation: "db_key_check", + hasKey: !!envKey, + keyLength: envKey?.length || 0, + meetsLengthRequirement: envKey && envKey.length >= 64 + }); + if (envKey && envKey.length >= 64) { this.databaseKey = Buffer.from(envKey, 'hex'); + databaseLogger.info("Using existing DATABASE_KEY from environment", { + operation: "db_key_use_existing", + keyLength: envKey.length + }); return; }