e35ac6a41c
Security Enhancements: - Fix SQL injection vulnerabilities by replacing whereRaw queries with parameterized queries - Add LIKE pattern escaping to prevent SQL injection in search functionality - Implement account lockout protection (5 failed attempts = 30 min lockout) - Add comprehensive login attempt tracking and audit trail - Enhance JWT tokens with issuer validation, IP tracking, and password change detection - Add logout endpoint and session management - Prevent user enumeration with generic error messages Database Changes: - Add login_attempts table for authentication tracking - Add security columns to admin_users (password_changed_at, last_login_ip, two_factor_enabled) New Security Features: - Brute force protection with configurable lockout duration - Automatic cleanup of old login attempts - Enhanced authentication middleware with stricter validation - Monitoring scripts for security health checks All fixes are backward compatible and production-ready with rollback plans included. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
4.6 KiB
4.6 KiB
Security Fixes Deployment Complete ✅
Current Protection Status
🛡️ FULLY PROTECTED Against:
-
SQL Injection ✅
- All
whereRawqueries replaced with parameterized queries - LIKE patterns properly escaped
- Input validation for all user inputs
- Status: ACTIVE & PROTECTING
- All
-
Brute Force Attacks ✅
- Account lockout after 5 failed attempts
- 30-minute lockout duration
- IP and user agent tracking
- Status: ACTIVE & PROTECTING
-
User Enumeration ✅
- Generic error messages for all auth failures
- Returns "Invalid credentials" consistently
- Status: ACTIVE & PROTECTING
-
Session Security ✅
- Enhanced JWT with issuer validation
- IP tracking in tokens
- Password change detection
- Logout endpoint functional
- Status: ACTIVE & PROTECTING
-
Audit Trail ✅
- All login attempts tracked in database
- Success/failure logging with timestamps
- IP address and user agent recording
- Status: ACTIVE & LOGGING
What Was Done
Database Changes
- ✅ Created
login_attemptstable for tracking - ✅ Added security columns to
admin_users:password_changed_atlast_login_iptwo_factor_enabledtwo_factor_secret
Code Changes
- ✅ SQL injection fixes in 3 files
- ✅ Enhanced auth middleware deployed
- ✅ Enhanced auth routes active
- ✅ Security utilities in place
- ✅ Cleanup job running
Files Modified/Created
backend/
├── src/
│ ├── utils/
│ │ ├── sqlSecurity.js ✅
│ │ └── authSecurity.js ✅
│ ├── middleware/
│ │ └── auth-enhanced.js ✅
│ └── routes/
│ ├── auth-enhanced.js ✅
│ ├── adminDashboard.js ✅ (SQL fixes)
│ ├── adminEvents.js ✅ (SQL fixes)
│ └── adminPhotos.js ✅ (SQL fixes)
└── server.js ✅ (using enhanced auth)
Monitoring Commands
Check Login Attempts
docker exec wedding-photo-sharing-backend-1 node -e "
const {db} = require('./src/database/db');
db('login_attempts')
.orderBy('attempt_time', 'desc')
.limit(10)
.then(attempts => {
console.log('Recent login attempts:');
attempts.forEach(a => {
console.log(\`\${a.attempt_time} - \${a.identifier} - \${a.success ? 'SUCCESS' : 'FAILED'}\`);
});
})
.then(() => db.destroy());
"
Check Locked Accounts
docker exec wedding-photo-sharing-backend-1 node -e "
const {db} = require('./src/database/db');
db('login_attempts')
.select('identifier')
.where('success', false)
.where('attempt_time', '>', new Date(Date.now() - 15*60*1000).toISOString())
.groupBy('identifier')
.havingRaw('COUNT(*) >= 5')
.then(locked => console.log('Locked accounts:', locked))
.then(() => db.destroy());
"
Monitor Health
node scripts/monitor-auth-health.js
Rollback Plan (If Needed)
Quick Rollback
# Restore original server.js
cp server.js.backup.1752359680463 server.js
# Restart
docker-compose restart backend
Clear Lockouts
docker exec wedding-photo-sharing-backend-1 node -e "
const {db} = require('./src/database/db');
db('login_attempts').where('success', false).delete()
.then(() => console.log('All lockouts cleared'))
.then(() => db.destroy());
"
Next Steps
Immediate
- Monitor logs for any auth errors
- Watch for excessive lockouts
- Review login attempts daily
Short Term (1-2 weeks)
- Analyze login patterns
- Adjust lockout thresholds if needed
- Set up alerts for suspicious activity
Long Term
- Implement 2FA (columns already added)
- Add IP whitelisting for admins
- Implement password complexity requirements
- Add password expiration policies
Security Improvements Summary
| Vulnerability | Before | After | Impact |
|---|---|---|---|
| SQL Injection | ❌ Direct interpolation | ✅ Parameterized queries | Critical fix |
| Brute Force | ❌ Unlimited attempts | ✅ 5 attempt lockout | High impact |
| User Enum | ❌ Different errors | ✅ Generic errors | Medium impact |
| Audit Trail | ❌ No tracking | ✅ Complete logging | High value |
| Session Mgmt | ❌ Basic JWT | ✅ Enhanced validation | Medium impact |
Final Notes
- All fixes are backward compatible
- Existing sessions remain valid
- No user impact expected
- Quick rollback available
- Monitoring in place
The application is now significantly more secure with protection against common attack vectors. The enhanced authentication system provides defense-in-depth with multiple layers of security.