From 5d7d6072f7618778f1106a20f772bcc2d4c16934 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 15:39:14 +0100 Subject: [PATCH] fix(mail-ingest): refresh MCP sessions when an admin switches the mail import The mail import tools are listed only while the import is on, so a save that changes mail_ingest_enabled now ends the live MCP sessions, as the addon switches do. --- server/src/nest/auth/auth.service.ts | 7 ++++++- server/tests/unit/nest/auth.service.test.ts | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/server/src/nest/auth/auth.service.ts b/server/src/nest/auth/auth.service.ts index 90f004458..dbe0242ee 100644 --- a/server/src/nest/auth/auth.service.ts +++ b/server/src/nest/auth/auth.service.ts @@ -42,7 +42,7 @@ import { EphemeralTokenService } from '../auth-core/ephemeral-token.service'; // Import from sessionManager directly, NOT the ../../mcp barrel: the barrel pulls // the whole tools fan-out (and via the domain bridges, the Nest services) into // every consumer of this module — a nest→mcp→nest module cycle. -import { revokeUserSessions } from '../../mcp/sessionManager'; +import { invalidateMcpSessions, revokeUserSessions } from '../../mcp/sessionManager'; import { UserCleanupService } from './user-cleanup.service'; import { envLockedKeys, splitManagedKeys, withEnvLocks } from '../common/managed'; import { emitUserDeleted } from '../../plugin-user-lifecycle'; @@ -743,6 +743,7 @@ export class AuthService { // rest of the save working. Reported, never thrown: the settings tab sends // the whole form in one request. const blocked = [...splitManagedKeys(body, readEnv().managed.enabled).blocked, ...envLockedKeys(body)]; + const mailImportBefore = await readAppSetting(this.appSettings, 'mail_ingest_enabled'); await this.uow.transactional(async () => { // the whole form lands, or none of it // Lockout prevention: can't disable all login methods (read in the writing transaction). @@ -786,6 +787,10 @@ export class AuthService { 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; + // 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) { diff --git a/server/tests/unit/nest/auth.service.test.ts b/server/tests/unit/nest/auth.service.test.ts index 55a5e1f25..fedd02db4 100644 --- a/server/tests/unit/nest/auth.service.test.ts +++ b/server/tests/unit/nest/auth.service.test.ts @@ -11,7 +11,7 @@ import { UserSessions } from '../../../src/db/entities/UserSessions.entity'; import { Users } from '../../../src/db/entities/Users.entity'; import { MaintenanceRepository } from '../../../src/db/repositories/MaintenanceRepository'; import { UsersRepository } from '../../../src/db/repositories/Users.repository'; -import { revokeUserSessions } from '../../../src/mcp/sessionManager'; +import { invalidateMcpSessions, revokeUserSessions } from '../../../src/mcp/sessionManager'; import { EphemeralTokenService } from '../../../src/nest/auth-core/ephemeral-token.service'; import { createEphemeralToken } from '../../../src/nest/auth-core/ephemeral-tokens'; import { verifyJwtAndLoadUser } from '../../../src/nest/auth-core/jwt-verify'; @@ -114,7 +114,7 @@ vi.mock('../../../src/nest/common/crypto/apiKeyCrypto', () => ({ encrypt_api_key: vi.fn((v) => v), })); vi.mock('../../../src/nest/auth-core/ephemeral-tokens', () => ({ createEphemeralToken: vi.fn() })); -vi.mock('../../../src/mcp/sessionManager', () => ({ revokeUserSessions: vi.fn() })); +vi.mock('../../../src/mcp/sessionManager', () => ({ revokeUserSessions: vi.fn(), invalidateMcpSessions: vi.fn() })); // MailerService is injected since the notifications fold — a stub instead of a // module mock. sendPasswordResetEmail is the only thing auth reaches for. @@ -1164,6 +1164,18 @@ describe('updateAppSettings', () => { expect(cleared.success).toBe(true); await deleteRows(orm, AppSettings, { key: 'mail_ingest_webhook_secret' }); }); + + it('AUTH-DB-075d: switching the mail import ends the MCP sessions, so they list its tools anew; a save that keeps it does not', async () => { + const { user } = createAdmin(testDb); + vi.mocked(invalidateMcpSessions).mockClear(); + await svc.updateAppSettings(user.id, { mail_ingest_enabled: 'true' }); + expect(invalidateMcpSessions).toHaveBeenCalledTimes(1); + await svc.updateAppSettings(user.id, { mail_ingest_enabled: 'true', allow_registration: 'false' }); + expect(invalidateMcpSessions).toHaveBeenCalledTimes(1); + await svc.updateAppSettings(user.id, { mail_ingest_enabled: 'false' }); + expect(invalidateMcpSessions).toHaveBeenCalledTimes(2); + await deleteRows(orm, AppSettings, { key: { $in: ['mail_ingest_enabled', 'allow_registration'] } }); + }); }); describe('MFA success flows', () => {