Skip to content

Security: Replace eval() with ast.literal_eval() to prevent code injection (CWE-94)#2033

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

Security: Replace eval() with ast.literal_eval() to prevent code injection (CWE-94)#2033
anxovatomica wants to merge 1 commit into
FoundationAgents:mainfrom
anxovatomica:security-fix-eval-injection

Conversation

@anxovatomica
Copy link
Copy Markdown

Security Fix: Replace eval() with ast.literal_eval() to prevent Code Injection (CWE-94)

Vulnerability Details

  • Category: Code Injection / Unsafe Deserialization
  • Severity: Critical
  • CWE: CWE-94 (Improper Control of Generation of Code)
  • Files Affected:

Impact

The codebase uses Python's function to parse data that originates from LLM outputs and serialized configurations. Since executes arbitrary Python code, an attacker who controls the LLM response or manipulates serialized data can achieve Remote Code Execution (RCE) with the privileges of the MetaGPT process.

Attack Scenario

  1. An attacker crafts a malicious prompt or manipulates the LLM API response
  2. The LLM returns a payload like
  3. executes this payload directly
  4. The attacker gains arbitrary code execution on the host

Fix Applied

Replaced all calls with , which safely evaluates literal expressions (lists, dicts, strings, numbers) without executing arbitrary code.

File Before After

Testing

  • correctly parses the expected data types (list, dict, tuples)
  • No behavioral change for valid inputs
  • Malicious payloads now raise instead of executing

References

…ction

Replaces unsafe eval() calls with ast.literal_eval() in three files:
- metagpt/actions/action_node.py: list/dict parsing from LLM output
- metagpt/strategy/tot.py: thought parsing from LLM output
- metagpt/utils/serialize.py: type deserialization

eval() on untrusted input (especially LLM-generated content) allows
arbitrary code execution. ast.literal_eval() safely evaluates literals
without executing arbitrary code.

Fixes CWE-94: Improper Control of Generation of Code ('Code Injection')
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