File size: 8,044 Bytes
96cc624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94fd0b4
96cc624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03cfe3b
96cc624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03cfe3b
 
 
96cc624
03cfe3b
 
96cc624
03cfe3b
 
96cc624
03cfe3b
 
96cc624
 
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
"""
Input validation and sanitization module for Prompt2Frame.

This module provides comprehensive validation for user prompts and generated code
to prevent security vulnerabilities and ensure safe execution.
"""

import re
from typing import List, Tuple
import logging

logger = logging.getLogger(__name__)


class PromptValidator:
    """Validates and sanitizes user prompts."""
    
    # Dangerous patterns that might be used for code injection
    DANGEROUS_PATTERNS = [
        # File system operations
        r'\bopen\s*\(',
        r'\bfile\s*\(',
        r'\bread\s*\(',
        r'\bwrite\s*\(',
        r'\bos\.',
        r'\bpath\.',
        r'__file__',
        r'__path__',
        
        # Network operations
        r'\burllib\b',
        r'\brequests\b',
        r'\bsocket\b',
        r'\bhttp\.',
        r'\bftp\b',
        
        # System/subprocess
        r'\bsubprocess\b',
        r'\bsystem\(',
        r'\bexec\(',
        r'\beval\(',
        r'\bcompile\(',
        r'__import__',
        
        # Database
        r'\bsql\b',
        r'\binsert\b.*\binto\b',
        r'\bselect\b.*\bfrom\b',
        r'\bdrop\b.*\btable\b',
        
        # Code execution
        r'globals\(',
        r'locals\(',
        r'vars\(',
        r'dir\(',
    ]
    
    @classmethod
    def validate_prompt(cls, prompt: str) -> Tuple[bool, str]:
        """
        Validate a user prompt for security and content.
        
        Args:
            prompt: The user's prompt
            
        Returns:
            Tuple of (is_valid, error_message)
        """
        # Check length
        if not prompt or not prompt.strip():
            return False, "Prompt cannot be empty"
        
        if len(prompt) < 3:
            return False, "Prompt is too short (minimum 3 characters)"
        
        if len(prompt) > 500:
            return False, "Prompt is too long (maximum 500 characters)"
        
        # Check for dangerous patterns
        prompt_lower = prompt.lower()
        for pattern in cls.DANGEROUS_PATTERNS:
            if re.search(pattern, prompt_lower, re.IGNORECASE):
                logger.warning(f"Dangerous pattern detected in prompt: {pattern}")
                return False, (
                    "Your prompt contains potentially unsafe content. "
                    "Please rephrase to describe visual animations only."
                )
        
        # Check for excessive punctuation (spam indicator)
        special_char_ratio = sum(c in '!@#$%^&*()_+=' for c in prompt) / len(prompt)
        if special_char_ratio > 0.3:
            return False, "Prompt contains too many special characters"
        
        # Check for repeated characters (spam indicator)
        if re.search(r'(.)\1{10,}', prompt):
            return False, "Prompt contains excessive repeated characters"
        
        return True, ""
    
    @classmethod
    def sanitize_prompt(cls, prompt: str) -> str:
        """
        Sanitize a prompt by removing potentially harmful content.
        
        Args:
            prompt: The raw prompt
            
        Returns:
            Sanitized prompt
        """
        # Remove null bytes
        prompt = prompt.replace('\x00', '')
        
        # Remove control characters except newlines and tabs
        prompt = ''.join(char for char in prompt if char.isprintable() or char in '\n\t')
        
        # Normalize whitespace
        prompt = ' '.join(prompt.split())
        
        # Trim to max length
        prompt = prompt[:500]
        
        return prompt.strip()


class CodeSecurityValidator:
    """Enhanced security validation for generated code."""
    
    # Comprehensive list of dangerous operations
    DANGEROUS_OPERATIONS = [
        # File I/O
        'open(', 'file(', 'with open',
        
        # OS operations  
        'os.', 'sys.', 'subprocess.', 'shutil.', 'pathlib.',
        
        # Network
        'urllib', 'requests', 'socket', 'http.',
        
        # Code execution
        'exec', 'eval', 'compile', '__import__',
        'globals()', 'locals()', 'vars()',
        
        # Dangerous imports
        'import os', 'import sys', 'import subprocess',
        'import shutil', 'import requests', 'import urllib',
        'import socket', 'import pickle',
        
        # Shell commands
        'popen(', 'shell=',
    ]
    
    # Allowed imports only
    ALLOWED_IMPORTS = [
        'from manim import *',
        'import random',
        'import numpy as np',
        'import math',  # Safe math operations
    ]
    
    @classmethod
    def validate_code_safety(cls, code: str) -> Tuple[bool, str]:
        """
        Validate that generated code is safe to execute.
        
        Args:
            code: The generated Python code
            
        Returns:
            Tuple of (is_safe, error_message)
        """
        # Check for dangerous operations
        code_lower = code.lower()
        for operation in cls.DANGEROUS_OPERATIONS:
            if operation in code_lower:
                logger.error(f"Dangerous operation detected: {operation}")
                return False, f"Code contains forbidden operation: {operation}"
        
        # Validate imports
        import_lines = [line.strip() for line in code.split('\n') 
                       if line.strip().startswith(('import ', 'from '))]
        
        for imp_line in import_lines:
            # Check if it's an allowed import
            is_allowed = any(allowed in imp_line for allowed in cls.ALLOWED_IMPORTS)
            if not is_allowed:
                logger.error(f"Unauthorized import: {imp_line}")
                return False, f"Unauthorized import statement: {imp_line}"
        
        # Check for attempts to access internals
        if '__' in code and any(dangerous in code for dangerous in ['__file__', '__path__', '__dict__', '__class__']):
            return False, "Code attempts to access Python internals"
        
        # Check code length (prevent DoS)
        if len(code) > 10000:
            return False, "Generated code is too large"
        
        # Count class definitions (should be exactly 1 Scene subclass)
        class_count = code.count('class ')
        if class_count < 1:
            return False, "No class definition found"
        if class_count > 3:
            return False, "Too many class definitions"
        
        return True, ""
    
    @classmethod
    def analyze_code_complexity(cls, code: str) -> dict:
        """
        Analyze code complexity to prevent resource exhaustion.
        
        Args:
            code: The generated code
            
        Returns:
            Dict with complexity metrics
        """
        return {
            'line_count': len(code.split('\n')),
            'object_count': code.count('Circle(') + code.count('Square(') + 
                          code.count('Rectangle(') + code.count('Triangle('),
            'animation_count': code.count('self.play('),
            'loop_count': code.count('for ') + code.count('while '),
            'function_count': code.count('def '),
        }
    
    @classmethod
    def validate_code_complexity(cls, code: str) -> Tuple[bool, str]:
        """
        Validate that code complexity is within acceptable limits.
        
        Args:
            code: The generated code
            
        Returns:
            Tuple of (is_valid, error_message)
        """
        metrics = cls.analyze_code_complexity(code)
        
        # Check limits (Relaxed for complex animations)
        if metrics['object_count'] > 100:
            return False, "Too many objects (limit: 100)"
        
        if metrics['animation_count'] > 100:
            return False, "Too many animations (limit: 100)"
        
        if metrics['loop_count'] > 50:
            return False, "Too many loops (limit: 50)"
        
        if metrics['function_count'] > 50:
            return False, "Too many function definitions (limit: 50)"
        
        return True, ""