# Fence

> Wrap any command in a container-free sandbox that blocks the network by default and limits what it can write

- Category: Agents & Automation / Code Sandboxes & Isolation
- Language: Go
- Licence: Apache-2.0
- Repository: https://github.com/fencesandbox/fence
- Website: https://fencesandbox.com/docs
- Canonical page: https://ai-tldr.dev/tools/fence/

## In short

Fence is a command-line tool that wraps a command in a sandbox which blocks network access by default and restricts filesystem operations according to configurable rules. It sits in the Code Sandboxes & Isolation category of the Agents & Automation tools and is open-source, written in Go and licensed Apache-2.0.

## Overview

Fence is a command-line tool that wraps a command in a sandbox which blocks network access by default and restricts filesystem operations according to configurable rules. You put fence in front of the command — fence npm install, fence -- claude — and it runs with no outbound network unless you allowlist domains, and with writes limited to the paths you choose. It is aimed at semi-trusted code: package installs, build scripts, CI jobs and unfamiliar repositories, where you want controlled side effects rather than "run it and hope".

It is deliberately lightweight. Instead of a container or VM, Fence uses process-level operating-system primitives: sandbox-exec on macOS and bubblewrap on Linux, with outbound traffic filtered through a proxy so that domain allowlists can be enforced. Rules live in a fence.jsonc (or fence.json) file that Fence finds in the current directory or its parents, falling back to a user config under ~/.config/fence. Configs can extend built-in templates such as code, and fence config show prints the inheritance chain and the fully resolved policy as JSON.

Fence also works as a permission manager for CLI coding agents. Its code template is listed as working with Claude Code, Codex, Gemini CLI, OpenCode, Amp, Droid, GitHub Copilot and others, and agent hooks can rewrite an agent's shell tool calls to run through fence -c. The project is explicit about its limits: it is not a hardened containment boundary for actively malicious code, it does not cap CPU, memory or disk, and allowing a domain means code can send data to that domain. For stronger isolation its docs point to Firecracker, gVisor or Kata Containers. Fence can be used as a CLI or as a Go package.

## What it does

- Network isolation: all outbound traffic blocked by default, with domains allowlisted in config
- Filesystem restrictions on read and write paths, plus built-in protection that blocks writes to files such as .git/hooks, shell startup files and IDE config directories
- Command blocking for dangerous commands like rm -rf /, git push or npm publish, and SSH filtering of allowed hosts and commands
- Built-in templates, including code and code-relaxed for guarding CLI coding agents, that configs can extend
- Monitor mode (-m) that logs blocked requests and violations in real time, optionally to a separate log file
- Cross-platform on macOS (sandbox-exec) and Linux (bubblewrap), usable as a CLI or a Go library

## Getting started

Install the CLI, watch it block a network call, then allowlist what you need in a config file and wrap a coding agent with a built-in template.

1. Install Fence

   Use the install script on macOS or Linux, or Homebrew on macOS. go install github.com/fencesandbox/fence/cmd/fence@latest also works.

```bash
curl -fsSL https://cli.fencesandbox.com/install.sh | sh

# or, on macOS
brew tap fencesandbox/tap
brew install fencesandbox/tap/fence
```

2. Add the Linux dependencies

   On Linux, Fence needs bubblewrap for sandboxing and socat for network bridging. Root is not normally required; fence --linux-features shows what your environment supports.

```bash
# Ubuntu/Debian
sudo apt install bubblewrap socat
```

3. Run your first sandboxed command

   With no config, every outbound request is blocked, so this fails with a 403 from the CONNECT tunnel. Use -c for compound shell commands.

```bash
fence curl https://example.com
# curl: (56) CONNECT tunnel failed, response 403

fence -c "echo hello && ls -la"
```

4. Allowlist domains and writes in a config

   fence config init writes a starter config that extends the code template. A project's fence.jsonc can add allowed domains, writable paths and denied commands on top of it.

```json
{
  "$schema": "https://raw.githubusercontent.com/fencesandbox/fence/main/docs/schema/fence.schema.json",
  "extends": "code",
  "network": { "allowedDomains": ["private.company.com"] },
  "filesystem": { "allowWrite": ["."] },
  "command": { "deny": ["git push", "npm publish"] }
}
```

5. Wrap a coding agent and watch what it tries

   Run an agent under the code template, and use monitor mode to see blocked requests. For full-screen TUIs, send Fence's logs to a file and follow them in another terminal. Use -- to separate Fence's flags from the command's.

```bash
fence -t code -- claude

fence -m --fence-log-file /tmp/fence.log -- claude
tail -f /tmp/fence.log
```

6. Expose a dev server port

   -p lets your machine connect to a server inside the sandbox (bound to 127.0.0.1) while outbound network stays restricted.

```bash
fence -p 3000 -c "npm run dev"
```

## When to use it

- Running npm install, pip install or a build script in an unfamiliar repository with default-deny egress and writes scoped to the workspace
- Adding a tool-agnostic, defense-in-depth layer around CLI coding agents such as Claude Code, Codex or Gemini CLI, with one policy shared across them
- Committing a reviewed sandbox config once and applying the same rules on developer machines and in CI jobs
- Auditing what a command tries to reach with monitor mode before deciding which domains to allow
