From 96fa7ca4e8f462607fb4f0c6d5e16ad24230d067 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 16:23:21 +0100 Subject: [PATCH] refactor(server): keep the fixes within the size and strict ratchets The admin settings audit summary moves to auth.helpers, the booking stay check to DayAccommodationsRepository.idOnTrip, the strict typings declare the MCP SDK types module, and the useAdmin test leaves the format baseline. --- client/scripts/format-baseline.json | 1 - server/scripts/size-baseline.json | 2 +- .../strict-typings/mcp-sdk-client.d.ts | 5 +++-- .../DayAccommodations.repository.ts | 5 +++++ server/src/nest/auth/auth.helpers.ts | 22 +++++++++++++++++++ server/src/nest/auth/auth.service.ts | 22 ++++--------------- .../nest/reservations/reservations.service.ts | 8 ++----- .../DayAccommodations.repository.test.ts | 3 +++ server/tests/unit/nest/trips.service.test.ts | 2 +- 9 files changed, 41 insertions(+), 29 deletions(-) diff --git a/client/scripts/format-baseline.json b/client/scripts/format-baseline.json index d6e36450b..3c4571a9f 100644 --- a/client/scripts/format-baseline.json +++ b/client/scripts/format-baseline.json @@ -1507,7 +1507,6 @@ "src/pages/admin/ProviderBlock.tsx", "src/pages/admin/TrekApiCard.tsx", "src/pages/admin/adminModel.ts", - "src/pages/admin/useAdmin.test.tsx", "src/pages/admin/useAdmin.ts", "src/pages/admin/useNotificationDefaults.ts", "src/pages/atlas/AtlasCountrySearch.tsx", diff --git a/server/scripts/size-baseline.json b/server/scripts/size-baseline.json index b611e1cc0..0243a260a 100644 --- a/server/scripts/size-baseline.json +++ b/server/scripts/size-baseline.json @@ -6,7 +6,7 @@ "src/db/repositories/Trips.repository.ts": 1120, "src/db/repositories/Users.repository.ts": 1421, "src/nest/atlas/atlas.service.ts": 1166, - "src/nest/auth/auth.service.ts": 1141, + "src/nest/auth/auth.service.ts": 1132, "src/nest/budget/budget.service.ts": 1607, "src/nest/collections/collections.service.ts": 1414, "src/nest/doc-sync/doc-sync.service.ts": 1060, diff --git a/server/scripts/strict-typings/mcp-sdk-client.d.ts b/server/scripts/strict-typings/mcp-sdk-client.d.ts index 5078f3796..7dc747ea1 100644 --- a/server/scripts/strict-typings/mcp-sdk-client.d.ts +++ b/server/scripts/strict-typings/mcp-sdk-client.d.ts @@ -1,9 +1,10 @@ -// The MCP SDK's client and in-memory transport resolve, through the `paths` +// The MCP SDK's client, in-memory transport and types resolve, through the `paths` // redirect in tsconfig.json, to its CommonJS build, which ships no type // definitions next to it, so the build reads them as untyped modules. Only // the strict ratchet (tsconfig.strict.json) loads this file: it declares the -// two modules untyped there too, so an import of them is not counted as an +// modules untyped there too, so an import of them is not counted as an // error the build does not have. The build and the tests never see it. declare module '@modelcontextprotocol/sdk/client/index'; declare module '@modelcontextprotocol/sdk/inMemory'; +declare module '@modelcontextprotocol/sdk/types'; diff --git a/server/src/db/repositories/DayAccommodations.repository.ts b/server/src/db/repositories/DayAccommodations.repository.ts index e6d45e4ae..06dd641e7 100644 --- a/server/src/db/repositories/DayAccommodations.repository.ts +++ b/server/src/db/repositories/DayAccommodations.repository.ts @@ -145,6 +145,11 @@ export class DayAccommodationsRepository extends TrekRepository { + return id && (await this.existsInTrip(id, trip_id)) ? id : null; + } + /** RS38 — `SELECT check_in FROM day_accommodations WHERE id = ?`. */ async getCheckIn(id: number): Promise { const row = await this.qb('a') diff --git a/server/src/nest/auth/auth.helpers.ts b/server/src/nest/auth/auth.helpers.ts index 43d285cc7..cd8515dc7 100644 --- a/server/src/nest/auth/auth.helpers.ts +++ b/server/src/nest/auth/auth.helpers.ts @@ -2,6 +2,7 @@ import { USER_MFA_BACKUP_CODES } from '../../db/json-columns'; import { utcSuffix } from '../../db/types'; import { User } from '../../types'; import { decodeJson } from '../../utils/json-column'; +import { MASKED_ADMIN_SETTING_KEYS, type AppSettingKey } from '../common/app-settings.registry'; import { decrypt_api_key } from '../common/crypto/apiKeyCrypto'; import bcrypt from 'bcryptjs'; @@ -140,3 +141,24 @@ export function parseBackupCodeHashes(raw: string | null | undefined): string[] export function backupCodeIndex(plaintext: string, raw: string | null | undefined): number { return parseBackupCodeHashes(raw).findIndex((hash) => matchBackupCode(plaintext, hash)); } + +/** What an admin settings save writes to the audit log: what changed, with every secret masked. */ +export function appSettingsAudit( + changedKeys: readonly AppSettingKey[], + body: Record, +): { summary: Record; debugDetails: Record } { + const changed = (prefix: string) => changedKeys.some((k) => k.startsWith(prefix)); + const summary: Record = {}; + if (changedKeys.includes('notification_channels')) summary.notification_channels = body.notification_channels; + if (changedKeys.includes('admin_webhook_url')) summary.admin_webhook_url_updated = true; + if (changed('admin_ntfy_')) summary.admin_ntfy_updated = true; + if (changed('smtp_')) summary.smtp_settings_updated = true; + if (changedKeys.includes('allow_registration')) summary.allow_registration = body.allow_registration; + if (changedKeys.includes('allowed_file_types')) summary.allowed_file_types_updated = true; + if (changedKeys.includes('require_mfa')) summary.require_mfa = body.require_mfa; + if (changed('mail_ingest_')) summary.mail_ingest_settings_updated = true; + const debugDetails = Object.fromEntries( + changedKeys.map((k) => [k, MASKED_ADMIN_SETTING_KEYS.has(k) ? '***' : body[k]]), + ); + return { summary, debugDetails }; +} diff --git a/server/src/nest/auth/auth.service.ts b/server/src/nest/auth/auth.service.ts index dbe0242ee..65c3a8e34 100644 --- a/server/src/nest/auth/auth.service.ts +++ b/server/src/nest/auth/auth.service.ts @@ -68,6 +68,7 @@ import { matchBackupCode, parseBackupCodeHashes, stripUserForClient, + appSettingsAudit, } from './auth.helpers'; import { ADMIN_FORM_SETTING_KEYS, @@ -777,25 +778,10 @@ export class AuthService { // A masked key sent back as its mask was not changed, and a secret's value never reaches the audit row. const changedKeys = ADMIN_FORM_SETTING_KEYS.filter(k => !blocked.includes(k) && body[k] !== undefined && !isMaskEcho(k, body[k])); - const summary: Record = {}; - const smtpChanged = changedKeys.some(k => k.startsWith('smtp_')); - if (changedKeys.includes('notification_channels')) summary.notification_channels = body.notification_channels; - if (changedKeys.includes('admin_webhook_url')) summary.admin_webhook_url_updated = true; - if (changedKeys.some(k => k.startsWith('admin_ntfy_'))) summary.admin_ntfy_updated = true; - if (smtpChanged) summary.smtp_settings_updated = true; - if (changedKeys.includes('allow_registration')) summary.allow_registration = body.allow_registration; - if (changedKeys.includes('allowed_file_types')) summary.allowed_file_types_updated = true; - if (changedKeys.includes('require_mfa')) summary.require_mfa = body.require_mfa; - if (changedKeys.some(k => k.startsWith('mail_ingest_'))) summary.mail_ingest_settings_updated = true; + const { summary, debugDetails } = appSettingsAudit(changedKeys, body); // The mail import tools are listed only while it is on, and a session's tool list is fixed when it connects. - if (changedKeys.includes('mail_ingest_enabled') && (await readAppSetting(this.appSettings, 'mail_ingest_enabled')) !== mailImportBefore) { - invalidateMcpSessions(); - } - - const debugDetails: Record = {}; - for (const k of changedKeys) { - debugDetails[k] = MASKED_ADMIN_SETTING_KEYS.has(k) ? '***' : body[k]; - } + const mailImportAfter = await readAppSetting(this.appSettings, 'mail_ingest_enabled'); + if (changedKeys.includes('mail_ingest_enabled') && mailImportAfter !== mailImportBefore) invalidateMcpSessions(); // The reminder crons read their enable gates and channels per tick, so a // notification-settings change takes effect at the next run — no restart. diff --git a/server/src/nest/reservations/reservations.service.ts b/server/src/nest/reservations/reservations.service.ts index aa596ca46..f04a6b571 100644 --- a/server/src/nest/reservations/reservations.service.ts +++ b/server/src/nest/reservations/reservations.service.ts @@ -605,7 +605,6 @@ export class ReservationsService { let stayMirror = noStayMirror(); // Auto-create accommodation for hotel reservations. - // // The stay row is written here rather than through createAccommodation: this // surface has its own field set and its own COALESCE semantics, and folding // the two together would bend one of them out of shape. The day stop is the @@ -613,11 +612,8 @@ export class ReservationsService { // The payload's accommodation_id is untrusted input and goes through // `toRowId` the way update() reads it: a `'0x1'` or an `'abc'` used to // be stored as `'0x1.0'`, which Postgres cannot cast back for the join. - let resolvedAccommodationId: number | null = toRowId(accommodation_id) || null; - // A stay that is not on this trip is dropped, as update() drops it. - if (resolvedAccommodationId && !(await this.dayAccommodationsRepo.existsInTrip(resolvedAccommodationId, this.rowIdNum(tripId)))) { - resolvedAccommodationId = null; - } + // Like there, a stay that is not on this trip is dropped. + let resolvedAccommodationId = await this.dayAccommodationsRepo.idOnTrip(toRowId(accommodation_id), Number(tripId)); if (type === 'hotel' && !resolvedAccommodationId && create_accommodation) { const { place_id: accPlaceId, start_day_id, end_day_id, check_in, check_out, confirmation: accConf } = create_accommodation; if (start_day_id && end_day_id) { diff --git a/server/tests/unit/db/repositories/DayAccommodations.repository.test.ts b/server/tests/unit/db/repositories/DayAccommodations.repository.test.ts index d09d5a6a3..77ebbe7ec 100644 --- a/server/tests/unit/db/repositories/DayAccommodations.repository.test.ts +++ b/server/tests/unit/db/repositories/DayAccommodations.repository.test.ts @@ -106,6 +106,9 @@ describe('DayAccommodationsRepository — fully seeded world', () => { const { trip, named, otherStay } = await seed(); expect(await repo.existsInTrip(named.id, trip.id)).toBe(true); expect(await repo.existsInTrip(otherStay.id, trip.id)).toBe(false); + expect(await repo.idOnTrip(named.id, trip.id)).toBe(named.id); + expect(await repo.idOnTrip(otherStay.id, trip.id)).toBeNull(); + expect(await repo.idOnTrip(null, trip.id)).toBeNull(); expect(await repo.getCheckIn(named.id)).toBe('15:00'); expect(await repo.getTripId(named.id)).toBe(trip.id); expect(await repo.getTripId(999999)).toBeUndefined(); diff --git a/server/tests/unit/nest/trips.service.test.ts b/server/tests/unit/nest/trips.service.test.ts index d8ce58dd0..044c2f30e 100644 --- a/server/tests/unit/nest/trips.service.test.ts +++ b/server/tests/unit/nest/trips.service.test.ts @@ -305,7 +305,7 @@ beforeAll(async () => { (await sharedTestOrm(testDb)).em, new SettingsService(await createTestUnitOfWork(testDb), await createTestAppSettingsRepo(testDb), await createTestSettingsRepo(testDb), membership.get(TripMembershipService)), { copyOverrides: async () => {} } as never, ); - membersSvc = new TripMembersService(budgetSvc, new UserCleanupService(new MaintenanceRepository(dbsEm!), budgetSvc, await createTestUnitOfWork(testDb), await createTestUsersRepo(testDb), await createTestTripMembersRepo(testDb), await createTestBudgetItemsRepo(testDb), await createTestBudgetSettlementsRepo(testDb), await createTestJourneyShareTokensRepo(testDb), await createTestJourneysRepo(testDb), await createTestJourneyEntriesRepo(testDb), await createTestJourneyContributorsRepo(testDb), await createTestShareTokensRepo(testDb), await createTestPluginsRepo(testDb), await createTestPluginUserErasureQueueRepo(testDb), { releaseValues: async () => [] } as never, { handToTripOwners: async () => {} } as never), new PermissionsService(await createTestAppSettingsRepo(testDb), await createTestUnitOfWork(testDb)), realtime, notificationsStub(), await createTestUnitOfWork(testDb), await createTestTripsRepo(testDb), await createTestTripMembersRepo(testDb), await createTestUsersRepo(testDb)); + membersSvc = new TripMembersService(budgetSvc, new UserCleanupService(new MaintenanceRepository(dbsEm!), budgetSvc, await createTestUnitOfWork(testDb), await createTestUsersRepo(testDb), await createTestTripMembersRepo(testDb), await createTestBudgetItemsRepo(testDb), await createTestBudgetSettlementsRepo(testDb), await createTestJourneyShareTokensRepo(testDb), await createTestJourneysRepo(testDb), await createTestJourneyEntriesRepo(testDb), await createTestJourneyContributorsRepo(testDb), await createTestShareTokensRepo(testDb), await createTestPluginsRepo(testDb), await createTestPluginUserErasureQueueRepo(testDb), {} as never, {} as never), new PermissionsService(await createTestAppSettingsRepo(testDb), await createTestUnitOfWork(testDb)), realtime, notificationsStub(), await createTestUnitOfWork(testDb), await createTestTripsRepo(testDb), await createTestTripMembersRepo(testDb), await createTestUsersRepo(testDb)); readModelSvc = new TripReadModelService( await createTestTripsRepo(testDb), membersSvc, daysSvc, accommodationsSvc, budgetSvc, new PackingService(