Skip to content

Commit f0be269

Browse files
feat: add MAXKB_SANDBOX_PYTHON_BANNED_HOSTS env to ban host for sandbox in tools code.
1 parent d7a6de1 commit f0be269

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

apps/common/utils/tool_code.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def __init__(self, sandbox=False):
2828
if self.sandbox:
2929
os.system(f"chown -R {self.user}:root {self.sandbox_path}")
3030
self.banned_keywords = CONFIG.get("SANDBOX_PYTHON_BANNED_KEYWORDS", 'nothing_is_banned').split(',');
31+
self.banned_hosts = CONFIG.get("SANDBOX_PYTHON_BANNED_HOSTS", 'no_host_is_banned');
3132

3233
def _createdir(self):
3334
old_mask = os.umask(0o077)
@@ -53,6 +54,8 @@ def exec_code(self, code_str, keywords):
5354
path_to_exclude = ['/opt/py3/lib/python3.11/site-packages', '/opt/maxkb-app/apps']
5455
sys.path = [p for p in sys.path if p not in path_to_exclude]
5556
sys.path += {python_paths}
57+
os.environ['LD_PRELOAD'] = '/opt/maxkb-app/sandbox/sandbox.so'
58+
os.environ['SANDBOX_BANNED_HOSTS'] = {self.banned_hosts}
5659
locals_v={'{}'}
5760
keywords={keywords}
5861
globals_v=globals()
@@ -159,6 +162,8 @@ def generate_mcp_server_code(self, code_str, params):
159162
path_to_exclude = ['/opt/py3/lib/python3.11/site-packages', '/opt/maxkb-app/apps']
160163
sys.path = [p for p in sys.path if p not in path_to_exclude]
161164
sys.path += {python_paths}
165+
os.environ['LD_PRELOAD'] = '/opt/maxkb-app/sandbox/sandbox.so'
166+
os.environ['SANDBOX_BANNED_HOSTS'] = {self.banned_hosts}
162167
exec({dedent(code)!a})
163168
"""
164169

@@ -197,7 +202,7 @@ def _exec_sandbox(self, _code, _id):
197202
file.write(_code)
198203
os.system(f"chown {self.user}:root {exec_python_file}")
199204
kwargs = {'cwd': BASE_DIR}
200-
kwargs['env'] = {'LD_PRELOAD': '/opt/maxkb-app/apps/sandbox.so'}
205+
kwargs['env'] = {}
201206
subprocess_result = subprocess.run(
202207
['su', '-s', python_directory, '-c', "exec(open('" + exec_python_file + "').read())", self.user],
203208
text=True,

installer/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ RUN apt-get update && \
1313
apt-get clean all && \
1414
rm -rf /var/lib/apt/lists/*
1515
WORKDIR /opt/maxkb-app
16-
RUN gcc -shared -fPIC -o /opt/maxkb-app/apps/sandbox.so /opt/maxkb-app/installer/sandbox.c -ldl && \
16+
RUN gcc -shared -fPIC -o /opt/maxkb-app/sandbox/sandbox.so /opt/maxkb-app/installer/sandbox.c -ldl && \
1717
rm -rf /opt/maxkb-app/ui && \
1818
pip install uv --break-system-packages && \
1919
python -m uv pip install -r pyproject.toml && \

installer/sandbox.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <regex.h>
99
#include <unistd.h>
1010

11-
static const char *ENV_NAME = "MAXKB_SANDBOX_PYTHON_BANNED_HOSTS";
11+
static const char *ENV_NAME = "SANDBOX_BANNED_HOSTS";
1212

1313
static int match_env_patterns(const char *target, const char *env_val) {
1414
if (!target || !env_val || !*env_val) return 0;

0 commit comments

Comments
 (0)