mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
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:
@@ -3,9 +3,15 @@ import path from 'path';
|
||||
import { readEnv } from '../app-config';
|
||||
|
||||
const dataDir = path.join(__dirname, '../../data');
|
||||
const dbPath = path.join(dataDir, 'travel.db');
|
||||
const baselinePath = path.join(dataDir, 'travel-baseline.db');
|
||||
|
||||
// Where the live DB actually is. database.ts honours TREK_DB_FILE, so hardcoding
|
||||
// data/travel.db here would copy the baseline over an unrelated file and leave
|
||||
// the database we just closed untouched. The open connection knows its own path.
|
||||
function liveDbPath(db: { name: string }): string {
|
||||
return db.name;
|
||||
}
|
||||
|
||||
function resetDemoUser(): void {
|
||||
if (!fs.existsSync(baselinePath)) {
|
||||
console.log('[Demo Reset] No baseline found, skipping. Admin must save baseline first.');
|
||||
@@ -13,6 +19,11 @@ function resetDemoUser(): void {
|
||||
}
|
||||
|
||||
const { db, closeDb, reinitialize } = require('../db/database');
|
||||
const dbPath = liveDbPath(db);
|
||||
if (dbPath === ':memory:') {
|
||||
console.log('[Demo Reset] In-memory database, nothing to restore.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Save admin's current credentials and API keys (these should survive the reset)
|
||||
// NOTE: different default than demo-seed (admin@trek.app) — pinned legacy quirk.
|
||||
@@ -98,6 +109,11 @@ function resetDemoUser(): void {
|
||||
|
||||
function saveBaseline(): void {
|
||||
const { db } = require('../db/database');
|
||||
const dbPath = liveDbPath(db);
|
||||
if (dbPath === ':memory:') {
|
||||
console.log('[Demo] In-memory database, no baseline to save.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Flush WAL so baseline file is self-contained
|
||||
try { db.exec('PRAGMA wal_checkpoint(TRUNCATE)'); } catch (e) {}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import bcrypt from 'bcryptjs';
|
||||
import Database from 'better-sqlite3';
|
||||
import { readEnv } from '../app-config';
|
||||
// Static like in demo-reset.job.ts: the module top is inert, everything that
|
||||
// touches the database happens inside the functions.
|
||||
import { saveBaseline, hasBaseline } from './demo-reset';
|
||||
|
||||
function seedDemoData(db: Database.Database): { adminId: number; demoId: number } {
|
||||
const ADMIN_USER = readEnv().demo.adminUser;
|
||||
@@ -12,6 +15,15 @@ function seedDemoData(db: Database.Database): { adminId: number; demoId: number
|
||||
// Create admin user if not exists
|
||||
let admin = db.prepare('SELECT id FROM users WHERE email = ?').get(ADMIN_EMAIL) as { id: number } | undefined;
|
||||
if (!admin) {
|
||||
if (!readEnv().demo.adminPassSet) {
|
||||
// The default is published in the docs and in this file, so an operator who
|
||||
// never set DEMO_ADMIN_PASS is handing out an admin account. Say so loudly;
|
||||
// changing the default would lock out every existing demo instance.
|
||||
console.warn(
|
||||
`[Demo] SECURITY: DEMO_ADMIN_PASS is not set. The admin account ${ADMIN_EMAIL} is being created ` +
|
||||
'with the public default password. Set DEMO_ADMIN_PASS before exposing this instance.',
|
||||
);
|
||||
}
|
||||
const hash = bcrypt.hashSync(ADMIN_PASS, 10);
|
||||
const r = db.prepare('INSERT INTO users (username, email, password_hash, role) VALUES (?, ?, ?, ?)').run(ADMIN_USER, ADMIN_EMAIL, hash, 'admin');
|
||||
admin = { id: Number(r.lastInsertRowid) };
|
||||
@@ -46,7 +58,6 @@ function seedDemoData(db: Database.Database): { adminId: number; demoId: number
|
||||
seedExampleTrips(db, admin.id, demo.id);
|
||||
|
||||
// Auto-save baseline after first seed
|
||||
const { saveBaseline, hasBaseline } = require('./demo-reset');
|
||||
if (!hasBaseline()) {
|
||||
saveBaseline();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user