Skip to content

fix(sandbox): proxy pass-through + bwrap compat on CentOS 7 / kernel 3.10 - #811

Merged
yaojin3616 merged 1 commit into
mainfrom
sandbox/proxy-and-bwrap-oldkernel-compat
Aug 3, 2026
Merged

fix(sandbox): proxy pass-through + bwrap compat on CentOS 7 / kernel 3.10#811
yaojin3616 merged 1 commit into
mainfrom
sandbox/proxy-and-bwrap-oldkernel-compat

Conversation

@yaojin3616

Copy link
Copy Markdown
Collaborator

问题

在 CentOS 7(Linux 内核 3.10)环境下运行 bubblewrap 沙箱时报错:

bwrap: Cannot create new cgroup namespace because the kernel does not support it.

根本原因有三:

  1. --unshare-cgroup 要求 Linux ≥ 4.6,旧内核不支持 cgroup namespace。
  2. --unshare-useruser.max_user_namespaces=0 或未设置 namespace.unpriv_enable=1 时无法创建 user namespace。
  3. Dockerfile 安装了 bubblewrap 但未设 setuid 位,在 user namespace 不可用时无法以特权方式运行。

此外,subprocess 子进程缺乏 http_proxy / https_proxy 代理环境变量透传机制。

修改内容

bwrap 旧内核兼容性

  • --unshare-cgroup 替换为 --unshare-cgroup-try(内核不支持时优雅跳过)
  • 移除 --unshare-user(在无 unprivileged namespace 支持的系统上阻塞启动)
  • backend/Dockerfile 中为 /usr/bin/bwrap 增加 SetUID 位,支持无 user namespace 的宿主机环境

代理透传

  • SandboxConfig 新增 http_proxyhttps_proxyno_proxy 字段,from_dict() 同步支持 fallback 解析
  • Settings 新增 HTTPS_PROXYNO_PROXYSANDBOX_HTTP_PROXYSANDBOX_HTTPS_PROXYSANDBOX_NO_PROXY 配置项
  • get_sandbox_config() 优先使用 SANDBOX_* 环境变量,回退到全局 HTTP_PROXY / HTTPS_PROXY / NO_PROXY
  • SubprocessBackend._build_safe_env() 将代理变量注入子进程环境(同时写大小写两个变体)
  • SubprocessBackend._build_bwrap_command() 通过 --setenv 将代理变量透传进 bubblewrap 沙箱内部
  • DockerBackend 容器 env 同步注入代理变量

测试

新增单元测试覆盖:

  • _build_safe_env 中代理变量注入
  • _build_bwrap_command 中 bwrap flag 和代理 --setenv 验证(含断言 --unshare-cgroup-try 在、--unshare-user / --unshare-cgroup 不在)
  • SandboxConfig.from_dict 代理字段解析

所有测试通过(5/5),Ruff 检查干净。

…3.10

- Add http_proxy/https_proxy/no_proxy fields to SandboxConfig and from_dict()
- Pass proxy env vars from global settings (HTTP_PROXY/HTTPS_PROXY/NO_PROXY and
  per-sandbox SANDBOX_HTTP_PROXY/SANDBOX_HTTPS_PROXY/SANDBOX_NO_PROXY) into
  get_sandbox_config() with the sandbox-specific setting taking precedence
- Inject proxy variables into subprocess env (_build_safe_env) and into bwrap
  sandbox via --setenv flags (_build_bwrap_command)
- Propagate proxy env into DockerBackend container environment

Fix bwrap incompatibility with CentOS 7 / Linux 3.10 kernels:
- Replace --unshare-cgroup (requires Linux ≥ 4.6) with --unshare-cgroup-try
  (gracefully skips if kernel does not support cgroup namespace)
- Remove --unshare-user which is blocked when
  user.max_user_namespaces=0 or sysctl namespace.unpriv_enable=1 is not set
- Add SetUID bit on /usr/bin/bwrap in Dockerfile so it can operate in
  privileged mode when user namespaces are unavailable

Add unit tests covering proxy propagation in safe env, bwrap command,
and SandboxConfig.from_dict
@yaojin3616
yaojin3616 merged commit 4f84355 into main Aug 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant