sbx driver

The driver for sandboxes on one machine. This page is for whoever runs kōjō on a single host with hardware virtualization, as the single VM install does; Sandboxes compares it with the other kinds.

The sbx driver runs each session in a Docker Sandboxes microVM with its own kernel and a private Docker daemon. It drives the sbx CLI on the host: every operation is an sbx command (sbx create, sbx exec, sbx secret set, sbx policy ..., sbx rm).

What the driver assumes beyond Docker’s documentation is listed in assumptions.go.

Requirements

  • kōjō runs on the same host as sbx, as a host process and not in a container. Postgres can run in compose next to it.
  • sbx 0.48.0 or newer.
  • Hardware virtualization: a usable /dev/kvm on Linux (KVM on, the user in the kvm group), or Apple silicon on macOS.
  • The user kōjō runs as has signed in and set the deny-all network preset:
sbx login
sbx policy init deny-all

kōjō expects the deny-all preset. Under balanced or allow-all, every sandbox can also reach the preset’s hosts, and under allow-all the driver reports egress as not enforced. To switch a host that already has another preset:

sbx policy reset --force && sbx policy init deny-all

Configuration

The sandbox block of kojo.yaml:

sandbox:
  driver: sbx
  ttl: 4h
  sbx:
    binary: sbx
    kits_dir: /var/lib/kojo/sbx-kits
Key Default Meaning
sandbox.sbx.binary sbx on PATH The sbx CLI.
sandbox.sbx.kits_dir <user config dir>/kojo/sbx-kits Where kōjō writes the kit it generates per skill and its per-sandbox state. Every kōjō process that should see the same sandboxes must share it.

How it works

  • For each sandbox, kōjō generates an sbx kit (v3 format): a descriptor with the skill’s egress allowlist and one credential route per credential and host, and a Dockerfile that starts FROM the skill’s image. The kit holds names and hosts, never a secret.
  • sbx keeps the image of every kit it builds (several hundred megabytes each). kōjō’s reaper removes a kit and its image once no sandbox has used it for a day. make dev-down removes the ones of the dev stack at once.
  • No host directory is shared with a sandbox. The workspace lives inside the microVM.

Egress and credentials

  • sbx has its own proxy on the host, outside the microVM. It enforces the allowlist and sets the credential headers. kojo-proxy is not used with this driver, so sandbox.proxy has no effect.
  • An allowlist entry without a port allows every port of its host in sbx. kojo-proxy allows only 443 and 80 for such an entry. An entry with a port, registry.example.com:5000, goes to sbx as written and allows that port.
  • Credential values are handed to sbx secret set --sandbox on stdin. They appear in no kit, no environment and no command line.
  • kōjō writes sbx’s credential approvals file (~/.config/sbx/credentials.yaml) itself, because there is no terminal to approve a kit’s credentials on.
  • Inside a sandbox, localhost is the microVM itself. A forge that runs on the same machine as kōjō under a loopback name (the dev stack’s Forgejo on localhost:3000) is reached as host.docker.internal: when a policy allows localhost or 127.0.0.1, with or without a port, the driver passes git configuration that rewrites such URLs, on every command. Only git is covered. A forge with a name of its own needs none of this.

Read-only credentials

Every forge’s clone credential is read-only: on a host that serves git it may be set on git’s fetch and nothing else, and on a host that serves only an API on reads. sbx’s proxy sets a credential on every request to its host and cannot send a request on without it, so the driver adds sandbox-scoped HTTP deny rules (sbx policy deny network --sandbox ID HOST --method … --path …) for each host of a read-only credential, and what they match is refused. Which rules depends on what the host serves:

  • A host that serves only an API (api.github.com, api.bitbucket.org) gets one rule: POST, PUT, PATCH and DELETE on any path.
  • A host that serves git (github.com or a GitHub Enterprise Server host, a GitLab or Forgejo host, bitbucket.org) gets 38:
    • HEAD, OPTIONS, PUT, PATCH and DELETE on any path;
    • GET and POST under a forge’s API route, at any depth: /api/v1 (Forgejo, Gitea), /api/v3 (GitHub Enterprise Server), /api/v4 (GitLab), /api/1.0 and /api/2.0 (bitbucket.org), /api/graphql, /api/forgejo, /api/packages, and Bitbucket’s /!api. A fetch from a GitLab subgroup path such as /acme/api/v4/… is refused too;
    • POST to any path whose last segment does not start with git-u, and to a top-level /git-u…;
    • GET of any path whose last two segments are not info/refs, and of /info/refs and any one-segment path. That covers the API’s reads, which hand out secrets to the bot (a GitLab project’s CI/CD variables and runners token, a Forgejo repository’s Actions variables and webhooks), the routes that answer with a registry or runner registration token, and the forge’s web pages.

