Skip to main content

Safe Command Execution

Rafter validates shell commands before execution to prevent dangerous operations.

Quick Start

Execute a command with security checks:

How It Works

When you run rafter agent exec, Rafter:
  1. Evaluates the command against security policies
  2. Scans staged files (for git commands)
  3. Assesses risk level (low/medium/high/critical)
  4. Blocks or requires approval based on risk
  5. Logs the execution to audit log
agent.riskLevel (minimal/moderate/aggressive, set by rafter agent init --risk-level or rafter agent config set agent.riskLevel) is stored and shown by rafter agent status, but the interceptor below doesn’t read it — it does not change which commands are blocked or need approval. Only the command’s assessed tier plus commandPolicy.mode/blockedPatterns/requireApproval/allowedPatterns (below) do that.

Risk Levels

Commands that can cause catastrophic damage — hard-blocked unconditionally, no policy, mode, or --force can allow them:
  • rm -rf / or rm -rf /<home|etc|usr|boot|root|sys|proc|lib|lib64|bin|sbin|opt> — delete the filesystem or a critical top-level directory
  • :(){ :|:& };: — fork bomb
  • dd if=... of=/dev/sd... — wipe a disk
  • > /dev/sd... — overwrite a disk
  • mkfs, fdisk, parted — format or repartition
  • Reverse shells — bash -i >& /dev/tcp/..., nc -e/-ve ..., a mkfifo + nc + shell relay, socat ... exec:/system:, and equivalent Python/Perl/Node/PowerShell socket-to-shell one-liners
  • Any statement that names Rafter’s own directory (~/.rafter, $HOME/.rafter, an absolute path, a glob or brace list that expands to it) and is not a known read-only command, and any cd into it — see Command Policies
Commands with significant risk — denied unless a person approves at an interactive terminal (see Approval Model below):
  • rm -rf <anything> / rm -fr — recursive deletion
  • sudo rm — delete with elevated privileges
  • chmod 777 — insecure permissions
  • curl ... | sh / wget ... | sh (or bash/zsh/dash) — pipe to shell
  • git push --force / -f / --force-with-lease / --force-if-includes, or a +branch refspec — force push
  • docker system prune — delete Docker data
  • npm publish — publish a package
  • A pseudo-terminal wrapper around rafter agent exec (script -c, expect, unbuffer, Python’s pty.spawn) — it would fake the interactive terminal that approval requires
Commands the classifier labels medium risk: any use of sudo, chmod, chown, systemctl, service, kill -9, pkill, killall. Today, a medium-tier command is allowed — it isn’t held for approval unless it separately matches a requireApproval pattern you’ve added yourself. The “medium” label still appears in the audit log and in --dry-run output.
Everything that doesn’t match a higher tier — npm install, git commit, ls, cat, grep, echo, touch, and so on.Allowed immediately.

Usage Examples

Safe Command

Executes immediately:

Git Commit with Auto-Scan

Scans staged files before committing:
If secrets are detected in staged files, the commit is blocked:
Note: rafter agent scan still works but is deprecated — it will be removed in a future major version.

High-Risk Command

Requires approval:
At an interactive terminal, you’ll see:
The prompt is only offered when stdin is an interactive TTY. From a script, a CI job, or an agent’s tool call (none of which are an interactive terminal), the same command is denied immediately — exit 1 — with a message telling a person to run it themselves:
A piped yes is not an approval and there is no flag that substitutes for a person at a terminal — anything an agent could pass on --force or on stdin, the agent could pass on its own, so neither route is honored.

Dry Run

Classify a command and print the verdict without running it, scanning staged files, or writing to the audit log:
Exit code is 0 for allowed, 1 for blocked, 2 for requires-approval — useful for a script that wants the verdict without the side effects.

The -- Form

Pass the command as separate words after -- instead of one quoted string; Rafter re-quotes them before classifying, so what the classifier sees is exactly what the shell will run:

Force Execution (No Longer Skips Approval)

--force is still accepted — so old scripts and muscle memory don’t suddenly error — but it is a no-op. It prints a warning and then follows the normal approval path above (deny immediately outside a TTY; prompt at one):
There is no flag that skips approval for a high-risk command. To widen what’s allowed, the machine owner changes commandPolicy in ~/.rafter/config.json (see Command Policies below) — not a per-call flag.

Skip File Scanning

Skip pre-execution scanning for git commands:

Command Policies

Configure how Rafter handles commands:
Rafter’s own security settings are protected from the agent it guards: rafter agent config set on a hooks, commandPolicy or riskLevel key and rafter agent disable <component> rate CRITICAL and are hard-blocked, the same tier as rm -rf /. The PreToolUse hook also denies file-write tool calls (including Codex apply_patch) to anything under ~/.rafter/, including through a symlink that points there. A shell command that names that directory is CRITICAL unless it is a known read-only command with no output redirect: rm, cp, tee, sed -i or echo x > onto ~/.rafter/config.json are blocked, as is cd ~/.rafter, while cat ~/.rafter/config.json | jq ., tail ~/.rafter/audit.jsonl, ls -la ~/.rafter and running ~/.rafter/bin/betterleaks are allowed. A statement that doesn’t name the directory is unaffected, so cat ~/.rafter/config.json && npm test runs. .rafter.yml and .rafterignore are not the directory. Change these settings yourself, outside an agent session.
Dot-paths below are Node’s camelCase (agent.commandPolicy.mode); Python takes the same keys in snake_case (agent.command_policy.mode). config get rejects the other runtime’s casing (Key not found); config set does not validate the path at all, so setting the wrong casing reports success and silently changes nothing. See Configuration Keys for details.

