Harden dashboard security surfaces
This commit is contained in:
@@ -8,6 +8,10 @@ import {
|
||||
verifySessionCookieValue,
|
||||
} from "@/app/lib/controlAuth"
|
||||
import { updateLocalAuthCredentials } from "@/app/lib/controlAuthConfig"
|
||||
import { checkRateLimit, clearRateLimit, rateLimitKey, recordRateLimitFailure } from "@/app/lib/rateLimit"
|
||||
|
||||
const SETUP_WINDOW_MS = 15 * 60 * 1000
|
||||
const SETUP_MAX_ATTEMPTS = 8
|
||||
|
||||
function validPassword(password: string) {
|
||||
return password.length >= 8
|
||||
@@ -19,6 +23,15 @@ export async function POST(request: NextRequest) {
|
||||
const password = typeof body?.password === "string" ? body.password : ""
|
||||
const currentPassword = typeof body?.currentPassword === "string" ? body.currentPassword : ""
|
||||
const recoveryToken = typeof body?.recoveryToken === "string" ? body.recoveryToken : ""
|
||||
const limitKey = rateLimitKey(request, "auth:setup")
|
||||
const limit = checkRateLimit(limitKey, SETUP_MAX_ATTEMPTS, SETUP_WINDOW_MS)
|
||||
|
||||
if (limit.limited) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Too many setup attempts. Try again shortly." },
|
||||
{ status: 429, headers: { "retry-after": String(limit.retryAfterSeconds) } },
|
||||
)
|
||||
}
|
||||
|
||||
if (!validPassword(password)) {
|
||||
return NextResponse.json({ ok: false, error: "Password must be at least 8 characters." }, { status: 400 })
|
||||
@@ -30,9 +43,11 @@ export async function POST(request: NextRequest) {
|
||||
const firstRun = !settings.configured
|
||||
|
||||
if (!firstRun && !signedIn && !currentPasswordOk && !recoveryOk) {
|
||||
recordRateLimitFailure(limitKey, SETUP_WINDOW_MS)
|
||||
return NextResponse.json({ ok: false, error: "Current password or recovery token required." }, { status: 401 })
|
||||
}
|
||||
|
||||
clearRateLimit(limitKey)
|
||||
const result = await updateLocalAuthCredentials(password)
|
||||
const response = NextResponse.json({
|
||||
ok: true,
|
||||
|
||||
Reference in New Issue
Block a user