cad5b14382
mcp-py 的 run_code/secure_sandbox 此前全是桩。落地两层防御:
1) 静态守卫 sandbox.SecureSandbox.static_guard(纯 AST,执行前第一道)
- 拦危险导入(os/sys/subprocess/socket/ctypes/pickle/requests…)、危险调用
(eval/exec/compile/__import__/open…)、逃逸属性(__subclasses__/__globals__…)、语法错误。
- 返回 (放行, 原因)。
2) 隔离执行 interpreter.CodeInterpreter.execute(Docker,真隔离)
- network_disabled 禁网;user=65534 非 root + cap_drop=ALL + no-new-privileges;
read_only 根 + /tmp tmpfs;mem/memswap(禁swap)/nano_cpus/pids_limit 限资源;
python -I 隔离模式;wait 超时即 kill;容器一次性 remove。
- 无 Docker SDK/daemon 时 available()=False 优雅降级,不阻断服务。
gateway:run_code(标准档 256m/0.5cpu/10s) 与 secure_sandbox(紧档 128m/5s) 均走
守卫→隔离,结果整理为 stdout/stderr/exit 可读文本。pyproject 启用 docker 依赖。
验证:
- 守卫 6 单测(放行安全码 / 拦危险导入·调用·逃逸属性 / 语法错误)全过。
- 隔离 4 项实跑(真 Docker):sum(range(10))→45 exit0;非root uid=65534;
禁网 urlopen 失败(DNS解析错);while True 超时 3s 被 kill。
- 无 Docker 降级测过。
生产加固:可把执行运行时换 gVisor(runsc)/Kata(已在注释/PROGRESS 标注)。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
23 lines
826 B
TOML
23 lines
826 B
TOML
[project]
|
||
name = "sundynix-mcp-py"
|
||
version = "0.1.0"
|
||
description = "sundynix-agentix · 第 5 层 Python 算法型 MCP 工具微服务"
|
||
requires-python = ">=3.11"
|
||
dependencies = [
|
||
"nats-py>=2.7.0", # 接入 NATS 骨干网
|
||
"python-docx>=1.1.0", # Word 解析
|
||
"openpyxl>=3.1.0", # Excel 解析
|
||
"pypdf>=4.0.0", # PDF 文本解析
|
||
"docker>=7.1.0", # Docker 隔离沙箱 / Code Interpreter(代码执行隔离)
|
||
# 以下随对应功能接真时再开(当前为桩,避免拖累安装):
|
||
# "mcp>=1.2.0", # MCP 协议(真实 MCP server)
|
||
# "magic-pdf", # MinerU 多模态解析 (PaddleOCR),扫描件 OCR
|
||
]
|
||
|
||
[build-system]
|
||
requires = ["hatchling"]
|
||
build-backend = "hatchling.build"
|
||
|
||
[tool.hatch.build.targets.wheel]
|
||
packages = ["src/sundynix_mcp_py"]
|