Spaces:
Sleeping
Sleeping
File size: 3,012 Bytes
525d39f b63a85d 525d39f b63a85d 525d39f | 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 | import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { randomUUID } from 'crypto';
/**
* Security middleware for request tracking and security headers
* Phase 3 Security Audit
*/
@Injectable()
export class SecurityMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction): void {
// Generate or use existing request ID for tracing
const requestId = (req.headers['x-request-id'] as string) || randomUUID();
req.headers['x-request-id'] = requestId;
res.setHeader('X-Request-ID', requestId);
// Add additional security headers
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('X-XSS-Protection', '1; mode=block');
// Remove server header to avoid fingerprinting
res.removeHeader('X-Powered-By');
next();
}
}
/**
* Sanitize input strings to prevent XSS and injection attacks
*/
export function sanitizeString(input: string): string {
if (typeof input !== 'string') return input;
return input
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/\//g, '/')
.replace(/\\/g, '\')
.replace(/`/g, '`');
}
/**
* Validate that a string matches safe patterns
*/
export function isValidSessionName(name: string): boolean {
// Only allow alphanumeric, hyphens, underscores, 3-50 chars
return /^[a-zA-Z0-9_-]{3,50}$/.test(name);
}
/**
* Validate WhatsApp chat ID format
*/
export function isValidChatId(chatId: string): boolean {
// Format: handle various JID types (c.us, g.us, broadcast, newsletter, lid, etc)
return /^[a-zA-Z0-9.\-_]+@[a-zA-Z0-9.\-_]+$/.test(chatId);
}
/**
* Validate phone number format
*/
export function isValidPhoneNumber(phone: string): boolean {
// Allow 5-15 digits to accommodate short codes and international numbers
return /^\d{5,15}$/.test(phone);
}
/**
* Mask sensitive data for logging
*/
export function maskSensitiveData(data: string, visibleChars: number = 4): string {
if (!data || data.length <= visibleChars) return '***';
return data.substring(0, visibleChars) + '*'.repeat(Math.min(data.length - visibleChars, 8));
}
/**
* Security configuration constants
*/
export const SecurityConfig = {
// Rate limiting defaults
rateLimit: {
short: { ttl: 1000, limit: 10 },
medium: { ttl: 60000, limit: 100 },
long: { ttl: 3600000, limit: 1000 },
},
// Input validation limits
inputLimits: {
maxTextLength: 65536,
maxSessionNameLength: 50,
maxUrlLength: 2048,
maxArrayItems: 100,
},
// Session limits
sessionLimits: {
maxSessionsPerInstance: 100,
sessionIdleTimeout: 86400000, // 24 hours
qrCodeTimeout: 120000, // 2 minutes
},
// API key configuration
apiKey: {
prefix: 'owa_k1_',
hashAlgorithm: 'sha256',
randomBytes: 32,
prefixLength: 12,
},
} as const;
|