第一次接触LLM Sandbox时,最让我头疼的就是如何平衡功能开放性和安全性。记得去年在做一个AI代码生成项目时,就因为没做好沙箱隔离,导致测试环境的服务器被恶意代码清空了日志文件。后来花了整整两天才恢复数据,这个教训让我深刻认识到容器安全的重要性。
LLM Sandbox默认使用Docker作为后端,这给我们提供了很好的隔离基础。但就像给房子装防盗门一样,默认配置往往不够用,我们需要额外加固。先来看最基本的Docker安全配置:
bash复制# 创建专用网络隔离沙箱容器
docker network create --driver bridge sandbox-net
# 运行容器时的基础安全参数
docker run -d \
--name llm-sandbox \
--network sandbox-net \
--memory 1g \
--cpus 1 \
--read-only \
--security-opt no-new-privileges \
python:3.12
这里有几个关键点需要注意:
--read-only 将容器文件系统设为只读,防止代码修改系统文件--security-opt no-new-privileges 禁止进程提升权限在实际项目中,我发现很多开发者容易忽略用户权限配置。默认以root用户运行容器是非常危险的,正确的做法是:
dockerfile复制FROM python:3.12-slim
RUN groupadd -r sandbox && useradd -r -g sandbox sandbox
USER sandbox
WORKDIR /home/sandbox
LLM Sandbox最强大的特性就是它的运行时安全策略系统。刚开始使用时,我习惯先配置一个基础防护策略:
python复制from llm_sandbox.security import (
SecurityPolicy,
SecurityPattern,
RestrictedModule,
SecurityIssueSeverity
)
base_policy = SecurityPolicy(
severity_threshold=SecurityIssueSeverity.MEDIUM,
patterns=[
SecurityPattern(
pattern=r"os\.system\s*\(",
description="系统命令执行",
severity=SecurityIssueSeverity.HIGH
)
],
restricted_modules=[
RestrictedModule(
name="subprocess",
description="子进程执行",
severity=SecurityIssueSeverity.HIGH
)
]
)
这个策略会拦截所有直接执行系统命令的尝试,以及subprocess模块的使用。但实际使用中发现,这样的配置还是太宽松了。有一次测试时,用户通过os.popen()绕过了防护,后来我增加了这些补充规则:
python复制advanced_patterns = [
SecurityPattern(
pattern=r"os\.(popen|spawn|exec)",
description="替代命令执行方式",
severity=SecurityIssueSeverity.HIGH
),
SecurityPattern(
pattern=r"__import__\s*\(",
description="动态导入",
severity=SecurityIssueSeverity.MEDIUM
)
]
另一个常见的安全需求是控制网络访问。在金融领域的项目中,我们通常需要完全禁止沙箱容器的外网访问,但允许访问特定的内部API。这是我们的典型配置:
python复制network_policy = {
"network_mode": "none", # 禁用所有网络
"extra_hosts": {
"internal.api.company.com": "10.0.0.123"
}
}
with SandboxSession(
runtime_configs=network_policy,
security_policy=base_policy
) as session:
# 只能访问internal.api.company.com
session.run("import requests; requests.get('http://internal.api.company.com')")
对于需要精细控制网络的情况,可以使用白名单模式:
python复制network_rules = {
"network_mode": "bridge",
"sysctls": {
"net.ipv4.ip_forward": "0" # 禁用IP转发
},
"iptables": [
"-A OUTPUT -d 8.8.8.8 -j DROP", # 屏蔽特定IP
"-A OUTPUT -p tcp --dport 443 -j ACCEPT" # 只允许HTTPS
]
}
在电商内容生成项目中,我们遇到了更复杂的安全需求 - 需要动态分析代码行为。LLM Sandbox提供了强大的动态检测机制:
python复制dynamic_policy = SecurityPolicy(
severity_threshold=SecurityIssueSeverity.HIGH,
dynamic_checks=[
lambda code: ("while True" in code) and SecurityIssue(
description="无限循环检测",
severity=SecurityIssueSeverity.MEDIUM
)
]
)
这个策略会检测代码中是否包含无限循环。我们还可以扩展更复杂的检查:
python复制def check_resource_hog(code):
patterns = [
"import tensorflow",
"torch.cuda",
"multiprocessing.Pool"
]
if any(p in code for p in patterns):
return SecurityIssue(
description="高资源消耗操作",
severity=SecurityIssueSeverity.HIGH
)
return None
对于需要文件读写的场景,必须严格控制访问范围。这是我们的标准做法:
python复制fs_policy = {
"read_only": False,
"volumes": {
"/tmp/sandbox": {
"bind": "/workspace",
"mode": "rw"
}
},
"tmpfs": {
"/tmp": "size=100m,exec"
}
}
with SandboxSession(
runtime_configs=fs_policy,
workdir="/workspace"
) as session:
# 只能读写/workspace目录
session.run("with open('/workspace/test.txt', 'w') as f: f.write('safe')")
特别注意,一定要禁用对敏感目录的访问:
python复制forbidden_paths = [
"/etc", "/bin", "/usr", "/var", "/home"
]
security_opts = [
f"no-new-privileges:true",
f"apparmor:llm-sandbox-profile"
]
在生产环境中,完善的日志记录至关重要。我们通常这样配置:
python复制audit_config = {
"log_driver": "syslog",
"log_opts": {
"syslog-address": "udp://logserver:514",
"tag": "llm-sandbox"
},
"labels": {
"com.company.audit": "true"
}
}
session = SandboxSession(
runtime_configs=audit_config,
security_policy=production_policy
)
日志内容应该包括:
经过多次性能测试,我们发现这些配置对性能影响最小:
对于高性能场景,建议:
python复制optimized_policy = SecurityPolicy(
severity_threshold=SecurityIssueSeverity.HIGH,
fast_mode=True # 跳过复杂正则检查
)
最后要提醒的是,安全策略需要定期review和更新。我们团队每月都会进行一次安全演练,模拟各种攻击场景来测试防护效果。