Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions metagpt/actions/run_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,40 @@ class RunCode(Action):

@classmethod
async def run_text(cls, code) -> Tuple[str, str]:
"""Execute code in a subprocess for sandbox isolation.

SECURITY: Code is written to a temporary file and executed via
subprocess instead of exec() to prevent arbitrary code execution
in the MetaGPT process.
"""
import tempfile
import os as _os
try:
# We will document_store the result in this dictionary
namespace = {}
exec(code, namespace)
# Write code to a temporary file for subprocess execution
with tempfile.NamedTemporaryFile(
mode="w", suffix=".py", delete=False, encoding="utf-8"
) as tmp:
tmp.write(code)
tmp.flush()
tmp_path = tmp.name

# Execute in isolated subprocess with timeout
proc = subprocess.run(
[sys.executable, tmp_path],
capture_output=True,
text=True,
timeout=30,
)
return proc.stdout, proc.stderr
except subprocess.TimeoutExpired:
return "", "Execution timed out after 30 seconds"
except Exception as e:
return "", str(e)
return namespace.get("result", ""), ""
finally:
try:
_os.unlink(tmp_path)
except Exception:
pass

async def run_script(self, working_directory, additional_python_paths=[], command=[]) -> Tuple[str, str]:
working_directory = str(working_directory)
Expand Down
Loading