RiskboundDocs

Policy library

Running code or shell commands

An agent that can run commands can do anything the machine can. The real control is where it runs. This template makes the sandbox the line, and adds a short deny list as a backstop.

What it guards#

ToolWhen an agent calls it
run_codeTo run a shell command or a script

Params#

ParamTypeRequiredMeaning
commandstringyesThe full command line
sandboxedbooleanyestrue if it runs in an isolated container or VM with no access to production
networkbooleanyestrue if it can reach the network

Set sandboxed and network from how your code will run the command, not from what the model says.

Outcomes#

RequestOutcomePolicy id
Sandboxed, no networkallowcode-sandboxed-offline
Sandboxed, with networkask_humancode-sandboxed-network
Not sandboxed, caller has a dry rundry_runcode-show-only
Not sandboxed, no dry runblockno_matching_policy
Matches a deny patternblockcode-denied-patterns

Tune it#

  • Add patterns to code-denied-patterns. In Cedar like, * matches any text. Patterns are easy to get around, so never rely on them alone.
  • To let some commands run on the host, add a narrow act permit, such as when { context.command == "git status" }.

The policy#

This is code-execution.cedar as it is in the repository.

cedar
// Running code or shell commands.
//
// Tool: run_code
// Params: command (string), sandboxed (boolean), network (boolean)
//
//   in a sandbox, no network            allow
//   in a sandbox, with network          ask a human
//   outside a sandbox                   dry run (show the command only)
//   a command matching a deny pattern   block
//
// The deny patterns are a backstop, not a sandbox. A determined command
// can be written to avoid any pattern. The real control is `sandboxed`.

@id("code-sandboxed-offline")
permit (principal, action == Action::"act", resource == Tool::"run_code")
when { context.sandboxed && !context.network };

@id("code-sandboxed-network")
permit (principal, action == Action::"act_with_approval", resource == Tool::"run_code")
when { context.sandboxed };

@id("code-show-only")
permit (principal, action == Action::"dry_run", resource == Tool::"run_code");

@id("code-denied-patterns")
forbid (principal, action, resource == Tool::"run_code")
when {
  context.command like "*rm -rf /*" ||
  context.command like "*sudo *" ||
  context.command like "*| sh" ||
  context.command like "*| sh *" ||
  context.command like "*| bash" ||
  context.command like "*| bash *" ||
  context.command like "*mkfs*" ||
  context.command like "*/etc/shadow*"
};