fix: Resolve database encryption atomicity issues and enhance debugging #430

Merged
ZacharyZcR merged 7 commits from fix/database-encryption-atomicity into dev-1.8.1 2025-11-06 03:21:21 +00:00

7 Commits

Author SHA1 Message Date
Luke Gustafson
bcc35be865 Update src/backend/utils/database-file-encryption.ts
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-05 21:20:05 -06:00
Luke Gustafson
c18ac89b2d Update src/backend/utils/database-file-encryption.ts
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-05 21:19:58 -06:00
Luke Gustafson
333f4ca0a7 Update src/backend/utils/database-file-encryption.ts
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-05 21:19:53 -06:00
Luke Gustafson
7e96b8dcfa Update src/backend/utils/database-file-encryption.ts
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-05 21:19:46 -06:00
Luke Gustafson
090cc3f924 Update src/backend/utils/database-file-encryption.ts
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-05 21:19:33 -06:00
LukeGus
bb6214ca85 fix: Cleanup PR 2025-11-05 21:19:00 -06:00
ZacharyZcR
6c3fa14064 fix: Resolve database encryption atomicity issues and enhance debugging
This commit addresses critical data corruption issues caused by non-atomic
file writes during database encryption, and adds comprehensive diagnostic
logging to help debug encryption-related failures.

**Problem:**
Users reported "Unsupported state or unable to authenticate data" errors
when starting the application after system crashes or Docker container
restarts. The root cause was non-atomic writes of encrypted database files:

1. Encrypted data file written (step 1)
2. Metadata file written (step 2)
→ If process crashes between steps 1 and 2, files become inconsistent
→ New IV/tag in data file, old IV/tag in metadata
→ GCM authentication fails on next startup
→ User data permanently inaccessible

**Solution - Atomic Writes:**

1. Write-to-temp + atomic-rename pattern:
   - Write to temporary files (*.tmp-timestamp-pid)
   - Perform atomic rename operations
   - Clean up temp files on failure

2. Data integrity validation:
   - Add dataSize field to metadata
   - Verify file size before decryption
   - Early detection of corrupted writes

3. Enhanced error diagnostics:
   - Key fingerprints (SHA256 prefix) for verification
   - File modification timestamps
   - Detailed GCM auth failure messages
   - Automatic diagnostic info generation

**Changes:**

database-file-encryption.ts:
- Implement atomic write pattern in encryptDatabaseFromBuffer
- Implement atomic write pattern in encryptDatabaseFile
- Add dataSize field to EncryptedFileMetadata interface
- Validate file size before decryption in decryptDatabaseToBuffer
- Enhanced error messages for GCM auth failures
- Add getDiagnosticInfo() function for comprehensive debugging
- Add debug logging for all encryption/decryption operations

system-crypto.ts:
- Add detailed logging for DATABASE_KEY initialization
- Log key source (env var vs .env file)
- Add key fingerprints to all log messages
- Better error messages when key loading fails

db/index.ts:
- Automatically generate diagnostic info on decryption failure
- Log detailed debugging information to help users troubleshoot

**Debugging Info Added:**

- Key initialization: source, fingerprint, length, path
- Encryption: original size, encrypted size, IV/tag prefixes, temp paths
- Decryption: file timestamps, metadata content, key fingerprint matching
- Auth failures: .env file status, key availability, file consistency
- File diagnostics: existence, readability, size validation, mtime comparison

**Backward Compatibility:**
- dataSize field is optional (metadata.dataSize?: number)
- Old encrypted files without dataSize continue to work
- No migration required

**Testing:**
- Compiled successfully
- No breaking changes to existing APIs
- Graceful handling of legacy v1 encrypted files

Fixes data loss issues reported by users experiencing container restarts
and system crashes during database saves.
2025-11-06 08:18:18 +08:00