Skip to content

security: replace eval() with safe parsers to prevent arbitrary code execution#2032

Open
anxovatomica wants to merge 1 commit into
FoundationAgents:mainfrom
anxovatomica:fix-eval-rce
Open

security: replace eval() with safe parsers to prevent arbitrary code execution#2032
anxovatomica wants to merge 1 commit into
FoundationAgents:mainfrom
anxovatomica:fix-eval-rce

Conversation

@anxovatomica
Copy link
Copy Markdown

Summary

Replaces dangerous eval() calls with safe alternatives (ast.literal_eval(), json.loads()) across three core components.

Vulnerability

eval() was used to parse LLM-generated output and serialized mapping data. An attacker can achieve arbitrary code execution via prompt injection by crafting inputs that cause the LLM to output malicious Python code.

Affected code paths

  1. metagpt/actions/action_node.pyfill() method evaluates raw_value from LLM XML-tagged output:

    extracted_data[field_name] = eval(raw_value)  # RCE if LLM injects malicious code
  2. metagpt/strategy/tot.pygenerate_thoughts() evaluates LLM JSON output:

    thoughts = eval(thoughts)  # RCE if LLM outputs __import__('os').system('id')
  3. metagpt/utils/serialize.pyactionoutput_str_to_mapping() evaluates serialized strings:

    new_mapping[key] = eval(value)  # RCE if serialized mapping is poisoned

Fix

  • eval()ast.literal_eval() for Python literal parsing (lists, dicts, tuples)
  • eval()json.loads() for JSON parsing in TOT strategy

Impact

Eliminates arbitrary code execution via prompt injection (CWE-94). Multi-tenant deployments where untrusted users can trigger LLM calls are now safe from this attack vector.

…ent arbitrary code execution

Three components used eval() on LLM-generated or serialized data:
- action_node.py: eval(raw_value) from LLM XML output → ast.literal_eval()
- tot.py: eval(thoughts) from LLM JSON output → json.loads()
- serialize.py: eval(value) on serialized mapping strings → ast.literal_eval()

Using eval() allows prompt-injected LLM responses to execute arbitrary Python
code. ast.literal_eval() and json.loads() safely parse structured data without
code execution.

Fixes arbitrary code execution via prompt injection (CWE-94).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant