Security Improvements: - Auto-generate AUTH_SECRET and admin credentials on first launch - Cryptographically secure random generation - Stored in database for persistence - Logged once to container logs for admin retrieval - Implement CSRF protection with double-submit cookie pattern - Three-way validation: cookie, header, and session database - Automatic client-side injection via plugin - Server middleware for automatic validation - Zero frontend code changes required - Add session fixation prevention with automatic invalidation - Regenerate sessions on password changes - Keep current session active, invalidate others on profile password change - Invalidate ALL sessions on forgot-password reset - Invalidate ALL sessions on admin password reset - Upgrade password reset codes to 8-char alphanumeric - Increased from 1M to 1.8 trillion combinations - Uses crypto.randomInt() for cryptographic randomness - Excluded confusing characters (I, O) for better UX - Case-insensitive verification - Implement dual-layer account lockout - IP-based rate limiting (existing) - Per-account lockout: 10 attempts = 30 min lock - Automatic unlock after expiration - Admin manual unlock via UI - Visual status indicators in users table Database Changes: - Add csrf_token column to sessions table - Add failed_login_attempts and locked_until columns to users table - Add settings table for persistent AUTH_SECRET storage - All migrations backward-compatible with try-catch New Files: - server/utils/csrf.ts - CSRF protection utilities - server/middleware/csrf.ts - Automatic CSRF validation middleware - plugins/csrf.client.ts - Automatic CSRF header injection - server/api/users/unlock/[id].post.ts - Admin unlock endpoint Modified Files: - server/utils/database.ts - Core security functions and schema updates - server/utils/email.ts - Enhanced reset code generation - server/api/auth/login.post.ts - CSRF + account lockout logic - server/api/auth/register.post.ts - CSRF token generation - server/api/auth/logout.post.ts - CSRF cookie cleanup - server/api/auth/reset-password.post.ts - Session invalidation - server/api/auth/verify-reset-code.post.ts - Case-insensitive codes - server/api/profile/update.put.ts - Session invalidation on password change - server/api/users/password/[id].put.ts - Session invalidation on admin reset - pages/users.vue - Lock status display and unlock functionality - docker-compose.yml - Removed default credentials - nuxt.config.ts - Support auto-generation All changes follow OWASP best practices and are production-ready. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
import { getPasswordResetCode, resetPasswordByEmail, deletePasswordResetCode, deleteAllUserSessions, getUserByEmail } from '~/server/utils/database'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const body = await readBody(event)
|
|
const { email, code, newPassword } = body
|
|
|
|
if (!email || !code || !newPassword) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: 'Email, code, and new password are required',
|
|
})
|
|
}
|
|
|
|
// Validate password requirements
|
|
if (newPassword.length < 8) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: 'Password must be at least 8 characters long',
|
|
})
|
|
}
|
|
|
|
const hasUpperCase = /[A-Z]/.test(newPassword)
|
|
const hasLowerCase = /[a-z]/.test(newPassword)
|
|
const hasNumberOrSymbol = /[0-9!@#$%^&*(),.?":{}|<>]/.test(newPassword)
|
|
|
|
if (!hasUpperCase || !hasLowerCase || !hasNumberOrSymbol) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: 'Password must contain uppercase, lowercase, and number/symbol',
|
|
})
|
|
}
|
|
|
|
// Verify code exists and hasn't expired
|
|
// Convert to uppercase for case-insensitive comparison
|
|
const resetCode = getPasswordResetCode(email, code.toUpperCase())
|
|
|
|
if (!resetCode) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
message: 'Invalid or expired reset code',
|
|
})
|
|
}
|
|
|
|
// Get user info before resetting password
|
|
const user = getUserByEmail(email)
|
|
if (!user) {
|
|
throw createError({
|
|
statusCode: 404,
|
|
message: 'User not found',
|
|
})
|
|
}
|
|
|
|
// Reset password
|
|
resetPasswordByEmail(email, newPassword)
|
|
|
|
// SECURITY: Invalidate ALL sessions when password is reset via forgot-password flow
|
|
// User must log in again with new password
|
|
deleteAllUserSessions(user.username)
|
|
|
|
// Delete used reset code
|
|
deletePasswordResetCode(email)
|
|
|
|
return {
|
|
success: true,
|
|
message: 'Password reset successfully. Please log in with your new password.'
|
|
}
|
|
})
|