44import shutil
55import tempfile
66from dataclasses import dataclass , field
7- from typing import Dict , List , Optional
7+ from typing import Dict , Optional
88
99
1010# =========================
@@ -180,6 +180,36 @@ class ExecutionSafetyManager:
180180 r"\bbash\b" ,
181181 ]
182182
183+ _POSIX_SYSTEM_PREFIXES = [
184+ r"/etc/\w+" ,
185+ r"/tmp/\w+" ,
186+ r"/var/\w+" ,
187+ r"/usr/\w+" ,
188+ r"/root/\w+" ,
189+ r"/home/\w+/" ,
190+ r"/proc/\w+" ,
191+ r"/sys/\w+" ,
192+ r"/dev/\w+" ,
193+ r"/boot/\w+" ,
194+ r"/opt/\w+" ,
195+ r"/mnt/\w+" ,
196+ r"/media/\w+" ,
197+ ]
198+
199+ # Pre-compiled regular expressions for performance
200+ _COMPILED_WRITE_PATTERNS = tuple (re .compile (p , re .IGNORECASE ) for p in _WRITE_PATTERNS )
201+ _COMPILED_WRITE_ON_HANDLE_PATTERNS = tuple (re .compile (p , re .IGNORECASE ) for p in _WRITE_ON_HANDLE_PATTERNS )
202+ _COMPILED_SENSITIVE_POSIX_PREFIXES = tuple (re .compile (p , re .IGNORECASE ) for p in _SENSITIVE_POSIX_PREFIXES )
203+ _COMPILED_DESTRUCTIVE_PATTERNS = tuple (re .compile (p ) for p in _DESTRUCTIVE_PATTERNS )
204+ _COMPILED_SHELL_PATTERNS = tuple (re .compile (p ) for p in _SHELL_PATTERNS )
205+ _COMPILED_POSIX_SYSTEM_PREFIXES = tuple (re .compile (p , re .IGNORECASE ) for p in _POSIX_SYSTEM_PREFIXES )
206+
207+ _COMPILED_RD_PATTERN = re .compile (r"\brd\s+/s\s+/q\b" )
208+ _COMPILED_OPEN_ARGS_PATTERN = re .compile (r"open\s*\(\s*([\"'][^\"']+[\"'])" , re .IGNORECASE )
209+ _COMPILED_WIN_DRIVE_PATTERN = re .compile (r"[a-z]:[\\/]" )
210+ _COMPILED_POSIX_ABS_PATTERN = re .compile (r"""["']/[^"'\s]""" )
211+ _COMPILED_WIN_DRIVE_EXACT_PATTERN = re .compile (r"[a-zA-Z]:[\\/]" )
212+
183213 def __init__ (self , unsafe_mode : bool = False ):
184214 self .unsafe_mode = unsafe_mode
185215
@@ -228,7 +258,7 @@ def _has_write_operation(self, code: str) -> bool:
228258 """Return True if *code* contains any write operation that must be
229259 blocked in SAFE mode.
230260 """
231- return any (re .search (p , code , re . IGNORECASE ) for p in self ._WRITE_PATTERNS )
261+ return any (p .search (code ) for p in self ._COMPILED_WRITE_PATTERNS )
232262
233263 # =========================
234264 # WRITE-ON-HANDLE DETECTION
@@ -240,52 +270,37 @@ def _has_write_on_handle(self, code: str) -> bool:
240270 """Return True if *code* calls .write() on any object (handle check).
241271 This is intentionally only evaluated when an absolute path is present.
242272 """
243- return any (re .search (p , code , re . IGNORECASE ) for p in self ._WRITE_ON_HANDLE_PATTERNS )
273+ return any (p .search (code ) for p in self ._COMPILED_WRITE_ON_HANDLE_PATTERNS )
244274
245275 # =========================
246276 # HOST ABSOLUTE PATH CHECK
247277 # =========================
248278 def _is_host_absolute_path (self , code : str ) -> bool :
249279 """Return True if *code* references a host absolute path."""
250280 # Windows drive-letter path
251- if re . search (r"[a-z]:[\\/]" , code .lower ()):
281+ if self . _COMPILED_WIN_DRIVE_PATTERN . search (code .lower ()):
252282 return True
253283
254284 # Quoted POSIX absolute path: '/...' or "/..."
255- if re . search (r"""["']/[^"'\s]""" , code ):
285+ if self . _COMPILED_POSIX_ABS_PATTERN . search (code ):
256286 return True
257287
258288 # Unquoted well-known POSIX system directory prefixes
259- _posix_system_prefixes = [
260- r"/etc/\w+" ,
261- r"/tmp/\w+" ,
262- r"/var/\w+" ,
263- r"/usr/\w+" ,
264- r"/root/\w+" ,
265- r"/home/\w+/" ,
266- r"/proc/\w+" ,
267- r"/sys/\w+" ,
268- r"/dev/\w+" ,
269- r"/boot/\w+" ,
270- r"/opt/\w+" ,
271- r"/mnt/\w+" ,
272- r"/media/\w+" ,
273- ]
274- if any (re .search (p , code , re .IGNORECASE ) for p in _posix_system_prefixes ):
289+ if any (p .search (code ) for p in self ._COMPILED_POSIX_SYSTEM_PREFIXES ):
275290 return True
276291
277292 # open() call whose first positional argument is an absolute path string
278- open_args = re . findall (r"open\s*\(\s*([\"'][^\"']+[\"'])" , code , re . IGNORECASE )
293+ open_args = self . _COMPILED_OPEN_ARGS_PATTERN . findall (code )
279294 for arg in open_args :
280295 path = arg .strip ("'\" " )
281- if path .startswith ("/" ) or re . match (r"[a-zA-Z]:[\\/]" , path ):
296+ if path .startswith ("/" ) or self . _COMPILED_WIN_DRIVE_EXACT_PATTERN . match (path ):
282297 return True
283298
284299 return False
285300
286301 def _is_sensitive_posix_path (self , code : str ) -> bool :
287302 """Return True if *code* references a sensitive POSIX system path."""
288- return any (re .search (p , code , re . IGNORECASE ) for p in self ._SENSITIVE_POSIX_PREFIXES )
303+ return any (p .search (code ) for p in self ._COMPILED_SENSITIVE_POSIX_PREFIXES )
289304
290305 # =========================
291306 # MAIN CHECK
@@ -297,7 +312,7 @@ def assess_execution(self, code: str, mode: str) -> Decision:
297312 code_lower = code .lower ()
298313
299314 # HARD BLOCK WINDOWS RECURSIVE DELETE (CRITICAL FIX)
300- if re . search (r"\brd\s+/s\s+/q\b" , code_lower ):
315+ if self . _COMPILED_RD_PATTERN . search (code_lower ):
301316 return Decision (False , ["Recursive deletion is blocked." ])
302317
303318 # UNSAFE MODE - still detect dangerous operations but allow with warnings
@@ -326,15 +341,15 @@ def assess_execution(self, code: str, mode: str) -> Decision:
326341 # (shutdown, reboot, mkfs, dd, format, diskpart) in addition to
327342 # filesystem deletes.
328343 # =========================
329- if any (re .search (p , code_lower ) for p in self ._DESTRUCTIVE_PATTERNS ):
344+ if any (p .search (code_lower ) for p in self ._COMPILED_DESTRUCTIVE_PATTERNS ):
330345 return Decision (False , ["Destructive operation blocked." ])
331346
332347 # =========================
333348 # SHELL BLOCK
334349 # BUG FIX #2: Uses _SHELL_PATTERNS with \b word-boundary regex instead
335350 # of plain substring `in` check to avoid false positives.
336351 # =========================
337- if any (re .search (p , code_lower ) for p in self ._SHELL_PATTERNS ):
352+ if any (p .search (code_lower ) for p in self ._COMPILED_SHELL_PATTERNS ):
338353 return Decision (False , ["Shell execution is blocked." ])
339354
340355 # =========================
@@ -370,7 +385,7 @@ def is_dangerous_operation(self, code: str) -> bool:
370385 if not code or not code .strip ():
371386 return False
372387 code_lower = code .lower ()
373- return any (re .search (p , code_lower ) for p in self ._DESTRUCTIVE_PATTERNS )
388+ return any (p .search (code_lower ) for p in self ._COMPILED_DESTRUCTIVE_PATTERNS )
374389
375390 # =========================
376391 # ARTIFACT EXPORT
0 commit comments