From 60885627b1373aed05702b66498adbc0de943a20 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 14:59:21 +0100 Subject: [PATCH] fix(scripts): rotate the wanderer token with the encryption key The key rotation now re-encrypts wanderer_connections.token, so a wanderer connection keeps working after the key changes instead of calling out without its token. --- server/scripts/migrate-encryption.ts | 15 +++++++++ .../unit/db/migrate-encryption-parity.test.ts | 32 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/server/scripts/migrate-encryption.ts b/server/scripts/migrate-encryption.ts index 4f9be158d..3e83c717b 100644 --- a/server/scripts/migrate-encryption.ts +++ b/server/scripts/migrate-encryption.ts @@ -562,6 +562,21 @@ async function rotateSecrets(store: SecretStore, oldKey: string, newKey: string) } } + // --- wanderer_connections: token --- + // One row per user, like dawarich_connections. Left out of a rotation, the + // connection still shows as connected but every call goes out without a token. + if (await store.tableExists('wanderer_connections')) { + const connections = await store.all<{ user_id: number; token: string }>( + 'SELECT user_id, token FROM wanderer_connections WHERE token IS NOT NULL', + ); + for (const row of connections) { + const newVal = migrateApiKeyValue(row.token, `wanderer_connections[${row.user_id}].token`); + if (newVal !== null) { + await store.run('UPDATE wanderer_connections SET token = ? 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', diff --git a/server/tests/unit/db/migrate-encryption-parity.test.ts b/server/tests/unit/db/migrate-encryption-parity.test.ts index d0c5a8210..af8f7b4de 100644 --- a/server/tests/unit/db/migrate-encryption-parity.test.ts +++ b/server/tests/unit/db/migrate-encryption-parity.test.ts @@ -431,6 +431,38 @@ describe('migrate-encryption.ts rotates the stores the app writes', () => { expect(output).toContain('All secrets successfully re-encrypted.'); }, 30000); + // wanderer.service.ts stores the token with maybe_encrypt_api_key; missed by a + // rotation, the connection still reads as connected but calls go out without it. + it('ROTPAR-017: re-encrypts the wanderer token', () => { + const wanderer = fs.readFileSync(path.join(SERVER_ROOT, 'src', 'nest', 'wanderer', 'wanderer.service.ts'), 'utf8'); + expect(wanderer).toContain('maybe_encrypt_api_key(typed)'); + const seed = new Database(dbPath); + seed.exec(BASE_SCHEMA); + seed.exec('CREATE TABLE wanderer_connections (user_id INTEGER PRIMARY KEY, url TEXT NOT NULL, token TEXT)'); + seed + .prepare('INSERT INTO wanderer_connections (user_id, url, token) VALUES (1, ?, ?)') + .run('https://wanderer.example', encryptWith('old-key', 'wanderer-token')); + seed.prepare("INSERT INTO wanderer_connections (user_id, url, token) VALUES (2, 'https://w2.example', NULL)").run(); + seed.close(); + + execFileSync(process.execPath, ['--import', 'tsx', 'scripts/migrate-encryption.ts'], { + cwd: SERVER_ROOT, + env: { ...process.env, DB_PATH: dbPath }, + input: ['old-key', 'new-key', 'yes', ''].join(String.fromCharCode(10)), + stdio: 'pipe', + encoding: 'utf8', + }); + + const after = new Database(dbPath, { readonly: true }); + const rows = after.prepare('SELECT user_id, token FROM wanderer_connections ORDER BY user_id').all() as Array<{ + user_id: number; + token: string | null; + }>; + after.close(); + expect(decryptWith('new-key', rows[0].token!)).toBe('wanderer-token'); + expect(rows[1].token).toBeNull(); + }, 30000); + it('ROTPAR-013: re-encrypts the AI Parsing addon key and moves the key file to the new key', () => { const seed = new Database(dbPath); seed.exec(BASE_SCHEMA);