Skip to content
Python

Settings: with OpenShell chosen, only the allow list is offered

Rohit C Prasadedeaad822907
diff --git a/coworker/sandbox/settings.py b/coworker/sandbox/settings.py
index 973b3edc..a2440f94 100644
--- a/coworker/sandbox/settings.py
+++ b/coworker/sandbox/settings.py
@@ -79,7 +79,9 @@ def snapshot(cfg: Optional[app_config.Config] = None) -> dict[str, Any]:
"providers": providers,
"windows_setup": _windows_setup_info(),
"network_profile": _profile_for_display(cfg.sandbox_network_profile),
- "network_profiles": [{"name": name} for name in network_profiles.PROFILES],
+ # The choices the page offers. OpenShell cannot allow every site (its policy has
+ # to name each one), so with OpenShell there is only the allow list.
+ "network_profiles": [{"name": name} for name in network_profiles.PROFILES if _offered(name, cfg.sandbox_provider or effective)],
"network_sites": [{"group": group, "hosts": [f"{h}:443" for h in hosts]} for group, hosts in network_profiles.SITES.items()],
"network_hosts": network_profiles.clean_hosts(cfg.sandbox_network_hosts),
"credentials": _for_display(credentials.listed(cfg.sandbox_credentials)),
@@ -89,6 +91,16 @@ def snapshot(cfg: Optional[app_config.Config] = None) -> dict[str, Any]:
}
+def _offered(profile: str, provider: str) -> bool:
+ return not (provider == OPENSHELL and network_profiles.is_open(profile))
+
+
+OPENSHELL_NEEDS_SITES = (
+ "OpenShell cannot allow every site: its policy has to name each site. "
+ 'Choose "Only the sites you allow" and tick the sites the work needs.'
+)
+
+
def _profile_for_display(configured: Optional[str]) -> str:
"""The machine's profile, or the default rule's when it has none."""
try:
@@ -211,12 +223,19 @@ def update(body: dict[str, Any]) -> dict[str, Any]:
if provider == OPENSHELL:
openshell_problem(fresh=True)
+ # "Allow everything" does not exist with OpenShell: a machine that had it moves
+ # to the allow list, or its sessions would not start.
+ stored = (app_config.load_config().sandbox_network_profile or "").strip().lower()
+ if stored and network_profiles.is_open(_profile_for_display(stored)) and "network_profile" not in body:
+ app_config.set_global_value("sandbox_network_profile", network_profiles.ALLOWLIST)
if "network_profile" in body:
profile = str(body.get("network_profile") or "").strip().lower()
try:
profile = network_profiles.check(profile)
except ValueError as exc:
return {"ok": False, "error": str(exc)}
+ if not _offered(profile, app_config.load_config().sandbox_provider or ""):
+ return {"ok": False, "error": OPENSHELL_NEEDS_SITES}
app_config.set_global_value("sandbox_network_profile", profile)
if "network_hosts" in body:
items = body.get("network_hosts")
diff --git a/surfaces/gui/src/components/SandboxSection.tsx b/surfaces/gui/src/components/SandboxSection.tsx
index 28dec968..5ea1eac3 100644
--- a/surfaces/gui/src/components/SandboxSection.tsx
+++ b/surfaces/gui/src/components/SandboxSection.tsx
@@ -485,7 +485,8 @@ export function SandboxSection({ machine, onProviderChanged }: { machine?: Machi
<>
<div className={FIELD_LABEL + " mb-2"}>{t("settingsx.sandbox.network")}</div>
<div className={CARD + " mb-5 divide-y divide-line"} role="radiogroup" aria-label={t("settingsx.sandbox.network")}>
- {(["allowlist", "open"] as const).map((name) => (
+ {/* The server lists the choices: with OpenShell there is no "Allow everything". */}
+ {(["allowlist", "open"] as const).filter((name) => cfg.network_profiles.some((p) => p.name === name)).map((name) => (
<label key={name} className="flex items-start gap-3 px-4 py-2.5 cursor-pointer">
<input
type="radio"
diff --git a/surfaces/gui/src/components/SettingsView.sandbox.test.tsx b/surfaces/gui/src/components/SettingsView.sandbox.test.tsx
index 33c0112c..39346ff6 100644
--- a/surfaces/gui/src/components/SettingsView.sandbox.test.tsx
+++ b/surfaces/gui/src/components/SettingsView.sandbox.test.tsx
@@ -262,6 +262,26 @@ describe("Settings ▸ Sandbox", () => {
expect(screen.getByTestId("sandbox-card-files")).toBeTruthy();
});
+ it("OpenShell chosen: only the allow list is offered, no Allow everything", async () => {
+ snapshot = {
+ ...base,
+ platform: "linux",
+ provider: "openshell",
+ effective_provider: "openshell",
+ providers: [
+ { name: "direct", usable: true, why: "", state: "ready" },
+ { name: "openshell", usable: true, why: "", state: "ready" },
+ ],
+ network_profiles: [{ name: "allowlist" }], // what the server sends with OpenShell
+ };
+ render(<SettingsView initialTab="sandbox" />);
+ await screen.findByTestId("sandbox-section");
+ expect(document.querySelectorAll('input[name="sandbox-network"]').length).toBe(1);
+ expect((screen.getByTestId("sandbox-network-allowlist") as HTMLInputElement).checked).toBe(true);
+ expect(screen.queryByTestId("sandbox-network-open")).toBeNull();
+ expect(screen.queryByText("Allow everything")).toBeNull();
+ });
+
it("a chosen type shows two network choices and two closed panels; off writes direct", async () => {
snapshot = chosenSeatbelt();
render(<SettingsView initialTab="sandbox" />);
diff --git a/tests/test_sandbox_settings.py b/tests/test_sandbox_settings.py
index 406af25e..dcce786a 100644
--- a/tests/test_sandbox_settings.py
+++ b/tests/test_sandbox_settings.py
@@ -258,3 +258,25 @@ def test_changing_the_provider_rebuilds_live_engines_built_under_the_old_rule(tm
mgr._engines["d"] = Engine(Direct())
res = client.post("/v1/settings/sandbox", json={"network_profile": "open"}).json()
assert res["ok"] and "rebuilt_sessions" not in res and "d" in mgr._engines
+
+
+def test_with_openshell_there_is_no_allow_everything(config_file, monkeypatch):
+ """OpenShell's policy has to name each site, so its only network choice is the allow list."""
+ from coworker.sandbox import network_profiles, selection
+
+ monkeypatch.setattr(selection, "openshell_problem", lambda fresh=False: "")
+ # Another sandbox, set to "Allow everything": both choices are offered.
+ assert settings.update({"provider": "direct", "network_profile": "open"})["ok"]
+ assert [n["name"] for n in settings.snapshot()["network_profiles"]] == ["allowlist", "open"]
+
+ # Choosing OpenShell moves the machine to the allow list and offers nothing else.
+ out = settings.update({"provider": "openshell"})
+ assert out["network_profile"] == network_profiles.ALLOWLIST
+ assert [n["name"] for n in out["network_profiles"]] == ["allowlist"]
+ assert 'sandbox_network_profile = "allowlist"' in config_file.read_text()
+
+ # Asking for it afterwards is refused, in plain words, and nothing changes.
+ refused = settings.update({"network_profile": "open"})
+ assert refused["ok"] is False and "cannot allow every site" in refused["error"]
+ assert 'sandbox_network_profile = "allowlist"' in config_file.read_text()
+