Files
picpeak/backend/scripts/test-enhanced-auth-docker.js
T
paul e35ac6a41c
Test Gitea Actions / test (push) Successful in 20s
continuous-integration/drone/push Build is passing
feat: implement critical security fixes for SQL injection and authentication vulnerabilities
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>
2025-07-13 00:40:05 +02:00

75 lines
2.5 KiB
JavaScript

#!/usr/bin/env node
/**
* Test enhanced authentication features in Docker
*/
const { db } = require('../src/database/db');
const {
checkAccountLockout,
trackFailedAttempt,
trackSuccessfulLogin
} = require('../src/utils/authSecurity');
console.log('=== Testing Enhanced Auth Features ===\n');
async function testAuthFeatures() {
try {
// Test 1: Check if tables exist
console.log('1. Checking database tables...');
const loginAttempts = await db('login_attempts').count().first();
console.log('✓ login_attempts table exists');
// Test 2: Test failed attempt tracking
console.log('\n2. Testing failed attempt tracking...');
await trackFailedAttempt('test-user', '127.0.0.1', 'Test User Agent');
const attempts = await db('login_attempts')
.where('identifier', 'test-user')
.count()
.first();
console.log(`✓ Failed attempt tracked (${attempts.count} total)`);
// Test 3: Test lockout check
console.log('\n3. Testing lockout detection...');
// Add 4 more failures to trigger lockout
for (let i = 0; i < 4; i++) {
await trackFailedAttempt('test-user', '127.0.0.1', 'Test User Agent');
}
const lockoutStatus = await checkAccountLockout('test-user');
console.log(`✓ Lockout check works: ${lockoutStatus.isLocked ? 'LOCKED' : 'NOT LOCKED'}`);
if (lockoutStatus.isLocked) {
console.log(` Remaining lockout time: ${lockoutStatus.remainingTime} seconds`);
}
// Test 4: Test successful login tracking
console.log('\n4. Testing successful login tracking...');
await trackSuccessfulLogin('test-user', '127.0.0.1', 'Test User Agent');
console.log('✓ Successful login tracked');
// Test 5: Check if auth routes load
console.log('\n5. Testing enhanced auth routes...');
try {
const authRoutes = require('../src/routes/auth-enhanced');
console.log('✓ Enhanced auth routes load successfully');
} catch (e) {
console.log('✗ Error loading enhanced auth routes:', e.message);
}
// Clean up test data
await db('login_attempts').where('identifier', 'test-user').delete();
console.log('\n✓ Test data cleaned up');
console.log('\n✅ Enhanced auth features are working correctly!');
console.log('\nNext step: Update server.js to use enhanced auth routes');
} catch (error) {
console.error('\n❌ Test failed:', error);
} finally {
await db.destroy();
}
}
testAuthFeatures();