Policy Modes

Critical commands are always blocked regardless of mode. commandPolicy.mode only changes what happens to a high-risk command that isn’t separately caught by blockedPatterns or requireApproval:

Custom Blocked Patterns

View or replace the list of patterns that are always denied (unanchored regex, matched case-insensitively against the sanitized command):
agent config set writes any key path you give it, including a JSON array value, without validating it against anything — so a typo’d key silently does nothing. There’s no “add one pattern” command; to keep the built-in deny-list and add to it, include both in the array you set (or edit ~/.rafter/config.json directly). A project .rafter.yml can only make blockedPatterns/requireApproval stricter — see Policy File.

Custom Approval Patterns

Same idea for patterns that require approval:

Allowlist Patterns

allowedPatterns is the inverse: an unanchored regex that forces a matching command to low and skips the approval prompt, for a known-safe command that would otherwise trip a broader rule (the motivating case is git push --force-with-lease to a feature branch whose main is protected server-side):
blockedPatterns always takes precedence, a critical command is never allowlisted, and a match is ignored when the command contains more than one statement (&&, ||, ;, |, &, newline). It’s also a grant, not a restriction, so it’s honored only from the machine owner’s global config: a project .rafter.yml can tighten blockedPatterns/requireApproval, but it cannot add to allowedPatterns unless the owner has separately set agent.commandPolicy.allowProjectOverride: true in the global config.

Audit Logging

All command executions are logged:
Audit entries include:
  • Timestamp
  • Command executed
  • Risk level
  • Action taken (allowed/blocked/overridden)
See Audit Log for the full schema.

Integration with Agents

OpenClaw

When integrated with OpenClaw, commands are automatically routed through Rafter:

Claude Code

Claude Code integration uses the PreToolUse hook (rafter hook pretool) to intercept commands before execution, plus MCP tools for agent-initiated scans. The hook can only allow or deny — it has no way to prompt — so a command that would need approval from rafter agent exec is denied by the hook, with a reason telling a person to run it themselves: rafter agent exec '<command>' at an interactive terminal. If the hook hits an internal error while evaluating a call, it denies rather than allows, with reason rafter hook error: <type>: <message>; denying to be safe. A malformed .rafter.yml is a handled condition, not an error — it produces a warning and falls back to default settings, not a denial. See Claude Code Integration for setup, and MCP Integration for the MCP server.

Exit Codes

For a normal (non---dry-run) rafter agent exec:
  • 0 — the command ran and exited successfully
  • 1 — denied (critical, blocked pattern, or approval denied/unavailable), or the command ran and failed — in the failure case, Rafter passes through the command’s own exit code, not always 1
  • 2 — the pre-execution secret scan on a git command couldn’t run (e.g. a staged diff that failed to read); the commit is blocked without ever executing
For --dry-run, the exit code is the classification itself, independent of whether anything would have run: 0 allowed, 1 blocked, 2 requires approval. Use in scripts:

Best Practices

Recommended Practices

  1. Always use for git commits: Evaluates risk and scans staged files
  2. Don’t rely on --force: it no longer skips approval — use --dry-run to check a command’s classification instead
  3. Review audit logs: Check rafter agent audit after suspicious activity
  4. Configure policies: Adjust commandPolicy.mode and the pattern lists for your environment
  5. Test in development: Use --dry-run to confirm a policy change classifies commands the way you expect before deploying to agents

Advanced Configuration

Custom Risk Patterns

There’s no way to declare a custom severity label beyond low/medium/high/critical, or to change which built-in commands land in which tier. What you can do today is add to blockedPatterns (always deny), requireApproval (always gate), or allowedPatterns (force low and skip approval) — see Command Policies above.

Troubleshooting

Command Incorrectly Blocked

If a safe command is blocked or keeps asking for approval:
  1. Check current policy and which pattern matched — the --dry-run output names the matched pattern:
  2. If it’s a critical hard-block (rm -rf /, a reverse-shell pattern, etc.), it cannot be allowed by any config change — that’s by design.
  3. If it’s caught by requireApproval or blockedPatterns, switching commandPolicy.mode to allow-all will not help — those two lists are checked unconditionally, regardless of mode. Either remove the matching entry from requireApproval/blockedPatterns, or add a narrower allowedPatterns entry for just this command (see Allowlist Patterns above).
  4. If it’s only the default approve-dangerous high-risk gate (no explicit requireApproval match), rafter agent config set agent.commandPolicy.mode allow-all does remove that gate.
  5. Report false positive: rafter-cli/issues

Secrets Not Detected

If secrets aren’t caught during git commits:
  1. Ensure scanning is enabled (default):
  2. Test scanner separately:

Next Steps

OpenClaw Integration

Set up with OpenClaw agents

Command Reference

Complete CLI command reference