python.unsafe_eval
Correctness
Critical
Detects eval() or exec() calls with potentially untrusted input.
Why It Matters
Section titled “Why It Matters”eval and exec execute arbitrary Python code. With untrusted input:
- Remote code execution — Attackers run any code on your server
- Data exfiltration — All your data can be copied out
- System compromise — Can install backdoors, delete files, pivot to other systems
- Total control — This is game over if exploited
This is one of the most severe security vulnerabilities possible.
Example
Section titled “Example”# ❌ Beforeresult = eval(user_input)If user_input is "__import__('os').system('rm -rf /')", you’ve just wiped your server.
# ✅ After (for data parsing)import astresult = ast.literal_eval(user_input)ast.literal_eval only parses Python literals (strings, numbers, lists, dicts). It won’t execute code.
What Unfault Detects
Section titled “What Unfault Detects”eval()with any variable inputexec()with any variable inputcompile()with user input followed byexec__import__()with variable arguments
Auto-Fix
Section titled “Auto-Fix”For data parsing cases, Unfault replaces eval() with ast.literal_eval(). For other cases, manual review is required.
Safe Alternatives
Section titled “Safe Alternatives”# For JSON dataimport jsondata = json.loads(user_input)
# For Python literals (strings, numbers, lists, dicts)import astdata = ast.literal_eval(user_input)
# For configurationimport yaml # with safe_loadconfig = yaml.safe_load(config_string)
# For math expressions (if really needed)import numexprresult = numexpr.evaluate(expression)Never Do This
Section titled “Never Do This”# NEVER eval user input, even "sanitized"eval(user_input.replace(";", "")) # Still vulnerable
# NEVER use eval for JSONeval(json_string) # Use json.loads
# NEVER use eval for configeval(f"config['{key}']") # Use dict access