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
@@ -1,12 +1,19 @@
|
||||
import "server-only";
|
||||
|
||||
import { APIError } from "better-auth";
|
||||
import { createAuthMiddleware } from "better-auth/api";
|
||||
import { drizzleAdapter } from "@better-auth/drizzle-adapter";
|
||||
import { betterAuth, type BetterAuthOptions } from "better-auth";
|
||||
import { twoFactor } from "better-auth/plugins";
|
||||
|
||||
import { env } from "@/config/env";
|
||||
import { db } from "@/db/client";
|
||||
import { accounts, sessions, users, verifications } from "@/db/schema";
|
||||
import { accounts, sessions, twoFactors, users, verifications } from "@/db/schema";
|
||||
import { mailer } from "@/modules/mail/mailer";
|
||||
import {
|
||||
validatePasswordPolicy,
|
||||
MIN_PASSWORD_LENGTH,
|
||||
} from "@/modules/security/password-policy";
|
||||
import { consumeRateLimit, registrationPolicy } from "@/modules/security/rate-limit";
|
||||
import {
|
||||
enforceRateLimit,
|
||||
@@ -74,12 +81,29 @@ export function createPortalAuth({
|
||||
defaultValue: "EDITORIAL",
|
||||
input: false,
|
||||
},
|
||||
twoFactorEnabled: {
|
||||
type: "boolean",
|
||||
required: false,
|
||||
defaultValue: false,
|
||||
input: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
twoFactor({
|
||||
// TOTP + backup codes only: without `otpOptions.sendOTP` no OTP-e-mail
|
||||
// surface exists, and trust-device stays off to keep the attack area
|
||||
// small. Backup codes stay encrypted (plugin default).
|
||||
}),
|
||||
],
|
||||
hooks: {
|
||||
before: createPasswordPolicyHook(),
|
||||
},
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
requireEmailVerification: true,
|
||||
autoSignIn: false,
|
||||
minPasswordLength: MIN_PASSWORD_LENGTH,
|
||||
resetPasswordTokenExpiresIn: 60 * 60,
|
||||
revokeSessionsOnPasswordReset: true,
|
||||
async sendResetPassword({ user, url }) {
|
||||
@@ -129,6 +153,32 @@ export function createPortalAuth({
|
||||
return { auth, registrationService };
|
||||
}
|
||||
|
||||
export function createPasswordPolicyHook() {
|
||||
// Applies the shared policy to password-reset and change-password flows,
|
||||
// which bypass the registration validation. Registration is covered by
|
||||
// validateRegistration(); sign-up additionally enforces Better Auth's
|
||||
// minPasswordLength.
|
||||
return createAuthMiddleware(async (ctx) => {
|
||||
if (ctx.path !== "/reset-password" && ctx.path !== "/change-password") {
|
||||
return;
|
||||
}
|
||||
const body = (ctx.body ?? {}) as { newPassword?: unknown; password?: unknown };
|
||||
const candidate =
|
||||
typeof body.newPassword === "string"
|
||||
? body.newPassword
|
||||
: typeof body.password === "string"
|
||||
? body.password
|
||||
: undefined;
|
||||
if (candidate === undefined) {
|
||||
return;
|
||||
}
|
||||
const result = validatePasswordPolicy(candidate);
|
||||
if (!result.ok) {
|
||||
throw new APIError("BAD_REQUEST", { message: result.reason });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const registrationRepository = createEditorialRegistrationRepository(db);
|
||||
const portalAuth = createPortalAuth({
|
||||
database: drizzleAdapter(db, {
|
||||
@@ -138,6 +188,7 @@ const portalAuth = createPortalAuth({
|
||||
account: accounts,
|
||||
session: sessions,
|
||||
verification: verifications,
|
||||
twoFactor: twoFactors,
|
||||
},
|
||||
}),
|
||||
mailerService: mailer,
|
||||
|
||||
Reference in new issue
Block a user