fix(db): stop losing secrets and notes to the boot and migration paths

An unreadable or empty encryption-key file was treated the same as a missing
one: regenerate and overwrite. That is the one case where regenerating is
the worst possible move - every secret already at rest becomes undecryptable,
permanently. Both now refuse to start and say which file to fix.

The key-rotation script advertised re-encrypting all stored secrets while its
hardcoded column lists had drifted: the AirTrail key, the per-user LLM key
and the plugin OAuth tokens were never migrated, so they silently stopped
decrypting after a rotation. The parity test now pins the full set against
what the code actually encrypts, rather than two tables of it.

The schema_version bump ran outside the migration's transaction, so a crash
in between left the schema advanced and the version stale - the next boot
replayed a non-idempotent step and exited, turning one crash into a boot
loop. And the TICKETJSON receipt migration matched with LIKE, which SQLite
treats case-insensitively, so a note somebody had typed starting 'ticketjson:'
was cut up and its text dropped.

Also: APP_VERSION was interpolated into migration SQL instead of bound, the
capture-metadata backfill passed (photoId, userId) to a (userId, photoId)
signature and its mock keyed on the wrong argument, TRUST_PROXY=0 was
accepted and silently coerced to 1, demo-reset hardcoded the db path instead
of honouring TREK_DB_FILE, and a demo instance with no DEMO_ADMIN_PASS set
now says out loud that its admin password is the published default.
This commit is contained in:
Maurice
2026-08-23 09:48:12 +02:00
committed by Maurice
parent 8e5fdaa877
commit a7aaa29c16
17 changed files with 699 additions and 16 deletions
+21 -1
View File
@@ -36,10 +36,30 @@ if (_encryptionKey) {
// Try the dedicated key file first (covers all installs after first start).
try {
_encryptionKey = fs.readFileSync(encKeyFile, 'utf8').trim();
} catch {
} catch (readErr: unknown) {
if ((readErr as NodeJS.ErrnoException)?.code !== 'ENOENT') {
// The file is there, we just cannot read it (permissions, I/O error).
// Continuing would resolve a different key and overwrite this one below,
// which permanently orphans every stored secret. Refuse to start instead.
console.error(
'FATAL: could not read',
encKeyFile,
'-',
readErr instanceof Error ? readErr.message : readErr,
);
console.error('Fix the file permissions or set ENCRYPTION_KEY explicitly.');
process.exit(1);
}
// File not found — first start on an existing or fresh install.
}
if (!_encryptionKey && fs.existsSync(encKeyFile)) {
// Present but empty: a truncated write, a full disk, a broken restore.
// Same reasoning as above. Regenerating here would silently replace the real key.
console.error('FATAL:', encKeyFile, 'is empty. Restore it from backup or set ENCRYPTION_KEY.');
process.exit(1);
}
if (!_encryptionKey) {
// One-time migration: existing install upgrading for the first time.
// Use the JWT secret as the encryption key and immediately write it to