From 52a6fbbc92ae09e5a7de7b5c5bf45fa31df74883 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 15:02:04 +0100 Subject: [PATCH] fix(scripts): read the server's .env in the admin reset and the key rotation Both scripts now load server/.env as the server does, so an install whose TREK_DB_URL lives there resets the admin and rotates the key in its Postgres database instead of in a SQLite file left behind by the move. --- server/reset-admin.js | 3 ++ server/scripts/migrate-encryption.ts | 3 ++ .../db/maintenance-scripts-postgres.test.ts | 42 +++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/server/reset-admin.js b/server/reset-admin.js index e90204729..9c30400fe 100644 --- a/server/reset-admin.js +++ b/server/reset-admin.js @@ -21,6 +21,9 @@ const path = require('path'); const crypto = require('crypto'); const bcrypt = require('bcryptjs'); +// The server reads its .env too (src/index.ts), so TREK_DB_URL kept there picks the same database here. +require('dotenv').config({ path: path.join(__dirname, '.env'), quiet: true }); + // Kept in sync with the seeder/authService cost factor. const BCRYPT_COST = 12; diff --git a/server/scripts/migrate-encryption.ts b/server/scripts/migrate-encryption.ts index 019650956..a206f14ca 100644 --- a/server/scripts/migrate-encryption.ts +++ b/server/scripts/migrate-encryption.ts @@ -32,6 +32,7 @@ import { MikroORM } from '@mikro-orm/core'; import Database from 'better-sqlite3'; import crypto from 'crypto'; +import { config as loadDotenv } from 'dotenv'; import fs from 'fs'; import path from 'path'; import readline from 'readline'; @@ -861,6 +862,8 @@ async function mainPostgres(): Promise { } async function main() { + // The server reads its .env too (index.ts), so TREK_DB_URL kept there picks the same database here. + loadDotenv({ path: path.join(__dirname, '../.env'), quiet: true }); openPrompts(); console.log('=== TREK Encryption Key Migration ===\n'); console.log('This script re-encrypts all stored secrets under a new ENCRYPTION_KEY.'); diff --git a/server/tests/unit/db/maintenance-scripts-postgres.test.ts b/server/tests/unit/db/maintenance-scripts-postgres.test.ts index 93566327b..719070a89 100644 --- a/server/tests/unit/db/maintenance-scripts-postgres.test.ts +++ b/server/tests/unit/db/maintenance-scripts-postgres.test.ts @@ -82,3 +82,45 @@ describe('maintenance scripts with TREK_DB_URL', () => { expect(fs.readdirSync(tmpDir)).toEqual([]); }, 60000); }); + +describe('maintenance scripts with TREK_DB_URL in the .env beside them', () => { + /** The server's own dependencies, for a copy of a script outside server/. */ + const NODE_PATH = [path.join(SERVER_ROOT, 'node_modules'), path.join(SERVER_ROOT, '..', 'node_modules')].join( + path.delimiter, + ); + + /** Copies `files` (relative to server/) into the temp dir, with a .env naming the Postgres database. */ + function install(files: string[]): void { + for (const file of files) { + fs.mkdirSync(path.dirname(path.join(tmpDir, file)), { recursive: true }); + fs.copyFileSync(path.join(SERVER_ROOT, file), path.join(tmpDir, file)); + } + fs.writeFileSync(path.join(tmpDir, '.env'), `TREK_DB_URL=${PG_URL}\n`); + } + + it('PGSCRIPTS-004: reset-admin.js reads TREK_DB_URL from its .env, as the server does', () => { + install(['reset-admin.js']); + const dbPath = path.join(tmpDir, 'travel.db'); + const r = run([path.join(tmpDir, 'reset-admin.js')], { + NODE_PATH, + TREK_DB_FILE: dbPath, + RESET_ADMIN_PASSWORD: 'x', + }); + expect(r.status).toBe(1); + expect(r.stderr).toContain('TREK_DB_URL and TREK_DB_FILE are both set'); + expect(fs.existsSync(dbPath)).toBe(false); + }, 60000); + + it('PGSCRIPTS-005: migrate-encryption.ts reads TREK_DB_URL from the .env, as the server does', () => { + install(['scripts/migrate-encryption.ts', 'scripts/migrate-encryption-stores.ts']); + const dbPath = path.join(tmpDir, 'travel.db'); + const r = run( + ['--import', 'tsx', path.join(tmpDir, 'scripts', 'migrate-encryption.ts')], + { NODE_PATH, DB_PATH: dbPath }, + 'old-key\nnew-key\nyes\n', + ); + expect(r.status).toBe(1); + expect(r.stderr).toContain('TREK_DB_URL and DB_PATH are both set'); + expect(fs.existsSync(dbPath)).toBe(false); + }, 60000); +});