Skip to main content

Shell Tool

The shell tool executes shell commands in the workspace directory with comprehensive security controls:
  • Command validation against SecurityPolicy
  • Environment sanitization to prevent API key leaks
  • Working directory control with cwd parameter
  • Timeout enforcement (default: 60 seconds)
  • Output limits (default: 1 MB)

Parameters

string
required
The shell command to execute
string
Working directory (absolute path within allowed paths; defaults to workspace)

Configuration

Usage

Basic Command

Response:

Custom Working Directory

Response:

Pipeline Commands

Response:

Security Model

SecurityPolicy Validation

When a SecurityPolicy is configured, all commands are validated before execution:
Command Risk Levels:
  • Low risk — Read-only operations (ls, cat, grep, pwd, echo)
  • Medium risk — Write operations (touch, mkdir, git add, git commit)
  • High risk — Destructive operations (rm -rf, dd, mkfs, iptables, kill)
Policy Actions: *If require_approval_for_medium_risk = true
**If block_high_risk_commands = true

Example: Approval Required

Response (if approval required):

Example: Blocked Command

Response:

Allowed Commands Allowlist

When allowed_commands is configured, only listed commands are permitted:
Response:

Environment Sanitization

The shell tool clears all environment variables by default, then re-adds only safe functional variables: Safe environment variables:
  • PATH — Command search path
  • HOME — User home directory
  • TERM — Terminal type
  • LANG, LC_ALL, LC_CTYPE — Locale settings
  • USER — Username
  • SHELL — Default shell
  • TMPDIR — Temporary directory
Blocked variables:
  • OPENAI_API_KEY
  • ANTHROPIC_API_KEY
  • GEMINI_API_KEY
  • All other environment variables
This prevents accidental API key leaks via command injection (CWE-200).

Working Directory Control

Default: workspace_dir

By default, commands run in workspace_dir:
Response:

Custom cwd (within workspace)

Response:

Custom cwd (outside workspace)

Requires allowed_paths configuration:
Allowed (in allowed_paths)
Blocked:

cwd Validation Rules

  • cwd must be an absolute path
  • cwd must be within workspace_dir or allowed_paths
  • cwd is resolved with realpathAlloc() to prevent symlink escapes
  • Relative cwd is rejected: “cwd must be an absolute path”

Platform Shells

The shell tool uses the platform’s default shell: Commands are passed as a single string to the shell’s -c flag (Unix) or /c flag (Windows).

Timeout and Output Limits

Timeout

Commands are terminated after timeout_ns nanoseconds:
Example: Long-running command
Response (after 60 seconds):

Output Limit

Stdout and stderr are truncated to max_output_bytes:
Example: Large output
Response:

Error Handling

Command Failure (Non-Zero Exit Code)

Response:
ToolResult.success = false

Signal Termination

Response (after timeout):

Missing Command

Response:

No Output

If a command succeeds but produces no output:
Response:

Use Cases

Git Operations

Build Commands

Testing

System Info

Always validate shell commands for security risks. Use SecurityPolicy to enforce safe command patterns.

Source

src/tools/shell.zig:21-117

Testing

Run shell tool tests:
Tests cover:
  • Basic command execution
  • Path validation (cwd)
  • Policy enforcement
  • Timeout handling
  • Output limits
  • Error cases