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.
This commit is contained in:
Maurice
2026-11-23 15:39:14 +01:00
parent 4cd3f3d650
commit 5d7d6072f7
2 changed files with 20 additions and 3 deletions
+6 -1
View File
@@ -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<string, unknown> = {};
for (const k of changedKeys) {
+14 -2
View File
@@ -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', () => {