This document outlines the comprehensive security measures implemented to protect against malicious file uploads and prevent attacks on the File Vault AI system.
The application implements multiple layers of security validation to prevent file size spoofing, oversized uploads, and other file-based attacks.
- Location:
src/types/file.tsand client components - Purpose: Immediate user feedback and basic filtering
- Limitations: Can be bypassed by attackers
- Implementation:
- File size check against
MAX_FILE_SIZE(10MB) - File type validation against
SUPPORTED_FILE_TYPES
- File size check against
- Location:
middleware.tsandsrc/utils/uploadMiddleware.ts - Purpose: Early request termination before processing
- Features:
- Content-Length header validation
- Immediate 413 (Payload Too Large) response for oversized requests
- Connection termination to prevent bandwidth waste
- Security headers injection
- Location:
src/utils/fileUtils.ts-validateFileStream() - Purpose: Validate actual file content during upload
- Features:
- Real-time byte counting during file streaming
- Upload termination if size exceeds limits
- Detection of file header spoofing
- Comparison between claimed size and actual size
- Location:
src/utils/fileUtils.ts-saveFile() - Purpose: Final validation during file system write
- Features:
- Stream-based file writing with size monitoring
- Automatic cleanup of partial files on failure
- Defense-in-depth validation
// Maximum file size: 10MB
export const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MBexport const SUPPORTED_FILE_TYPES: SupportedFileType[] = [
"image/jpeg", "image/png", "image/gif", "image/webp",
"application/pdf", "text/plain",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
];The system applies the following security headers to file upload endpoints:
X-Content-Type-Options: nosniff- Prevents MIME type sniffingX-Frame-Options: DENY- Prevents clickjackingX-XSS-Protection: 1; mode=block- Enables XSS protectionCache-Control: no-store, no-cache, must-revalidate- Prevents caching of sensitive upload responses
Attack: Attacker sends a small Content-Length header but uploads a large file Protection:
- Middleware validates Content-Length if present
- Stream validation counts actual bytes during upload
- Upload is terminated immediately when size limit is exceeded
- Mismatch detection between claimed and actual file size
Attack: Attacker attempts to overwhelm server with large files Protection:
- Early termination at middleware level
- Connection closure to prevent bandwidth waste
- Rate limiting on upload endpoints
- Stream-based processing to limit memory usage
Attack: Attacker uploads executable or dangerous file types Protection:
- Strict whitelist of allowed MIME types
- File extension validation
- Content-Type header validation
Attack: Large files causing server memory issues Protection:
- Stream-based file processing (no full buffering)
- Chunked reading and writing
- Immediate cleanup of failed uploads
Returned when:
- Content-Length exceeds maximum allowed size
- Actual file size exceeds limit during streaming
- File validation fails due to size constraints
Returned when:
- Invalid file type
- Missing file in request
- File header spoofing detected
Returned when:
- Rate limit exceeded for uploads
- Includes retry-after information
- Always use
validateFileStream()instead of basicvalidateFile()for server-side validation - Handle file streams properly and clean up resources
- Log security events for monitoring
- Test with various file sizes and types
- Monitor upload patterns for suspicious activity
- Set up alerts for repeated 413 errors from same IP
- Regularly review file upload logs
- Consider implementing IP-based rate limiting
To test the security system:
- Size Spoofing Test: Create a small file, modify Content-Length header to claim larger size
- Oversized Upload Test: Attempt to upload files larger than 10MB
- Invalid Type Test: Try uploading executable files or unsupported formats
- Rate Limit Test: Perform multiple rapid uploads from same client
// next.config.ts
api: {
bodyParser: {
sizeLimit: "10mb",
},
}MAX_UPLOAD_SIZE: Override default file size limitUPLOAD_RATE_LIMIT: Configure rate limiting parameters
Key metrics to monitor:
- Upload success/failure rates
- 413 error frequency
- File size distribution
- Upload duration patterns
- Rate limit violations
- Network Level: Consider implementing additional DDoS protection
- File Scanning: Consider adding virus/malware scanning for uploaded files
- Storage: Ensure uploaded files are stored outside web root
- Access Control: Implement proper file access permissions
- Audit Trail: Log all file operations for security auditing
- Regularly review and update supported file types
- Monitor for new attack vectors
- Update security headers as needed
- Test security measures after any upload-related changes
- Keep dependencies updated for security patches