Python
Inside OpenShell 0.1: web fetches are not refused for the placeholder address, and the prompt says how a block looks
Rohit C Prasad956d005c0319
diff --git a/coworker/sandbox/inside.py b/coworker/sandbox/inside.pyindex 0600e9da..a0f36c68 100644--- a/coworker/sandbox/inside.py+++ b/coworker/sandbox/inside.py@@ -7,8 +7,8 @@ and three things follow from that, all here:- the agent is told what a blocked request looks like and what to do about it;- OpenShell's own skills folder is readable, so the agent can follow OpenShell's policyskill as OpenShell prescribes (it exists only when the user turned proposals on);-- the web tools leave the name lookup to OpenShell's proxy (web/guard.py): inside the- sandbox no name resolves and nothing connects directly.+- the web tools leave names to OpenShell's network layer (web/guard.py): what a name+ resolves to inside the sandbox is not its real address.Sites are added from outside the sandbox with OpenShell's own commands. OpenWorker does notgrant them, so `request_network_access` and the allowed-sites card are not part of this.@@ -37,24 +37,18 @@ def policy_skill_present() -> bool:return False-def proxied(scheme: str) -> bool:- """Whether a request of this scheme leaves through a proxy named in the environment- (the way OpenShell routes everything). The HTTP client reads the same variables."""- names = ("HTTPS_PROXY", "https_proxy") if scheme == "https" else ("HTTP_PROXY", "http_proxy")- return any(os.environ.get(name, "").strip() for name in (*names, "ALL_PROXY", "all_proxy"))--def context() -> str:"""The lines the agent reads each turn. Empty outside OpenShell."""if not inside_openshell():return ""lines = ["This session runs inside an OpenShell sandbox. Every network request, from your "- "commands and your web tools alike, goes through its proxy, which allows only what "- "its policy lists. A request the policy does not allow fails with a 403 from the "- "proxy (for example `CONNECT tunnel failed, response 403`) or says `policy_denied`. "- "That means the sandbox blocked it. Do not retry it unchanged, and do not look for "- "another route to the same place."+ "commands and your web tools alike, is held to its policy, which allows only what "+ "it lists. A request the policy does not allow fails in one of these ways: the "+ "connection is refused at once (`curl: (7) Failed to connect ... Couldn't connect "+ "to server`), a proxy answers 403 (`CONNECT tunnel failed, response 403`), or the "+ "reply says `policy_denied`. That means the sandbox blocked it. Do not retry it "+ "unchanged, and do not look for another route to the same place."]if policy_skill_present():lines.append(diff --git a/coworker/web/guard.py b/coworker/web/guard.pyindex 84b93140..c926077b 100644--- a/coworker/web/guard.py+++ b/coworker/web/guard.py@@ -77,13 +77,15 @@ def _vet(url: str) -> tuple[Optional[str], Optional[str]]:reason = _blocked_reason(literal)return (f"refusing to fetch {host}: {reason}" if reason else None), None- # Inside an OpenShell sandbox no name resolves and nothing connects directly: every- # request goes to OpenShell's proxy, which looks the name up itself and holds it to the- # sandbox's policy. So the name is not vetted or pinned here; the client hands it to- # the proxy. (A literal address was still judged above.)- from ..sandbox.inside import inside_openshell, proxied-- if inside_openshell() and proxied(parts.scheme):+ # Inside an OpenShell sandbox the sandbox's own network layer decides where a request+ # may go, by name, against its policy. What a name resolves to in there says nothing:+ # 0.0.x resolves no names at all (everything goes to its proxy), and 0.1 answers every+ # name with a placeholder address in 198.18.0.0/15, which this guard would refuse as+ # private. So the name is neither vetted nor pinned here. (A literal address was still+ # judged above.)+ from ..sandbox.inside import inside_openshell++ if inside_openshell():return None, Nonetry:diff --git a/tests/test_openshell_inside.py b/tests/test_openshell_inside.pyindex 396a2738..4566e45d 100644--- a/tests/test_openshell_inside.py+++ b/tests/test_openshell_inside.py@@ -80,6 +80,7 @@ def test_inside_the_agent_is_told_what_a_blocked_request_looks_like(in_openshelltext = _engine(tmp_path).context_provider()assert "runs inside an OpenShell sandbox" in textassert "CONNECT tunnel failed, response 403" in text and "policy_denied" in text+ assert "Couldn't connect to server" in text # how OpenShell 0.1 refuses# Proposals are off, so there is no skill to load: the person changes the policy.assert "openshell policy update --add-endpoint HOST:PORT" in textassert "openshell-policy-advisor" not in text@@ -141,17 +142,17 @@ def test_inside_a_literal_internal_address_is_still_refused(in_openshell, monkeyassert reason and "refusing to fetch" in reason and pin is None-def test_inside_without_a_proxy_for_the_scheme_the_name_is_still_vetted(in_openshell, monkeypatch: pytest.MonkeyPatch) -> None:- # Only HTTPS_PROXY is set: a plain http request would connect directly, so it is checked.- looked_up: list[str] = []-- def lookup(host, *_a, **_k):- looked_up.append(host)- return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("127.0.0.1", 80))]-- monkeypatch.setattr(socket, "getaddrinfo", lookup)- reason, _pin = guard._vet("http://rebind.example.org/")- assert looked_up == ["rebind.example.org"] and reason and "refusing to fetch" in reason+def test_inside_openshell_0_1_a_name_is_not_judged_by_its_placeholder_address(in_openshell, monkeypatch: pytest.MonkeyPatch) -> None:+ # OpenShell 0.1 sets no proxy variables and answers every name with an address in+ # 198.18.0.0/15, which the guard refuses as private. Seen on 0.1.2: example.org -> 198.18.0.2.+ monkeypatch.delenv("HTTPS_PROXY", raising=False)+ monkeypatch.setattr(socket, "getaddrinfo", lambda *_a, **_k: [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("198.18.0.2", 443))])+ assert guard._vet("https://example.org/page") == (None, None)+ assert guard._vet("http://example.org/page") == (None, None)+ # The same answer outside a sandbox is refused, as before.+ monkeypatch.delenv("OPENSHELL_SANDBOX")+ reason, _pin = guard._vet("https://example.org/page")+ assert reason and "198.18.0.2" in reasondef test_outside_a_name_is_looked_up_and_pinned_as_before(outside, monkeypatch: pytest.MonkeyPatch) -> None: