Harden dashboard security surfaces
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import { createSessionCookieValue, getAuthSettings, sessionCookieOptions, verifyPassword } from "@/app/lib/controlAuth"
|
||||
import { checkRateLimit, clearRateLimit, rateLimitKey, recordRateLimitFailure } from "@/app/lib/rateLimit"
|
||||
|
||||
const AUTH_WINDOW_MS = 5 * 60 * 1000
|
||||
const AUTH_MAX_ATTEMPTS = 8
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const settings = getAuthSettings()
|
||||
@@ -13,11 +17,22 @@ export async function POST(request: NextRequest) {
|
||||
const body = await request.json().catch(() => ({}))
|
||||
const password = typeof body?.password === "string" ? body.password : ""
|
||||
const nextPath = typeof body?.next === "string" && body.next.startsWith("/") ? body.next : "/"
|
||||
const limitKey = rateLimitKey(request, "auth:login")
|
||||
const limit = checkRateLimit(limitKey, AUTH_MAX_ATTEMPTS, AUTH_WINDOW_MS)
|
||||
|
||||
if (limit.limited) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Too many login attempts. Try again shortly." },
|
||||
{ status: 429, headers: { "retry-after": String(limit.retryAfterSeconds) } },
|
||||
)
|
||||
}
|
||||
|
||||
if (!await verifyPassword(password)) {
|
||||
recordRateLimitFailure(limitKey, AUTH_WINDOW_MS)
|
||||
return NextResponse.json({ ok: false, error: "Invalid password" }, { status: 401 })
|
||||
}
|
||||
|
||||
clearRateLimit(limitKey)
|
||||
const response = NextResponse.json({ ok: true, next: nextPath })
|
||||
response.cookies.set(settings.cookieName, await createSessionCookieValue(), sessionCookieOptions)
|
||||
return response
|
||||
|
||||
@@ -1,20 +1,35 @@
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import { createSessionCookieValue, getAuthSettings, sessionCookieOptions, verifyRecoveryToken } from "@/app/lib/controlAuth"
|
||||
import { updateLocalAuthCredentials } from "@/app/lib/controlAuthConfig"
|
||||
import { checkRateLimit, clearRateLimit, rateLimitKey, recordRateLimitFailure } from "@/app/lib/rateLimit"
|
||||
|
||||
const RECOVERY_WINDOW_MS = 15 * 60 * 1000
|
||||
const RECOVERY_MAX_ATTEMPTS = 5
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const body = await request.json().catch(() => ({}))
|
||||
const token = typeof body?.recoveryToken === "string" ? body.recoveryToken : ""
|
||||
const password = typeof body?.password === "string" ? body.password : ""
|
||||
const limitKey = rateLimitKey(request, "auth:recover")
|
||||
const limit = checkRateLimit(limitKey, RECOVERY_MAX_ATTEMPTS, RECOVERY_WINDOW_MS)
|
||||
|
||||
if (limit.limited) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: "Too many recovery attempts. Try again shortly." },
|
||||
{ status: 429, headers: { "retry-after": String(limit.retryAfterSeconds) } },
|
||||
)
|
||||
}
|
||||
|
||||
if (password.length < 8) {
|
||||
return NextResponse.json({ ok: false, error: "Password must be at least 8 characters." }, { status: 400 })
|
||||
}
|
||||
|
||||
if (!await verifyRecoveryToken(token)) {
|
||||
recordRateLimitFailure(limitKey, RECOVERY_WINDOW_MS)
|
||||
return NextResponse.json({ ok: false, error: "Invalid recovery token." }, { status: 401 })
|
||||
}
|
||||
|
||||
clearRateLimit(limitKey)
|
||||
const result = await updateLocalAuthCredentials(password)
|
||||
const settings = getAuthSettings()
|
||||
const response = NextResponse.json({
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { uploadSandboxFile } from "@/app/lib/sandboxFiles"
|
||||
import { assertRequestContentLength, uploadSandboxFile } from "@/app/lib/sandboxFiles"
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
@@ -7,6 +7,7 @@ export async function POST(
|
||||
) {
|
||||
try {
|
||||
const { sandboxId } = await params
|
||||
assertRequestContentLength(request)
|
||||
const form = await request.formData()
|
||||
const file = form.get("file")
|
||||
const rawPath = form.get("path")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { restoreSandboxArchive } from "@/app/lib/sandboxFiles"
|
||||
import { assertRequestContentLength, restoreSandboxArchive } from "@/app/lib/sandboxFiles"
|
||||
import { recordActivity } from "@/app/lib/activityLog"
|
||||
|
||||
export async function POST(
|
||||
@@ -8,6 +8,7 @@ export async function POST(
|
||||
) {
|
||||
try {
|
||||
const { sandboxId } = await params
|
||||
assertRequestContentLength(request)
|
||||
const form = await request.formData()
|
||||
const file = form.get("archive")
|
||||
const rawTargetPath = form.get("targetPath")
|
||||
|
||||
Reference in New Issue
Block a user