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.
This commit is contained in:
Maurice
2026-11-23 16:23:21 +01:00
parent 8a9f5367b1
commit 96fa7ca4e8
9 changed files with 41 additions and 29 deletions
@@ -145,6 +145,11 @@ export class DayAccommodationsRepository extends TrekRepository<DayAccommodation
return !!row;
}
/** `id` while that stay is on the trip, else null: what a booking may link to. */
async idOnTrip(id: number | null, trip_id: number): Promise<number | null> {
return id && (await this.existsInTrip(id, trip_id)) ? id : null;
}
/** RS38 — `SELECT check_in FROM day_accommodations WHERE id = ?`. */
async getCheckIn(id: number): Promise<string | null | undefined> {
const row = await this.qb('a')
+22
View File
@@ -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<string, unknown>,
): { summary: Record<string, unknown>; debugDetails: Record<string, unknown> } {
const changed = (prefix: string) => changedKeys.some((k) => k.startsWith(prefix));
const summary: Record<string, unknown> = {};
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 };
}
+4 -18
View File
@@ -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<string, unknown> = {};
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<string, unknown> = {};
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.
@@ -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) {