Skip to content

Security: Thomastienn/python2cpp

Security

SECURITY.md

Security Implementation Documentation

Overview

This document outlines the comprehensive security enhancements implemented for the Python2Cpp project to address critical vulnerabilities.

Security Features Implemented

1. Input Validation and Sanitization

Backend API (webbackend/src/main.py)

  • Input Size Limits: Maximum 10KB Python code input
  • AST Node Limits: Maximum 1000 AST nodes to prevent resource exhaustion
  • Pydantic Validation: Server-side input validation with size constraints
  • Dangerous Import Detection: Basic heuristic to detect potentially unsafe imports

CLI Tool (main.py)

  • File Size Validation: Maximum 50KB file size limit
  • Path Validation: Comprehensive file path validation to prevent directory traversal
  • Input Sanitization: Secure handling of user input and file paths

Frontend (webfrontend/src/App.tsx)

  • Client-side Validation: Pre-validates input before sending to server
  • File Upload Restrictions: Type and size validation for uploads
  • Content Size Checking: Validates content size after file reading

2. Enhanced Rate Limiting and Abuse Prevention

Rate Limiting

  • Reduced Limits: Changed from 10/minute to 3/minute for API endpoints
  • Per-IP Tracking: Monitors individual IP addresses for abuse patterns
  • Progressive Penalties: Implements exponential backoff with temporary bans

Abuse Prevention

  • Strike System: 5 strikes before temporary ban
  • Ban Duration: 5-minute temporary bans for repeat offenders
  • Comprehensive Logging: All security events are logged for monitoring

3. Secure File Operations

Path Security

  • Directory Traversal Protection: Validates and resolves file paths securely
  • Secure Temporary Files: Uses secure temporary file creation methods
  • Output Directory Validation: Ensures output files stay within safe boundaries

File Validation

  • Extension Checking: Only allows .py files for input
  • Size Limits: Enforces file size restrictions at multiple levels
  • Content Validation: Validates file content after reading

4. Improved Error Handling

Error Sanitization

  • Information Disclosure Prevention: Removes sensitive information from error messages
  • Debug Mode Handling: Provides detailed errors only in debug mode
  • Generic Error Messages: Uses safe, generic messages for production

Security Event Logging

  • Comprehensive Logging: All security events are logged with timestamps
  • IP Tracking: Logs IP addresses for security monitoring
  • Structured Logging: Uses consistent format for easy parsing

5. Security Headers and CORS Configuration

HTTP Security Headers

  • Content Security Policy: Implements strict CSP headers
  • XSS Protection: Enables browser XSS protection
  • Content Type Protection: Prevents MIME sniffing attacks
  • Frame Options: Prevents clickjacking attacks

CORS Configuration

  • Restricted Origins: Changed from wildcard "*" to specific allowed origins
  • Method Restrictions: Only allows necessary HTTP methods
  • Header Restrictions: Limits allowed headers for security

6. Frontend Security Enhancements

Input Validation

  • Real-time Validation: Validates input as user types
  • File Upload Security: Comprehensive file validation for uploads
  • Drag and Drop Security: Secure handling of drag and drop operations

Error Handling

  • Timeout Handling: 30-second timeout for API requests
  • Error Sanitization: Cleans error messages before display
  • User-friendly Messages: Provides clear, safe error messages

Configuration

Security Constants (py2c/utils/constants.py)

SECURITY_CONFIG = {
    'MAX_INPUT_SIZE': 10 * 1024,        # 10KB max Python code
    'MAX_AST_NODES': 1000,              # Max AST nodes
    'MAX_FILE_SIZE': 50 * 1024,         # 50KB max file size
    'API_RATE_LIMIT': '3/minute',       # API rate limit
    'CONVERSION_TIMEOUT': 30,           # Conversion timeout
    'ALLOWED_ORIGINS': [...],           # Allowed CORS origins
    'CSP_HEADER': "...",               # Content Security Policy
}

Security Utilities (py2c/utils/utils.py)

SecurityUtils Class

  • validate_input_size(): Validates input size limits
  • validate_ast_complexity(): Checks AST node count limits
  • sanitize_filename(): Prevents path traversal attacks
  • validate_file_path(): Comprehensive file path validation
  • sanitize_error_message(): Removes sensitive information from errors

TimeoutContext Class

  • Implements timeout mechanisms for long-running operations
  • Prevents resource exhaustion from infinite loops or complex operations

Testing

Security Validation Tests

  • Input size and complexity validation tests
  • Path traversal protection tests
  • Error message sanitization tests
  • CLI security feature tests

Integration Tests

  • API rate limiting verification
  • File upload security tests
  • CORS and security header validation

Backward Compatibility

All security enhancements maintain backward compatibility:

  • Existing CLI usage patterns continue to work
  • API responses maintain same structure (with added security)
  • Configuration is additive, not replacing existing functionality

Monitoring and Logging

Security Event Logging

  • All security violations are logged to security.log
  • Includes IP addresses, timestamps, and violation details
  • Structured format for easy parsing and analysis

Monitoring Recommendations

  1. Monitor security.log for suspicious patterns
  2. Set up alerts for repeated violations from same IP
  3. Regularly review file size and complexity limits
  4. Monitor API response times for performance impact

Deployment Considerations

Production Settings

  • Ensure PROD = True in constants.py
  • Set up log rotation for security.log
  • Configure firewall rules based on security logs
  • Regularly review and update CORS allowed origins

Performance Impact

  • Input validation adds minimal overhead
  • Rate limiting may affect high-volume users
  • Security logging has minimal performance impact
  • File size limits prevent resource exhaustion

Future Enhancements

Potential Improvements

  1. Integration with Web Application Firewall (WAF)
  2. Advanced malware scanning for uploaded files
  3. Machine learning-based anomaly detection
  4. Integration with security monitoring tools
  5. Advanced rate limiting with different tiers

Maintenance

  • Regularly review security logs for new attack patterns
  • Update dangerous import patterns based on threats
  • Adjust rate limits based on usage patterns
  • Keep security headers updated with latest recommendations

There aren’t any published security advisories