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 the account moved before an admin saved it
Maurice1fdf74e7f2df
diff --git a/server/src/db/migrations/Migration20200101046500_an_address_moved_before_an_admin_saved_the_account.ts b/server/src/db/migrations/Migration20200101046500_an_address_moved_before_an_admin_saved_the_account.tsnew file mode 100644index 000000000..28b231b8c--- /dev/null+++ b/server/src/db/migrations/Migration20200101046500_an_address_moved_before_an_admin_saved_the_account.ts@@ -0,0 +1,128 @@+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;+ targetUser?: unknown;+ fields?: unknown;+}++/** One admin save of an account: the address it found, and whether it sent an email at all. */+interface AdminSave {+ found: string | null;+ sentEmail: boolean;+}++interface History {+ /** The addresses the account was created or registered with; null once one of them is unreadable. */+ baselines: string[] | null;+ /** The admin saves since its creation, oldest first. */+ saves: AdminSave[];+}++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 {};+ }+}++const normalize = (email: string) => email.trim().toLowerCase();++/**+ * Marks an address an account set itself even when an admin saved the account+ * afterwards.+ *+ * `Migration20200101042800_an_email_the_account_typed_in_itself` and+ * `Migration20200101046480_an_address_changed_after_an_admin_made_the_account`+ * set `email_self_asserted` for an account whose address moved away from the one+ * it was registered or created with, unless an `admin.user_update` row names+ * `email`. But the edit dialog sends the email on every save, so every admin save+ * names it, a role change too, and one save of the account at any time kept the+ * flag at 0, even long after the account had moved its own address.+ *+ * Each such row also holds the address the account had when the admin saved it+ * (`targetUser`). So the history answers who set the current address: if every+ * admin save already found it, it was set before the first save, and between the+ * creation and that save only the account itself could change it. If the last+ * save that found another address sent no email, the address moved after that+ * save, again by the account itself. Either way the account is marked.+ *+ * When that last save did send an email, the admin may have typed the current+ * address in, and the rows cannot tell: the admin's new address is not logged,+ * and neither is a change the account made in its settings before 5.0.0. Such an+ * account stays as it is, the same as every account at its registered address and+ * every account with no creation row: marking it would refuse the first SSO+ * sign-in of an address an admin vouched for. Since 5.0.0 a change in the+ * settings sets the flag directly and an admin's new address clears it, so an+ * account this can still mark is one whose address moved before that.+ *+ * A creation row starts the account's history anew, so the saves of an account+ * that once held the same id do not count. An unreadable creation row or save+ * leaves the account as it is. Only accounts still at 0 are touched, and nothing+ * is ever cleared.+ */+export class Migration20200101046500_an_address_moved_before_an_admin_saved_the_account extends Migration {+ override name = 'Migration20200101046500_an_address_moved_before_an_admin_saved_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')+ ORDER BY id`,+ )) as AuditRow[];++ const histories = new Map<string, History>();+ for (const row of rows) {+ const details = readDetails(row.details);+ if (row.action === 'admin.user_update') {+ const history = row.resource ? histories.get(row.resource) : undefined;+ if (!history) continue;+ const found = typeof details.targetUser === 'string' ? normalize(details.targetUser) : null;+ const sentEmail = !Array.isArray(details.fields) || details.fields.includes('email');+ history.saves.push({ found, sentEmail });+ 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) continue;+ const id = String(owner);+ const before = histories.get(id)?.baselines;+ const baselines =+ typeof details.email === 'string' && before !== null ? [...(before ?? []), normalize(details.email)] : null;+ histories.set(id, { baselines, saves: [] });+ }+ if (histories.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[];+ // Queued, run after up() returns; the id comes from the database and is+ // checked to be an integer, nothing a user typed.+ for (const user of users) {+ const history = histories.get(String(user.id));+ if (!history?.baselines) continue;+ const current = normalize(user.email);+ if (history.baselines.includes(current)) continue;+ const lastOther = [...history.saves].reverse().find((save) => save.found !== current);+ if (lastOther && (lastOther.found === null || lastOther.sentEmail)) continue;+ const rowId = Number(user.id);+ if (!Number.isInteger(rowId)) continue;+ this.addSql(`UPDATE users SET email_self_asserted = 1 WHERE id = ${rowId}`);+ }+ }+}diff --git a/server/tests/unit/db/email-self-asserted-migration.test.ts b/server/tests/unit/db/email-self-asserted-migration.test.tsindex 983524de8..6af9edd36 100644--- a/server/tests/unit/db/email-self-asserted-migration.test.ts+++ b/server/tests/unit/db/email-self-asserted-migration.test.ts@@ -7,9 +7,15 @@* `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.+ *+ * `Migration20200101046500_an_address_moved_before_an_admin_saved_the_account`+ * reads the address each admin save found, so a save after the account moved+ * its own address no longer keeps it at 0, while a save that may have typed the+ * current address in still does.*/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 { Migration20200101046500_an_address_moved_before_an_admin_saved_the_account } from '../../../src/db/migrations/Migration20200101046500_an_address_moved_before_an_admin_saved_the_account';import {createMigrationOrm,migrateTo,@@ -24,6 +30,7 @@ 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';+const SAVED_AFTER = 'Migration20200101046500_an_address_moved_before_an_admin_saved_the_account';async function ormBeforeTarget(target = TARGET): Promise<MikroORM> {const orm = await createMigrationOrm();@@ -193,3 +200,123 @@ describe('admin-made account email migration', () => {}}, 30000);});++async function seedSaves(orm: MikroORM): Promise<void> {+ // 9 is the admin; every save below is the edit dialog's unless said otherwise,+ // which sends the email each time. 1 was made by the admin, moved its address+ // itself, and the admin changed its role afterwards; 2 the same but the save+ // came first, so the admin may have typed the new address in; 3 got its new+ // address from the admin; 4 registered, moved, and the admin saved it later;+ // 5 registered and an API save sent no email before the account moved; 6 has a+ // save whose details are broken; 7 was made under an id an earlier account+ // held, whose save does not count; 8 moved, was saved, then the admin gave it+ // another address; 10 moved with no save at all; 11 is still at the address it+ // was made with; 12 is already marked; 13 has no creation row; 14 is a guest.+ await rawExec(+ orm,+ `INSERT INTO users (id, username, email, password_hash, email_self_asserted, is_guest) VALUES+ (1, 'later', 'colleague-1@test', 'x', 0, 0),+ (2, 'earlier', 'colleague-2@test', 'x', 0, 0),+ (3, 'given', 'given-by-admin@test', 'x', 0, 0),+ (4, 'registered', 'colleague-4@test', 'x', 0, 0),+ (5, 'api', 'colleague-5@test', 'x', 0, 0),+ (6, 'broken', 'colleague-6@test', 'x', 0, 0),+ (7, 'reused', 'colleague-7@test', 'x', 0, 0),+ (8, 'regiven', 'regiven-by-admin@test', 'x', 0, 0),+ (9, 'admin', 'admin@test', 'x', 0, 0),+ (10, 'unsaved', 'colleague-10@test', 'x', 0, 0),+ (11, 'kept', 'Kept@Test', 'x', 0, 0),+ (12, 'marked', 'marked-now@test', 'x', 1, 0),+ (13, 'seeded', 'seeded-now@test', 'x', 0, 0),+ (14, '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":"later","email":"later@test","role":"user"}'),+ (9, 'admin.user_update', '1', '{"targetUser":"colleague-1@test","fields":["username","email","role"]}'),+ (9, 'admin.user_create', '2', '{"username":"earlier","email":"earlier@test","role":"user"}'),+ (9, 'admin.user_update', '2', '{"targetUser":"earlier@test","fields":["username","email","role"]}'),+ (9, 'admin.user_create', '3', '{"username":"given","email":"given@test","role":"user"}'),+ (9, 'admin.user_update', '3', '{"targetUser":"given@test","fields":["username","email","role"]}'),+ (4, 'user.register', NULL, '{"username":"registered","email":"registered@test","role":"user"}'),+ (9, 'admin.user_update', '4', '{"targetUser":"Colleague-4@Test","fields":["username","email","role"]}'),+ (5, 'user.register', NULL, '{"username":"api","email":"api@test","role":"user"}'),+ (9, 'admin.user_update', '5', '{"targetUser":"api@test","fields":["role"]}'),+ (9, 'admin.user_create', '6', '{"username":"broken","email":"broken@test","role":"user"}'),+ (9, 'admin.user_update', '6', 'not json'),+ (9, 'admin.user_create', '7', '{"username":"gone","email":"gone@test","role":"user"}'),+ (9, 'admin.user_update', '7', '{"targetUser":"gone@test","fields":["username","email","role"]}'),+ (7, 'user.register', NULL, '{"username":"reused","email":"reused@test","role":"user"}'),+ (9, 'admin.user_create', '8', '{"username":"regiven","email":"regiven@test","role":"user"}'),+ (9, 'admin.user_update', '8', '{"targetUser":"colleague-8@test","fields":["username","email","role"]}'),+ (9, 'admin.user_update', '8', '{"targetUser":"colleague-8@test","fields":["username","email","role"]}'),+ (9, 'admin.user_create', '10', '{"username":"unsaved","email":"unsaved@test","role":"user"}'),+ (9, 'admin.user_create', '11', '{"username":"kept","email":"kept@test","role":"user"}'),+ (9, 'admin.user_update', '11', '{"targetUser":"kept@test","fields":["username","email","role"]}'),+ (12, 'user.register', NULL, '{"username":"marked","email":"marked@test","role":"user"}'),+ (9, 'admin.user_update', '13', '{"targetUser":"seeded@test","fields":["role"]}'),+ (9, 'admin.user_create', '14', '{"username":"guest","email":"guest@test","role":"user"}')`,+ );+}++const SAVED_AFTER_EXPECTED = [+ { id: 1, email_self_asserted: 1 },+ { id: 2, email_self_asserted: 0 },+ { id: 3, email_self_asserted: 0 },+ { id: 4, email_self_asserted: 1 },+ { id: 5, email_self_asserted: 1 },+ { id: 6, email_self_asserted: 0 },+ { id: 7, email_self_asserted: 1 },+ { id: 8, email_self_asserted: 0 },+ { id: 9, email_self_asserted: 0 },+ { id: 10, email_self_asserted: 1 },+ { id: 11, email_self_asserted: 0 },+ { id: 12, email_self_asserted: 1 },+ { id: 13, email_self_asserted: 0 },+ { id: 14, email_self_asserted: 0 },+];++describe('admin saves after the account moved its address', () => {+ it('EMAILSELF-MIG-006: an address no admin save can have set is marked, whatever the save sent', async () => {+ const orm = await ormBeforeTarget(SAVED_AFTER);+ try {+ await seedSaves(orm);+ await migrateTo(orm, SAVED_AFTER);+ expect(await flags(orm)).toEqual(SAVED_AFTER_EXPECTED);+ } finally {+ await orm.close(true);+ }+ }, 30000);++ it('EMAILSELF-MIG-007: running it again changes nothing', async () => {+ const orm = await ormBeforeTarget(SAVED_AFTER);+ try {+ await seedSaves(orm);+ await migrateTo(orm, SAVED_AFTER);+ await runMigrationDirect(orm, Migration20200101046500_an_address_moved_before_an_admin_saved_the_account);+ expect(await flags(orm)).toEqual(SAVED_AFTER_EXPECTED);+ } finally {+ await orm.close(true);+ }+ }, 30000);++ it('EMAILSELF-MIG-008: an audit log without a creation row reads no account', async () => {+ const orm = await ormBeforeTarget(SAVED_AFTER);+ 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, 'admin.user_update', '1', '{"targetUser":"solo-now@test","fields":["role"]}')`,+ );+ await migrateTo(orm, SAVED_AFTER);+ 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 5a89e4825..1c9878af4 100644--- a/server/tests/unit/db/legacy-baseline.test.ts+++ b/server/tests/unit/db/legacy-baseline.test.ts@@ -116,6 +116,7 @@ const UNNUMBERED = ['Migration20200101046460_an_upgraded_install_gets_the_columns_its_history_skipped','Migration20200101046480_an_address_changed_after_an_admin_made_the_account','Migration20200101046490_a_trip_can_switch_its_own_addons',+ 'Migration20200101046500_an_address_moved_before_an_admin_saved_the_account',];// The last step the positional runner ever had; planLegacyBaseline refuses a// schema_version past it.