-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity-utils.js
More file actions
340 lines (289 loc) · 11.4 KB
/
security-utils.js
File metadata and controls
340 lines (289 loc) · 11.4 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Security and Validation Utilities
// This file contains advanced security measures and validation functions
class SecurityManager {
constructor() {
this.rateLimitData = new Map();
this.suspiciousIPs = new Set();
this.sessionToken = this.generateSessionToken();
}
// Generate unique session token for CSRF protection
generateSessionToken() {
const array = new Uint8Array(32);
crypto.getRandomValues(array);
return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
}
// Check rate limiting
checkRateLimit(identifier = 'default') {
const now = Date.now();
const limit = this.rateLimitData.get(identifier) || { count: 0, lastReset: now };
// Reset counter every hour
if (now - limit.lastReset > 3600000) {
limit.count = 0;
limit.lastReset = now;
}
// Check if limit exceeded
if (limit.count >= 5) {
this.logSecurityEvent('RATE_LIMIT_EXCEEDED', { identifier, count: limit.count });
return false;
}
// Increment counter
limit.count++;
this.rateLimitData.set(identifier, limit);
return true;
}
// Advanced input sanitization
sanitizeInput(input, type = 'default') {
if (typeof input !== 'string') return '';
let sanitized = input.trim();
// Remove null bytes
sanitized = sanitized.replace(/\0/g, '');
// Type-specific sanitization
switch (type) {
case 'name':
// Allow only letters, spaces, hyphens, and apostrophes
sanitized = sanitized.replace(/[^a-zA-Z\s'\-]/g, '');
break;
case 'email':
// Basic email character allowlist
sanitized = sanitized.replace(/[^a-zA-Z0-9@.\-_]/g, '');
break;
case 'phone':
// Allow only digits, spaces, parentheses, hyphens, and plus
sanitized = sanitized.replace(/[^0-9\s()\-+]/g, '');
break;
case 'regNumber':
// VIT registration number format
sanitized = sanitized.replace(/[^0-9A-Za-z]/g, '').toUpperCase();
break;
case 'text':
// Remove HTML tags and dangerous characters (but allow common punctuation)
sanitized = sanitized.replace(/<[^>]*>/g, '');
sanitized = sanitized.replace(/[<>\"'%;()]/g, ''); // Removed & and + from removal list
break;
default:
// General sanitization (allow common punctuation)
sanitized = sanitized.replace(/[<>\"'%;()]/g, ''); // Removed & and + from removal list
}
return sanitized;
}
// Validate input against patterns and rules
validateField(value, fieldName, rules) {
const errors = [];
// Required field check
if (rules.required && (!value || value.trim().length === 0)) {
errors.push(`${fieldName} is required`);
return errors;
}
// Skip further validation if field is empty and not required
if (!value || value.trim().length === 0) {
return errors;
}
// Length validation
if (rules.minLength && value.length < rules.minLength) {
errors.push(`${fieldName} must be at least ${rules.minLength} characters`);
}
if (rules.maxLength && value.length > rules.maxLength) {
errors.push(`${fieldName} must be no more than ${rules.maxLength} characters`);
}
// Pattern validation
if (rules.pattern && !rules.pattern.test(value)) {
errors.push(`${fieldName} format is invalid`);
}
// Custom validation for specific fields
switch (fieldName.toLowerCase()) {
case 'email':
if (!this.isValidEmail(value)) {
errors.push('Email address is invalid');
}
break;
case 'regnumber':
if (!this.isValidRegNumber(value)) {
errors.push('Registration number must follow VIT format (e.g., 22BCE1234)');
}
break;
case 'phone':
if (!this.isValidPhone(value)) {
errors.push('Phone number is invalid');
}
break;
}
return errors;
}
// Email validation with comprehensive checks
isValidEmail(email) {
// Basic format check
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) return false;
// Length check
if (email.length > 254) return false;
// Check for consecutive dots
if (email.includes('..')) return false;
// Check domain part
const [local, domain] = email.split('@');
if (local.length > 64 || domain.length > 253) return false;
return true;
}
// VIT registration number validation
isValidRegNumber(regNumber) {
const regRegex = /^[0-9]{2}[A-Z]{3}[0-9]{4}$/;
return regRegex.test(regNumber);
}
// Phone number validation
isValidPhone(phone) {
// Remove all non-digit characters for validation
const digits = phone.replace(/\D/g, '');
// Check for valid length (10-15 digits)
if (digits.length < 10 || digits.length > 15) return false;
// Check if it's all the same digit (likely fake)
if (new Set(digits).size === 1) return false;
return true;
}
// Detect suspicious patterns
detectSuspiciousActivity(formData) {
const suspiciousPatterns = [
// SQL injection patterns (more specific)
/(\b(SELECT|INSERT|UPDATE|DELETE|DROP|CREATE|ALTER|EXEC|UNION)\s+\w+)/i,
// XSS patterns
/<script[^>]*>.*?<\/script>/gi,
/javascript:/i,
/on\w+\s*=/i,
// Command injection patterns (more specific - avoid false positives)
/(\||&|;|`|\$\(){2,}/, // Multiple consecutive suspicious chars
/(;\s*(rm|del|format|shutdown|reboot))/i, // Dangerous commands
// Path traversal patterns
/\.\.\//g,
// Excessive repetition (spam)
/(.)\1{15,}/ // Increased threshold from 10 to 15
];
for (const [field, value] of Object.entries(formData)) {
if (typeof value === 'string') {
for (const pattern of suspiciousPatterns) {
if (pattern.test(value)) {
console.warn('Suspicious pattern detected:', {
field,
pattern: pattern.toString(),
value: value.substring(0, 100),
fullValue: value
});
this.logSecurityEvent('SUSPICIOUS_ACTIVITY', {
field,
pattern: pattern.toString(),
value: value.substring(0, 100)
});
return true;
}
}
}
}
return false;
}
// Log security events
logSecurityEvent(eventType, details) {
const logEntry = {
timestamp: new Date().toISOString(),
type: eventType,
details,
userAgent: navigator.userAgent,
sessionToken: this.sessionToken
};
// In a real application, this would send to a security monitoring service
console.warn('Security Event:', logEntry);
// Store in localStorage for demo purposes
try {
const existingLogs = JSON.parse(localStorage.getItem('securityLogs') || '[]');
existingLogs.push(logEntry);
// Keep only last 100 logs
if (existingLogs.length > 100) {
existingLogs.splice(0, existingLogs.length - 100);
}
localStorage.setItem('securityLogs', JSON.stringify(existingLogs));
} catch (error) {
console.error('Failed to log security event:', error);
}
}
// Honeypot field validation (trap for bots)
validateHoneypot(honeypotValue) {
// Honeypot field should always be empty
if (honeypotValue && honeypotValue.trim() !== '') {
this.logSecurityEvent('BOT_DETECTED', { honeypotValue });
return false;
}
return true;
}
// Generate application ID with security considerations
generateApplicationId() {
const timestamp = Date.now();
const random = crypto.getRandomValues(new Uint8Array(8));
const randomHex = Array.from(random, byte => byte.toString(16).padStart(2, '0')).join('');
return `APP_${timestamp}_${randomHex.toUpperCase()}`;
}
// Validate file uploads (if implemented later)
validateFile(file) {
const allowedTypes = ['image/jpeg', 'image/png', 'application/pdf'];
const maxSize = 5 * 1024 * 1024; // 5MB
if (!allowedTypes.includes(file.type)) {
return { valid: false, error: 'File type not allowed' };
}
if (file.size > maxSize) {
return { valid: false, error: 'File size too large' };
}
return { valid: true };
}
// Clean up old rate limit data
cleanupRateLimitData() {
const now = Date.now();
for (const [key, data] of this.rateLimitData.entries()) {
if (now - data.lastReset > 3600000) {
this.rateLimitData.delete(key);
}
}
}
}
// Export singleton instance (make available globally)
window.securityManager = new SecurityManager();
// Additional utility functions (make available globally)
window.securityUtils = {
// Generate secure random string
generateSecureRandom: (length = 32) => {
const array = new Uint8Array(length);
crypto.getRandomValues(array);
return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
},
// Debounce function for input validation
debounce: (func, wait) => {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
},
// Check if running in secure context
isSecureContext: () => {
return window.isSecureContext || location.protocol === 'https:';
},
// Fingerprint browser for additional security
generateFingerprint: () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.textBaseline = 'top';
ctx.font = '14px Arial';
ctx.fillText('Browser fingerprint', 2, 2);
return {
userAgent: navigator.userAgent,
language: navigator.language,
platform: navigator.platform,
screen: `${screen.width}x${screen.height}`,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
canvas: canvas.toDataURL(),
timestamp: Date.now()
};
}
};
// Clean up rate limit data every hour
setInterval(() => {
securityManager.cleanupRateLimitData();
}, 3600000);