Enforce the MFA policy as a guard, and stop 403ing public routes

The last live non-shell middleware verified the JWT and read the users row a
second time on every /api request, then threw the loaded user away - two
jwt.verify calls and two SELECTs per authenticated request, because it ran
before Nest and could not see what the auth guard had already resolved. The
guard reads req.user.

It also corrects an asymmetry that had grown quietly. The middleware decided
on two literal path lists, and every public /api endpoint added after those
lists were written - /api/config, /api/help/*, the public journey and share
routes, /api/health/features - answered a logged-in user without MFA with a
403 while answering the same request from a stranger perfectly well. Nothing
intended that; the lists simply stopped being maintained. Keying on @Public()
means the exemption cannot drift from the route again. This is a deliberate
behaviour change and the only one here.

@MfaExempt carries the reason for the other kind of exemption: the routes
that let somebody complete setup. Without them the policy locks out every new
account, which is why the middleware had its second list.

One ordering trap on the way: global guards run before route guards, so the
auth guard standing down on a controller with its own @UseGuards chain left
req.user unset when the MFA guard ran behind it, and it waved everything
through. AUTH-020 caught it. It resolves the user now without refusing - the
declared chain still owns the 401.
This commit is contained in:
Maurice
2026-08-10 11:27:02 +02:00
committed by Maurice
parent ffb73fac20
commit be8081c3b4
10 changed files with 281 additions and 216 deletions
+6
View File
@@ -43,6 +43,7 @@ import { CurrentUser } from './current-user.decorator';
import { getClientIp } from '../audit/client-ip';
import { AuditService } from '../audit/audit.service';
import type { User } from '../../types';
import { MfaExempt } from './mfa-policy.guard';
const WINDOW = 15 * 60 * 1000;
const ALLOWED_AVATAR_EXTS = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
@@ -83,6 +84,7 @@ export class AuthController {
}
@Get('me')
@MfaExempt('the client needs to know who it is to render the setup screen')
me(@CurrentUser() user: User) {
const loaded = this.auth.getCurrentUser(user.id);
if (!loaded) {
@@ -176,6 +178,7 @@ export class AuthController {
}
@Get('app-settings')
@MfaExempt('the setup screen reads the policy it is asking the user to satisfy')
getAppSettings(@CurrentUser() user: User) {
const result = this.auth.getAppSettings(user.id);
if (result.error) {
@@ -185,6 +188,7 @@ export class AuthController {
}
@Put('app-settings')
@MfaExempt('an admin locked out by their own policy must still be able to lift it')
updateAppSettings(@CurrentUser() user: User, @Body() body: AppSettingsUpdateDto, @Req() req: Request) {
const result = this.auth.updateAppSettings(user.id, body);
if (result.error) {
@@ -199,6 +203,7 @@ export class AuthController {
// AtlasModule import.
@Post('mfa/setup')
@MfaExempt('completing setup is the way out of the policy')
@HttpCode(200)
async mfaSetup(@CurrentUser() user: User) {
const result = this.auth.setupMfa(user.id, user.email);
@@ -215,6 +220,7 @@ export class AuthController {
}
@Post('mfa/enable')
@MfaExempt('completing setup is the way out of the policy')
@HttpCode(200)
mfaEnable(@CurrentUser() user: User, @Body() body: MfaEnableDto, @Req() req: Request) {
this.limit('mfa', req, 5);