security: replace eval() with safe parsers to prevent arbitrary code execution#2032
Open
anxovatomica wants to merge 1 commit into
Open
security: replace eval() with safe parsers to prevent arbitrary code execution#2032anxovatomica wants to merge 1 commit into
anxovatomica wants to merge 1 commit into
Conversation
…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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
metagpt/actions/action_node.py—fill()method evaluatesraw_valuefrom LLM XML-tagged output:metagpt/strategy/tot.py—generate_thoughts()evaluates LLM JSON output:metagpt/utils/serialize.py—actionoutput_str_to_mapping()evaluates serialized strings:Fix
eval()→ast.literal_eval()for Python literal parsing (lists, dicts, tuples)eval()→json.loads()for JSON parsing in TOT strategyImpact
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.