Skip to content

Commit 197d514

Browse files
refactor: clear all env from sandbox.
1 parent ec0be20 commit 197d514

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

apps/common/utils/tool_code.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ def exec_code(self, code_str, keywords):
8282
python_paths = CONFIG.get_sandbox_python_package_paths().split(',')
8383
_exec_code = f"""
8484
try:
85-
import sys, json, base64, builtins
85+
import os, sys, json, base64, builtins
8686
path_to_exclude = ['/opt/py3/lib/python3.11/site-packages', '/opt/maxkb-app/apps']
8787
sys.path = [p for p in sys.path if p not in path_to_exclude]
8888
sys.path += {python_paths}
8989
locals_v={'{}'}
9090
keywords={keywords}
9191
globals_v={'{}'}
92+
os.environ.clear()
9293
exec({dedent(code_str)!a}, globals_v, locals_v)
9394
f_name, f = locals_v.popitem()
9495
for local in locals_v:
@@ -180,16 +181,14 @@ def generate_mcp_server_code(self, code_str, params):
180181
python_paths = CONFIG.get_sandbox_python_package_paths().split(',')
181182
code = self._generate_mcp_server_code(code_str, params)
182183
return f"""
183-
import os
184-
import sys
185-
import logging
184+
import os, sys, logging
186185
logging.basicConfig(level=logging.WARNING)
187186
logging.getLogger("mcp").setLevel(logging.ERROR)
188187
logging.getLogger("mcp.server").setLevel(logging.ERROR)
189-
190188
path_to_exclude = ['/opt/py3/lib/python3.11/site-packages', '/opt/maxkb-app/apps']
191189
sys.path = [p for p in sys.path if p not in path_to_exclude]
192190
sys.path += {python_paths}
191+
os.environ.clear()
193192
exec({dedent(code)!a})
194193
"""
195194

0 commit comments

Comments
 (0)