kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

feat: OS-level sandboxing, parsed exec-policy engine, and network egress control for agent shell commands

Open Ram-Pavith opened this issue 1 month ago • 1 comments

Problem

Kimi Code gates agent shell commands on permission prompts only — there is no enforcement layer beneath the approval UX:

  1. No OS sandbox. Commands approved in manual/auto/yolo mode run with full user privileges. Under kimi -p (non-interactive, defaults to auto) and AgentSwarm/goal-driven autonomous runs, nothing confines what a spawned command can read, write, or connect to — prompt injection or a model mistake becomes full-workspace/user damage.
  2. No parsed command policy. Bash(pattern) permission rules are glob matches over the raw command string — rm -rf a; curl …, $(…), sudo, bash -c either bypass or false-positive, and there is no way to approve "just git status" by command shape. dangerous-command-ask exists but is AST-based only for a fixed builtin set and is skipped entirely in nonInteractive mode, so headless runs have zero command vetting.
  3. No network egress control. A filesystem sandbox without egress control still allows read-then-exfiltrate. There is no domain allowlist, no per-connection policy, and no proxy the sandbox can route through.

Comparable agents ship all three as one containment stack (OpenAI Codex: permission profiles → Seatbelt/bwrap → execpolicy rules → MITM network proxy; Claude Code: sandbox-runtime with deny-by-default fs/network).

Proposal

Implement the three layers behind experimental flags (all default off), in agent-core-v2:

  • OS sandbox (KIMI_CODE_EXPERIMENTAL_SANDBOX): [sandbox] config with off | read-only | workspace-write | danger-full-access modes; macOS Seatbelt and Linux bwrap backends; writable-root/deny-list policies with protected metadata (.git, ~/.ssh); denial detection → interactive escalation, fail-closed under -p.
  • Exec policy (KIMI_CODE_EXPERIMENTAL_EXEC_POLICY): layered .rules files (/etc/kimi-code/rules.d managed → ~/.kimi-code/rules.d user → .kimi-code/rules.d project → session-runtime) with prefix_rule/network_rule, evaluated per parsed command segment (reusing the bash AST parser); forbidden terminal, prompt → ask interactive / deny under -p; runs in nonInteractive mode where the current dangerous-command policy does not.
  • Network egress (KIMI_CODE_EXPERIMENTAL_NETWORK_EGRESS): [sandbox.network] allowlist/denylist + network_rule enforced by a loopback HTTP CONNECT/SOCKS5 proxy; token-scoped env injection for sandboxed spawns; platform kernel rules deny direct egress where supported.

Scope

  • Platforms: macOS + Linux first; Windows reports unsupported and degrades per sandbox.allow_unsandboxed_commands.
  • Surface: agent-initiated Bash/shell-command spawns; file tools keep existing permission rules.
  • Rollout: experimental flags → default-on per platform after soak.

A draft implementation of all three layers exists and will be linked in a PR.

Ram-Pavith avatar Oct 03 '26 08:10 Ram-Pavith

A working implementation of all three layers exists in #4115 (submitted alongside this issue) — macOS Seatbelt + Linux bwrap sandboxing behind KIMI_CODE_EXPERIMENTAL_SANDBOX, a layered .rules exec-policy engine (prefix_rule/network_rule, AST-based command segmentation reusing the existing bash parser, fail-closed under kimi -p), and a loopback CONNECT/SOCKS5 egress proxy enforcing [sandbox.network] allowlists — each behind its own default-off experimental flag, with tests and docs included.

Happy to treat that PR purely as a reference implementation: if the direction interests the team, I'm glad to split it into smaller PRs, rework the config surface / flag names / rule DSL, or hand the design over entirely — whatever fits the roadmap.

Ram-Pavith avatar Oct 03 '26 08:10 Ram-Pavith