feat: enforce admin 2FA, step-up auth, stronger password policy
Implements OWASP audit recommendations P1-P3: P1 - Two-factor authentication (TOTP) mandatory for admins: - better-auth twoFactor plugin; new two_factor table + user flag (migration 0010, MUST run before deploying the app) - requireAdmin throws TWO_FACTOR_REQUIRED without a finished setup; pages redirect to the new /zwei-faktor wizard which uses a weak guard (allowWithoutTwoFactor) so admins can never lock themselves out. Wizard lives outside /admin to avoid layout redirect loops. - login flow handles twoFactorRedirect and asks for the TOTP code; backup codes are shown once during enablement P2 - Step-up for destructive operations (ASVS 2.5.4): - hardDeleteEditorialAction and resetUsageDataAction reject sessions older than 15 minutes (SESSION_STALE) with actionable German messages instead of throwing opaque digests P3 - Password policy (NIST 800-63B): - minimum length raised from 8 to 12 plus common-password denylist, enforced for registration AND reset/change via a before-hook
This commit is contained in:
1 parent
9308b46d84
commit
0d939593c8
20 files changed
+2613
-29
No files matched your search
@@ -5,8 +5,9 @@ import { cache } from "react";
|
||||
import { headers } from "next/headers";
|
||||
|
||||
import { requirePageAccess } from "@/modules/auth/page-access";
|
||||
import { requireAdmin } from "@/modules/auth/principal";
|
||||
import { requireAdmin, requireAdminForSetup } from "@/modules/auth/principal";
|
||||
|
||||
/** Strict guard: valid admin session with finished two-factor setup. */
|
||||
export const requireAdminPage = cache(async () =>
|
||||
requirePageAccess(async () => requireAdmin(new Headers(await headers()))),
|
||||
);
|
||||
@@ -7,7 +7,7 @@ import { eq } from "drizzle-orm";
|
||||
import { db } from "@/db/client";
|
||||
import { editorialAccounts } from "@/db/schema";
|
||||
|
||||
import { requireAdmin } from "@/modules/auth/principal";
|
||||
import { requireAdmin, PrincipalAuthorizationError, DESTRUCTIVE_ACTION_MAX_SESSION_AGE_SECONDS } from "@/modules/auth/principal";
|
||||
import { editorialService, EditorialServiceError } from "@/modules/editorials/service";
|
||||
|
||||
// Returned to the client form so failures surface as inline German text
|
||||
@@ -65,23 +65,65 @@ export async function unarchiveEditorialAction(editorialId: string): Promise<voi
|
||||
revalidatePath("/admin/redaktionen");
|
||||
}
|
||||
|
||||
export async function hardDeleteEditorialAction(editorialId: string): Promise<void> {
|
||||
await requireAdmin(new Headers(await headers()));
|
||||
await editorialService.hardDeleteEditorial(editorialId);
|
||||
export type ResetUsageResult = { ok: boolean; message: string };
|
||||
|
||||
/**
|
||||
* Step-up guard for irreversible operations (OWASP ASVS 2.5.4): the session
|
||||
* must be recent, otherwise the admin re-authenticates first.
|
||||
*/
|
||||
async function freshAdminPrincipal(): Promise<
|
||||
Awaited<ReturnType<typeof requireAdmin>> | ResetUsageResult
|
||||
> {
|
||||
try {
|
||||
return await requireAdmin(new Headers(await headers()), {
|
||||
maxSessionAgeSeconds: DESTRUCTIVE_ACTION_MAX_SESSION_AGE_SECONDS,
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof PrincipalAuthorizationError) {
|
||||
return { ok: false, message: adminGuardMessage(error.code) };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function adminGuardMessage(code: PrincipalAuthorizationError["code"]): string {
|
||||
switch (code) {
|
||||
case "SESSION_STALE":
|
||||
return "Aus Sicherheitsgründen ist Ihre Sitzung zu alt für diese Aktion. Bitte melden Sie sich erneut an und wiederholen Sie den Schritt.";
|
||||
case "TWO_FACTOR_REQUIRED":
|
||||
return "Für diese Aktion ist Zwei-Faktor-Authentifizierung erforderlich.";
|
||||
default:
|
||||
return "Die Aktion ist fehlgeschlagen.";
|
||||
}
|
||||
}
|
||||
|
||||
function isPrincipal(result: unknown): result is Awaited<ReturnType<typeof requireAdmin>> {
|
||||
return typeof result === "object" && result !== null && "userId" in result;
|
||||
}
|
||||
|
||||
export async function hardDeleteEditorialAction(editorialId: string): Promise<ResetUsageResult> {
|
||||
const safeEditorialId = requiredId(editorialId);
|
||||
const principal = await freshAdminPrincipal();
|
||||
if (!isPrincipal(principal)) {
|
||||
return principal;
|
||||
}
|
||||
|
||||
await editorialService.hardDeleteEditorial(safeEditorialId);
|
||||
revalidatePath("/admin/redaktionen");
|
||||
redirect("/admin/redaktionen");
|
||||
}
|
||||
|
||||
export type ResetUsageResult = { ok: boolean; message: string };
|
||||
|
||||
export async function resetUsageDataAction(editorialId: string): Promise<ResetUsageResult> {
|
||||
const admin = await requireAdmin(new Headers(await headers()));
|
||||
const safeEditorialId = requiredId(editorialId);
|
||||
const principal = await freshAdminPrincipal();
|
||||
if (!isPrincipal(principal)) {
|
||||
return principal;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await editorialService.resetUsageData({
|
||||
editorialId: safeEditorialId,
|
||||
adminUserId: admin.userId,
|
||||
adminUserId: principal.userId,
|
||||
});
|
||||
revalidatePath(`/admin/redaktionen/${safeEditorialId}`);
|
||||
revalidatePath("/admin/redaktionen");
|
||||
|
||||
Reference in new issue
Block a user