The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
fix(auth): mark an address an admin-made account changed itself, so SSO no longer links to it
Maurice8fc9fe7945b7
diff --git a/server/src/db/migrations/Migration20200101046480_an_address_changed_after_an_admin_made_the_account.ts b/server/src/db/migrations/Migration20200101046480_an_address_changed_after_an_admin_made_the_account.tsnew file mode 100644index 000000000..176773fed--- /dev/null+++ b/server/src/db/migrations/Migration20200101046480_an_address_changed_after_an_admin_made_the_account.ts@@ -0,0 +1,94 @@+import { Migration } from '@mikro-orm/migrations';++interface AuditRow {+ action: string;+ user_id: number | string | null;+ resource: string | null;+ details: string | null;+}++interface UserEmailRow {+ id: number | string;+ email: string;+}++interface AuditDetails {+ email?: unknown;+ fields?: unknown;+}++function readDetails(details: string | null): AuditDetails {+ if (!details) return {};+ try {+ const value: unknown = JSON.parse(details);+ return value && typeof value === 'object' ? (value as AuditDetails) : {};+ } catch {+ return {};+ }+}++/**+ * Marks the address of an account an admin created as the account's own once+ * it moved away from the one the admin entered.+ *+ * `Migration20200101042800_an_email_the_account_typed_in_itself` set+ * `email_self_asserted` for an account whose address moved away from the one+ * it registered with, comparing against its `user.register` audit row. An+ * account made in Admin > Users has no such row, only the admin's+ * `admin.user_create` (the new account's id in `resource`, the address in its+ * details), so it kept 0 even when the account itself had changed its address+ * in its settings before the upgrade. An SSO sign-in that asserts that address+ * then still linked into the account, which is what the flag exists to stop.+ *+ * This applies the same rule with the address the admin entered as the+ * baseline: an account an admin created, whose address is none of the ones it+ * was created or registered with, and whose email no admin ever edited (an+ * `admin.user_update` row naming `email`), only changed it itself. Since 5.0.0+ * a change in the settings sets the flag directly and an admin's change clears+ * it with such a row, so an account the rule marks now is one the first+ * migration missed. Only accounts still at 0 are touched, and nothing is ever+ * cleared.+ *+ * An account with no creation row at all (made before the audit log existed,+ * or whose rows were pruned) stays as it is: nothing tells what its address was.+ */+export class Migration20200101046480_an_address_changed_after_an_admin_made_the_account extends Migration {+ override name = 'Migration20200101046480_an_address_changed_after_an_admin_made_the_account';++ override async up(): Promise<void> {+ const rows = (await this.execute(+ `SELECT action, user_id, resource, details FROM audit_log+ WHERE action IN ('user.register', 'admin.user_create', 'admin.user_update')`,+ )) as AuditRow[];++ const createdByAdmin = new Set<string>();+ const editedByAdmin = new Set<string>();+ const baselines = new Map<string, string[]>();+ for (const row of rows) {+ const details = readDetails(row.details);+ if (row.action === 'admin.user_update') {+ if (row.resource && Array.isArray(details.fields) && details.fields.includes('email')) {+ editedByAdmin.add(row.resource);+ }+ continue;+ }+ // A registration names the account in user_id, an admin's creation in resource.+ const owner = row.action === 'user.register' ? row.user_id : row.resource;+ if (owner === null || typeof details.email !== 'string') continue;+ const id = String(owner);+ if (row.action === 'admin.user_create') createdByAdmin.add(id);+ baselines.set(id, [...(baselines.get(id) ?? []), details.email.trim().toLowerCase()]);+ }+ if (createdByAdmin.size === 0) return;++ const users = (await this.execute(+ `SELECT id, email FROM users WHERE COALESCE(is_guest, 0) = 0 AND email_self_asserted = 0`,+ )) as UserEmailRow[];+ for (const user of users) {+ const id = String(user.id);+ if (!createdByAdmin.has(id) || editedByAdmin.has(id)) continue;+ if (baselines.get(id)?.includes(user.email.trim().toLowerCase())) continue;+ await this.execute('UPDATE users SET email_self_asserted = 1 WHERE id = ?', [user.id]);+ }+ }+}diff --git a/server/tests/unit/db/email-self-asserted-migration.test.ts b/server/tests/unit/db/email-self-asserted-migration.test.tsindex 84cfbd532..983524de8 100644--- a/server/tests/unit/db/email-self-asserted-migration.test.ts+++ b/server/tests/unit/db/email-self-asserted-migration.test.ts@@ -3,8 +3,13 @@* existing account starts out trusted (0), except one whose address provably* moved away from the one it registered with while no admin ever edited its* email. A broken audit row is skipped, and a second run changes nothing.+ *+ * `Migration20200101046480_an_address_changed_after_an_admin_made_the_account`+ * applies the same rule to an account an admin created, whose baseline is the+ * address in the `admin.user_create` row instead of a `user.register` one.*/import { Migration20200101042800_an_email_the_account_typed_in_itself } from '../../../src/db/migrations/Migration20200101042800_an_email_the_account_typed_in_itself';+import { Migration20200101046480_an_address_changed_after_an_admin_made_the_account } from '../../../src/db/migrations/Migration20200101046480_an_address_changed_after_an_admin_made_the_account';import {createMigrationOrm,migrateTo,@@ -18,11 +23,12 @@ import type { MikroORM } from '@mikro-orm/sqlite';import { describe, expect, it } from 'vitest';const TARGET = 'Migration20200101042800_an_email_the_account_typed_in_itself';+const ADMIN_MADE = 'Migration20200101046480_an_address_changed_after_an_admin_made_the_account';-async function ormBeforeTarget(): Promise<MikroORM> {+async function ormBeforeTarget(target = TARGET): Promise<MikroORM> {const orm = await createMigrationOrm();const names = await pendingNames(orm);- const idx = names.indexOf(TARGET);+ const idx = names.indexOf(target);expect(idx).toBeGreaterThan(0);await migrateTo(orm, names[idx - 1]);return orm;@@ -92,3 +98,98 @@ describe('email self-asserted migration', () => {}}, 30000);});++async function seedAdminMade(orm: MikroORM): Promise<void> {+ // 9 is the admin. 1 was made by the admin and kept the address; 2 was made+ // and changed it itself (the colleague's address); 3 was made, changed, and+ // the admin set an email later; 4 was made and changed only the case; 5 has a+ // broken creation row; 6 registered and is already marked; 7 registered, kept+ // its address and reuses the id of an account the admin once made; 8 has no+ // audit row at all; 10 is a guest.+ await rawExec(+ orm,+ `INSERT INTO users (id, username, email, password_hash, email_self_asserted, is_guest) VALUES+ (1, 'kept', 'kept@test', 'x', 0, 0),+ (2, 'moved', 'colleague@test', 'x', 0, 0),+ (3, 'fixed', 'fixed-by-admin@test', 'x', 0, 0),+ (4, 'cased', 'Cased@Test', 'x', 0, 0),+ (5, 'broken', 'broken-now@test', 'x', 0, 0),+ (6, 'marked', 'marked-now@test', 'x', 1, 0),+ (7, 'reused', 'reused@test', 'x', 0, 0),+ (8, 'seeded', 'seeded-now@test', 'x', 0, 0),+ (9, 'admin', 'admin@test', 'x', 0, 0),+ (10, 'guest', 'guest-now@test', '', 0, 1)`,+ );+ await rawExec(+ orm,+ `INSERT INTO audit_log (user_id, action, resource, details) VALUES+ (9, 'admin.user_create', '1', '{"username":"kept","email":"kept@test","role":"user"}'),+ (9, 'admin.user_create', '2', '{"username":"moved","email":"moved@test","role":"user"}'),+ (9, 'admin.user_create', '3', '{"username":"fixed","email":"fixed@test","role":"user"}'),+ (9, 'admin.user_update', '3', '{"targetUser":"fixed-self@test","fields":["email"]}'),+ (9, 'admin.user_update', '2', '{"targetUser":"colleague@test","fields":["role"]}'),+ (9, 'admin.user_create', '4', '{"username":"cased","email":"cased@test","role":"user"}'),+ (9, 'admin.user_create', '5', 'not json'),+ (6, 'user.register', NULL, '{"username":"marked","email":"marked@test","role":"user"}'),+ (9, 'admin.user_create', '7', '{"username":"gone","email":"gone@test","role":"user"}'),+ (7, 'user.register', NULL, '{"username":"reused","email":"reused@test","role":"user"}'),+ (9, 'admin.user_create', '10', '{"username":"guest","email":"guest@test","role":"user"}')`,+ );+}++const ADMIN_MADE_EXPECTED = [+ { id: 1, email_self_asserted: 0 },+ { id: 2, email_self_asserted: 1 },+ { id: 3, email_self_asserted: 0 },+ { id: 4, email_self_asserted: 0 },+ { id: 5, email_self_asserted: 0 },+ { id: 6, email_self_asserted: 1 },+ { id: 7, email_self_asserted: 0 },+ { id: 8, email_self_asserted: 0 },+ { id: 9, email_self_asserted: 0 },+ { id: 10, email_self_asserted: 0 },+];++describe('admin-made account email migration', () => {+ it('EMAILSELF-MIG-003: an address an admin-made account changed itself is marked', async () => {+ const orm = await ormBeforeTarget(ADMIN_MADE);+ try {+ await seedAdminMade(orm);+ await migrateTo(orm, ADMIN_MADE);+ expect(await flags(orm)).toEqual(ADMIN_MADE_EXPECTED);+ } finally {+ await orm.close(true);+ }+ }, 30000);++ it('EMAILSELF-MIG-004: running it again changes nothing', async () => {+ const orm = await ormBeforeTarget(ADMIN_MADE);+ try {+ await seedAdminMade(orm);+ await migrateTo(orm, ADMIN_MADE);+ await runMigrationDirect(orm, Migration20200101046480_an_address_changed_after_an_admin_made_the_account);+ expect(await flags(orm)).toEqual(ADMIN_MADE_EXPECTED);+ } finally {+ await orm.close(true);+ }+ }, 30000);++ it('EMAILSELF-MIG-005: without an admin-made account nothing is read past the audit log', async () => {+ const orm = await ormBeforeTarget(ADMIN_MADE);+ try {+ await rawExec(+ orm,+ `INSERT INTO users (id, username, email, password_hash) VALUES (1, 'solo', 'solo-now@test', 'x')`,+ );+ await rawExec(+ orm,+ `INSERT INTO audit_log (user_id, action, resource, details) VALUES+ (1, 'user.register', NULL, '{"username":"solo","email":"solo@test","role":"user"}')`,+ );+ await migrateTo(orm, ADMIN_MADE);+ expect(await flags(orm)).toEqual([{ id: 1, email_self_asserted: 0 }]);+ } finally {+ await orm.close(true);+ }+ }, 30000);+});diff --git a/server/tests/unit/db/legacy-baseline.test.ts b/server/tests/unit/db/legacy-baseline.test.tsindex 7f52c3321..fa237de08 100644--- a/server/tests/unit/db/legacy-baseline.test.ts+++ b/server/tests/unit/db/legacy-baseline.test.ts@@ -114,6 +114,7 @@ const UNNUMBERED = ['Migration20200101046440_a_trip_owns_its_tags','Migration20200101046450_a_carried_over_half_day_stays_half','Migration20200101046460_an_upgraded_install_gets_the_columns_its_history_skipped',+ 'Migration20200101046480_an_address_changed_after_an_admin_made_the_account',];// The last step the positional runner ever had; planLegacyBaseline refuses a// schema_version past it.