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";
|
||||
@@ -23,9 +23,20 @@ export const dynamic = "force-dynamic";
|
||||
* step-up (within MFA_STEP_UP_MAX_AGE_SECONDS) in addition to the typed
|
||||
* confirmation, so a hijacked live session cannot erase the record. Accounts
|
||||
* without MFA are unaffected and keep the typed-confirmation-only contract.
|
||||
*
|
||||
* Over Bearer the fresh step-up is a single-use elevation from
|
||||
* `POST /api/auth/step-up` in the `X-Step-Up` header, minted against a TOTP,
|
||||
* security-key, or passkey proof — a password proof is refused. App Store
|
||||
* guideline 5.1.1(v) requires in-app deletion, and until this arm existed an
|
||||
* MFA-enrolled account could not delete itself from the native app at all.
|
||||
* The elevation is spent immediately before the erasure, after every cheap
|
||||
* refusal, so a bad confirmation does not burn it.
|
||||
*/
|
||||
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 {
|
||||
@@ -57,6 +68,17 @@ export const DELETE = apiHandler(async (request: NextRequest) => {
|
||||
const userId = user.id;
|
||||
const username = user.username;
|
||||
|
||||
// Every refusal that costs nothing has passed; spend the Bearer elevation
|
||||
// now (a no-op on the cookie path). A lost claim throws the same 401 as a
|
||||
// missing one, so a concurrent redemption still has exactly one winner.
|
||||
//
|
||||
// One refusal can still land after this: the last-Guardian 409, which is
|
||||
// decided under the record lock inside the lifecycle transaction and has no
|
||||
// cheap pre-check. That caller spends a proof on a refusal and has to mint
|
||||
// another. Moving the spend past the transaction would be the worse trade —
|
||||
// the elevation would stay redeemable while the erasure was already running.
|
||||
await auth.commitElevation();
|
||||
|
||||
try {
|
||||
await deleteGuardianAccountWithLifecycle(userId, async (tx) => {
|
||||
// The invariant has now passed, so this success event cannot survive a
|
||||
|
||||
@@ -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