mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
auth: let a token prove a second factor for erasure
Account deletion and the record wipe both demand a fresh second-factor proof once a factor is enrolled, and that proof lived on the session row. A native client has no session row, so an account with TOTP or a security key could not delete itself from the app on any request it could build: requireFreshMfaIfEnrolled fell through to requireFreshMfa, which resolves via getSession and answers 401 to a perfectly valid token. App Store guideline 5.1.1(v) asks for exactly that deletion. Add requireFreshMfaOrElevationIfEnrolled beside the cookie-only sibling rather than softening it. Its cookie arm is the sibling's, unchanged; its Bearer arm takes the single-use elevation the second-factor management routes already accept, with the fresh-factor rule pinned on, so a password-proved elevation cannot erase an account any more than a password login satisfies the gate on the web. No factor rides the request body: one mint surface, one redemption path. Scope stays fail-closed, so a narrow token is refused before the header is read. The elevation is spent immediately before the erasure, after the typed confirmation and the last-admin check, so a rejected request does not burn a proof against a 5-per-15-minute mint ceiling. The Bearer branch of requireMfaManagementAuth moves into a shared helper so both gates read and claim in the same place. The elevation guard grows a section naming the two routes, because the set of surfaces an elevation can unlock only stays small if adding to it is a visible edit.
This commit is contained in:
1 parent
ad0a055fdc
commit
5856b2a547
6 files changed
+897
-26
No files matched your search
@@ -4,7 +4,7 @@ import { apiSuccess, apiError, getClientIp } from "@/lib/api-response";
|
||||
import { NextRequest } from "next/server";
|
||||
import {
|
||||
apiHandler,
|
||||
requireFreshMfaIfEnrolled,
|
||||
requireFreshMfaOrElevationIfEnrolled,
|
||||
MFA_STEP_UP_MAX_AGE_SECONDS,
|
||||
} from "@/lib/api-handler";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
@@ -47,10 +47,15 @@ const WIPE_TRANSACTION_MAX_WAIT_MS = 15_000;
|
||||
*
|
||||
* v1.23 — gated by a fresh step-up for MFA-enrolled accounts (see the account
|
||||
* deletion route); single-factor accounts keep the typed-confirmation-only
|
||||
* contract.
|
||||
* contract. Over Bearer the step-up is a fresh-factor elevation in the
|
||||
* `X-Step-Up` header, spent right before the transaction — the same arm the
|
||||
* account deletion route carries, for the same App Store reason.
|
||||
*/
|
||||
export const DELETE = apiHandler(async (request: NextRequest) => {
|
||||
const { user } = await requireFreshMfaIfEnrolled(MFA_STEP_UP_MAX_AGE_SECONDS);
|
||||
const auth = await requireFreshMfaOrElevationIfEnrolled(
|
||||
MFA_STEP_UP_MAX_AGE_SECONDS,
|
||||
);
|
||||
const { user } = auth;
|
||||
|
||||
let confirm = "";
|
||||
try {
|
||||
@@ -70,6 +75,10 @@ export const DELETE = apiHandler(async (request: NextRequest) => {
|
||||
|
||||
const userId = user.id;
|
||||
|
||||
// The confirmation has passed; spend the Bearer elevation before the wipe
|
||||
// (a no-op on the cookie path).
|
||||
await auth.commitElevation();
|
||||
|
||||
let counts: Record<string, number>;
|
||||
try {
|
||||
counts = await prisma.$transaction(
|
||||
|
||||
Reference in new issue
Block a user