Skip to content
Python

Sandbox credential grants: chosen home files copied into the sandbox

Devika Vermac9bbe009a1d8
diff --git a/coworker/agent.py b/coworker/agent.py
index b02cbf95..3685ea4c 100644
--- a/coworker/agent.py
+++ b/coworker/agent.py
@@ -346,6 +346,7 @@ def build_engine(
roots=root_list or None,
session_id=session_id or "",
agent=agent.name,
+ credentials=config.sandbox_credentials,
)
if ws is not None
else None
@@ -645,6 +646,13 @@ def build_engine(
ctx = roots_context()
if ctx:
parts.append(ctx)
+ # Credentials the user shared with the sandbox (section 11b): fixed for the
+ # session, so this cannot move on its own either.
+ sandbox_ctx = getattr(sandbox_workspace, "context", None)
+ if sandbox_ctx is not None:
+ text = sandbox_ctx()
+ if text:
+ parts.append(text)
# Live skill menu (SKILLS-SPEC §4.1): recomputed every turn like the roots list, so
# a skill installed/enabled/disabled mid-session applies from the NEXT MESSAGE —
# no new session, no lost context.
diff --git a/coworker/config.py b/coworker/config.py
index b8a6c702..9f8d7f45 100644
--- a/coworker/config.py
+++ b/coworker/config.py
@@ -119,6 +119,11 @@ class Config:
# only: a repository's own config must never be able to switch the sandbox off.
# Environment override: OPENWORKER_SANDBOX_PROVIDER.
sandbox_provider: Optional[str] = None
+ # Credential files the user chose to share with sandboxes (design doc, section 11b):
+ # `[[sandbox_credentials]]` tables with name, path, hosts, enabled. Machine-level only,
+ # for the same reason as the provider. The shipped entries and their defaults are in
+ # coworker/sandbox/credentials.py; an entry here edits or adds by name.
+ sandbox_credentials: list[dict[str, Any]] = field(default_factory=list)
_FIELDS = {
@@ -129,6 +134,7 @@ _FIELDS = {
"reasoning_effort",
"tool_result_max_bytes",
"sandbox_provider",
+ "sandbox_credentials",
"compaction_cap_tokens",
"compaction_summary_max_tokens",
"allowed_commands",
@@ -153,6 +159,7 @@ _FIELDS = {
# only (a repo must not be able to widen the agent's command or network reach).
_GLOBAL_ONLY_FIELDS = {
"sandbox_provider",
+ "sandbox_credentials",
"allowed_commands",
"auto_allow",
"allowed_domains",
diff --git a/coworker/sandbox/credentials.py b/coworker/sandbox/credentials.py
new file mode 100644
index 00000000..56944a1f
--- /dev/null
+++ b/coworker/sandbox/credentials.py
@@ -0,0 +1,258 @@
+"""Credential grants: files from the home folder a user chooses to share with a sandbox.
+
+Design doc rulings 30 to 33 and section 11b. Nothing is shared unless the user switched an
+entry on in the MACHINE config (`[[sandbox_credentials]]`); a repository's own config cannot
+add one. Each granted entry is COPIED into the sandbox's private folder for the session
+(tools like `ssh`, `aws` and `gh` write beside their credentials, and Windows OpenSSH refuses
+a key another user can read), and the copy dies with the sandbox. The real files are never
+opened for writing. Each entry also names the hosts its tool needs; those join the session's
+network allow list.
+
+Logins kept in a keychain or credential manager are not files and cannot be shared.
+"""
+
+from __future__ import annotations
+
+import os
+import shutil
+import stat
+from dataclasses import dataclass, field
+from pathlib import Path
+from typing import Any, Optional, Sequence
+
+# What ships. `hosts` are "host:port"; a port of 22 is an SSH tunnel through the proxy.
+DEFAULT_ENTRIES: list[dict[str, Any]] = [
+ {
+ "name": "ssh",
+ "title": "SSH keys",
+ "path": "~/.ssh",
+ "hosts": ["github.com:22", "gitlab.com:22"],
+ "does": "push and pull over SSH, and log in to servers, as you",
+ "enabled": False,
+ },
+ {
+ "name": "gh",
+ "title": "GitHub CLI",
+ "path": "~/.config/gh",
+ "hosts": ["api.github.com:443", "github.com:443"],
+ "does": "use gh as you: pull requests, issues, releases",
+ "enabled": False,
+ },
+ {
+ "name": "aws",
+ "title": "AWS",
+ "path": "~/.aws",
+ "hosts": ["*.amazonaws.com:443"],
+ "does": "use aws with your profiles",
+ "enabled": False,
+ },
+ {
+ "name": "kube",
+ "title": "Kubernetes",
+ "path": "~/.kube",
+ "hosts": [], # read from the kubeconfig at grant time
+ "does": "use kubectl with your clusters",
+ "enabled": False,
+ },
+]
+
+# Git's own settings are not a credential, but a sandbox whose HOME is redirected must still
+# see them (git refuses to commit without an identity). Always copied when present.
+_GIT_SETTINGS = [".gitconfig", ".gitignore_global", ".config/git"]
+
+
+@dataclass
+class Grant:
+ name: str
+ title: str
+ path: str # the real, absolute path
+ relative: str # the path under the home folder, e.g. ".ssh"
+ hosts: list[str] = field(default_factory=list)
+ does: str = ""
+
+
+@dataclass
+class CopiedCredentials:
+ """What copy_in produced: the sandbox's home folder, environment to set, hosts to allow."""
+
+ home: str
+ env: dict[str, str]
+ hosts: list[str]
+ grants: list[Grant]
+ path_dirs: list[str] = field(default_factory=list) # to put FIRST on the sandbox's PATH
+
+ def describe(self) -> list[dict[str, Any]]:
+ return [{"name": g.name, "title": g.title, "path": g.path, "does": g.does} for g in self.grants]
+
+
+def entries(configured: Optional[Sequence[dict[str, Any]]]) -> list[dict[str, Any]]:
+ """The machine's list: the shipped entries, with the user's edits and additions applied
+ by name. Unknown keys in a user entry are kept, missing ones default."""
+ by_name = {e["name"]: dict(e) for e in DEFAULT_ENTRIES}
+ for raw in configured or []:
+ if not isinstance(raw, dict) or not str(raw.get("name") or "").strip():
+ continue
+ name = str(raw["name"]).strip()
+ base = by_name.get(name, {"name": name, "title": name, "path": "", "hosts": [], "does": "", "enabled": False})
+ merged = {**base, **{k: v for k, v in raw.items() if v is not None}}
+ merged["hosts"] = [str(h) for h in (merged.get("hosts") or [])]
+ merged["enabled"] = bool(merged.get("enabled"))
+ by_name[name] = merged
+ return list(by_name.values())
+
+
+def _real(path: str, home: str) -> str:
+ text = str(path)
+ if text == "~" or text.startswith("~/"):
+ text = os.path.join(home, text[2:]) if len(text) > 1 else home
+ return os.path.realpath(os.path.expanduser(text))
+
+
+def granted(configured: Optional[Sequence[dict[str, Any]]], *, home: Optional[str] = None) -> list[Grant]:
+ """The entries that are switched on AND exist on this machine. An enabled entry whose
+ file is missing is skipped, not an error: the user has no such credential here."""
+ home = home or os.path.expanduser("~")
+ out: list[Grant] = []
+ for e in entries(configured):
+ if not e.get("enabled") or not e.get("path"):
+ continue
+ real = _real(str(e["path"]), home)
+ if not os.path.exists(real):
+ continue
+ home_real = os.path.realpath(home)
+ if real == home_real or not real.startswith(home_real + os.sep):
+ continue # never the home folder itself, never something outside it
+ relative = os.path.relpath(real, home_real)
+ hosts = list(e.get("hosts") or [])
+ if e["name"] == "kube" and not hosts:
+ hosts = _kubeconfig_hosts(real)
+ out.append(Grant(name=str(e["name"]), title=str(e.get("title") or e["name"]), path=real, relative=relative, hosts=hosts, does=str(e.get("does") or "")))
+ return out
+
+
+def _kubeconfig_hosts(kube_dir: str) -> list[str]:
+ """`server:` lines of the kubeconfig, as host:port. Plain text scan, no YAML needed."""
+ from urllib.parse import urlsplit
+
+ hosts: list[str] = []
+ for name in ("config",):
+ try:
+ text = Path(kube_dir, name).read_text(encoding="utf-8", errors="replace")
+ except OSError:
+ continue
+ for line in text.splitlines():
+ line = line.strip()
+ if line.startswith("server:"):
+ url = urlsplit(line.split(":", 1)[1].strip().strip("'\""))
+ if url.hostname:
+ hosts.append(f"{url.hostname}:{url.port or (443 if url.scheme == 'https' else 80)}")
+ return sorted(set(hosts))
+
+
+def _copy_private(src: str, dst: str) -> None:
+ """Copy a file or a folder, owner-only. Only regular files and folders are copied:
+ sockets (an ssh agent keeps one under `.ssh`), pipes and devices are skipped, and a
+ symlink is copied as the file it points to when that file is a regular file."""
+ if os.path.isdir(src):
+ os.makedirs(dst, mode=stat.S_IRWXU, exist_ok=True)
+ os.chmod(dst, stat.S_IRWXU)
+ for name in os.listdir(src):
+ _copy_private(os.path.join(src, name), os.path.join(dst, name))
+ elif os.path.isfile(src): # follows a symlink; False for sockets, pipes, devices
+ os.makedirs(os.path.dirname(dst), mode=stat.S_IRWXU, exist_ok=True)
+ shutil.copyfile(src, dst)
+ os.chmod(dst, stat.S_IRUSR | stat.S_IWUSR)
+
+
+def copy_in(
+ grants: Sequence[Grant],
+ runtime_dir: str,
+ *,
+ home: Optional[str] = None,
+ proxy_port: Optional[int] = None,
+ ssh_proxy_command: Optional[str] = None,
+) -> CopiedCredentials:
+ """Make the sandbox's private home under `runtime_dir` with the granted files inside.
+ Returns the environment the sandbox needs so each tool finds its copy. With no grants
+ the home holds only git's settings.
+
+ `ssh_proxy_command`: when the sandbox can only reach the network through the proxy, a
+ `ProxyCommand` for ssh (with `%h` and `%p`), put first in the copied ssh config so it wins.
+ """
+ home = os.path.realpath(home or os.path.expanduser("~"))
+ sandbox_home = os.path.join(os.path.realpath(runtime_dir), "home")
+ if sandbox_home == home or sandbox_home.startswith(home + os.sep) and os.path.realpath(runtime_dir) == home:
+ raise ValueError("the sandbox's runtime folder cannot be the home folder itself")
+ os.makedirs(sandbox_home, mode=stat.S_IRWXU, exist_ok=True)
+ for rel in _GIT_SETTINGS:
+ src = os.path.join(home, rel)
+ if os.path.exists(src):
+ _copy_private(src, os.path.join(sandbox_home, rel))
+ env: dict[str, str] = {"HOME": sandbox_home}
+ hosts: list[str] = []
+ path_dirs: list[str] = []
+ for g in grants:
+ dst = os.path.join(sandbox_home, g.relative)
+ _copy_private(g.path, dst)
+ hosts += g.hosts
+ if g.name == "ssh":
+ env.update(_prepare_ssh(dst, os.path.join(os.path.realpath(runtime_dir), "bin"), ssh_proxy_command))
+ path_dirs.append(os.path.join(os.path.realpath(runtime_dir), "bin"))
+ elif g.name == "gh":
+ env["GH_CONFIG_DIR"] = dst
+ elif g.name == "aws":
+ env["AWS_CONFIG_FILE"] = os.path.join(dst, "config")
+ env["AWS_SHARED_CREDENTIALS_FILE"] = os.path.join(dst, "credentials")
+ elif g.name == "kube":
+ env["KUBECONFIG"] = os.path.join(dst, "config")
+ return CopiedCredentials(home=sandbox_home, env=env, hosts=sorted(set(hosts)), grants=list(grants), path_dirs=path_dirs)
+
+
+def _prepare_ssh(ssh_dir: str, bin_dir: str, proxy_command: Optional[str]) -> dict[str, str]:
+ """Make the copied `.ssh` usable from inside the sandbox.
+
+ ssh reads its config and expands `~` from the ACCOUNT's home folder, not from `$HOME`,
+ so a copy under another path is ignored unless ssh is told about it. So: a `Host *`
+ block goes at the top of the copied config (ssh takes the first value it finds for an
+ option) naming the copied known_hosts and every private key in the copy, with no agent
+ (the real agent's socket is not reachable from inside, and sockets are not copied);
+ a tiny `ssh` wrapper first on PATH passes `-F <copy>/config`; and `GIT_SSH_COMMAND` does
+ the same for git. With `proxy_command`, connections go through the proxy."""
+ config = os.path.join(ssh_dir, "config")
+ existing = Path(config).read_text(encoding="utf-8", errors="replace") if os.path.exists(config) else ""
+ keys = sorted(
+ os.path.join(ssh_dir, name)
+ for name in os.listdir(ssh_dir)
+ if os.path.isfile(os.path.join(ssh_dir, name + ".pub")) and os.path.isfile(os.path.join(ssh_dir, name))
+ )
+ lines = ["# Added by OpenWorker: this is a copy of your .ssh inside the sandbox", "Host *"]
+ if proxy_command:
+ lines.append(f" ProxyCommand {proxy_command}")
+ lines.append(f" UserKnownHostsFile {os.path.join(ssh_dir, 'known_hosts')}")
+ lines.append(" IdentityAgent none")
+ lines.append(" AddKeysToAgent no")
+ lines += [f" IdentityFile {key}" for key in keys]
+ Path(config).write_text("\n".join(lines) + "\n\n" + existing, encoding="utf-8")
+ os.chmod(config, stat.S_IRUSR | stat.S_IWUSR)
+ Path(os.path.join(ssh_dir, "known_hosts")).touch(mode=stat.S_IRUSR | stat.S_IWUSR)
+ os.makedirs(bin_dir, mode=stat.S_IRWXU, exist_ok=True)
+ wrapper = os.path.join(bin_dir, "ssh")
+ Path(wrapper).write_text(f'#!/bin/sh\nexec /usr/bin/ssh -F "{config}" "$@"\n', encoding="utf-8")
+ os.chmod(wrapper, stat.S_IRWXU)
+ return {"GIT_SSH_COMMAND": f'/usr/bin/ssh -F "{config}"'}
+
+
+def mac_ssh_proxy_command(port: int) -> str:
+ """macOS ships BSD nc, which speaks HTTP CONNECT."""
+ return f"/usr/bin/nc -X connect -x 127.0.0.1:{int(port)} %h %p"
+
+
+def context_lines(copied: Optional[CopiedCredentials]) -> str:
+ """What the agent is told about the credentials it was given (ruling 21: the agent knows
+ its environment). Empty when nothing was granted."""
+ if copied is None or not copied.grants:
+ return ""
+ lines = ["Credentials shared with this sandbox (copies, deleted when the session ends):"]
+ for g in copied.grants:
+ lines.append(f"- {g.title} ({g.relative}): you can {g.does}.")
+ return "\n".join(lines)
diff --git a/coworker/sandbox/netproxy.py b/coworker/sandbox/netproxy.py
index bc57427b..7e4ac0fd 100644
--- a/coworker/sandbox/netproxy.py
+++ b/coworker/sandbox/netproxy.py
@@ -15,7 +15,7 @@ import select
import socket
import threading
from collections import deque
-from typing import Optional
+from typing import Optional, Sequence
from . import network_profiles
@@ -25,9 +25,16 @@ _MAX_HEAD = 16 * 1024
class AllowListProxy:
- def __init__(self, profile: str = network_profiles.DEFAULT_PROFILE) -> None:
+ def __init__(self, profile: str = network_profiles.DEFAULT_PROFILE, extra_hosts: Sequence[str] = ()) -> None:
+ """`extra_hosts`: "host:port" entries beyond the profile (credential grants, section
+ 11b); `*.example.com` matches any subdomain. A port of 22 is an SSH tunnel."""
self.profile = network_profiles.check(profile)
self._hosts = {h.lower() for h in network_profiles.hosts(profile)}
+ self._extra: set[tuple[str, int]] = set()
+ for item in extra_hosts:
+ host, _, port = str(item).rpartition(":")
+ if host and port.isdigit():
+ self._extra.add((host.lower().rstrip("."), int(port)))
self.denied: deque[str] = deque(maxlen=50) # recent refusals, newest last
self._server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@@ -42,7 +49,17 @@ class AllowListProxy:
return f"http://127.0.0.1:{self.port}"
def allows(self, host: str, port: int) -> bool:
- return port == 443 and host.lower().rstrip(".") in self._hosts
+ host = host.lower().rstrip(".")
+ if port == 443 and host in self._hosts:
+ return True
+ for allowed, allowed_port in self._extra:
+ if allowed_port != port:
+ continue
+ if allowed.startswith("*.") and host.endswith(allowed[1:]) and host != allowed[2:]:
+ return True
+ if host == allowed:
+ return True
+ return False
def close(self) -> None:
self._closed = True
@@ -75,7 +92,7 @@ class AllowListProxy:
return self._refuse(client, 400, "not an HTTP request")
method, target = parts[0].upper(), parts[1]
if method != "CONNECT":
- return self._refuse(client, 403, f"only HTTPS is allowed out of this sandbox (got {method} {target[:120]})", target)
+ return self._refuse(client, 403, f"only tunnelled connections are allowed out of this sandbox (got {method} {target[:120]})", target)
host, _, port_text = target.rpartition(":")
host = host.strip("[]")
if not host or not port_text.isdigit():
@@ -91,7 +108,9 @@ class AllowListProxy:
upstream = socket.create_connection((host, int(port_text)), timeout=_CONNECT_SECONDS)
except OSError as exc:
return self._refuse(client, 502, f"could not reach {host}: {exc}")
- client.sendall(b"HTTP/1.1 200 Connection established\r\n\r\n")
+ # HTTP/1.0 on purpose: BSD nc (the ssh ProxyCommand on macOS) accepts only that
+ # line for a tunnel, and every HTTP client accepts it too.
+ client.sendall(b"HTTP/1.0 200 Connection established\r\n\r\n")
rest = head.split(b"\r\n\r\n", 1)[1]
if rest:
upstream.sendall(rest)
diff --git a/coworker/sandbox/providers/openshell.py b/coworker/sandbox/providers/openshell.py
index 7829372d..32df2892 100644
--- a/coworker/sandbox/providers/openshell.py
+++ b/coworker/sandbox/providers/openshell.py
@@ -35,6 +35,7 @@ import yaml
from ..bundle import build_runner_zipapp
from ..transport import Transport
+from .. import credentials as creds
from . import openshell_policy as policy
from . import openshell_wire as wire
@@ -97,6 +98,7 @@ class OpenShellProvider:
profile: str = policy.DEFAULT_PROFILE,
image: Optional[str] = None,
label: str = "",
+ credentials: Sequence[creds.Grant] = (),
) -> None:
"""`roots`: [{"path", "writable"}], primary first. `label`: who this sandbox is for
(session and agent), stored on the sandbox so leftovers can be found."""
@@ -112,6 +114,8 @@ class OpenShellProvider:
self.sandbox_id: Optional[str] = None
self._runner = build_runner_zipapp()
self._tmp = tempfile.mkdtemp(prefix="ow-openshell-")
+ self.grants = list(credentials)
+ self.copied: Optional[creds.CopiedCredentials] = None
def describe(self) -> dict[str, Any]:
return {
@@ -120,6 +124,7 @@ class OpenShellProvider:
"reason": f"OpenShell {PINNED_VERSION}: Landlock and seccomp on every process, network profile '{self.profile}', no keys inside",
"sandbox": self.sandbox_name,
"image": self.image,
+ "credentials": self.copied.describe() if self.copied is not None else [],
}
# -- lifecycle --------------------------------------------------------------------
@@ -129,15 +134,22 @@ class OpenShellProvider:
if not os.path.isdir(root["path"]):
raise RuntimeError(f"folder does not exist on this machine: {root['path']}")
uid, gid = (os.getuid(), os.getgid()) if sys.platform.startswith("linux") else (None, None)
+ # The sandbox's private home with the copied credentials (section 11b), mounted at
+ # the same path it has on the machine, read-write. With no grants it holds only
+ # git's settings, and HOME stays the runtime folder.
+ self.copied = creds.copy_in(self.grants, self._tmp) if self.grants else None
+ home = self.copied.home if self.copied is not None else None
+ extra_hosts = self.copied.hosts if self.copied is not None else []
policy_file = Path(self._tmp) / "policy.yaml"
- policy_file.write_text(yaml.safe_dump(policy.render(self.roots, profile=self.profile, uid=uid, gid=gid), sort_keys=False), encoding="utf-8")
- driver_config = json.dumps(policy.mounts(self.roots, str(self._runner.parent)))
+ policy_file.write_text(yaml.safe_dump(policy.render(self.roots, profile=self.profile, uid=uid, gid=gid, home=home, extra_hosts=extra_hosts), sort_keys=False), encoding="utf-8")
+ driver_config = json.dumps(policy.mounts(self.roots, str(self._runner.parent), home))
command = [policy.PYTHON, "-S", f"{policy.RUNNER_MOUNT}/{self._runner.name}", "serve", "--socket", _SOCKET, "--cwd", self.cwd]
+ env = {"HOME": policy.RUNTIME_DIR, **(self.copied.env if self.copied is not None else {})}
args = [
"sandbox", "create", "--name", self.sandbox_name, "--from", self.image,
"--no-tty", "--detach", "--no-auto-providers",
"--policy", str(policy_file), "--driver-config-json", driver_config,
- "--label", f"{LABEL}=1", "--env", f"HOME={policy.RUNTIME_DIR}",
+ "--label", f"{LABEL}=1", *[x for k, v in env.items() for x in ("--env", f"{k}={v}")],
] # fmt: skip
if self.label:
args += ["--label", f"{LABEL}-owner={self.label}"]
diff --git a/coworker/sandbox/providers/openshell_policy.py b/coworker/sandbox/providers/openshell_policy.py
index 160c7cf1..8b6ac5cd 100644
--- a/coworker/sandbox/providers/openshell_policy.py
+++ b/coworker/sandbox/providers/openshell_policy.py
@@ -56,11 +56,15 @@ def render(
profile: str = DEFAULT_PROFILE,
uid: Optional[int] = None,
gid: Optional[int] = None,
+ home: Optional[str] = None,
+ extra_hosts: Sequence[str] = (),
) -> dict[str, Any]:
- """`roots`: [{"path": absolute path, "writable": bool}], primary first."""
+ """`roots`: [{"path": absolute path, "writable": bool}], primary first. `home`: the
+ sandbox's private home folder with the copied credentials (section 11b), read-write.
+ `extra_hosts`: "host:port" entries the grants need; `*.example.com` is a wildcard."""
if profile not in PROFILES:
raise ValueError(f"unknown network profile {profile!r} (known: {', '.join(sorted(PROFILES))})")
- read_write = [RUNTIME_DIR, "/dev/null", "/dev/pts", *[str(r["path"]) for r in roots if r.get("writable")]]
+ read_write = [RUNTIME_DIR, "/dev/null", "/dev/pts", *([home] if home else []), *[str(r["path"]) for r in roots if r.get("writable")]]
read_only = [*_SYSTEM_READ_ONLY, *_IMAGE_TOOLS_READ_ONLY, RUNNER_MOUNT, *[str(r["path"]) for r in roots if not r.get("writable")]]
policy: dict[str, Any] = {
"version": 1,
@@ -72,15 +76,24 @@ def render(
},
"network_policies": copy.deepcopy(PROFILES[profile]), # no shared objects: plain YAML, no anchors
}
+ if extra_hosts:
+ endpoints = []
+ for item in extra_hosts:
+ host, _, port = str(item).rpartition(":")
+ if host and port.isdigit():
+ endpoints.append({"host": host, "port": int(port)})
+ policy["network_policies"]["credentials"] = {"name": "shared-credentials", "endpoints": endpoints, "binaries": [dict(b) for b in _ANY_BINARY]}
return policy
-def mounts(roots: Sequence[dict[str, Any]], runner_dir: str) -> dict[str, Any]:
+def mounts(roots: Sequence[dict[str, Any]], runner_dir: str, home: Optional[str] = None) -> dict[str, Any]:
"""Driver config for the Docker driver: every folder at the same absolute path it has on
- the machine, and the runner's folder read-only."""
+ the machine, the runner's folder read-only, and the sandbox's private home read-write."""
binds = [
{"type": "bind", "source": str(r["path"]), "target": str(r["path"]), "read_only": not r.get("writable")}
for r in roots
]
binds.append({"type": "bind", "source": runner_dir, "target": RUNNER_MOUNT, "read_only": True})
+ if home:
+ binds.append({"type": "bind", "source": home, "target": home, "read_only": False})
return {"docker": {"mounts": binds}}
diff --git a/coworker/sandbox/providers/seatbelt.py b/coworker/sandbox/providers/seatbelt.py
index f3b9657b..f28edf5a 100644
--- a/coworker/sandbox/providers/seatbelt.py
+++ b/coworker/sandbox/providers/seatbelt.py
@@ -28,6 +28,7 @@ import uuid
from pathlib import Path
from typing import Any, Optional, Sequence
+from .. import credentials as creds
from .. import netproxy, network_profiles
from ..bundle import build_runner_zipapp
from ..transport import PipeTransport, Transport
@@ -97,8 +98,12 @@ class SeatbeltProvider:
network: bool = True,
runner_path: Optional[Path] = None,
relay_silence_seconds: Optional[float] = None,
+ credentials: Sequence[creds.Grant] = (),
) -> None:
+ """`credentials`: the grants (credentials.granted) to copy into the sandbox."""
self.roots = [{"path": seatbelt_profile.real(r["path"]), "writable": bool(r.get("writable"))} for r in roots]
+ self.grants = list(credentials)
+ self.copied: Optional[creds.CopiedCredentials] = None
self.cwd = seatbelt_profile.real(cwd)
self.profile = network_profiles.check(profile)
self.network = network
@@ -119,6 +124,7 @@ class SeatbeltProvider:
"sandbox": self.sandbox,
"enforcement": "full",
"reason": f"macOS sandbox: files limited to the session's folders; network: {network}",
+ "credentials": self.copied.describe() if self.copied is not None else [],
}
def profile_text(self) -> str:
@@ -133,6 +139,10 @@ class SeatbeltProvider:
def _environment(self) -> dict[str, str]:
env = clean_environment()
+ if self.copied is not None:
+ env.update(self.copied.env) # HOME is the sandbox's own; the copies live there
+ if self.copied.path_dirs:
+ env["PATH"] = os.pathsep.join([*self.copied.path_dirs, env.get("PATH", "")])
tmp = os.path.join(self._dir, "tmp")
env["TMPDIR"] = tmp + "/"
for variable, folder in _CACHE_VARIABLES.items():
@@ -144,7 +154,18 @@ class SeatbeltProvider:
def create(self) -> None:
preflight()
os.makedirs(os.path.join(self._dir, "tmp", "cache"), exist_ok=True)
- self._proxy = netproxy.shared(self.profile) if self.network else None
+ if self.network:
+ hosts = sorted({h for g in self.grants for h in g.hosts})
+ # A session with grants gets its own proxy, because its allow list is its own.
+ self._proxy = netproxy.AllowListProxy(self.profile, extra_hosts=hosts) if hosts else netproxy.shared(self.profile)
+ else:
+ self._proxy = None
+ self.copied = creds.copy_in(
+ self.grants,
+ self._dir,
+ proxy_port=self._proxy.port if self._proxy is not None else None,
+ ssh_proxy_command=creds.mac_ssh_proxy_command(self._proxy.port) if self._proxy is not None else None,
+ )
log = open(os.path.join(self._dir, "daemon.log"), "wb")
self._daemon = subprocess.Popen(
[
@@ -219,4 +240,6 @@ class SeatbeltProvider:
def destroy(self) -> None:
self._stop_daemon()
+ if self._proxy is not None and self._proxy is not netproxy._proxies.get(self.profile):
+ self._proxy.close() # this session's own proxy; the shared one stays
shutil.rmtree(self._dir, ignore_errors=True)
diff --git a/coworker/sandbox/workspace.py b/coworker/sandbox/workspace.py
index d3db7571..41175c79 100644
--- a/coworker/sandbox/workspace.py
+++ b/coworker/sandbox/workspace.py
@@ -140,6 +140,12 @@ class RunnerWorkspace(Workspace):
def executor(self) -> Executor:
return self._executor
+ def context(self) -> str:
+ """What the agent is told about this sandbox each turn (ruling 21)."""
+ from .credentials import context_lines
+
+ return context_lines(getattr(self.provider, "copied", None))
+
def describe(self) -> dict[str, Any]:
return {**self.provider.describe(), "runner": {k: self.hello.get(k) for k in ("runner_version", "os", "machine", "instance_id")}}
@@ -171,8 +177,11 @@ def open_workspace(
roots: Optional[list] = None,
session_id: str = "",
agent: str = "",
+ credentials: Optional[list] = None,
) -> Workspace:
- """The session's workspace for the configured provider. `direct` unless told otherwise.
+ """The session's workspace for the configured provider. `credentials`: the machine's
+ `sandbox_credentials` setting; the enabled entries are copied into the sandbox
+ (design doc, section 11b). Ignored in `direct` mode, where nothing is hidden anyway. `direct` unless told otherwise.
`roots`: the session's RootDir list (primary first); without it the workspace folder is
the only, writable, root. `session_id` and `agent` say who the sandbox is for; they go
into the registry and onto the sandbox as a label."""
@@ -185,12 +194,15 @@ def open_workspace(
return RunnerWorkspace(RunnerLocalProvider(cwd=cwd), cwd=cwd)
listed = [{"path": str(r.path), "writable": bool(r.writable)} for r in (roots or [])]
listed = listed or [{"path": str(cwd), "writable": True}]
+ from .credentials import granted
+
+ grants = granted(credentials)
if name == SEATBELT:
from .providers.seatbelt import SeatbeltProvider
from .registry import SandboxRegistry
return RunnerWorkspace(
- SeatbeltProvider(roots=listed, cwd=str(cwd)),
+ SeatbeltProvider(roots=listed, cwd=str(cwd), credentials=grants),
cwd=cwd,
registry=SandboxRegistry(),
session_id=session_id,
@@ -203,7 +215,7 @@ def open_workspace(
label = "-".join(part for part in (session_id[:24], agent[:24]) if part)
return RunnerWorkspace(
- OpenShellProvider(roots=listed, cwd=str(cwd), label=label),
+ OpenShellProvider(roots=listed, cwd=str(cwd), label=label, credentials=grants),
cwd=cwd,
registry=SandboxRegistry(),
session_id=session_id,
diff --git a/tests/test_sandbox_credentials.py b/tests/test_sandbox_credentials.py
new file mode 100644
index 00000000..d6ad4779
--- /dev/null
+++ b/tests/test_sandbox_credentials.py
@@ -0,0 +1,207 @@
+"""Credential grants (design doc, section 11b): the machine's list, the copy-in, the hosts,
+and, on a Mac, the copies inside a real Seatbelt sandbox with SSH going through the proxy.
+"""
+
+from __future__ import annotations
+
+import os
+import socket
+import stat
+import sys
+import threading
+from pathlib import Path
+
+import pytest
+
+from coworker.config import Config, load_config
+from coworker.sandbox import credentials as creds
+from coworker.sandbox import netproxy
+from coworker.sandbox.providers import openshell_policy
+
+
+def _home(tmp_path) -> Path:
+ home = tmp_path / "home"
+ (home / ".ssh").mkdir(parents=True)
+ (home / ".ssh" / "id_ed25519").write_text("PRIVATE KEY\n")
+ (home / ".ssh" / "id_ed25519.pub").write_text("ssh-ed25519 AAAA test\n")
+ (home / ".ssh" / "config").write_text("Host work\n HostName git.example.com\n")
+ (home / ".config" / "gh").mkdir(parents=True)
+ (home / ".config" / "gh" / "hosts.yml").write_text("github.com:\n oauth_token: gho_x\n")
+ (home / ".aws").mkdir()
+ (home / ".aws" / "credentials").write_text("[default]\naws_access_key_id = AKIA\n")
+ (home / ".kube").mkdir()
+ (home / ".kube" / "config").write_text("clusters:\n- cluster:\n server: https://k8s.example.com:6443\n")
+ (home / ".gitconfig").write_text("[user]\n\tname = Sam\n\temail = [email protected]\n")
+ (home / "Documents").mkdir()
+ return home
+
+
+# -- the list ---------------------------------------------------------------------------
+def test_nothing_is_granted_unless_switched_on(tmp_path):
+ home = _home(tmp_path)
+ assert creds.granted(None, home=str(home)) == []
+ assert [e["enabled"] for e in creds.entries(None)] == [False] * 4
+
+
+def test_a_user_entry_edits_or_adds_by_name(tmp_path):
+ home = _home(tmp_path)
+ configured = [
+ {"name": "ssh", "enabled": True},
+ {"name": "aws", "enabled": True, "path": "~/.aws-work"}, # edited path, missing here
+ {"name": "npm", "enabled": True, "path": "~/.npmrc", "hosts": ["registry.npmjs.org:443"]}, # added
+ {"name": "bogus", "enabled": True, "path": "~"}, # never the home folder itself
+ {"name": "outside", "enabled": True, "path": "/etc"}, # never outside the home folder
+ ]
+ (home / ".npmrc").write_text("//registry.npmjs.org/:_authToken=x\n")
+ got = {g.name: g for g in creds.granted(configured, home=str(home))}
+ assert set(got) == {"ssh", "npm"}
+ assert got["ssh"].relative == ".ssh" and got["ssh"].hosts == ["github.com:22", "gitlab.com:22"]
+ assert got["npm"].hosts == ["registry.npmjs.org:443"]
+
+
+def test_kube_hosts_come_from_the_kubeconfig(tmp_path):
+ home = _home(tmp_path)
+ (got,) = creds.granted([{"name": "kube", "enabled": True}], home=str(home))
+ assert got.hosts == ["k8s.example.com:6443"]
+
+
+def test_the_setting_is_machine_level_only(tmp_path, monkeypatch):
+ g = tmp_path / "global.toml"
+ g.write_text('[[sandbox_credentials]]\nname = "ssh"\nenabled = true\n')
+ ws = tmp_path / "repo"
+ (ws / ".coworker").mkdir(parents=True)
+ (ws / ".coworker" / "config.toml").write_text('[[sandbox_credentials]]\nname = "aws"\nenabled = true\n')
+ cfg = load_config(ws, global_path=g, workspace_trusted=True)
+ assert cfg.sandbox_credentials == [{"name": "ssh", "enabled": True}]
+ assert Config().sandbox_credentials == []
+
+
+# -- the copy ---------------------------------------------------------------------------
+def test_copy_in_makes_a_private_home_and_the_tools_environment(tmp_path):
+ home = _home(tmp_path)
+ grants = creds.granted([{"name": n, "enabled": True} for n in ("ssh", "gh", "aws", "kube")], home=str(home))
+ run = tmp_path / "run"
+ run.mkdir()
+ copied = creds.copy_in(grants, str(run), home=str(home), ssh_proxy_command="/usr/bin/nc -X connect -x 127.0.0.1:4545 %h %p")
+ sb = Path(copied.home)
+ assert sb == run / "home"
+ assert (sb / ".ssh" / "id_ed25519").read_text() == "PRIVATE KEY\n"
+ assert stat.S_IMODE((sb / ".ssh" / "id_ed25519").stat().st_mode) == 0o600
+ assert stat.S_IMODE((sb / ".ssh").stat().st_mode) == 0o700
+ assert (sb / ".gitconfig").exists() # git's settings always come along
+ assert not (sb / "Documents").exists() # nothing that was not granted
+ config = (sb / ".ssh" / "config").read_text()
+ assert config.startswith("# Added by OpenWorker") and "ProxyCommand /usr/bin/nc -X connect -x 127.0.0.1:4545 %h %p" in config
+ assert f"IdentityFile {sb / '.ssh' / 'id_ed25519'}" in config and "IdentityAgent none" in config
+ assert config.endswith("Host work\n HostName git.example.com\n") # the user's own config still there, after ours
+ assert copied.env["GIT_SSH_COMMAND"] == f'/usr/bin/ssh -F "{sb / ".ssh" / "config"}"'
+ assert (run / "bin" / "ssh").read_text().startswith("#!/bin/sh") and copied.path_dirs == [str(run / "bin")]
+ assert copied.env["HOME"] == str(sb)
+ assert copied.env["GH_CONFIG_DIR"] == str(sb / ".config" / "gh")
+ assert copied.env["AWS_SHARED_CREDENTIALS_FILE"] == str(sb / ".aws" / "credentials")
+ assert copied.env["KUBECONFIG"] == str(sb / ".kube" / "config")
+ assert copied.hosts == ["*.amazonaws.com:443", "api.github.com:443", "github.com:22", "github.com:443", "gitlab.com:22", "k8s.example.com:6443"]
+ # the real files were not touched
+ assert (home / ".ssh" / "config").read_text() == "Host work\n HostName git.example.com\n"
+ text = creds.context_lines(copied)
+ assert "SSH keys (.ssh): you can push and pull over SSH" in text and "deleted when the session ends" in text
+
+
+def test_no_grants_means_no_lines_for_the_agent(tmp_path):
+ assert creds.context_lines(None) == ""
+ (tmp_path / "run").mkdir()
+ copied = creds.copy_in([], str(tmp_path / "run"), home=str(_home(tmp_path)))
+ assert creds.context_lines(copied) == ""
+ assert copied.hosts == [] and (Path(copied.home) / ".gitconfig").exists()
+
+
+# -- the network ------------------------------------------------------------------------
+def test_proxy_allows_the_grants_hosts_including_ssh_and_wildcards():
+ proxy = netproxy.AllowListProxy("strict", extra_hosts=["github.com:22", "*.amazonaws.com:443", "k8s.example.com:6443"])
+ try:
+ assert proxy.allows("github.com", 22) and proxy.allows("github.com", 443)
+ assert proxy.allows("s3.us-east-1.amazonaws.com", 443) and not proxy.allows("amazonaws.com", 443)
+ assert proxy.allows("k8s.example.com", 6443) and not proxy.allows("k8s.example.com", 443)
+ assert not proxy.allows("gitlab.com", 22) and not proxy.allows("example.com", 443)
+ finally:
+ proxy.close()
+
+
+def test_openshell_policy_carries_the_home_and_the_hosts():
+ policy = openshell_policy.render(
+ [{"path": "/home/sam/proj", "writable": True}], home="/tmp/ow-x/home", extra_hosts=["github.com:22", "*.amazonaws.com:443"]
+ )
+ assert "/tmp/ow-x/home" in policy["filesystem_policy"]["read_write"]
+ extra = policy["network_policies"]["credentials"]
+ assert extra["endpoints"] == [{"host": "github.com", "port": 22}, {"host": "*.amazonaws.com", "port": 443}]
+ mounts = openshell_policy.mounts([{"path": "/home/sam/proj", "writable": True}], "/opt/r", "/tmp/ow-x/home")["docker"]["mounts"]
+ assert {"type": "bind", "source": "/tmp/ow-x/home", "target": "/tmp/ow-x/home", "read_only": False} in mounts
+
+
+# -- inside a real sandbox (macOS) ------------------------------------------------------
+def _seatbelt_usable() -> bool:
+ if sys.platform != "darwin":
+ return False
+ from coworker.sandbox.providers import seatbelt
+
+ try:
+ seatbelt.preflight()
+ return True
+ except seatbelt.SeatbeltUnavailable:
+ return False
+
+
[email protected](not _seatbelt_usable(), reason="needs a Mac where sandbox-exec can apply a profile")
+def test_ssh_and_gh_copies_work_inside_seatbelt_and_ssh_goes_through_the_proxy(tmp_path, monkeypatch):
+ """A fake `.ssh` and `.config/gh` are granted. Inside the sandbox: HOME is the copy,
+ `gh` finds its file, the real home is still hidden, and `ssh` reaches the listed host
+ through the proxy (a local listener stands in for github.com:22: the CONNECT arrives
+ at the proxy and is tunnelled; the SSH banner comes back)."""
+ from coworker.sandbox.bundle import build_runner_zipapp
+ from coworker.sandbox.providers import seatbelt
+ from coworker.sandbox.workspace import RunnerWorkspace
+
+ home = _home(tmp_path)
+ project = tmp_path / "project"
+ project.mkdir()
+ grants = creds.granted([{"name": "ssh", "enabled": True}, {"name": "gh", "enabled": True}], home=str(home))
+ # a stand-in for github.com:22 on this machine; the proxy resolves the host, so patch that
+ fake = socket.socket()
+ fake.bind(("127.0.0.1", 0))
+ fake.listen(1)
+
+ def banner() -> None:
+ conn, _ = fake.accept()
+ with conn:
+ conn.sendall(b"SSH-2.0-OpenSSH_9.9 fake\r\n")
+ conn.recv(64)
+
+ threading.Thread(target=banner, daemon=True).start()
+ real_connect = netproxy.socket.create_connection
+
+ def connect(address, *a, **k):
+ if address == ("github.com", 22):
+ return real_connect(fake.getsockname(), *a, **k)
+ return real_connect(address, *a, **k)
+
+ monkeypatch.setattr(netproxy.socket, "create_connection", connect)
+ provider = seatbelt.SeatbeltProvider(
+ roots=[{"path": str(project), "writable": True}], cwd=project, runner_path=build_runner_zipapp(tmp_path / "dist"), credentials=grants
+ )
+ ws = RunnerWorkspace(provider, cwd=project)
+ try:
+ run = ws.executor.run
+ assert run("echo $HOME")["output"].strip() == provider.copied.home
+ assert "PRIVATE KEY" in run("cat ~/.ssh/id_ed25519")["output"]
+ assert "gho_x" in run('cat "$GH_CONFIG_DIR/hosts.yml"')["output"]
+ assert "not permitted" in run(f"ls {home} 2>&1")["output"] # the real home stays hidden
+ assert ws.describe()["credentials"][0]["name"] == "ssh"
+ assert "SSH keys" in ws.context()
+ # ssh: the ProxyCommand in the copied config carries the connection through the proxy
+ assert run("command -v ssh")["output"].strip() == os.path.join(provider._dir, "bin", "ssh") # the wrapper wins
+ out = run("ssh -o BatchMode=yes -o ConnectTimeout=8 -o StrictHostKeyChecking=no -v [email protected] true 2>&1 | grep -i 'remote protocol version\\|proxy\\|banner' | head -3")
+ assert "SSH-2.0-OpenSSH_9.9 fake" in out["output"] or "Remote protocol version 2.0" in out["output"], out["output"]
+ finally:
+ ws.close()
+ fake.close()
+ assert not Path(provider.copied.home).exists() # the copies died with the sandbox
diff --git a/tests/test_sandbox_seatbelt.py b/tests/test_sandbox_seatbelt.py
index e93f87cb..a046331e 100644
--- a/tests/test_sandbox_seatbelt.py
+++ b/tests/test_sandbox_seatbelt.py
@@ -136,7 +136,7 @@ def test_proxy_tunnels_a_listed_host(monkeypatch):
with real_connect(("127.0.0.1", proxy.port), timeout=5) as s:
s.sendall(b"CONNECT github.com:443 HTTP/1.1\r\nHost: github.com:443\r\n\r\n")
s.settimeout(5)
- assert s.recv(4096).startswith(b"HTTP/1.1 200 ")
+ assert s.recv(4096).startswith(b"HTTP/1.0 200 ")
s.sendall(b"ping")
assert s.recv(16) == b"pong"
assert wanted == [("github.com", 443)]