Python
Sandbox network: shared host lists and an allow-list proxy
Devika Verma0ab12bb4dcce
diff --git a/coworker/sandbox/netproxy.py b/coworker/sandbox/netproxy.pynew file mode 100644index 00000000..bc57427b--- /dev/null+++ b/coworker/sandbox/netproxy.py@@ -0,0 +1,151 @@+"""The allow-list proxy: the only way out for a sandbox that cannot filter by host itself.++Seatbelt can say "this process may connect to one local port and nowhere else", but it+cannot say "only to github.com". So the sandbox is given this port, and this proxy decides.+It runs in the server, outside every sandbox, and does one thing: `CONNECT host:443` for a+host on the profile's list is tunnelled; anything else gets a 403 that says why. The+traffic inside the tunnel is end-to-end TLS; the proxy never sees it.++Standard library only, one thread per tunnel. One proxy per network profile per process.+"""++from __future__ import annotations++import select+import socket+import threading+from collections import deque+from typing import Optional++from . import network_profiles++_IDLE_SECONDS = 300.0+_CONNECT_SECONDS = 15.0+_MAX_HEAD = 16 * 1024+++class AllowListProxy:+ def __init__(self, profile: str = network_profiles.DEFAULT_PROFILE) -> None:+ self.profile = network_profiles.check(profile)+ self._hosts = {h.lower() for h in network_profiles.hosts(profile)}+ 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)+ self._server.bind(("127.0.0.1", 0)) # this machine only+ self._server.listen(64)+ self.port: int = self._server.getsockname()[1]+ self._closed = False+ threading.Thread(target=self._accept, name=f"sandbox-proxy-{profile}", daemon=True).start()++ @property+ def url(self) -> str:+ 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++ def close(self) -> None:+ self._closed = True+ try:+ self._server.close()+ except OSError:+ pass++ def _accept(self) -> None:+ while not self._closed:+ try:+ client, _ = self._server.accept()+ except OSError:+ return+ threading.Thread(target=self._serve, args=(client,), daemon=True).start()++ def _serve(self, client: socket.socket) -> None:+ upstream: Optional[socket.socket] = None+ try:+ client.settimeout(_CONNECT_SECONDS)+ head = b""+ while b"\r\n\r\n" not in head:+ chunk = client.recv(4096)+ if not chunk or len(head) > _MAX_HEAD:+ return+ head += chunk+ request_line = head.split(b"\r\n", 1)[0].decode("latin-1")+ parts = request_line.split()+ if len(parts) != 3:+ 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)+ host, _, port_text = target.rpartition(":")+ host = host.strip("[]")+ if not host or not port_text.isdigit():+ return self._refuse(client, 400, "bad CONNECT target")+ if not self.allows(host, int(port_text)):+ return self._refuse(+ client,+ 403,+ f"{host}:{port_text} is not on this sandbox's allow list (network profile '{self.profile}')",+ f"{host}:{port_text}",+ )+ try:+ 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")+ rest = head.split(b"\r\n\r\n", 1)[1]+ if rest:+ upstream.sendall(rest)+ self._pump(client, upstream)+ except OSError:+ pass+ finally:+ for sock in (client, upstream):+ if sock is not None:+ try:+ sock.close()+ except OSError:+ pass++ def _refuse(self, client: socket.socket, status: int, why: str, target: str = "") -> None:+ if status == 403 and target:+ self.denied.append(target)+ body = f"Blocked by the OpenWorker sandbox: {why}.\n".encode()+ reason = {400: "Bad Request", 403: "Forbidden", 502: "Bad Gateway"}[status]+ head = f"HTTP/1.1 {status} {reason}\r\nContent-Type: text/plain\r\nContent-Length: {len(body)}\r\nConnection: close\r\n\r\n"+ client.sendall(head.encode() + body)++ @staticmethod+ def _pump(a: socket.socket, b: socket.socket) -> None:+ a.settimeout(None)+ b.settimeout(None)+ while True:+ ready, _, _ = select.select([a, b], [], [], _IDLE_SECONDS)+ if not ready:+ return+ for src in ready:+ data = src.recv(65536)+ if not data:+ return+ (b if src is a else a).sendall(data)+++_lock = threading.Lock()+_proxies: dict[str, AllowListProxy] = {}+++def shared(profile: str = network_profiles.DEFAULT_PROFILE) -> AllowListProxy:+ """This process's proxy for a profile, started on first use."""+ with _lock:+ proxy = _proxies.get(profile)+ if proxy is None or proxy._closed:+ proxy = _proxies[profile] = AllowListProxy(profile)+ return proxy+++def environment(proxy: AllowListProxy) -> dict[str, str]:+ """What a sandboxed process needs in its environment to find the proxy. Programs that+ ignore these variables get no network at all, which is the safe way to fail."""+ names = ("HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy")+ env = {name: proxy.url for name in names}+ env.update({"NO_PROXY": "", "no_proxy": "", "NODE_USE_ENV_PROXY": "1"})+ return envdiff --git a/coworker/sandbox/network_profiles.py b/coworker/sandbox/network_profiles.pynew file mode 100644index 00000000..520ab16b--- /dev/null+++ b/coworker/sandbox/network_profiles.py@@ -0,0 +1,29 @@+"""Where an agent's commands may connect, in OUR words. Every provider renders these lists+into its own mechanism: OpenShell into `network_policies`, Seatbelt into the allow-list+proxy. HTTPS (port 443) only.+"""++from __future__ import annotations++CODE_HOSTS = ["github.com", "api.github.com", "codeload.github.com", "objects.githubusercontent.com", "raw.githubusercontent.com", "gitlab.com"]+PACKAGE_REGISTRIES = ["pypi.org", "files.pythonhosted.org", "registry.npmjs.org", "crates.io", "static.crates.io", "index.crates.io", "proxy.golang.org", "sum.golang.org"]+SEARCH_APIS = ["api.search.brave.com", "api.tavily.com", "html.duckduckgo.com", "duckduckgo.com"]++# name of the group -> hosts, per profile+PROFILES: dict[str, dict[str, list[str]]] = {+ # git and package registries: enough to clone, install and push.+ "strict": {"code-hosts": CODE_HOSTS, "package-registries": PACKAGE_REGISTRIES},+ # plus the search APIs.+ "standard": {"code-hosts": CODE_HOSTS, "package-registries": PACKAGE_REGISTRIES, "search-apis": SEARCH_APIS},+}+DEFAULT_PROFILE = "strict"+++def check(profile: str) -> str:+ if profile not in PROFILES:+ raise ValueError(f"unknown network profile {profile!r} (known: {', '.join(sorted(PROFILES))})")+ return profile+++def hosts(profile: str) -> list[str]:+ return [h for group in PROFILES[check(profile)].values() for h in group]diff --git a/coworker/sandbox/providers/openshell_policy.py b/coworker/sandbox/providers/openshell_policy.pyindex d119a8ff..160c7cf1 100644--- a/coworker/sandbox/providers/openshell_policy.py+++ b/coworker/sandbox/providers/openshell_policy.py@@ -12,6 +12,8 @@ from __future__ import annotationsimport copyfrom typing import Any, Optional, Sequence+from .. import network_profiles+RUNNER_MOUNT = "/opt/openworker"RUNTIME_DIR = "/tmp" # the runner's socket and HOME live here; always read-write@@ -38,23 +40,14 @@ def _hosts(name: str, hosts: Sequence[str]) -> dict[str, Any]:return {"name": name, "endpoints": [{"host": h, "port": 443} for h in hosts], "binaries": [dict(b) for b in _ANY_BINARY]}-_CODE_HOSTS = _hosts(- "code-hosts",- ["github.com", "api.github.com", "codeload.github.com", "objects.githubusercontent.com", "raw.githubusercontent.com", "gitlab.com"],-)-_PACKAGES = _hosts(- "package-registries",- ["pypi.org", "files.pythonhosted.org", "registry.npmjs.org", "crates.io", "static.crates.io", "index.crates.io", "proxy.golang.org", "sum.golang.org"],-)-_SEARCH = _hosts("search-apis", ["api.search.brave.com", "api.tavily.com", "html.duckduckgo.com", "duckduckgo.com"])-+# The host lists are ours and shared by every provider (network_profiles.py); here they are+# only put into OpenShell's shape. Policy keys use underscores.+_POLICY_KEYS = {"code-hosts": "code_hosts", "package-registries": "packages", "search-apis": "search"}PROFILES: dict[str, dict[str, dict[str, Any]]] = {- # git and package registries: enough to clone, install and push.- "strict": {"code_hosts": _CODE_HOSTS, "packages": _PACKAGES},- # plus the search APIs.- "standard": {"code_hosts": _CODE_HOSTS, "packages": _PACKAGES, "search": _SEARCH},+ profile: {_POLICY_KEYS[name]: _hosts(name, hosts) for name, hosts in groups.items()}+ for profile, groups in network_profiles.PROFILES.items()}-DEFAULT_PROFILE = "strict"+DEFAULT_PROFILE = network_profiles.DEFAULT_PROFILEdef render(