diff --git a/Dockerfile b/Dockerfile index b813097e9..0b6bb7742 100644 --- a/Dockerfile +++ b/Dockerfile @@ -120,9 +120,9 @@ COPY --chown=node:node wiki ./wiki # tsconfig-paths/register reads this at runtime to resolve MCP SDK paths. COPY --chown=node:node server/tsconfig.json ./server/ # Encryption-key rotation is run on demand via tsx (a prod dep) straight from the -# raw .ts source — it never enters dist, so it must be copied in explicitly or +# raw .ts source (the command and its stores module) — they never enter dist, so they must be copied in explicitly or # `node --import tsx scripts/migrate-encryption.ts` fails with module-not-found. -COPY --chown=node:node server/scripts/migrate-encryption.ts ./server/scripts/migrate-encryption.ts +COPY --chown=node:node server/scripts/migrate-encryption.ts server/scripts/migrate-encryption-stores.ts ./server/scripts/ # Admin recovery script (node server/reset-admin.js) for locked-out installs. COPY --chown=node:node server/reset-admin.js ./server/reset-admin.js COPY --chown=node:node --from=shared-builder /app/shared/dist ./shared/dist diff --git a/server/scripts/migrate-encryption-stores.ts b/server/scripts/migrate-encryption-stores.ts new file mode 100644 index 000000000..260f3b780 --- /dev/null +++ b/server/scripts/migrate-encryption-stores.ts @@ -0,0 +1,104 @@ +/** + * The database side of the encryption key rotation (`migrate-encryption.ts`): + * the statements it runs, on the SQLite file through better-sqlite3 or on a + * Postgres database through the server's MikroORM connection, and where the + * Postgres path finds the server's code. Kept beside the command so that one + * stays within the size limit; the image copies both. + */ +import type { EntityManager } from '@mikro-orm/core'; + +import type Database from 'better-sqlite3'; +import fs from 'fs'; +import path from 'path'; + +/** The parts of the server the Postgres path uses, loaded at run time (`loadServer`). */ +export interface ServerModules { + engine: typeof import('../src/db/engine'); + appConfig: typeof import('../src/app-config'); + pgTools: typeof import('../src/db/engine/postgres/pg-tools'); +} + +/** + * The statements the rotation needs, on either engine. Every statement is + * written once, with `?` placeholders both engines take; only the catalog + * lookups differ. The rotation runs inside one transaction the caller opens. + */ +export interface SecretStore { + all(sql: string, params?: unknown[]): Promise; + get(sql: string, params?: unknown[]): Promise; + run(sql: string, params?: unknown[]): Promise; + tableExists(name: string): Promise; + columnsOf(table: string): Promise>; +} + +/** The SQLite file through better-sqlite3, whose calls are synchronous; the store only wraps them. */ +export function sqliteStore(db: Database.Database): SecretStore { + return { + async all(sql: string, params: unknown[] = []) { + return db.prepare(sql).all(...params) as T[]; + }, + async get(sql: string, params: unknown[] = []) { + return db.prepare(sql).get(...params) as T | undefined; + }, + async run(sql: string, params: unknown[] = []) { + db.prepare(sql).run(...params); + }, + async tableExists(name: string) { + return !!db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?").get(name); + }, + async columnsOf(table: string) { + const rows = db.prepare('SELECT name FROM pragma_table_info(?)').all(table) as { name: string }[]; + return new Set(rows.map((r) => r.name)); + }, + }; +} + +/** + * A Postgres database through the server's MikroORM connection, inside the + * transaction `em` holds when it is a transactional fork. The catalog lookups + * read the current schema, the one TREK keeps its tables in. + */ +export function postgresStore(em: EntityManager): SecretStore { + const connection = em.getConnection(); + const ctx = em.getTransactionContext(); + const all = async (sql: string, params: unknown[] = []): Promise => + (await connection.execute(sql, params, 'all', ctx)) as T[]; + return { + all, + async get(sql: string, params: unknown[] = []) { + const [row] = await all(sql, params); + return row; + }, + async run(sql: string, params: unknown[] = []) { + await connection.execute(sql, params, 'run', ctx); + }, + async tableExists(name: string) { + const [row] = await all<{ present: boolean }>('SELECT to_regclass(?) IS NOT NULL AS present', [name]); + return row?.present === true; + }, + async columnsOf(table: string) { + const rows = await all<{ name: string }>( + 'SELECT column_name AS name FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = ?', + [table], + ); + return new Set(rows.map((r) => r.name)); + }, + }; +} + +/** + * The server's engine, app-config and Postgres tools: from src/ under tsx in a + * source checkout, so it runs without a build, else from the built dist/ the + * image ships (it has no src/). + */ +export function loadServer(): ServerModules { + const source = path.join(__dirname, '..', 'src'); + const root = fs.existsSync(path.join(source, 'db', 'engine', 'index.ts')) + ? source + : path.join(__dirname, '..', 'dist'); + return { + engine: require(path.join(root, 'db', 'engine')) as ServerModules['engine'], + appConfig: require(path.join(root, 'app-config')) as ServerModules['appConfig'], + pgTools: require(path.join(root, 'db', 'engine', 'postgres', 'pg-tools')) as ServerModules['pgTools'], + }; +} diff --git a/server/scripts/migrate-encryption.ts b/server/scripts/migrate-encryption.ts index b09a96bfa..4f9be158d 100644 --- a/server/scripts/migrate-encryption.ts +++ b/server/scripts/migrate-encryption.ts @@ -13,7 +13,23 @@ * * The script will prompt for the old and new keys interactively so they never * appear in shell history, process arguments, or log output. + * + * It works on the database the server runs on. Without TREK_DB_URL that is the + * SQLite file (DB_PATH, or data/travel.db), opened with better-sqlite3. With + * TREK_DB_URL it is the Postgres database, reached through the server's own + * engine (its pool, SSL settings and write lock): from src/ in a source + * checkout, from the built dist/ in the image. Either way the rotation is one + * transaction, taken after a copy of the database. */ +import { + loadServer, + postgresStore, + sqliteStore, + type SecretStore, + type ServerModules, +} from './migrate-encryption-stores'; +import { MikroORM } from '@mikro-orm/core'; + import Database from 'better-sqlite3'; import crypto from 'crypto'; import fs from 'fs'; @@ -84,19 +100,25 @@ function decryptMfa(value: string, encryptionKey: string): string | null { // Lines are collected into a queue as soon as readline emits them — this // prevents the race where a line event fires before the next listener is // registered (common with piped / pasted input that arrives all at once). +// +// Opened when the command starts, not on import: the Postgres-lane test +// imports the rotation and must not hold stdin open. -const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); +let rl: readline.Interface; const lineQueue: string[] = []; const lineWaiters: ((line: string) => void)[] = []; -rl.on('line', (line) => { - if (lineWaiters.length > 0) { - lineWaiters.shift()!(line); - } else { - lineQueue.push(line); - } -}); +function openPrompts(): void { + rl = readline.createInterface({ input: process.stdin, output: process.stdout }); + rl.on('line', (line) => { + if (lineWaiters.length > 0) { + lineWaiters.shift()!(line); + } else { + lineQueue.push(line); + } + }); +} function nextLine(): Promise { return new Promise((resolve) => { @@ -124,49 +146,541 @@ async function prompt(question: string): Promise { const line = await nextLine(); return line.trim(); } - // --------------------------------------------------------------------------- // Migration // --------------------------------------------------------------------------- -interface MigrationResult { +export interface MigrationResult { migrated: number; alreadyMigrated: number; skipped: number; errors: string[]; } -async function main() { - console.log('=== TREK Encryption Key Migration ===\n'); - console.log('This script re-encrypts all stored secrets under a new ENCRYPTION_KEY.'); - console.log('A backup of the database will be created before any changes are made.\n'); +/** A JSON column's value: text on SQLite, and on Postgres too except for `addons.config`, which is `jsonb` there. */ +function parseJson(raw: unknown): unknown { + return typeof raw === 'string' ? JSON.parse(raw) : raw; +} - // A Postgres install keeps its secrets in the database TREK_DB_URL names, - // which this script cannot rotate yet. It stops before it opens, creates or - // backs up any file: rotating a SQLite file left over from before the move - // would rewrite the key file while the secrets in Postgres stay under the - // old key. - if (process.env.TREK_DB_URL?.trim()) { - rl.close(); - console.error('TREK_DB_URL is set: this install keeps its secrets in Postgres, which this script cannot'); - console.error('re-encrypt yet. Nothing was changed. Keep the current ENCRYPTION_KEY; see the wiki page'); - console.error('Encryption-Key-Rotation.'); - process.exit(1); +/** + * Re-encrypts every stored secret from `oldKey` to `newKey` through `store`. + * A value already under the new key is left alone, one neither key opens is + * reported in `errors`, so running it twice is harmless. + */ +async function rotateSecrets(store: SecretStore, oldKey: string, newKey: string): Promise { + const result: MigrationResult = { migrated: 0, alreadyMigrated: 0, skipped: 0, errors: [] }; + + // Helper: migrate a single api-key-style value (enc:v1: prefix) + function migrateApiKeyValue(raw: string, label: string): string | null { + if (!raw || !raw.startsWith(ENCRYPTED_PREFIX)) { + result.skipped++; + console.warn(` SKIP ${label}: not an encrypted value (missing enc:v1: prefix)`); + return null; + } + + const plain = decryptApiKey(raw, oldKey); + if (plain !== null) { + result.migrated++; + return encryptApiKey(plain, newKey); + } + + // Try new key — already migrated? + const check = decryptApiKey(raw, newKey); + if (check !== null) { + result.alreadyMigrated++; + return null; // no change needed + } + + result.errors.push(`${label}: decryption failed with both keys`); + console.error(` ERROR ${label}: could not decrypt with either key — skipping`); + return null; } - // Resolve DB path - const dbPath = path.resolve(process.env.DB_PATH ?? path.join(__dirname, '../data/travel.db')); + // Helper: migrate a single MFA value (no prefix, raw base64) + function migrateMfaValue(raw: string, label: string): string | null { + if (!raw) { + result.skipped++; + return null; + } - if (!fs.existsSync(dbPath)) { - console.error(`Database not found at: ${dbPath}`); - console.error('Set DB_PATH env var if your database is in a non-standard location.'); - process.exit(1); + const plain = decryptMfa(raw, oldKey); + if (plain !== null) { + result.migrated++; + return encryptMfa(plain, newKey); + } + + const check = decryptMfa(raw, newKey); + if (check !== null) { + result.alreadyMigrated++; + return null; + } + + result.errors.push(`${label}: decryption failed with both keys`); + console.error(` ERROR ${label}: could not decrypt with either key — skipping`); + return null; } - console.log(`Database: ${dbPath}\n`); + // --- app_settings: oidc_client_secret, smtp_pass, admin_webhook_url, admin_ntfy_token, + // plus the instance-wide provider keys (#1939) --- + // + // The last two mirror INSTANCE_API_KEY_NAMES in + // src/nest/settings/instance-api-keys.ts. Spelled out rather than imported, + // because that module pulls in DatabaseService and this script deliberately + // stays independent of src/ (see the crypto note above). Miss one and a + // rotation leaves it encrypted under the old key, which reads back as "no + // key" and silently drops the install to OpenStreetMap, so the copy is + // pinned by tests/unit/db/migrate-encryption-parity.test.ts. + // + // web_push_vapid_private_key is the Web Push signing key VapidKeysService + // generates. Missed here it would no longer decrypt under the new key, and + // push would stay off on every device until the old key came back. + // + // service_key is the installation's key towards the liketrek services. + // Missed here it reads back as unusable, the installation registers anew and + // loses its id, and a revoked key would come back as a fresh one. + // + // The mail import's IMAP password and webhook secret: missed, the box stops + // being read and every signed mail is refused until an admin types them again. + for (const key of [ + 'oidc_client_secret', + 'smtp_pass', + 'admin_webhook_url', + 'admin_ntfy_token', + 'maps_api_key', + 'unsplash_api_key', + 'amap_api_key', + 'web_push_vapid_private_key', + 'service_key', + 'mail_ingest_imap_pass', + 'mail_ingest_webhook_secret', + ]) { + const row = await store.get<{ value: string }>('SELECT value FROM app_settings WHERE key = ?', [key]); + if (!row?.value) continue; + const newVal = migrateApiKeyValue(row.value, `app_settings.${key}`); + if (newVal !== null) { + await store.run('UPDATE app_settings SET value = ? WHERE key = ?', [newVal, key]); + } + } - // SQLite silently opens an unwritable file read-only (#2933); db.readonly can't tell. - const notWritable = [dbPath, path.dirname(dbPath)].find((p) => { + // --- users: api key columns + synology credentials --- + // unsplash_api_key was missing here for as long as the column has existed. It + // matters now that the resolver reads it as the per-user fallback (#1939): left + // out of a rotation it stays encrypted under the old key and reads back as unset. + // airtrail_api_key arrived with a later migration, so an older database may + // not have the column at all — filter the list against the real table rather + // than letting one missing column throw the whole rotation. + const userColumns = await store.columnsOf('users'); + const apiKeyColumns = [ + 'maps_api_key', + 'unsplash_api_key', + // Added with the Amap provider; filtered against the real table below, so a + // database that has not run that migration yet is not a rotation failure. + 'amap_api_key', + 'openweather_api_key', + 'immich_api_key', + 'synology_password', + 'synology_sid', + 'synology_did', + 'airtrail_api_key', + ].filter((c) => userColumns.has(c)); + const users = await store.all<{ id: number }>('SELECT id FROM users'); + + for (const user of users) { + const row = await store.get>(`SELECT ${apiKeyColumns.join(', ')} FROM users WHERE id = ?`, [ + user.id, + ]); + if (!row) continue; + + for (const col of apiKeyColumns) { + if (!row[col]) continue; + const newVal = migrateApiKeyValue(row[col], `users[${user.id}].${col}`); + if (newVal !== null) { + await store.run(`UPDATE users SET ${col} = ? WHERE id = ?`, [newVal, user.id]); + } + } + + // mfa_secret (mfa crypto) + const mfaRow = await store.get<{ mfa_secret: string }>( + 'SELECT mfa_secret FROM users WHERE id = ? AND mfa_secret IS NOT NULL', + [user.id], + ); + if (mfaRow?.mfa_secret) { + const newVal = migrateMfaValue(mfaRow.mfa_secret, `users[${user.id}].mfa_secret`); + if (newVal !== null) { + await store.run('UPDATE users SET mfa_secret = ? WHERE id = ?', [newVal, user.id]); + } + } + } + + // --- app_settings: storage.backends --- + // Storage backend secrets live encrypted INSIDE the storage.backends JSON + // array (BACKENDS_KEY in src/nest/storage/storage-registry.service.ts is + // its own app_settings row — 'storage.categories' is a sibling row with no + // secrets), on the fields the shared type registry marks `secret` (only + // options.secretAccessKey for s3 today; src/nest/storage/storage-secrets.ts + // encrypts it with the same enc:v1: scheme as the api-key columns above). + // This script can't import storageSecretFields from @trek/shared (kept + // independent of the app, see the header note), so the field list per + // backend type is pinned here. Miss one and that backend's secret stays + // encrypted under the old key after rotation, then fails to decrypt on the + // next read (StorageBackendError) — pinned by + // tests/unit/db/migrate-encryption-parity.test.ts. + const STORAGE_BACKEND_SECRET_FIELDS: Record = { + s3: ['secretAccessKey'], + }; + { + const row = await store.get<{ value: string }>("SELECT value FROM app_settings WHERE key = 'storage.backends'"); + if (row?.value) { + let backends: unknown; + try { + backends = JSON.parse(row.value); + } catch { + result.errors.push('app_settings.storage.backends: invalid JSON — skipping'); + backends = undefined; + } + if (backends !== undefined) { + if (Array.isArray(backends)) { + let changed = false; + for (const backend of backends as Record[]) { + if (!backend || typeof backend !== 'object') continue; + const type = typeof backend.type === 'string' ? backend.type : undefined; + const fields = type ? STORAGE_BACKEND_SECRET_FIELDS[type] : undefined; + if (!fields || fields.length === 0) continue; + const options = backend.options; + if (!options || typeof options !== 'object') continue; + const name = typeof backend.name === 'string' ? backend.name : '?'; + for (const field of fields) { + const raw = (options as Record)[field]; + if (typeof raw !== 'string' || !raw) continue; + const newVal = migrateApiKeyValue(raw, `app_settings.storage.backends[${name}].${field}`); + if (newVal !== null) { + (options as Record)[field] = newVal; + changed = true; + } + } + } + if (changed) { + await store.run("UPDATE app_settings SET value = ? WHERE key = 'storage.backends'", [ + JSON.stringify(backends), + ]); + } + } else { + result.errors.push('app_settings.storage.backends: not an array — skipping'); + } + } + } + } + + // --- settings: per-user encrypted keys --- + const encryptedSettingKeys = ['webhook_url', 'ntfy_token', 'mapbox_access_token', 'carto_api_key', 'llm_api_key']; + const settingRows = await store.all<{ user_id: number; key: string; value: string }>( + `SELECT user_id, key, value FROM settings WHERE key IN (${encryptedSettingKeys.map(() => '?').join(', ')})`, + encryptedSettingKeys, + ); + + for (const row of settingRows) { + if (!row.value) continue; + const newVal = migrateApiKeyValue(row.value, `settings[user=${row.user_id}].${row.key}`); + if (newVal !== null) { + await store.run('UPDATE settings SET value = ? WHERE user_id = ? AND key = ?', [newVal, row.user_id, row.key]); + } + } + + // --- plugins: OAuth tokens and secret settings fields --- + // The plugin host encrypts with the same enc:v1: scheme as everything above: + // plugin-oauth.service stores both tokens encrypted, and plugins.service + // encrypts every settings field the manifest marked secret (instance scope in + // plugins.config, user scope in plugin_user_config.config). Left out of a + // rotation, a connected plugin reads back as "not connected" and a secret + // setting as unset, with nothing in the log to say why. Every table here + // arrived with a later migration, so each block checks that it exists. + if (await store.tableExists('plugin_oauth_tokens')) { + const tokenRows = await store.all<{ + plugin_id: string; + user_id: number; + access_token: string | null; + refresh_token: string | null; + }>('SELECT plugin_id, user_id, access_token, refresh_token FROM plugin_oauth_tokens'); + for (const row of tokenRows) { + for (const col of ['access_token', 'refresh_token'] as const) { + const raw = row[col]; + if (!raw) continue; + const newVal = migrateApiKeyValue(raw, `plugin_oauth_tokens[${row.plugin_id},${row.user_id}].${col}`); + if (newVal !== null) { + await store.run(`UPDATE plugin_oauth_tokens SET ${col} = ? WHERE plugin_id = ? AND user_id = ?`, [ + newVal, + row.plugin_id, + row.user_id, + ]); + } + } + } + } + + if (await store.tableExists('plugin_settings_fields')) { + // Which keys are secret is per plugin and per scope, and it lives in the DB + // rather than in this script, so nothing has to be pinned by hand here. + const secretFields = await store.all<{ plugin_id: string; field_key: string; scope: string | null }>( + 'SELECT plugin_id, field_key, scope FROM plugin_settings_fields WHERE secret = 1', + ); + const secretKeysFor = (pluginId: string, scope: string): string[] => + secretFields.filter((f) => f.plugin_id === pluginId && (f.scope ?? 'instance') === scope).map((f) => f.field_key); + + const migrateConfigJson = (raw: string | null, keys: string[], label: string): string | null => { + if (!raw || keys.length === 0) return null; + let parsed: unknown; + try { + parsed = JSON.parse(raw); + } catch { + result.errors.push(`${label}: invalid JSON — skipping`); + return null; + } + if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { + result.errors.push(`${label}: not an object — skipping`); + return null; + } + const config = parsed as Record; + let changed = false; + for (const key of keys) { + const value = config[key]; + if (typeof value !== 'string' || !value) continue; + const newVal = migrateApiKeyValue(value, `${label}.${key}`); + if (newVal !== null) { + config[key] = newVal; + changed = true; + } + } + return changed ? JSON.stringify(config) : null; + }; + + if (await store.tableExists('plugins')) { + const pluginRows = await store.all<{ id: string; config: string | null }>('SELECT id, config FROM plugins'); + for (const row of pluginRows) { + const next = migrateConfigJson(row.config, secretKeysFor(row.id, 'instance'), `plugins[${row.id}].config`); + if (next !== null) { + await store.run('UPDATE plugins SET config = ? WHERE id = ?', [next, row.id]); + } + } + } + + if (await store.tableExists('plugin_user_config')) { + const userConfigRows = await store.all<{ plugin_id: string; user_id: number; config: string | null }>( + 'SELECT plugin_id, user_id, config FROM plugin_user_config', + ); + for (const row of userConfigRows) { + const next = migrateConfigJson( + row.config, + secretKeysFor(row.plugin_id, 'user'), + `plugin_user_config[${row.plugin_id},${row.user_id}].config`, + ); + if (next !== null) { + await store.run('UPDATE plugin_user_config SET config = ? WHERE plugin_id = ? AND user_id = ?', [ + next, + row.plugin_id, + row.user_id, + ]); + } + } + } + } + + // --- mail_ingest: forwarding address tokens and the stored mails --- + // A person sees their address again through its token_enc; a forwarded mail + // waits for its reading, and for the inbox's ways out, as raw_eml_enc. Missed, + // the addresses still take mail (they are found by hash) but show as unset, + // and every stored mail reads as unreadable. + for (const [table, column] of [ + ['mail_ingest_addresses', 'token_enc'], + ['mail_ingest_messages', 'raw_eml_enc'], + ] as const) { + if (!(await store.tableExists(table))) continue; + const rows = await store.all<{ id: number; value: string }>( + `SELECT id, ${column} AS value FROM ${table} WHERE ${column} IS NOT NULL`, + ); + for (const row of rows) { + const newVal = migrateApiKeyValue(row.value, `${table}[${row.id}].${column}`); + if (newVal !== null) { + await store.run(`UPDATE ${table} SET ${column} = ? WHERE id = ?`, [newVal, row.id]); + } + } + } + + // --- addons: the AI Parsing addon's instance-wide API key --- + // Stored encrypted as `apiKey` inside the addon's JSON config + // (llm-parse/llm-config.ts). Left out, LLM parsing quietly runs without a + // key after the rotation, since a failed decrypt reads as "no key". The + // column is text on SQLite and jsonb on Postgres, whose driver hands it back + // parsed; the JSON text written back fits both. + if (await store.tableExists('addons')) { + const addonRows = await store.all<{ id: string; config: unknown }>( + 'SELECT id, config FROM addons WHERE config IS NOT NULL', + ); + for (const row of addonRows) { + let config: unknown; + try { + config = parseJson(row.config); + } catch { + continue; + } + if (!config || typeof config !== 'object' || Array.isArray(config)) continue; + const apiKey = (config as Record).apiKey; + if (typeof apiKey !== 'string' || !apiKey) continue; + const newVal = migrateApiKeyValue(apiKey, `addons[${row.id}].config.apiKey`); + if (newVal !== null) { + await store.run('UPDATE addons SET config = ? WHERE id = ?', [ + JSON.stringify({ ...(config as Record), apiKey: newVal }), + row.id, + ]); + } + } + } + + // --- trip_album_links: passphrase --- + const albumLinks = await store.all<{ id: number; passphrase: string }>( + 'SELECT id, passphrase FROM trip_album_links WHERE passphrase IS NOT NULL', + ); + for (const row of albumLinks) { + const newVal = migrateApiKeyValue(row.passphrase, `trip_album_links[${row.id}].passphrase`); + if (newVal !== null) { + await store.run('UPDATE trip_album_links SET passphrase = ? WHERE id = ?', [newVal, row.id]); + } + } + + // --- dawarich_connections: api_key --- + // Its own table rather than a users column, so it needs its own block. The + // table arrived with a later migration, hence the existence check: an older + // database simply has nothing to rotate here. + if (await store.tableExists('dawarich_connections')) { + const connections = await store.all<{ user_id: number; api_key: string }>( + 'SELECT user_id, api_key FROM dawarich_connections WHERE api_key IS NOT NULL', + ); + for (const row of connections) { + const newVal = migrateApiKeyValue(row.api_key, `dawarich_connections[${row.user_id}].api_key`); + if (newVal !== null) { + await store.run('UPDATE dawarich_connections SET api_key = ? WHERE user_id = ?', [newVal, row.user_id]); + } + } + } + + // --- trek_photos: passphrase --- + const photos = await store.all<{ id: number; passphrase: string }>( + 'SELECT id, passphrase FROM trek_photos WHERE passphrase IS NOT NULL', + ); + for (const row of photos) { + const newVal = migrateApiKeyValue(row.passphrase, `trek_photos[${row.id}].passphrase`); + if (newVal !== null) { + await store.run('UPDATE trek_photos SET passphrase = ? WHERE id = ?', [newVal, row.id]); + } + } + + // --- document_connections: secrets --- + // Every credential of a document connection sits in ONE encrypted JSON blob + // (src/nest/doc-sync/doc-sync-secrets.ts) rather than in a column per + // provider, so a new provider changes nothing here: the blob is rotated as + // an opaque value. It also holds secrets the provider handed out itself, + // such as the DSM device token, which no form can re-enter. Left out of a + // rotation, every trip binding on the instance reads back as unauthorized + // and Synology needs a fresh OTP pairing. The table arrived with a later + // migration, hence the existence check. + if (await store.tableExists('document_connections')) { + const documentConnections = await store.all<{ id: number; secrets: string }>( + 'SELECT id, secrets FROM document_connections WHERE secrets IS NOT NULL', + ); + for (const row of documentConnections) { + const newVal = migrateApiKeyValue(row.secrets, `document_connections[${row.id}].secrets`); + if (newVal !== null) { + await store.run('UPDATE document_connections SET secrets = ? WHERE id = ?', [newVal, row.id]); + } + } + } + + // --- ai_connections: secrets --- + // The AI assistant's connections keep their credential in ONE encrypted + // JSON blob (src/nest/ai-connections/ai-secrets.ts): an API key, or a + // ChatGPT sign-in's tokens. Left out of a rotation, the instance's + // connection asks the admin for its key again and every ChatGPT user has + // to sign in anew. The table arrived with a later migration, hence the + // existence check. + if (await store.tableExists('ai_connections')) { + const aiConnections = await store.all<{ id: number; secrets: string }>( + 'SELECT id, secrets FROM ai_connections WHERE secrets IS NOT NULL', + ); + for (const row of aiConnections) { + const newVal = migrateApiKeyValue(row.secrets, `ai_connections[${row.id}].secrets`); + if (newVal !== null) { + await store.run('UPDATE ai_connections SET secrets = ? WHERE id = ?', [newVal, row.id]); + } + } + } + + // --- trip_document_links: webhook_secret --- + // The secret a provider signs its webhook calls with, stored through the + // same blob helper. It is written once when the link is created and never + // regenerated, so a rotation that misses it leaves the link with nothing to + // check a signature against. + if (await store.tableExists('trip_document_links')) { + const documentLinks = await store.all<{ id: number; webhook_secret: string }>( + 'SELECT id, webhook_secret FROM trip_document_links WHERE webhook_secret IS NOT NULL', + ); + for (const row of documentLinks) { + const newVal = migrateApiKeyValue(row.webhook_secret, `trip_document_links[${row.id}].webhook_secret`); + if (newVal !== null) { + await store.run('UPDATE trip_document_links SET webhook_secret = ? WHERE id = ?', [newVal, row.id]); + } + } + } + + return result; +} + +// --------------------------------------------------------------------------- +// The two engines +// --------------------------------------------------------------------------- + +/** + * The rotation on the SQLite file, in one transaction. better-sqlite3's + * `transaction()` takes no async function, so it is opened by hand; nothing + * else uses this connection meanwhile. IMMEDIATE takes the write lock up + * front, waiting out busy_timeout while a running server writes, instead of + * failing at the first UPDATE when the server wrote after the first read. + */ +async function rotateOnSqlite(db: Database.Database, oldKey: string, newKey: string): Promise { + db.exec('BEGIN IMMEDIATE'); + try { + const result = await rotateSecrets(sqliteStore(db), oldKey, newKey); + db.exec('COMMIT'); + return result; + } catch (err) { + if (db.inTransaction) db.exec('ROLLBACK'); + throw err; + } +} + +/** + * The rotation on Postgres, in one transaction that first takes the engine's + * write lock (`serializeWrites`), the one every `UnitOfWork` transaction of a + * running server takes too: the server's writes wait until the rotation has + * committed or rolled back. + */ +export async function rotateOnPostgres( + orm: MikroORM, + engine: Pick, + oldKey: string, + newKey: string, +): Promise { + return orm.em.fork().transactional(async (em) => { + await engine.serializeWrites(em); + return rotateSecrets(postgresStore(em), oldKey, newKey); + }); +} + +// --------------------------------------------------------------------------- +// The command +// --------------------------------------------------------------------------- + +/** The first of `paths` this process may not write, if any. */ +function firstNotWritable(paths: string[]): string | undefined { + return paths.find((p) => { try { fs.accessSync(p, fs.constants.W_OK); return false; @@ -174,15 +688,18 @@ async function main() { return true; } }); - if (notWritable) { - rl.close(); - console.error(`Cannot write to ${notWritable} as uid ${process.getuid?.() ?? '?'}.`); - console.error('Run this script as the user that owns the data directory. In the Docker image that is node:'); - console.error(' docker exec -it -u node -w /app/server trek node --import tsx scripts/migrate-encryption.ts'); - process.exit(1); - } +} - // Collect keys interactively +function refuseNotWritable(notWritable: string): never { + rl.close(); + console.error(`Cannot write to ${notWritable} as uid ${process.getuid?.() ?? '?'}.`); + console.error('Run this script as the user that owns the data directory. In the Docker image that is node:'); + console.error(' docker exec -it -u node -w /app/server trek node --import tsx scripts/migrate-encryption.ts'); + process.exit(1); +} + +/** Both keys and the confirmation, or the process ends. */ +async function askKeys(): Promise<{ oldKey: string; newKey: string }> { const oldKey = await promptSecret('Old ENCRYPTION_KEY: '); const newKey = await promptSecret('New ENCRYPTION_KEY: '); @@ -205,6 +722,160 @@ async function main() { console.log('Aborted.'); process.exit(0); } + return { oldKey, newKey }; +} + +/** + * Prints the outcome. On success the key file follows the rotation: the + * server refuses to start when ENCRYPTION_KEY and the key file disagree + * (src/config.ts). Only an existing file is touched; anything else is left + * to the operator. + */ +function finish(result: MigrationResult, backupPath: string, keyFile: string, newKey: string, noKeyFile: string): void { + rl.close(); + + console.log('\n=== Migration complete ==='); + console.log(` Migrated: ${result.migrated}`); + console.log(` Already on new key: ${result.alreadyMigrated}`); + console.log(` Skipped (empty): ${result.skipped}`); + if (result.errors.length > 0) { + console.warn(` Errors: ${result.errors.length}`); + result.errors.forEach((e) => console.warn(` - ${e}`)); + console.warn('\nSome secrets could not be migrated. Check the errors above.'); + console.warn(`Your original database is backed up at: ${backupPath}`); + process.exit(1); + } else { + console.log('\nAll secrets successfully re-encrypted.'); + if (fs.existsSync(keyFile)) { + fs.writeFileSync(keyFile, newKey, { mode: 0o600 }); + console.log(`Key file updated: ${keyFile}`); + } else { + console.log(noKeyFile); + } + console.log(`Backup retained at: ${backupPath}`); + } +} + +/** + * The Postgres database TREK_DB_URL names. Every check that can fail runs + * before the keys are asked for: the database is reached and holds TREK's + * tables, and the data directory, which takes the copy and holds the key + * file, is writable. The copy is a pg_dump custom-format file, the format of + * the copy a migration takes (wiki: Updating, Rolling back a Postgres update); + * without it nothing is changed. + */ +async function mainPostgres(): Promise { + for (const name of ['DB_PATH', 'TREK_DB_FILE']) { + if (process.env[name]?.trim()) { + rl.close(); + console.error( + `TREK_DB_URL and ${name} are both set, so it is not clear which database is meant; nothing was changed.`, + ); + process.exit(1); + } + } + + let server: ServerModules; + try { + server = loadServer(); + } catch (err) { + rl.close(); + console.error(`The server's code is needed to reach a Postgres database: ${(err as Error).message}`); + console.error('Run this inside the TREK container, or from a source checkout with its dependencies installed.'); + process.exit(1); + } + + const engine = server.engine.resolveEngine(); + const target = server.engine.postgresTarget(server.appConfig.readEnv().db); + if (!target) { + rl.close(); + console.error('TREK_DB_URL does not name a Postgres database; nothing was changed.'); + process.exit(1); + } + const paths = server.appConfig.resolveDataPaths(); + const notWritable = firstNotWritable( + fs.existsSync(paths.encryptionKeyFile) ? [paths.dataDir, paths.encryptionKeyFile] : [paths.dataDir], + ); + if (notWritable) refuseNotWritable(notWritable); + + console.log(`Database: ${engine.describe()}\n`); + + let orm: MikroORM; + let hasTables: boolean; + try { + orm = await MikroORM.init({ ...server.engine.buildOrmConfig(engine), pool: { min: 0, max: 1 } }); + hasTables = await postgresStore(orm.em.fork()).tableExists('app_settings'); + } catch (err) { + rl.close(); + console.error(`Could not reach the database TREK_DB_URL names: ${(err as Error).message}`); + console.error('Nothing was changed.'); + process.exit(1); + } + let outcome: { result: MigrationResult; backupPath: string; newKey: string }; + try { + if (!hasTables) { + rl.close(); + console.error('The database TREK_DB_URL names holds no TREK tables; nothing was changed.'); + process.exit(1); + } + + const { oldKey, newKey } = await askKeys(); + + const backupPath = path.join(paths.dataDir, `pre-key-rotation-${Date.now()}.pgdump`); + try { + await server.pgTools.pgDump(target.url, backupPath, target.binDir); + } catch (err) { + rl.close(); + console.error(`Could not copy the database before the rotation: ${(err as Error).message}`); + console.error('Nothing was changed.'); + process.exit(1); + } + console.log(`\nBackup created: ${backupPath}`); + + outcome = { result: await rotateOnPostgres(orm, engine, oldKey, newKey), backupPath, newKey }; + } finally { + await orm.close(true); + } + finish( + outcome.result, + outcome.backupPath, + paths.encryptionKeyFile, + outcome.newKey, + 'No key file in the data directory. Store the new key wherever TREK reads it from (ENCRYPTION_KEY or data/.encryption_key).', + ); +} + +async function main() { + openPrompts(); + console.log('=== TREK Encryption Key Migration ===\n'); + console.log('This script re-encrypts all stored secrets under a new ENCRYPTION_KEY.'); + console.log('A backup of the database will be created before any changes are made.\n'); + + // A Postgres install keeps its secrets in the database TREK_DB_URL names. No + // SQLite file is opened, created or backed up then: rotating one left over + // from before the move would rewrite the key file while the secrets in + // Postgres stay under the old key. + if (process.env.TREK_DB_URL?.trim()) { + await mainPostgres(); + return; + } + + // Resolve DB path + const dbPath = path.resolve(process.env.DB_PATH ?? path.join(__dirname, '../data/travel.db')); + + if (!fs.existsSync(dbPath)) { + console.error(`Database not found at: ${dbPath}`); + console.error('Set DB_PATH env var if your database is in a non-standard location.'); + process.exit(1); + } + + console.log(`Database: ${dbPath}\n`); + + // SQLite silently opens an unwritable file read-only (#2933); db.readonly can't tell. + const notWritable = firstNotWritable([dbPath, path.dirname(dbPath)]); + if (notWritable) refuseNotWritable(notWritable); + + const { oldKey, newKey } = await askKeys(); // Backup const backupPath = `${dbPath}.backup-${Date.now()}`; @@ -259,514 +930,23 @@ async function main() { db.pragma(`synchronous = ${synchronous}`); console.log(`Journal mode: ${db.pragma('journal_mode', { simple: true })}\n`); - const result: MigrationResult = { migrated: 0, alreadyMigrated: 0, skipped: 0, errors: [] }; - - // Helper: migrate a single api-key-style value (enc:v1: prefix) - function migrateApiKeyValue(raw: string, label: string): string | null { - if (!raw || !raw.startsWith(ENCRYPTED_PREFIX)) { - result.skipped++; - console.warn(` SKIP ${label}: not an encrypted value (missing enc:v1: prefix)`); - return null; - } - - const plain = decryptApiKey(raw, oldKey); - if (plain !== null) { - result.migrated++; - return encryptApiKey(plain, newKey); - } - - // Try new key — already migrated? - const check = decryptApiKey(raw, newKey); - if (check !== null) { - result.alreadyMigrated++; - return null; // no change needed - } - - result.errors.push(`${label}: decryption failed with both keys`); - console.error(` ERROR ${label}: could not decrypt with either key — skipping`); - return null; - } - - // Helper: migrate a single MFA value (no prefix, raw base64) - function migrateMfaValue(raw: string, label: string): string | null { - if (!raw) { - result.skipped++; - return null; - } - - const plain = decryptMfa(raw, oldKey); - if (plain !== null) { - result.migrated++; - return encryptMfa(plain, newKey); - } - - const check = decryptMfa(raw, newKey); - if (check !== null) { - result.alreadyMigrated++; - return null; - } - - result.errors.push(`${label}: decryption failed with both keys`); - console.error(` ERROR ${label}: could not decrypt with either key — skipping`); - return null; - } - - db.transaction(() => { - // --- app_settings: oidc_client_secret, smtp_pass, admin_webhook_url, admin_ntfy_token, - // plus the instance-wide provider keys (#1939) --- - // - // The last two mirror INSTANCE_API_KEY_NAMES in - // src/nest/settings/instance-api-keys.ts. Spelled out rather than imported, - // because that module pulls in DatabaseService and this script deliberately - // stays independent of src/ (see the crypto note above). Miss one and a - // rotation leaves it encrypted under the old key, which reads back as "no - // key" and silently drops the install to OpenStreetMap, so the copy is - // pinned by tests/unit/db/migrate-encryption-parity.test.ts. - // - // web_push_vapid_private_key is the Web Push signing key VapidKeysService - // generates. Missed here it would no longer decrypt under the new key, and - // push would stay off on every device until the old key came back. - // - // service_key is the installation's key towards the liketrek services. - // Missed here it reads back as unusable, the installation registers anew and - // loses its id, and a revoked key would come back as a fresh one. - // - // The mail import's IMAP password and webhook secret: missed, the box stops - // being read and every signed mail is refused until an admin types them again. - for (const key of [ - 'oidc_client_secret', - 'smtp_pass', - 'admin_webhook_url', - 'admin_ntfy_token', - 'maps_api_key', - 'unsplash_api_key', - 'amap_api_key', - 'web_push_vapid_private_key', - 'service_key', - 'mail_ingest_imap_pass', - 'mail_ingest_webhook_secret', - ]) { - const row = db.prepare('SELECT value FROM app_settings WHERE key = ?').get(key) as { value: string } | undefined; - if (!row?.value) continue; - const newVal = migrateApiKeyValue(row.value, `app_settings.${key}`); - if (newVal !== null) { - db.prepare('UPDATE app_settings SET value = ? WHERE key = ?').run(newVal, key); - } - } - - // --- users: api key columns + synology credentials --- - // unsplash_api_key was missing here for as long as the column has existed. It - // matters now that the resolver reads it as the per-user fallback (#1939): left - // out of a rotation it stays encrypted under the old key and reads back as unset. - // airtrail_api_key arrived with a later migration, so an older database may - // not have the column at all — filter the list against the real table rather - // than letting one missing column throw the whole rotation. - const userColumns = new Set( - (db.prepare("SELECT name FROM pragma_table_info('users')").all() as { name: string }[]).map((r) => r.name), - ); - const apiKeyColumns = [ - 'maps_api_key', - 'unsplash_api_key', - // Added with the Amap provider; filtered against the real table below, so a - // database that has not run that migration yet is not a rotation failure. - 'amap_api_key', - 'openweather_api_key', - 'immich_api_key', - 'synology_password', - 'synology_sid', - 'synology_did', - 'airtrail_api_key', - ].filter((c) => userColumns.has(c)); - const users = db.prepare('SELECT id FROM users').all() as { id: number }[]; - - for (const user of users) { - const row = db.prepare(`SELECT ${apiKeyColumns.join(', ')} FROM users WHERE id = ?`).get(user.id) as Record< - string, - string - >; - - for (const col of apiKeyColumns) { - if (!row[col]) continue; - const newVal = migrateApiKeyValue(row[col], `users[${user.id}].${col}`); - if (newVal !== null) { - db.prepare(`UPDATE users SET ${col} = ? WHERE id = ?`).run(newVal, user.id); - } - } - - // mfa_secret (mfa crypto) - const mfaRow = db.prepare('SELECT mfa_secret FROM users WHERE id = ? AND mfa_secret IS NOT NULL').get(user.id) as - { mfa_secret: string } | undefined; - if (mfaRow?.mfa_secret) { - const newVal = migrateMfaValue(mfaRow.mfa_secret, `users[${user.id}].mfa_secret`); - if (newVal !== null) { - db.prepare('UPDATE users SET mfa_secret = ? WHERE id = ?').run(newVal, user.id); - } - } - } - - // --- app_settings: storage.backends --- - // Storage backend secrets live encrypted INSIDE the storage.backends JSON - // array (BACKENDS_KEY in src/nest/storage/storage-registry.service.ts is - // its own app_settings row — 'storage.categories' is a sibling row with no - // secrets), on the fields the shared type registry marks `secret` (only - // options.secretAccessKey for s3 today; src/nest/storage/storage-secrets.ts - // encrypts it with the same enc:v1: scheme as the api-key columns above). - // This script can't import storageSecretFields from @trek/shared (kept - // independent of the app, see the header note), so the field list per - // backend type is pinned here. Miss one and that backend's secret stays - // encrypted under the old key after rotation, then fails to decrypt on the - // next read (StorageBackendError) — pinned by - // tests/unit/db/migrate-encryption-parity.test.ts. - const STORAGE_BACKEND_SECRET_FIELDS: Record = { - s3: ['secretAccessKey'], - }; - { - const row = db.prepare("SELECT value FROM app_settings WHERE key = 'storage.backends'").get() as - { value: string } | undefined; - if (row?.value) { - let backends: unknown; - try { - backends = JSON.parse(row.value); - } catch { - result.errors.push('app_settings.storage.backends: invalid JSON — skipping'); - backends = undefined; - } - if (backends !== undefined) { - if (Array.isArray(backends)) { - let changed = false; - for (const backend of backends as Record[]) { - if (!backend || typeof backend !== 'object') continue; - const type = typeof backend.type === 'string' ? backend.type : undefined; - const fields = type ? STORAGE_BACKEND_SECRET_FIELDS[type] : undefined; - if (!fields || fields.length === 0) continue; - const options = backend.options; - if (!options || typeof options !== 'object') continue; - const name = typeof backend.name === 'string' ? backend.name : '?'; - for (const field of fields) { - const raw = (options as Record)[field]; - if (typeof raw !== 'string' || !raw) continue; - const newVal = migrateApiKeyValue(raw, `app_settings.storage.backends[${name}].${field}`); - if (newVal !== null) { - (options as Record)[field] = newVal; - changed = true; - } - } - } - if (changed) { - db.prepare("UPDATE app_settings SET value = ? WHERE key = 'storage.backends'").run( - JSON.stringify(backends), - ); - } - } else { - result.errors.push('app_settings.storage.backends: not an array — skipping'); - } - } - } - } - - // --- settings: per-user encrypted keys --- - const encryptedSettingKeys = ['webhook_url', 'ntfy_token', 'mapbox_access_token', 'carto_api_key', 'llm_api_key']; - const settingRows = db - .prepare( - `SELECT user_id, key, value FROM settings WHERE key IN (${encryptedSettingKeys.map(() => '?').join(', ')})`, - ) - .all(...encryptedSettingKeys) as { user_id: number; key: string; value: string }[]; - - for (const row of settingRows) { - if (!row.value) continue; - const newVal = migrateApiKeyValue(row.value, `settings[user=${row.user_id}].${row.key}`); - if (newVal !== null) { - db.prepare('UPDATE settings SET value = ? WHERE user_id = ? AND key = ?').run(newVal, row.user_id, row.key); - } - } - - // --- plugins: OAuth tokens and secret settings fields --- - // The plugin host encrypts with the same enc:v1: scheme as everything above: - // plugin-oauth.service stores both tokens encrypted, and plugins.service - // encrypts every settings field the manifest marked secret (instance scope in - // plugins.config, user scope in plugin_user_config.config). Left out of a - // rotation, a connected plugin reads back as "not connected" and a secret - // setting as unset, with nothing in the log to say why. Every table here - // arrived with a later migration, so each block checks that it exists. - const tableExists = (name: string): boolean => - !!db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?").get(name); - - if (tableExists('plugin_oauth_tokens')) { - const tokenRows = db - .prepare('SELECT plugin_id, user_id, access_token, refresh_token FROM plugin_oauth_tokens') - .all() as { plugin_id: string; user_id: number; access_token: string | null; refresh_token: string | null }[]; - for (const row of tokenRows) { - for (const col of ['access_token', 'refresh_token'] as const) { - const raw = row[col]; - if (!raw) continue; - const newVal = migrateApiKeyValue(raw, `plugin_oauth_tokens[${row.plugin_id},${row.user_id}].${col}`); - if (newVal !== null) { - db.prepare(`UPDATE plugin_oauth_tokens SET ${col} = ? WHERE plugin_id = ? AND user_id = ?`).run( - newVal, - row.plugin_id, - row.user_id, - ); - } - } - } - } - - if (tableExists('plugin_settings_fields')) { - // Which keys are secret is per plugin and per scope, and it lives in the DB - // rather than in this script, so nothing has to be pinned by hand here. - const secretFields = db - .prepare('SELECT plugin_id, field_key, scope FROM plugin_settings_fields WHERE secret = 1') - .all() as { plugin_id: string; field_key: string; scope: string | null }[]; - const secretKeysFor = (pluginId: string, scope: string): string[] => - secretFields - .filter((f) => f.plugin_id === pluginId && (f.scope ?? 'instance') === scope) - .map((f) => f.field_key); - - const migrateConfigJson = (raw: string | null, keys: string[], label: string): string | null => { - if (!raw || keys.length === 0) return null; - let parsed: unknown; - try { - parsed = JSON.parse(raw); - } catch { - result.errors.push(`${label}: invalid JSON — skipping`); - return null; - } - if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { - result.errors.push(`${label}: not an object — skipping`); - return null; - } - const config = parsed as Record; - let changed = false; - for (const key of keys) { - const value = config[key]; - if (typeof value !== 'string' || !value) continue; - const newVal = migrateApiKeyValue(value, `${label}.${key}`); - if (newVal !== null) { - config[key] = newVal; - changed = true; - } - } - return changed ? JSON.stringify(config) : null; - }; - - if (tableExists('plugins')) { - const pluginRows = db.prepare('SELECT id, config FROM plugins').all() as { - id: string; - config: string | null; - }[]; - for (const row of pluginRows) { - const next = migrateConfigJson(row.config, secretKeysFor(row.id, 'instance'), `plugins[${row.id}].config`); - if (next !== null) { - db.prepare('UPDATE plugins SET config = ? WHERE id = ?').run(next, row.id); - } - } - } - - if (tableExists('plugin_user_config')) { - const userConfigRows = db.prepare('SELECT plugin_id, user_id, config FROM plugin_user_config').all() as { - plugin_id: string; - user_id: number; - config: string | null; - }[]; - for (const row of userConfigRows) { - const next = migrateConfigJson( - row.config, - secretKeysFor(row.plugin_id, 'user'), - `plugin_user_config[${row.plugin_id},${row.user_id}].config`, - ); - if (next !== null) { - db.prepare('UPDATE plugin_user_config SET config = ? WHERE plugin_id = ? AND user_id = ?').run( - next, - row.plugin_id, - row.user_id, - ); - } - } - } - } - - // --- mail_ingest: forwarding address tokens and the stored mails --- - // A person sees their address again through its token_enc; a forwarded mail - // waits for its reading, and for the inbox's ways out, as raw_eml_enc. Missed, - // the addresses still take mail (they are found by hash) but show as unset, - // and every stored mail reads as unreadable. - for (const [table, column] of [ - ['mail_ingest_addresses', 'token_enc'], - ['mail_ingest_messages', 'raw_eml_enc'], - ] as const) { - if (!tableExists(table)) continue; - const rows = db.prepare(`SELECT id, ${column} AS value FROM ${table} WHERE ${column} IS NOT NULL`).all() as { - id: number; - value: string; - }[]; - for (const row of rows) { - const newVal = migrateApiKeyValue(row.value, `${table}[${row.id}].${column}`); - if (newVal !== null) { - db.prepare(`UPDATE ${table} SET ${column} = ? WHERE id = ?`).run(newVal, row.id); - } - } - } - - // --- addons: the AI Parsing addon's instance-wide API key --- - // Stored encrypted as `apiKey` inside the addon's JSON config - // (llm-parse/llm-config.ts). Left out, LLM parsing quietly runs without a - // key after the rotation, since a failed decrypt reads as "no key". - if (tableExists('addons')) { - const addonRows = db.prepare('SELECT id, config FROM addons WHERE config IS NOT NULL').all() as { - id: string; - config: string; - }[]; - for (const row of addonRows) { - let config: unknown; - try { - config = JSON.parse(row.config); - } catch { - continue; - } - if (!config || typeof config !== 'object' || Array.isArray(config)) continue; - const apiKey = (config as Record).apiKey; - if (typeof apiKey !== 'string' || !apiKey) continue; - const newVal = migrateApiKeyValue(apiKey, `addons[${row.id}].config.apiKey`); - if (newVal !== null) { - db.prepare('UPDATE addons SET config = ? WHERE id = ?').run( - JSON.stringify({ ...(config as Record), apiKey: newVal }), - row.id, - ); - } - } - } - - // --- trip_album_links: passphrase --- - const albumLinks = db.prepare('SELECT id, passphrase FROM trip_album_links WHERE passphrase IS NOT NULL').all() as { - id: number; - passphrase: string; - }[]; - for (const row of albumLinks) { - const newVal = migrateApiKeyValue(row.passphrase, `trip_album_links[${row.id}].passphrase`); - if (newVal !== null) { - db.prepare('UPDATE trip_album_links SET passphrase = ? WHERE id = ?').run(newVal, row.id); - } - } - - // --- dawarich_connections: api_key --- - // Its own table rather than a users column, so it needs its own block. The - // table arrived with a later migration, hence the existence check: an older - // database simply has nothing to rotate here. - if (tableExists('dawarich_connections')) { - const connections = db - .prepare('SELECT user_id, api_key FROM dawarich_connections WHERE api_key IS NOT NULL') - .all() as { user_id: number; api_key: string }[]; - for (const row of connections) { - const newVal = migrateApiKeyValue(row.api_key, `dawarich_connections[${row.user_id}].api_key`); - if (newVal !== null) { - db.prepare('UPDATE dawarich_connections SET api_key = ? WHERE user_id = ?').run(newVal, row.user_id); - } - } - } - - // --- trek_photos: passphrase --- - const photos = db.prepare('SELECT id, passphrase FROM trek_photos WHERE passphrase IS NOT NULL').all() as { - id: number; - passphrase: string; - }[]; - for (const row of photos) { - const newVal = migrateApiKeyValue(row.passphrase, `trek_photos[${row.id}].passphrase`); - if (newVal !== null) { - db.prepare('UPDATE trek_photos SET passphrase = ? WHERE id = ?').run(newVal, row.id); - } - } - - // --- document_connections: secrets --- - // Every credential of a document connection sits in ONE encrypted JSON blob - // (src/nest/doc-sync/doc-sync-secrets.ts) rather than in a column per - // provider, so a new provider changes nothing here: the blob is rotated as - // an opaque value. It also holds secrets the provider handed out itself, - // such as the DSM device token, which no form can re-enter. Left out of a - // rotation, every trip binding on the instance reads back as unauthorized - // and Synology needs a fresh OTP pairing. The table arrived with a later - // migration, hence the existence check. - if (tableExists('document_connections')) { - const documentConnections = db - .prepare('SELECT id, secrets FROM document_connections WHERE secrets IS NOT NULL') - .all() as { id: number; secrets: string }[]; - for (const row of documentConnections) { - const newVal = migrateApiKeyValue(row.secrets, `document_connections[${row.id}].secrets`); - if (newVal !== null) { - db.prepare('UPDATE document_connections SET secrets = ? WHERE id = ?').run(newVal, row.id); - } - } - } - - // --- ai_connections: secrets --- - // The AI assistant's connections keep their credential in ONE encrypted - // JSON blob (src/nest/ai-connections/ai-secrets.ts): an API key, or a - // ChatGPT sign-in's tokens. Left out of a rotation, the instance's - // connection asks the admin for its key again and every ChatGPT user has - // to sign in anew. The table arrived with a later migration, hence the - // existence check. - if (tableExists('ai_connections')) { - const aiConnections = db.prepare('SELECT id, secrets FROM ai_connections WHERE secrets IS NOT NULL').all() as { - id: number; - secrets: string; - }[]; - for (const row of aiConnections) { - const newVal = migrateApiKeyValue(row.secrets, `ai_connections[${row.id}].secrets`); - if (newVal !== null) { - db.prepare('UPDATE ai_connections SET secrets = ? WHERE id = ?').run(newVal, row.id); - } - } - } - - // --- trip_document_links: webhook_secret --- - // The secret a provider signs its webhook calls with, stored through the - // same blob helper. It is written once when the link is created and never - // regenerated, so a rotation that misses it leaves the link with nothing to - // check a signature against. - if (tableExists('trip_document_links')) { - const documentLinks = db - .prepare('SELECT id, webhook_secret FROM trip_document_links WHERE webhook_secret IS NOT NULL') - .all() as { id: number; webhook_secret: string }[]; - for (const row of documentLinks) { - const newVal = migrateApiKeyValue(row.webhook_secret, `trip_document_links[${row.id}].webhook_secret`); - if (newVal !== null) { - db.prepare('UPDATE trip_document_links SET webhook_secret = ? WHERE id = ?').run(newVal, row.id); - } - } - } - })(); - + const result = await rotateOnSqlite(db, oldKey, newKey); db.close(); - rl.close(); - console.log('\n=== Migration complete ==='); - console.log(` Migrated: ${result.migrated}`); - console.log(` Already on new key: ${result.alreadyMigrated}`); - console.log(` Skipped (empty): ${result.skipped}`); - if (result.errors.length > 0) { - console.warn(` Errors: ${result.errors.length}`); - result.errors.forEach((e) => console.warn(` - ${e}`)); - console.warn('\nSome secrets could not be migrated. Check the errors above.'); - console.warn(`Your original database is backed up at: ${backupPath}`); - process.exit(1); - } else { - console.log('\nAll secrets successfully re-encrypted.'); - // The server refuses to start when ENCRYPTION_KEY and the key file disagree - // (src/config.ts), so the file follows the rotation. Only an existing file - // next to the database is touched; anything else is left to the operator. - const keyFile = path.join(path.dirname(dbPath), '.encryption_key'); - if (fs.existsSync(keyFile)) { - fs.writeFileSync(keyFile, newKey, { mode: 0o600 }); - console.log(`Key file updated: ${keyFile}`); - } else { - console.log( - 'No key file next to the database. Store the new key wherever TREK reads it from (ENCRYPTION_KEY or data/.encryption_key).', - ); - } - console.log(`Backup retained at: ${backupPath}`); - } + // The key file next to the database. + finish( + result, + backupPath, + path.join(path.dirname(dbPath), '.encryption_key'), + newKey, + 'No key file next to the database. Store the new key wherever TREK reads it from (ENCRYPTION_KEY or data/.encryption_key).', + ); } -main().catch((err) => { - console.error('Unexpected error:', err); - process.exit(1); -}); +// Run as a command only; the Postgres-lane test imports the rotation itself. +if (require.main === module) { + main().catch((err) => { + console.error('Unexpected error:', err); + process.exit(1); + }); +} diff --git a/server/tests/unit/production-dependencies.test.ts b/server/tests/unit/production-dependencies.test.ts index 3e7d90b6d..28f441a92 100644 --- a/server/tests/unit/production-dependencies.test.ts +++ b/server/tests/unit/production-dependencies.test.ts @@ -49,7 +49,11 @@ function* walk(dir: string): Generator { } describe('production dependencies', () => { - const files = [...walk(path.join(serverRoot, 'src')), path.join(serverRoot, 'scripts', 'migrate-encryption.ts')]; + const files = [ + ...walk(path.join(serverRoot, 'src')), + path.join(serverRoot, 'scripts', 'migrate-encryption.ts'), + path.join(serverRoot, 'scripts', 'migrate-encryption-stores.ts'), + ]; const usedBy = new Map(); for (const file of files) { for (const pkg of externalImports(file)) { diff --git a/server/tsconfig.scripts.json b/server/tsconfig.scripts.json index 8538143bf..e7d530f81 100644 --- a/server/tsconfig.scripts.json +++ b/server/tsconfig.scripts.json @@ -4,5 +4,5 @@ "rootDir": ".", "noEmit": true }, - "include": ["scripts/generate-entities.ts", "scripts/generate-db-types.ts", "scripts/generate-pg-baseline.ts", "scripts/generate-pg-migration.ts", "scripts/migrate-encryption.ts", "scripts/pg-probe.ts", "scripts/pg-probe/*.ts"] + "include": ["scripts/generate-entities.ts", "scripts/generate-db-types.ts", "scripts/generate-pg-baseline.ts", "scripts/generate-pg-migration.ts", "scripts/migrate-encryption.ts", "scripts/migrate-encryption-stores.ts", "scripts/pg-probe.ts", "scripts/pg-probe/*.ts"] }