sbx matches the rules against the request’s path after decoding percent-escapes and dropping empty and . segments and a trailing slash, so /%61pi/v4/…, /api//v4/… and /post/ meet the rules for /api/v4/… and /post. It refuses a path with a .. segment before any rule, with a 403 whose body says “Denied due to an invalid authorization request”. Both were measured on sbx 0.55.0.

sbx refuses a request the rules match with its own 403, whose body starts Blocked by local rule; the request does not reach the forge. kojo-proxy sends such a request on without the credential; sbx does not send it at all, so on sbx a sandbox reaches a forge’s git host for git’s fetch only. A request that works anonymously, such as a public repository’s API, pages and archives, or Git LFS’s batch request (POST …/info/lfs/objects/batch), fails there.

flowchart TD
    R["Request from the sandbox to a host with a read-only credential"] --> N["sbx decodes escapes and drops empty and . segments and a trailing slash"]
    N --> DOT{"A .. segment?"}
    DOT -- yes --> BAD["Refused by sbx: 403, invalid authorization request"]
    DOT -- no --> K{"What the host serves"}
    K -- "an API only" --> AM{"Method"}
    AM -- "GET, HEAD, OPTIONS" --> SET["Sent with the credential"]
    AM -- "POST, PUT, PATCH, DELETE" --> DENY["Refused by sbx: 403, Blocked by local rule"]
    K -- git --> A{"Under /api/v1, v3, v4, 1.0, 2.0, graphql, forgejo, packages, or /!api?"}
    A -- yes --> DENY
    A -- no --> M{"Method"}
    M -- "HEAD, OPTIONS, PUT, PATCH, DELETE" --> DENY
    M -- GET --> GR{"Last two segments info/refs, after another segment?"}
    GR -- "no: API reads, web pages, token routes" --> DENY
    GR -- "yes: git's ref discovery, whatever the query" --> SET
    M -- POST --> G{"Last segment starts with git-u, after another segment?"}
    G -- "no: git-receive-pack, LFS, any other POST" --> DENY
    G -- "yes: git's fetch, with or without .git" --> SET

The rules go in right after sbx create and before the credential’s value is stored, so the credential is never set on a write. ApplyPolicy adds them for a host that became read-only and removes them, by rule ID, once a credential is withdrawn or no longer read-only. The driver’s state file keeps the IDs and a fingerprint of the rule set they came from: when the current set differs (a newer kōjō with other rules, or a host that now serves only an API), ApplyPolicy adds the current rules first and removes the old ones after, so the host is never without rules. sbx rm removes them with the sandbox.

kojo-proxy’s rule is exact; sbx cannot express it, because a deny always beats an allow, a path glob cannot say “not this suffix”, and no rule matches a query. On a git host sbx lets some requests through with the credential that kojo-proxy sends without it: …/info/refs with another query (a push’s ref discovery, service=git-receive-pack, or git’s older protocol with none, which list the same refs as a fetch’s; the push itself is refused), git’s paths in another case, a POST whose last segment only starts with git-u (git-upload-packs, git-upload-archive), a path that sbx cleans or decodes into git’s (//, ., a trailing slash, a percent-escape), an API version no forge uses (/api/v2/…, /api/3.0/…), a POST to /, and methods no forge serves. No forge writes or hands out a secret there. Each rule takes about 0.4 seconds to add, so a sandbox with a forge’s clone credential (one git host, 38 rules) takes about 15 seconds longer to create, and an API-only host adds one more.

Limits

  • sbx fixes a sandbox’s credential routes when it is created. kōjō therefore creates the sandbox with every credential the session’s skills on that image declare. ApplyPolicy refreshes their values and changes the allowlist, but cannot add a credential later.
  • The read-only rules are per host: a credential that may write, on the same host as a read-only one, is held to them too.
  • *.example.com matches one label in sbx, not any depth. A credential host cannot be a wildcard.
  • sbx has no disk size setting: a kit’s resources.disk is ignored. CPU is rounded up to whole cores, and memory has a floor of 512 MiB, after sandbox.max_resources has lowered them.
  • sbx has no TTL for local sandboxes. kōjō removes expired ones itself (see Resources, timeouts and lifetime).
Scroll to move. + - zoom, 0 fit, Esc close.100%