构建最终要执行的命令 — 源码 bash.rs:182-207 这里有一个关键的决策树: 1. 如果 Linux + 命名空间可用 → 用 unshare 包装 2. 否则 → 直接用 sh -lc 执行,但仍然做文件系统隔离 即使不能用 unshare,文件系统隔离仍然生效: 通过修改 HOME 和 TMPDIR 环境变量,限制程序能写的位置。 这不是内核级隔离,但比什么都没有好很多。
(
command: str,
cwd: Path,
sandbox_status: SandboxStatus,
create_dirs: bool = True,
)
| 418 | |
| 419 | |
| 420 | def prepare_command( |
| 421 | command: str, |
| 422 | cwd: Path, |
| 423 | sandbox_status: SandboxStatus, |
| 424 | create_dirs: bool = True, |
| 425 | ) -> Tuple[List[str], Dict[str, str]]: |
| 426 | """构建最终要执行的命令 — 源码 bash.rs:182-207 |
| 427 | |
| 428 | 这里有一个关键的决策树: |
| 429 | 1. 如果 Linux + 命名空间可用 → 用 unshare 包装 |
| 430 | 2. 否则 → 直接用 sh -lc 执行,但仍然做文件系统隔离 |
| 431 | |
| 432 | 即使不能用 unshare,文件系统隔离仍然生效: |
| 433 | 通过修改 HOME 和 TMPDIR 环境变量,限制程序能写的位置。 |
| 434 | 这不是内核级隔离,但比什么都没有好很多。 |
| 435 | """ |
| 436 | if create_dirs: |
| 437 | prepare_sandbox_dirs(cwd) |
| 438 | |
| 439 | # 尝试构建 Linux 沙箱命令 |
| 440 | launcher = build_linux_sandbox_command(command, cwd, sandbox_status) |
| 441 | if launcher is not None: |
| 442 | return [launcher.program] + launcher.args, launcher.env |
| 443 | |
| 444 | # 降级: 直接用 sh 执行,但重定向 HOME/TMPDIR |
| 445 | cmd_args = ["sh", "-lc", command] |
| 446 | env = dict(os.environ) |
| 447 | if sandbox_status.filesystem_active: |
| 448 | env["HOME"] = str(cwd / ".sandbox-home") |
| 449 | env["TMPDIR"] = str(cwd / ".sandbox-tmp") |
| 450 | |
| 451 | return cmd_args, env |
| 452 | |
| 453 | |
| 454 | def execute_bash(inp: BashCommandInput) -> BashCommandOutput: |
no test coverage detected