Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

feat: stock-mode sandbox network reaches only package registries

ogsamrat2bc89a681845
diff --git a/apps/desktop/box/go/cmd/boxd/egress.go b/apps/desktop/box/go/cmd/boxd/egress.go
new file mode 100644
index 00000000..73ae3f63
--- /dev/null
+++ b/apps/desktop/box/go/cmd/boxd/egress.go
@@ -0,0 +1,227 @@
+//go:build linux
+
+// Farm egress (BOXD_EGRESS=farm): the agent's commands run in a network
+// namespace of their own that has only loopback, so nothing in the box
+// reaches the internet directly (git remotes, curl, requests.get, fetch all
+// find no route or no DNS). boxd itself stays on the box's network: the
+// launcher still reaches it, and it runs a small HTTP/CONNECT proxy whose
+// listener is inside the commands' namespace (127.0.0.1) and whose dials are
+// outside it. The proxy lets through the package registries in farmAllow
+// and answers 403 with a readable body for every other host, at once.
+// Commands get HTTP(S)_PROXY and the package managers' proxy settings.
+//
+// Where boxd may not make a namespace (a container without CAP_SYS_ADMIN),
+// the proxy still runs and the variables are still set, but a program that
+// ignores the proxy is not stopped: boxd logs "egress: proxy only".
+package main
+
+import (
+ "fmt"
+ "io"
+ "log"
+ "net"
+ "net/http"
+ "runtime"
+ "strconv"
+ "strings"
+ "sync/atomic"
+ "syscall"
+ "time"
+ "unsafe"
+)
+
+// farmAllow: the hosts the farm proxy lets through (exact, or "*." for any subdomain).
+// The registries the farm asked for, plus the mirrors the box images themselves use
+// (Debian apt; Node headers for node-gyp; Maven and RubyGems for the java and ruby stacks).
+var farmAllow = []string{
+ "registry.npmjs.org", "registry.yarnpkg.com",
+ "pypi.org", "files.pythonhosted.org",
+ "index.crates.io", "static.crates.io", "crates.io",
+ "proxy.golang.org", "sum.golang.org",
+ "repo.packagist.org",
+ "deb.debian.org", "security.debian.org",
+ "nodejs.org",
+ "repo.maven.apache.org", "repo1.maven.org",
+ "rubygems.org", "index.rubygems.org",
+}
+
+// The proxy's port inside the commands' namespace (0: no farm egress) and that namespace (-1: none).
+var (
+ egressPort atomic.Int64
+ egressNS = -1
+)
+
+// setns(2): not in package syscall (the box helpers are built for amd64 and arm64).
+var sysSetns = map[string]uintptr{"amd64": 308, "arm64": 268}[runtime.GOARCH]
+
+func allowedHost(host string) bool {
+ host = strings.TrimSuffix(strings.ToLower(host), ".")
+ for _, a := range farmAllow {
+ if host == a || strings.HasPrefix(a, "*.") && strings.HasSuffix(host, a[1:]) {
+ return true
+ }
+ }
+ return false
+}
+
+// startEgress makes the commands' namespace and starts the proxy in it.
+func startEgress(mode string) error {
+ if mode != "farm" {
+ return fmt.Errorf("unknown BOXD_EGRESS %q", mode)
+ }
+ type made struct {
+ ln net.Listener
+ ns int
+ err error
+ }
+ ch := make(chan made, 1)
+ go func() {
+ // Never unlocked: this thread's namespace changes, so the runtime drops it at the end.
+ runtime.LockOSThread()
+ ns := -1
+ if err := syscall.Unshare(syscall.CLONE_NEWNET); err != nil {
+ log.Printf("egress: proxy only (no network namespace: %v)", err)
+ } else if err := loopbackUp(); err != nil {
+ ch <- made{err: fmt.Errorf("loopback: %w", err)}
+ return
+ } else if ns, err = syscall.Open("/proc/thread-self/ns/net", syscall.O_RDONLY|syscall.O_CLOEXEC, 0); err != nil {
+ ch <- made{err: err}
+ return
+ }
+ ln, err := net.Listen("tcp", "127.0.0.1:0")
+ ch <- made{ln, ns, err}
+ }()
+ m := <-ch
+ if m.err != nil {
+ return m.err
+ }
+ egressNS = m.ns
+ egressPort.Store(int64(m.ln.Addr().(*net.TCPAddr).Port))
+ go (&http.Server{Handler: http.HandlerFunc(proxy), ReadHeaderTimeout: 30 * time.Second}).Serve(m.ln)
+ log.Printf("egress: farm proxy on 127.0.0.1:%d (namespace=%v)", egressPort.Load(), egressNS >= 0)
+ return nil
+}
+
+// loopbackUp sets lo up in the current (new) network namespace.
+func loopbackUp() error {
+ fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM|syscall.SOCK_CLOEXEC, 0)
+ if err != nil {
+ return err
+ }
+ defer syscall.Close(fd)
+ var ifr [40]byte // struct ifreq: name[16], then flags (short)
+ copy(ifr[:], "lo")
+ if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.SIOCGIFFLAGS, uintptr(unsafe.Pointer(&ifr[0]))); e != 0 {
+ return e
+ }
+ *(*uint16)(unsafe.Pointer(&ifr[16])) |= syscall.IFF_UP
+ if _, _, e := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), syscall.SIOCSIFFLAGS, uintptr(unsafe.Pointer(&ifr[0]))); e != 0 {
+ return e
+ }
+ return nil
+}
+
+// inNet runs fn on a thread in the commands' namespace (a process fn starts is born there);
+// without farm egress, fn runs as is.
+func inNet(fn func() error) error {
+ if egressNS < 0 {
+ return fn()
+ }
+ ch := make(chan error, 1)
+ go func() {
+ runtime.LockOSThread() // never unlocked: the runtime drops the thread when this goroutine ends
+ if _, _, e := syscall.Syscall(sysSetns, uintptr(egressNS), syscall.CLONE_NEWNET, 0); e != 0 {
+ ch <- fmt.Errorf("enter the sandbox network: %w", e)
+ return
+ }
+ ch <- fn()
+ }()
+ return <-ch
+}
+
+// egressEnv: what each command gets so the tools that honour a proxy use the farm proxy.
+func egressEnv() map[string]string {
+ port := egressPort.Load()
+ if port == 0 {
+ return nil
+ }
+ url := "http://127.0.0.1:" + strconv.FormatInt(port, 10)
+ noProxy := "localhost,127.0.0.1,::1"
+ return map[string]string{
+ "HTTP_PROXY": url, "HTTPS_PROXY": url, "http_proxy": url, "https_proxy": url,
+ "NO_PROXY": noProxy, "no_proxy": noProxy,
+ "npm_config_proxy": url, "npm_config_https_proxy": url,
+ "YARN_HTTP_PROXY": url, "YARN_HTTPS_PROXY": url,
+ "PIP_PROXY": url, "CARGO_HTTP_PROXY": url,
+ "GOPROXY": "https://proxy.golang.org", // no ",direct": a module not on the proxy fails with its message
+ }
+}
+
+func blocked(w http.ResponseWriter, host string) {
+ w.Header().Set("Content-Type", "text/plain; charset=utf-8")
+ w.Header().Set("X-Omnirush-Blocked", host)
+ w.WriteHeader(http.StatusForbidden)
+ fmt.Fprintf(w, "omnirush: %s is blocked in this environment (no internet access here; only package registries are reachable)\n", host)
+}
+
+var direct = &http.Transport{Proxy: nil, DialContext: (&net.Dialer{Timeout: 15 * time.Second}).DialContext, ResponseHeaderTimeout: 60 * time.Second}
+
+func proxy(w http.ResponseWriter, r *http.Request) {
+ host, port := r.Host, ""
+ if h, p, err := net.SplitHostPort(r.Host); err == nil {
+ host, port = h, p
+ }
+ if r.Method == http.MethodConnect {
+ if port != "443" || !allowedHost(host) {
+ log.Printf("egress: blocked CONNECT %s", r.Host)
+ blocked(w, r.Host)
+ return
+ }
+ upstream, err := net.DialTimeout("tcp", r.Host, 15*time.Second)
+ if err != nil {
+ http.Error(w, "omnirush: "+err.Error(), http.StatusBadGateway)
+ return
+ }
+ client, rw, err := http.NewResponseController(w).Hijack()
+ if err != nil {
+ upstream.Close()
+ return
+ }
+ _, _ = io.WriteString(client, "HTTP/1.1 200 Connection established\r\n\r\n")
+ go func() { _, _ = rw.Reader.WriteTo(upstream); upstream.(*net.TCPConn).CloseWrite() }()
+ _, _ = io.Copy(client, upstream)
+ client.Close()
+ upstream.Close()
+ return
+ }
+ // A plain http:// request through the proxy (apt, some pip mirrors).
+ if r.URL.Scheme != "http" || (port != "" && port != "80") || !allowedHost(host) {
+ log.Printf("egress: blocked %s %s", r.Method, r.URL.Redacted())
+ blocked(w, r.Host)
+ return
+ }
+ r.RequestURI = ""
+ r.Header.Del("Proxy-Connection")
+ r.Header.Del("Proxy-Authorization")
+ res, err := direct.RoundTrip(r)
+ if err != nil {
+ http.Error(w, "omnirush: "+err.Error(), http.StatusBadGateway)
+ return
+ }
+ defer res.Body.Close()
+ for k, vs := range res.Header {
+ for _, v := range vs {
+ w.Header().Add(k, v)
+ }
+ }
+ w.WriteHeader(res.StatusCode)
+ _, _ = io.Copy(w, res.Body)
+}
+
+// netProc: where the commands' network's /proc files are, read from a thread in it.
+func netProc() string {
+ if egressNS < 0 {
+ return "/proc/net/"
+ }
+ return "/proc/thread-self/net/"
+}
diff --git a/apps/desktop/box/go/cmd/boxd/egress_test.go b/apps/desktop/box/go/cmd/boxd/egress_test.go
new file mode 100644
index 00000000..f6ff3763
--- /dev/null
+++ b/apps/desktop/box/go/cmd/boxd/egress_test.go
@@ -0,0 +1,83 @@
+//go:build linux
+
+package main
+
+import (
+ "bufio"
+ "io"
+ "net"
+ "net/http"
+ "strings"
+ "testing"
+ "time"
+)
+
+func TestAllowedHost(t *testing.T) {
+ for host, want := range map[string]bool{
+ "registry.npmjs.org": true, "PyPI.org.": true, "files.pythonhosted.org": true, "static.crates.io": true,
+ "proxy.golang.org": true, "repo.packagist.org": true,
+ "api.github.com": false, "github.com": false, "raw.githubusercontent.com": false, "evil.registry.npmjs.org.attacker.io": false,
+ "x.fly.dev": false, "api.crossref.org": false, "127.0.0.1": false, "": false,
+ } {
+ if got := allowedHost(host); got != want {
+ t.Errorf("allowedHost(%q) = %v, want %v", host, got, want)
+ }
+ }
+}
+
+func startProxy(t *testing.T) string {
+ t.Helper()
+ ln, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatal(err)
+ }
+ srv := &http.Server{Handler: http.HandlerFunc(proxy)}
+ go srv.Serve(ln)
+ t.Cleanup(func() { srv.Close() })
+ return ln.Addr().String()
+}
+
+func TestProxyRefusesFast(t *testing.T) {
+ addr := startProxy(t)
+ for _, req := range []string{
+ "CONNECT api.github.com:443 HTTP/1.1\r\nHost: api.github.com:443\r\n\r\n",
+ "CONNECT registry.npmjs.org:22 HTTP/1.1\r\nHost: registry.npmjs.org:22\r\n\r\n",
+ "GET http://zhihu.com/ HTTP/1.1\r\nHost: zhihu.com\r\n\r\n",
+ } {
+ start := time.Now()
+ c, err := net.Dial("tcp", addr)
+ if err != nil {
+ t.Fatal(err)
+ }
+ io.WriteString(c, req)
+ res, err := http.ReadResponse(bufio.NewReader(c), nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ body, _ := io.ReadAll(io.LimitReader(res.Body, 4096))
+ c.Close()
+ if res.StatusCode != 403 || !strings.Contains(string(body), "is blocked in this environment") {
+ t.Errorf("%q: %d %q", req, res.StatusCode, body)
+ }
+ if time.Since(start) > time.Second {
+ t.Errorf("%q: refusal took %v", req, time.Since(start))
+ }
+ }
+}
+
+func TestEgressEnvOnlyUnderFarm(t *testing.T) {
+ cfg := config{home: "/root", baseEnv: []string{"PATH=/usr/bin"}}
+ for _, kv := range commandEnv(cfg, nil) {
+ if strings.Contains(strings.ToLower(kv), "proxy") {
+ t.Fatalf("normal box got %s", kv)
+ }
+ }
+ egressPort.Store(4321)
+ t.Cleanup(func() { egressPort.Store(0) })
+ env := strings.Join(commandEnv(cfg, map[string]string{"HTTPS_PROXY": "http://elsewhere"}), "\n")
+ for _, want := range []string{"HTTPS_PROXY=http://127.0.0.1:4321", "https_proxy=http://127.0.0.1:4321", "NO_PROXY=localhost,127.0.0.1,::1", "npm_config_https_proxy=http://127.0.0.1:4321"} {
+ if !strings.Contains(env, want+"\n") {
+ t.Errorf("farm env lacks %s", want)
+ }
+ }
+}
diff --git a/apps/desktop/box/go/cmd/boxd/main.go b/apps/desktop/box/go/cmd/boxd/main.go
index a8c4560a..cf1527bc 100644
--- a/apps/desktop/box/go/cmd/boxd/main.go
+++ b/apps/desktop/box/go/cmd/boxd/main.go
@@ -20,6 +20,7 @@
// BOXD_IDLE_EXIT minutes with no request after which it exits (0: never; the
// bubblewrap and WSL boxes, which have no engine to keep them)
// BOXD_SHADOWS the build folders boxrun init shadowed (JSON list; shadows.go)
+// BOXD_EGRESS "farm": commands get no internet but the package registries (egress.go)
package main
import (
@@ -65,6 +66,7 @@ type config struct {
idle time.Duration
baseEnv []string
shadows string
+ egress string
}
// snapshotGate lets a snapshot read the files with no new command starting
@@ -153,6 +155,12 @@ func main() {
n, err := writeBaseline()
log.Printf("baseline: %d entries in %v (err=%v)", n, time.Since(start), err)
}
+ if cfg.egress != "" {
+ // Before the port file: no command starts on a box whose network is not set yet.
+ if err := startEgress(cfg.egress); err != nil {
+ log.Fatalf("egress: %v", err)
+ }
+ }
ln, err := net.Listen("tcp", cfg.listen)
if err != nil {
log.Fatalf("listen: %v", err)
@@ -246,6 +254,7 @@ func loadConfig() config {
portFile: os.Getenv("BOXD_PORT_FILE"),
idle: time.Duration(atoi("BOXD_IDLE_EXIT")) * time.Minute,
shadows: os.Getenv("BOXD_SHADOWS"),
+ egress: os.Getenv("BOXD_EGRESS"),
}
if cfg.shell == "" {
cfg.shell = "/bin/bash"
@@ -259,7 +268,7 @@ func loadConfig() config {
}
cfg.baseEnv = append(cfg.baseEnv, kv)
}
- for _, k := range []string{"BOXD_TOKEN", "BOXD_LISTEN", "BOXD_UID", "BOXD_GID", "BOXD_HOME", "BOXD_ROOT", "BOXD_PATH", "BOXD_ENV_FILE", "BOXD_SHELL", "BOXD_PORT_FILE", "BOXD_IDLE_EXIT", "BOXD_SHADOWS"} {
+ for _, k := range []string{"BOXD_TOKEN", "BOXD_LISTEN", "BOXD_UID", "BOXD_GID", "BOXD_HOME", "BOXD_ROOT", "BOXD_PATH", "BOXD_ENV_FILE", "BOXD_SHELL", "BOXD_PORT_FILE", "BOXD_IDLE_EXIT", "BOXD_SHADOWS", "BOXD_EGRESS"} {
_ = os.Unsetenv(k)
}
if cfg.token == "" {
@@ -534,6 +543,9 @@ func commandEnv(cfg config, extra map[string]string) []string {
for k, v := range extra {
set(k, v)
}
+ for k, v := range egressEnv() {
+ set(k, v)
+ }
delete(env, "BOXD_CHOWN_DIRS")
// The package-manager wrappers' folder, written by the launcher after the start.
if pre, ok := env["BOXD_PATH_PREFIX"]; ok {
@@ -598,7 +610,7 @@ func runExec(cfg config, c *conn, req proto.Request, start time.Time) {
return
}
cmd.Stdout, cmd.Stderr = outW, errW
- if err := cmd.Start(); err != nil {
+ if err := inNet(cmd.Start); err != nil {
outW.Close()
errW.Close()
outR.Close()
diff --git a/apps/desktop/box/go/cmd/boxd/ports.go b/apps/desktop/box/go/cmd/boxd/ports.go
index da29d0fd..db70d6b6 100644
--- a/apps/desktop/box/go/cmd/boxd/ports.go
+++ b/apps/desktop/box/go/cmd/boxd/ports.go
@@ -45,7 +45,10 @@ const tcpListen = "0A"
func listeners() []proto.Listener {
byPort := map[int]proto.Listener{}
for _, file := range procNetFiles {
- f, err := os.Open(file)
+ var f *os.File
+ var err error
+ // /proc/net is boxd's own network; thread-self/net is the commands' one under farm egress.
+ _ = inNet(func() error { f, err = os.Open(strings.Replace(file, "/proc/net/", netProc(), 1)); return nil })
if err != nil {
continue
}
@@ -175,7 +178,7 @@ func connectPort(c *conn, req proto.Request, start time.Time) {
var target net.Conn
var lastErr error
for _, addr := range dialTargets(req.Port, listeners()) {
- target, lastErr = net.DialTimeout("tcp", addr, 3*time.Second)
+ _ = inNet(func() error { target, lastErr = net.DialTimeout("tcp", addr, 3*time.Second); return nil })
if lastErr == nil {
break
}
diff --git a/apps/desktop/box/go/cmd/boxrun/main.go b/apps/desktop/box/go/cmd/boxrun/main.go
index 1557d17d..2d00581d 100644
--- a/apps/desktop/box/go/cmd/boxrun/main.go
+++ b/apps/desktop/box/go/cmd/boxrun/main.go
@@ -622,6 +622,7 @@ type state struct {
Started int64 `json:"started"`
Version string `json:"version"`
Shadows []string `json:"shadows,omitempty"`
+ Egress string `json:"egress,omitempty"` // boxd's BOXD_EGRESS ("" = the box's own network as is)
}
func readState(box string) *state {
@@ -727,8 +728,10 @@ func cmdUp(args []string) error {
*p, _ = filepath.Abs(*p)
}
start := time.Now()
+ egress := envValue(envs, "BOXD_EGRESS")
+ // A box running with another egress (a farm run on a normal box, or back) starts again.
if s := readState(*box); alive(s) {
- if port := readPort(*portFile); port > 0 && portOpen(port) {
+ if port := readPort(*portFile); port > 0 && portOpen(port) && s.Egress == egress {
// A box started by an older boxrun is registered now (best effort).
if boxImage(*box) != *image && !strings.ContainsAny(*image, "/\\.") {
if unlock, err := (oci.Store{Dir: *store}).Lock(*image); err == nil {
@@ -951,7 +954,7 @@ func cmdUp(args []string) error {
}
// The mount points init made on the host, kept in the box folder (the box never sees it).
recordShadowMade(*box, initInfo.Made)
- s := state{Pid: cmd.Process.Pid, Port: port, Launcher: mode, IDMap: ids.Mode, Overlay: initInfo.Overlay, Image: *image, Started: time.Now().Unix(), Version: version, Shadows: initInfo.Shadows}
+ s := state{Pid: cmd.Process.Pid, Port: port, Launcher: mode, IDMap: ids.Mode, Overlay: initInfo.Overlay, Image: *image, Started: time.Now().Unix(), Version: version, Shadows: initInfo.Shadows, Egress: egress}
_ = writeJSONFile(filepath.Join(*box, "state.json"), s)
emit(map[string]any{"ok": true, "reused": false, "pid": s.Pid, "port": port, "launcher": mode, "idmap": ids.Mode, "overlay": s.Overlay, "shadows": nonNil(s.Shadows), "ms": time.Since(start).Milliseconds()})
if *foreground {
diff --git a/apps/server/src/box/backends/bwrap.ts b/apps/server/src/box/backends/bwrap.ts
index 6c6dc3b5..435c091d 100644
--- a/apps/server/src/box/backends/bwrap.ts
+++ b/apps/server/src/box/backends/bwrap.ts
@@ -10,7 +10,8 @@
// unpacks it (~/.omnirush/box/bwrap/store), and runs each project's box as
// that root filesystem under a per-project overlay upper layer
// (~/.omnirush/box/bwrap/boxes/<key>), in its own user, mount, PID, IPC and
-// UTS namespaces, on the host's network. The upper layer stays on disk, so
+// UTS namespaces, on the host's network (farm runs: boxd runs the agent's commands in a network of
+// their own with only the package registries reachable, box/go/cmd/boxd/egress.go). The upper layer stays on disk, so
// installs survive restarts; boxd is the same helper the containers run.
//
// Ubuntu 23.10+ blocks unprivileged user namespaces through AppArmor
@@ -23,6 +24,7 @@ import path from "node:path";
import { GOARCH, RUN_DIR, SHIMS_DIR, containerName, containerRoot, helperPath, newToken, readJson, writeJson, type BoxInstallLog, type BoxRuntime } from "../common.js";
import { REQUIRED_RULES, shadowId, type ShadowPlan } from "../required.js";
+import { stockMode } from "../../stock-mode/stock-mode.mjs";
import { runCommand, type RunCommand, type RunOptions } from "../run.js";
import type { RuntimeResult } from "../runtime.js";
import { YOUNG_IMAGE_MS, removableImage, removeTree, staleEntries } from "../retention.mjs";
@@ -157,12 +159,21 @@ async function pull(runtime: BoxRuntime, image: { pull: string }, { run = runCom
return out?.ok ? { ok: true } : { ok: false, error: String(out?.error || res.stderr || `boxrun pull exited ${res.code}`).slice(0, 300) };
}
+/**
+ * The box's egress: "farm" in farm runs (OMNIRUSH_FARM): boxd gives the agent's commands no internet
+ * but the package registries (box/go/cmd/boxd/egress.go). null: the box's network as is. CLI: the same.
+ */
+export function boxEgress(env: Env = process.env): "farm" | null {
+ return stockMode(env) ? "farm" : null;
+}
+
/** boxd's environment in a box (what `docker create -e` gives the containers). */
-export function boxdEnv({ token, croot, idleMinutes = IDLE_MINUTES, listen = "127.0.0.1:0" }: { token: string; croot: string; idleMinutes?: number; listen?: string }): string[] {
+export function boxdEnv({ token, croot, idleMinutes = IDLE_MINUTES, listen = "127.0.0.1:0", egress = null }: { token: string; croot: string; idleMinutes?: number; listen?: string; egress?: string | null }): string[] {
return [
`BOXD_TOKEN=${token}`, "BOXD_UID=0", "BOXD_GID=0", "BOXD_HOME=/root", `BOXD_ROOT=${croot}`,
`BOXD_ENV_FILE=${RUN_DIR}/env`, `BOXD_LISTEN=${listen}`, `BOXD_PORT_FILE=${RUN_DIR}/boxd.port`, `BOXD_IDLE_EXIT=${idleMinutes}`,
"LANG=C.UTF-8", "DEBIAN_FRONTEND=noninteractive",
+ ...(egress ? [`BOXD_EGRESS=${egress}`] : []),
];
}
@@ -175,12 +186,12 @@ export function lazyShadowRules(): Array<{ dir: string; markers: string[] }> {
}
/** `boxrun up` arguments for a project's box. */
-export function upArgs({ box, store, image, boxd, portFile, root, croot, runFolder, shims = null, pkglog = null, token, bwrap = null, idleMinutes = IDLE_MINUTES, listen, platform = "linux", mapPath = (p: string) => p, shadows = null, shadowStore = null }: { box: string; store: string; image: string; boxd: string; portFile: string; root: string; croot: string; runFolder: string; shims?: string | null; pkglog?: Pick<BoxInstallLog, "root"> | null; token: string; bwrap?: string | null; idleMinutes?: number; listen?: string; platform?: NodeJS.Platform; mapPath?: (p: string) => string; shadows?: Pick<ShadowPlan, "binds"> | null; shadowStore?: string | null }): string[] {
+export function upArgs({ box, store, image, boxd, portFile, root, croot, runFolder, shims = null, pkglog = null, token, bwrap = null, idleMinutes = IDLE_MINUTES, listen, platform = "linux", mapPath = (p: string) => p, shadows = null, shadowStore = null, egress = null }: { box: string; store: string; image: string; boxd: string; portFile: string; root: string; croot: string; runFolder: string; shims?: string | null; pkglog?: Pick<BoxInstallLog, "root"> | null; token: string; bwrap?: string | null; idleMinutes?: number; listen?: string; platform?: NodeJS.Platform; mapPath?: (p: string) => string; shadows?: Pick<ShadowPlan, "binds"> | null; shadowStore?: string | null; egress?: string | null }): string[] {
const args = ["up", "--box", box, "--store", store, "--image", image, "--boxd", boxd, "--port-file", portFile,
"--bind", `${mapPath(root)}=${croot}`, "--bind", `${mapPath(runFolder)}=${RUN_DIR}`];
if (shims) args.push("--ro-bind", `${mapPath(shims)}=${SHIMS_DIR}-src`);
if (pkglog) args.push("--bind", `${mapPath(pkglog.root)}=${containerRoot(pkglog.root, platform)}`);
- for (const kv of boxdEnv({ token, croot, idleMinutes, ...(listen ? { listen } : {}) })) args.push("--env", kv);
+ for (const kv of boxdEnv({ token, croot, idleMinutes, egress, ...(listen ? { listen } : {}) })) args.push("--env", kv);
if (bwrap) args.push("--bwrap", bwrap);
// The box's own build folders (required.ts shadowPlan; backends whose boxrun runs as root):
// the per-box store on the backend's own disk, and each folder bound from it (boxrun init Binds).
@@ -224,7 +235,7 @@ async function open(ctx: OpenContext): Promise<Opened> {
if (!boxd) return { ok: false, error: "the sandbox helper boxd is missing from this install" };
const portFile = path.join(runFolder, "boxd.port");
const idle = Number(env.OMNIRUSH_BOX_IDLE_MIN) > 0 ? Number(env.OMNIRUSH_BOX_IDLE_MIN) : IDLE_MINUTES;
- const args = upArgs({ box: boxDir(home, key), store: storeDir(home), image: meta.image, boxd, portFile, root, croot, runFolder, shims, pkglog, token: meta.token, bwrap: runtime.bwrap ?? null, idleMinutes: idle });
+ const args = upArgs({ box: boxDir(home, key), store: storeDir(home), image: meta.image, boxd, portFile, root, croot, runFolder, shims, pkglog, token: meta.token, bwrap: runtime.bwrap ?? null, idleMinutes: idle, egress: boxEgress(env) });
// OMNIRUSH_BOX_OVERLAY=fuse: fuse-overlayfs only (a kernel without overlayfs in user namespaces, or a test of one).
if (String(env.OMNIRUSH_BOX_OVERLAY || "").toLowerCase() === "fuse") args.push("--overlay", "fuse");
const res = await run(runtime.bin, args, { timeoutMs: 45_000, env: { PATH: env.PATH || "/usr/bin:/bin", USER: env.USER || "" } });
diff --git a/apps/server/src/box/backends/wsl.ts b/apps/server/src/box/backends/wsl.ts
index e1a00041..6217ed60 100644
--- a/apps/server/src/box/backends/wsl.ts
+++ b/apps/server/src/box/backends/wsl.ts
@@ -38,7 +38,7 @@ import { runCommand, type RunCommand, type RunResult } from "../run.js";
import { externalFetch } from "../../server-fetch.js";
import type { RuntimeResult } from "../runtime.js";
import bundledPins from "../wsl.json" with { type: "json" };
-import { IDLE_MINUTES, newMeta, upArgs, type BoxMeta } from "./bwrap.js";
+import { IDLE_MINUTES, boxEgress, newMeta, upArgs, type BoxMeta } from "./bwrap.js";
import { YOUNG_IMAGE_MS, removableImage, removeTree, staleEntries } from "../retention.mjs";
import { boxshExec, num, parseOut, str } from "./shared.js";
import type { BoxDriver, OpenContext, Opened, PresentImage, Removed, SpawnLike } from "./types.js";
@@ -402,7 +402,7 @@ async function open(ctx: OpenContext): Promise<Opened> {
const args = upArgs({
box: boxPath(key), store: STORE, image: meta.image, boxd: "/opt/omnirush/boxd", portFile: `${wslPath(runFolder)}/boxd.port`,
root, croot, runFolder, shims, pkglog, token: meta.token, idleMinutes: idle, listen: listenAny ? "0.0.0.0:0" : "127.0.0.1:0",
- platform: "win32", mapPath: wslPath, shadows, shadowStore: shadows ? `${SHADOW_STORES}/${key}` : null,
+ platform: "win32", mapPath: wslPath, shadows, shadowStore: shadows ? `${SHADOW_STORES}/${key}` : null, egress: boxEgress(env),
});
const kept = await startKeeper(runtime, args, { env, ...(spawnImpl ? { spawnImpl } : {}) });
if (!kept.ok) return { ok: false, error: `could not start the sandbox: ${kept.error}` };
diff --git a/apps/server/src/box/backends/wslc.ts b/apps/server/src/box/backends/wslc.ts
index 64286cfd..dc132594 100644
--- a/apps/server/src/box/backends/wslc.ts
+++ b/apps/server/src/box/backends/wslc.ts
@@ -33,6 +33,7 @@ import { versionAtLeast } from "./shared.js";
import { removableImage, removalTarget } from "../retention.mjs";
import type { BoxDriver, OpenContext, Opened, PresentImage, Removed } from "./types.js";
import { ENABLE_HINT, decodeWsl, wslCall, wslExe } from "./wsl.js";
+import { boxEgress } from "./bwrap.js";
type Env = NodeJS.ProcessEnv;
type Row = Record<string, unknown>;
@@ -122,7 +123,7 @@ export function wslcResources({ env = process.env, cpus = os.cpus().length, memo
return ["--cpus", String(c), "-m", m];
}
-export function createWslcArgs({ name, key, root, croot, image, runFolder, shims = null, pkglog = null, token, port, env = process.env }: { name: string; key: string; root: string; croot: string; image: string; runFolder: string; shims?: string | null; pkglog?: Pick<BoxInstallLog, "root"> | null; token: string; port: number; env?: Env }): string[] {
+export function createWslcArgs({ name, key, root, croot, image, runFolder, shims = null, pkglog = null, token, port, env = process.env, egress = null }: { name: string; key: string; root: string; croot: string; image: string; runFolder: string; shims?: string | null; pkglog?: Pick<BoxInstallLog, "root"> | null; token: string; port: number; env?: Env; egress?: string | null }): string[] {
return [
"container", "create", "--name", name,
"--label", `${LABEL}=1`, "--label", `${LABEL}.key=${key}`, "--label", `${LABEL}.root=${root}`, "--label", `${LABEL}.version=1`, "--label", `${LABEL}.port=${port}`,
@@ -135,6 +136,8 @@ export function createWslcArgs({ name, key, root, croot, image, runFolder, shims
"-p", `127.0.0.1:${port}:${BOX_PORT}`,
"-e", `BOXD_TOKEN=${token}`, "-e", "BOXD_UID=0", "-e", "BOXD_GID=0", "-e", `BOXD_HOME=${HOME_IN_BOX}`, "-e", `BOXD_ROOT=${croot}`, "-e", `BOXD_ENV_FILE=${RUN_DIR}/env`,
"-e", "LANG=C.UTF-8", "-e", "DEBIAN_FRONTEND=noninteractive",
+ // Farm runs (bwrap.ts boxEgress): boxd's egress, and a label so a box made the other way is made again.
+ ...(egress ? ["-e", `BOXD_EGRESS=${egress}`, "--label", `${LABEL}.egress=${egress}`] : []),
"-w", croot,
...wslcResources({ env }),
"--entrypoint", "/opt/omnirush/bin/boxd",
@@ -188,6 +191,14 @@ async function open(ctx: OpenContext): Promise<Opened> {
let info = await row(runtime, name, { run });
const labels = labelsOf(info?.Labels);
if (info && labels[`${LABEL}.root`] && labels[`${LABEL}.root`] !== root) return { ok: false, error: `container ${name} belongs to another folder` };
+ const egress = boxEgress(env);
+ if (info && (labels[`${LABEL}.egress`] ?? null) !== egress) {
+ // A box made for the other kind of run (farm egress or not): its network is fixed at create.
+ log("info", "box made again for this run's network", { key, egress });
+ const gone = await wslc(runtime, ["container", "remove", "--force", name], { run, timeoutMs: 60_000 });
+ if (gone.code !== 0) return { ok: false, error: `could not replace the sandbox: ${firstLine(gone.stderr) || firstLine(gone.stdout)}` };
+ info = null;
+ }
const placed = placeBoxd(runFolder, env, runtime.arch || "amd64");
if (placed) return { ok: false, error: placed };
const tokenFile = path.join(runFolder, "box-token");
@@ -205,7 +216,7 @@ async function open(ctx: OpenContext): Promise<Opened> {
const token = newToken();
writeFileSync(tokenFile, token, { mode: 0o600 });
const port = await freePort();
- const made = await wslc(runtime, createWslcArgs({ name, key, root, croot, image: image.pull, runFolder, shims, pkglog, token, port, env }), { run, timeoutMs: 120_000 });
+ const made = await wslc(runtime, createWslcArgs({ name, key, root, croot, image: image.pull, runFolder, shims, pkglog, token, port, env, egress }), { run, timeoutMs: 120_000 });
if (made.code !== 0) return { ok: false, error: `could not create the sandbox: ${firstLine(made.stderr) || firstLine(made.stdout)}` };
created = { ref: image.ref, pull: image.pull, digest: image.digest || present.digest, id: present.id, stack: image.stack };
log("info", "box created", { key, image: image.pull, stack: image.stack, backend: "wslc" });
diff --git a/apps/server/src/box/box-backends.test.ts b/apps/server/src/box/box-backends.test.ts
index bc365fa5..85fe2666 100644
--- a/apps/server/src/box/box-backends.test.ts
+++ b/apps/server/src/box/box-backends.test.ts
@@ -14,7 +14,7 @@ import path from "node:path";
import { PassThrough } from "node:stream";
import { OPT_IN, BACKENDS, REQUIRED_FIRST, backendsFor, detectBackend, driverFor, normalizeBackend, ORDER } from "./backends/index.js";
-import { apparmorProfile, boxdEnv, boxrunPath, bwrapCandidates, ENABLED_BWRAP, explainProbe, parseOut, probeBwrap, upArgs } from "./backends/bwrap.js";
+import { apparmorProfile, boxEgress, boxdEnv, boxrunPath, bwrapCandidates, ENABLED_BWRAP, explainProbe, parseOut, probeBwrap, upArgs } from "./backends/bwrap.js";
import { createAppleArgs, findDigest, macosMajor, parseVersions, probeApple, snapshotInfo, SUPPORTED as APPLE, systemProductVersion } from "./backends/apple.js";
import { versionAtLeast } from "./backends/shared.js";
import { decodeWsl, download, ENABLE_HINT, inDistro, parseWslList, probeWslDistro, startKeeper } from "./backends/wsl.js";
@@ -191,6 +191,24 @@ describe("box backends: Linux", () => {
expect(serverBackends(state)).toBeNull();
});
+ test("farm egress: only farm runs give boxd BOXD_EGRESS (bwrap, WSL distro, wslc); other runs keep their exact args", () => {
+ expect(boxEgress({})).toBeNull();
+ expect(boxEgress({ OMNIRUSH_FARM: "0" })).toBeNull();
+ expect(boxEgress({ OMNIRUSH_FARM: "1" })).toBe("farm");
+ const base = { box: "/b", store: "/s", image: "i", boxd: "/d", portFile: "/p", root: "/r", croot: "/r", runFolder: "/f", token: "t" };
+ const plain = upArgs(base);
+ expect(upArgs({ ...base, egress: null })).toEqual(plain);
+ expect(plain.some((a) => a.startsWith("BOXD_EGRESS"))).toBe(false);
+ expect(upArgs({ ...base, egress: "farm" })).toEqual([...plain, "--env", "BOXD_EGRESS=farm"]);
+ const wbase = { name: "n", key: "k", root: "C:\\p", croot: "/c/p", image: "img", runFolder: "C:\\r", token: "t", port: 1, env: {} };
+ const wplain = createWslcArgs(wbase);
+ expect(createWslcArgs({ ...wbase, egress: null })).toEqual(wplain);
+ const wfarm = createWslcArgs({ ...wbase, egress: "farm" });
+ expect(wfarm).toContain("BOXD_EGRESS=farm");
+ expect(wfarm).toContain("ai.omnirush.box.egress=farm");
+ expect(wfarm.length).toBe(wplain.length + 4);
+ });
+
test("bwrap box start: the project at its path, the run folder, boxd on loopback with a port file, nothing of $HOME", () => {
const args = upArgs({ box: "/h/.omnirush/box/bwrap/boxes/k", store: "/h/.omnirush/box/bwrap/store", image: "abc-amd64", boxd: "/pkg/boxd", portFile: "/h/.omnirush/box/projects/k/boxd.port", root: "/home/u/proj", croot: "/home/u/proj", runFolder: "/h/.omnirush/box/projects/k", pkglog: { root: "/h/.omnirush/pkglog" }, token: "t0k", idleMinutes: 90 });
const binds = args.flatMap((a, i) => (a === "--bind" || a === "--ro-bind" ? [args[i + 1]] : []));