Repository navigation
Expand file tree
/
Copy pathsetup-security.js
More file actions
154 lines (131 loc) Β· 5.63 KB
/
Copy pathsetup-security.js
File metadata and controls
154 lines (131 loc) Β· 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env node
/**
* Ryvie Security Setup Script
* This script helps configure security features for the Ryvie application
*/
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
console.log('π Ryvie Security Setup');
console.log('========================\n');
// Generate secure keys
function generateSecureKey(length = 32) {
return crypto.randomBytes(length).toString('base64');
}
function generateJWTSecret() {
return crypto.randomBytes(64).toString('hex');
}
// Check if .env file exists
const envPath = path.join(__dirname, 'Ryvie-Back', '.env');
const envExists = fs.existsSync(envPath);
console.log('π Security Configuration Checklist:');
console.log('====================================\n');
// 1. Generate encryption keys
console.log('1. π Encryption Keys');
const encryptionKey = generateSecureKey(32);
const jwtEncryptionKey = generateSecureKey(32);
const jwtSecret = generateJWTSecret();
console.log(' Generated new encryption keys:');
console.log(` ENCRYPTION_KEY=${encryptionKey}`);
console.log(` JWT_ENCRYPTION_KEY=${jwtEncryptionKey}`);
console.log(` JWT_SECRET=${jwtSecret}`);
console.log(' β
Add these to your .env file\n');
// 2. Check dependencies
console.log('2. π¦ Required Dependencies');
const backendPackageJson = path.join(__dirname, 'Ryvie-Back', 'package.json');
if (fs.existsSync(backendPackageJson)) {
const pkg = JSON.parse(fs.readFileSync(backendPackageJson, 'utf8'));
const requiredDeps = ['express-rate-limit', 'helmet', 'bcrypt'];
const missing = requiredDeps.filter(dep => !pkg.dependencies?.[dep] && !pkg.devDependencies?.[dep]);
if (missing.length > 0) {
console.log(' β Missing dependencies:');
missing.forEach(dep => console.log(` - ${dep}`));
console.log(` Run: cd Ryvie-Back && npm install ${missing.join(' ')}`);
} else {
console.log(' β
All security dependencies installed');
}
} else {
console.log(' β οΈ Could not check package.json');
}
console.log('');
// 3. Environment configuration
console.log('3. βοΈ Environment Configuration');
if (envExists) {
const envContent = fs.readFileSync(envPath, 'utf8');
const hasEncryptionKey = envContent.includes('ENCRYPTION_KEY=');
const hasJWTSecret = envContent.includes('JWT_SECRET=');
if (hasEncryptionKey && hasJWTSecret) {
console.log(' β
Security keys found in .env');
} else {
console.log(' β Missing security keys in .env');
console.log(' Add the generated keys above to your .env file');
}
} else {
console.log(' β .env file not found');
console.log(' Create .env file in Ryvie-Back/ directory');
}
console.log('');
// 4. Redis configuration
console.log('4. ποΈ Redis Configuration');
console.log(' β
Redis client configured for brute force protection');
console.log(' β
Token allowlist using Redis');
console.log(' Make sure Redis server is running\n');
// 5. Security features summary
console.log('5. π‘οΈ Security Features Implemented');
console.log(' β
Rate limiting (5 attempts per 15 minutes)');
console.log(' β
Brute force protection with Redis tracking');
console.log(' β
LDAP injection prevention');
console.log(' β
Secure JWT token management');
console.log(' β
Client-side attempt tracking');
console.log(' β
Helmet.js security headers');
console.log(' β
Input sanitization and validation');
console.log(' β
Secure Electron window management\n');
// 6. Testing recommendations
console.log('6. π§ͺ Testing Security Features');
console.log(' Test brute force protection:');
console.log(' - Try logging in with wrong credentials 6+ times');
console.log(' - Verify blocking message appears');
console.log(' - Check Redis for brute force keys');
console.log('');
console.log(' Test rate limiting:');
console.log(' - Make multiple rapid API requests');
console.log(' - Verify 429 responses after limits');
console.log('');
// 7. Production recommendations
console.log('7. π Production Deployment');
console.log(' β Change default encryption keys');
console.log(' β Enable HTTPS');
console.log(' β Configure proper CORS origins');
console.log(' β Set up log monitoring');
console.log(' β Regular security updates');
console.log('');
// Generate sample .env additions
console.log('8. π Sample .env Configuration');
console.log(' Add these lines to your Ryvie-Back/.env file:');
console.log(' =====================================');
console.log(` ENCRYPTION_KEY=${encryptionKey}`);
console.log(` JWT_ENCRYPTION_KEY=${jwtEncryptionKey}`);
console.log(` JWT_SECRET=${jwtSecret}`);
console.log(' AUTH_RATE_LIMIT_WINDOW_MS=900000');
console.log(' AUTH_RATE_LIMIT_MAX_ATTEMPTS=5');
console.log(' API_RATE_LIMIT_WINDOW_MS=900000');
console.log(' API_RATE_LIMIT_MAX_REQUESTS=100');
console.log(' BRUTE_FORCE_MAX_ATTEMPTS=5');
console.log(' BRUTE_FORCE_BLOCK_DURATION_MS=900000');
console.log(' ENABLE_SECURITY_LOGGING=true');
console.log('');
console.log('π Security setup complete!');
console.log('π See SECURITY.md for detailed documentation');
console.log('π Review all configurations before production deployment');
// Optional: Write keys to a secure file
const keysFile = path.join(__dirname, 'security-keys.txt');
const keysContent = `# Generated Security Keys - KEEP SECURE!
# Generated: ${new Date().toISOString()}
ENCRYPTION_KEY=${encryptionKey}
JWT_ENCRYPTION_KEY=${jwtEncryptionKey}
JWT_SECRET=${jwtSecret}
# Add these to your .env file and then DELETE this file!
`;
fs.writeFileSync(keysFile, keysContent);
console.log(`\nπ Keys saved to: ${keysFile}`);
console.log('β IMPORTANT: Delete this file after copying keys to .env!');