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#
| Tool | When an agent calls it |
|---|---|
run_code | To run a shell command or a script |
Params#
| Param | Type | Required | Meaning |
|---|---|---|---|
command | string | yes | The full command line |
sandboxed | boolean | yes | true if it runs in an isolated container or VM with no access to production |
network | boolean | yes | true 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#
| Request | Outcome | Policy id |
|---|---|---|
| Sandboxed, no network | allow | code-sandboxed-offline |
| Sandboxed, with network | ask_human | code-sandboxed-network |
| Not sandboxed, caller has a dry run | dry_run | code-show-only |
| Not sandboxed, no dry run | block | no_matching_policy |
| Matches a deny pattern | block | code-denied-patterns |
Tune it#
- Add patterns to
code-denied-patterns. In Cedarlike,*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
actpermit, such aswhen { context.command == "git status" }.
The policy#
This is code-execution.cedar as it is in the repository.
// 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*"
};