nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Feature] OS sandbox for execute_bash

Open Dhirenderchoudhary opened this issue 3 months ago • 2 comments

Description

execute_bash runs with the user's full privileges. File tools do not.

resolveFilePath in source/utils/path-validation.ts (around 156) realpath-checks and throws if a read/write leaves the project. Bash never gets that. BashExecutor.execute in source/services/bash-executor.ts (around 79-85) is a raw spawn:

const proc = isWindows
  ? spawn('cmd', ['/c', command], {cwd})
  : spawn('sh', ['-c', spawnCommand], {cwd, detached: true});

cwd is getSafeSessionCwd(). That is the start directory, not a jail. curl, rm -rf ~, writes via symlink, network, all still work.

Approval in source/tools/approval-policy.ts is only "ask / yolo / alwaysAllow". execute-bash.tsx still prompts in auto-accept, but yolo is bypassed centrally. Once you say yes (or yolo), there is no OS isolation.

docs/features/custom-tools.md already says custom tools are not a sandbox. Same is true of execute_bash and !command, which share this executor.

#287 asked for a big security framework and was closed as too large, with a request to reopen as smaller issues. This is one of those: confine the shell, nothing else.

#1027 is custom-tool cwd containment (path), not a process jail. #1032 hooks can run a user script, they cannot confine the kernel.

OpenCode already has experimental macOS sandbox-exec (anomalyco/opencode#21538). Claude Code and Codex isolate the shell. We only have a confirm prompt.

Use Case

Local-first users run yolo against Ollama on a laptop. One bad execute_bash should not hit $HOME or the network.

Proposed Solution

v1: wrap execute_bash / !cmd only (same BashExecutor). Config flag, e.g. nanocoder.sandbox, default off.

When on:

  • macOS: sandbox-exec deny network, confine writes to the project root.
  • Linux: bubblewrap or landlock if present. If missing, fail the tool with a clear error. Do not fall through to unsandboxed bash.
  • Windows: skip in v1, document it.

Timeouts stay as they are. Sandbox spawn failure is a tool error returned to the model.

Do not try to statically parse bash.

Alternatives Considered

  • Keep asking for approval. Does nothing in yolo, and a yes still runs unsandboxed.
  • Reuse file-tool resolveFilePath. Bash never goes through that.
  • Skill subscribe: / #1032 hooks. Wrong layer.
  • Sandbox custom tools in the same PR. Separate surface.

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate (#287 closed as too broad)
  • [x] This feature aligns with the project's goals (local-first AI assistance)

Happy to take this if assigned.

Dhirenderchoudhary avatar Aug 29 '26 17:08 Dhirenderchoudhary

@akramcodez can I take this one? looked at current main first so we are not duplicating. happy to start once assigned.

Dhirenderchoudhary avatar Aug 29 '26 17:08 Dhirenderchoudhary

Agreed on the layering and one thing to watch: BashExecutor writes its cwd-capture file to os.tmpdir() (bash-executor.ts:69-77), so a profile confining writes to the project root will silently break cd persistence.

addyCooks avatar Aug 30 '26 07:08 addyCooks