From 2ff493d804a2e0e3903e48c5f2f039384b955911 Mon Sep 17 00:00:00 2001 From: Joshua Ryder Date: Wed, 5 Nov 2025 17:36:31 -0500 Subject: [PATCH] feat: Implement comprehensive security hardening MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- docker-compose.yml | 8 +- nuxt.config.ts | 12 +- pages/users.vue | 71 +++++- plugins/csrf.client.ts | 51 ++++ server/api/auth/login.post.ts | 34 ++- server/api/auth/logout.post.ts | 10 +- server/api/auth/register.post.ts | 17 +- server/api/auth/reset-password.post.ts | 25 +- server/api/auth/verify-reset-code.post.ts | 3 +- server/api/profile/update.put.ts | 22 +- server/api/users/password/[id].put.ts | 31 ++- server/api/users/unlock/[id].post.ts | 58 +++++ server/middleware/csrf.ts | 22 ++ server/utils/csrf.ts | 124 ++++++++++ server/utils/database.ts | 275 ++++++++++++++++++++-- server/utils/email.ts | 52 +++- 16 files changed, 754 insertions(+), 61 deletions(-) create mode 100644 plugins/csrf.client.ts create mode 100644 server/api/users/unlock/[id].post.ts create mode 100644 server/middleware/csrf.ts create mode 100644 server/utils/csrf.ts diff --git a/docker-compose.yml b/docker-compose.yml index c8551a7..3318ee2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,10 +8,12 @@ services: - ./data:/app/data environment: - NODE_ENV=production - - AUTH_SECRET=change-this-secret-in-production-please - SITE_URL=https://nlcc.rydertech.us - - ADMIN_USERNAME=admin - - ADMIN_PASSWORD=Admin123! + # Optional: Customize admin username (default: "admin") + # - ADMIN_USERNAME=admin + # Optional: Set custom admin password (otherwise auto-generated) + # - ADMIN_PASSWORD=your-secure-password + # Email configuration for password resets and notifications - EMAIL_HOST=smtp.example.com - EMAIL_PORT=587 - EMAIL_USER=noreply@example.com diff --git a/nuxt.config.ts b/nuxt.config.ts index 57d38b4..57276ef 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -27,16 +27,20 @@ export default defineNuxtConfig({ css: ['~/assets/css/main.css'], runtimeConfig: { - authSecret: process.env.AUTH_SECRET || 'change-this-secret-in-production', + // AUTH_SECRET is now auto-generated and stored in database + // Only used if explicitly provided (for advanced users who want manual control) + authSecret: process.env.AUTH_SECRET || '', + // Admin credentials - auto-generated on first launch if not provided adminUsername: process.env.ADMIN_USERNAME || 'admin', - adminPassword: process.env.ADMIN_PASSWORD || 'admin123', + adminPassword: process.env.ADMIN_PASSWORD || '', + // Email configuration emailHost: process.env.EMAIL_HOST || 'smtp.example.com', emailPort: process.env.EMAIL_PORT || '587', emailUser: process.env.EMAIL_USER || 'noreply@example.com', - emailPassword: process.env.EMAIL_PASSWORD || 'your-email-password', + emailPassword: process.env.EMAIL_PASSWORD || '', emailFrom: process.env.EMAIL_FROM || 'New Life Christian Church ', public: { - siteUrl: process.env.SITE_URL || 'https://newlife-christian.com' + siteUrl: process.env.SITE_URL || 'http://localhost:3000' } } }) diff --git a/pages/users.vue b/pages/users.vue index ffb2e24..eb8deee 100644 --- a/pages/users.vue +++ b/pages/users.vue @@ -57,6 +57,9 @@ Role + + Status + Actions @@ -74,14 +77,46 @@
{{ user.last_name || '-' }}
- {{ user.is_admin ? 'Admin' : 'User' }} + +
+ + 🔒 Locked + + + {{ user.failed_login_attempts }} failed attempt{{ user.failed_login_attempts !== 1 ? 's' : '' }} + + + Active + + + Until {{ formatLockTime(user.locked_until!) }} + +
+ +