From cdbea83e1ccec6860797adfa2197e01d879b0a1a Mon Sep 17 00:00:00 2001 From: Francesco Bonacci Date: Sun, 16 Aug 2026 16:51:10 +0000 Subject: [PATCH] fix(computer-use): keep a v3 capability manifest on approval-bypassed runs `--yolo` / `-z` route the session onto a private embedded daemon in `unrestricted` mode. That daemon was constructed without the configured capability manifest, and the serve command only attached `--capability-manifest` when the mode was exactly `bounded`. So the moment a run was bypassed, the user's declared ceiling was dropped: without -z: --permission-mode bounded --capability-manifest ... with -z: --permission-mode unrestricted --dangerously-bypass-approvals No manifest, no warning. The most carefully configured run - a reviewed ceiling, written by hand - became the least constrained one, silently, and it failed open. That was never a driver limitation. cua-driver documents the manifest as a ceiling across modes ("A manifest can narrow a profile but never widen it"; its own authorization table calls it `optional_capability_manifest_ceiling`), and accepts it alongside `--permission-mode unrestricted`. The forwarding is version-aware, because the two manifest schemas differ (cua-driver session_manifest.rs): * v1/v2 are legacy and must declare `mode: bounded`. Handing one to an unrestricted runtime aborts startup with "legacy capability manifest mode must be bounded", so a naive forward would turn a working session into a hard failure. These are forwarded for bounded only, and a warning names the migration when one cannot apply. * v3 must not declare a mode. It is the mode-independent ceiling, and it now rides along with unrestricted. Unreadable or unparseable manifests are not forwarded outside bounded, on the same fail-safe reasoning; bounded still forwards unconditionally and lets the driver be the authority there. Verified against cua-driver 0.20.0 on Windows. Launch args now carry `--permission-mode unrestricted --dangerously-bypass-approvals --capability-manifest --approve-capability-manifest`, and the ceiling is enforced in the bypassed run - a tool outside the manifest is refused ("outside the capability manifest for this session ... blocked as a protected resource") where the same config previously ran unbounded. A legacy manifest was confirmed to abort driver startup when forwarded, which is what the version gate prevents. Co-Authored-By: Claude Opus 5 (1M context) --- ...test_computer_use_browser_authorization.py | 130 ++++++++++++++++++ tools/computer_use/cua_backend.py | 85 ++++++++++-- 2 files changed, 204 insertions(+), 11 deletions(-) diff --git a/tests/tools/test_computer_use_browser_authorization.py b/tests/tools/test_computer_use_browser_authorization.py index ed72f8a040..03851d6db5 100644 --- a/tests/tools/test_computer_use_browser_authorization.py +++ b/tests/tools/test_computer_use_browser_authorization.py @@ -478,3 +478,133 @@ def test_backend_bounded_without_manifest_fails_loudly(monkeypatch): def test_backend_rejects_unknown_mode(): with pytest.raises(ValueError, match="unsupported"): cb.CuaDriverBackend(permission_mode="wide-open") + + +# ── manifest is a ceiling, not a mode ─────────────────────────────────── + + +def _captured_serve_command(monkeypatch, daemon): + captured: Dict[str, Any] = {} + + class _FakeProc: + stderr = None + + def poll(self): + return None + + def _fake_popen(command, **kwargs): + captured["command"] = list(command) + return _FakeProc() + + def _fake_run(command, **kwargs): + class _Probe: + returncode = 0 + + return _Probe() + + monkeypatch.setattr(cb.subprocess, "Popen", _fake_popen) + monkeypatch.setattr(cb.subprocess, "run", _fake_run) + monkeypatch.setattr(cb, "_resolve_mcp_invocation", lambda cmd: (cmd, ["mcp"])) + daemon.start() + return captured["command"] + + +def test_unrestricted_daemon_carries_a_v3_manifest(monkeypatch, tmp_path): + """An approval bypass must not silently discard a v3 ceiling. + + cua-driver accepts a v3 manifest alongside any permission mode and it can + only narrow a profile, never widen it. Pairing it with unrestricted is + what bounds an approval-bypassed run to declared scope; dropping it meant + the most carefully configured run became the least constrained one. + """ + manifest = tmp_path / "cua-capabilities.yaml" + manifest.write_text("version: 3\nallow:\n tools:\n - list_windows\n", encoding="utf-8") + daemon = _EmbeddedCuaDaemon( + "cua-driver", "unrestricted", capability_manifest=str(manifest) + ) + + command = _captured_serve_command(monkeypatch, daemon) + + assert "--dangerously-bypass-approvals" in command + assert "--capability-manifest" in command + assert command[command.index("--capability-manifest") + 1] == str(manifest) + assert "--approve-capability-manifest" in command + assert command[command.index("--permission-mode") + 1] == "unrestricted" + + +def test_unrestricted_daemon_does_not_forward_a_legacy_manifest(monkeypatch, tmp_path): + """v1/v2 manifests must declare mode: bounded, so they cannot ride along. + + Forwarding one anyway aborts driver startup with "legacy capability + manifest mode must be bounded" — turning a working session into a hard + failure. Verified against cua-driver 0.20.0. + """ + manifest = tmp_path / "legacy.yaml" + manifest.write_text( + "version: 1\nmode: bounded\nexpires_after: 1h\nidle_timeout: 5m\n", + encoding="utf-8", + ) + daemon = _EmbeddedCuaDaemon( + "cua-driver", "unrestricted", capability_manifest=str(manifest) + ) + + command = _captured_serve_command(monkeypatch, daemon) + + assert "--dangerously-bypass-approvals" in command + assert "--capability-manifest" not in command + + +def test_bounded_forwards_a_legacy_manifest_unchanged(monkeypatch, tmp_path): + """bounded is exactly where legacy manifests belong; nothing changes.""" + manifest = tmp_path / "legacy.yaml" + manifest.write_text( + "version: 1\nmode: bounded\nexpires_after: 1h\nidle_timeout: 5m\n", + encoding="utf-8", + ) + daemon = _EmbeddedCuaDaemon( + "cua-driver", "bounded", capability_manifest=str(manifest) + ) + + command = _captured_serve_command(monkeypatch, daemon) + + assert command[command.index("--permission-mode") + 1] == "bounded" + assert command[command.index("--capability-manifest") + 1] == str(manifest) + assert "--approve-capability-manifest" in command + + +def test_unparseable_manifest_is_not_forwarded_to_unrestricted(monkeypatch, tmp_path): + """Fail safe: never turn a working bypassed run into a startup abort.""" + manifest = tmp_path / "broken.yaml" + manifest.write_text("{{{ not yaml", encoding="utf-8") + daemon = _EmbeddedCuaDaemon( + "cua-driver", "unrestricted", capability_manifest=str(manifest) + ) + + command = _captured_serve_command(monkeypatch, daemon) + + assert "--capability-manifest" not in command + + +def test_unrestricted_daemon_without_a_manifest_is_unchanged(monkeypatch): + """No manifest configured -> nothing new on the command line.""" + daemon = _EmbeddedCuaDaemon("cua-driver", "unrestricted") + + command = _captured_serve_command(monkeypatch, daemon) + + assert "--dangerously-bypass-approvals" in command + assert "--capability-manifest" not in command + + +def test_unrestricted_daemon_rejects_a_missing_manifest_path(tmp_path): + """A declared-but-absent ceiling fails loudly rather than silently opening up.""" + with pytest.raises(ValueError, match="capability manifest not found"): + _EmbeddedCuaDaemon( + "cua-driver", + "unrestricted", + capability_manifest=str(tmp_path / "does-not-exist.yaml"), + ) + + +def test_bounded_still_requires_a_manifest(): + with pytest.raises(ValueError, match="requires computer_use.capability_manifest"): + _EmbeddedCuaDaemon("cua-driver", "bounded") diff --git a/tools/computer_use/cua_backend.py b/tools/computer_use/cua_backend.py index 53cd1f1ce2..6b436c0308 100644 --- a/tools/computer_use/cua_backend.py +++ b/tools/computer_use/cua_backend.py @@ -286,6 +286,37 @@ def _cua_grant_existing_profile() -> bool: return bool(_computer_use_cfg().get("grant_existing_profile", False)) +def _manifest_is_mode_independent(path: str) -> bool: + """True when this capability manifest may accompany any permission mode. + + cua-driver splits manifests by schema version (session_manifest.rs): + + * v1/v2 are "legacy" — they must declare ``mode: bounded`` (or + ``autonomous``), so handing one to an unrestricted runtime aborts + startup with "legacy capability manifest mode must be bounded". + * v3 must NOT declare a mode. It is the mode-independent ceiling that + "can narrow a profile but never widen it", and the driver accepts it + alongside ``--permission-mode unrestricted``. + + Unreadable or unparseable manifests return False: forwarding one to a + non-bounded runtime would turn a working session into a hard startup + failure, and bounded forwards unconditionally anyway so the driver stays + the authority on validity there. + """ + try: + import yaml + + with open(path, "r", encoding="utf-8") as handle: + parsed = yaml.safe_load(handle) + except Exception: + logger.debug("could not read capability manifest %s", path, exc_info=True) + return False + if not isinstance(parsed, dict): + return False + version = parsed.get("version") + return isinstance(version, int) and not isinstance(version, bool) and version >= 3 + + def _standard_runtime_launch_args( args: List[str], *, @@ -532,6 +563,13 @@ class _EmbeddedCuaDaemon: launch via ``--approve-capability-manifest``. The manifest, not a runtime prompt, is the authorization boundary; calls outside it fail closed inside cua-driver. + + The manifest is a ceiling, not a mode. cua-driver accepts it alongside + any permission mode and it "can narrow a profile but never widen it", so + a configured manifest is forwarded here even when YOLO selected + ``unrestricted`` — that pairing is what bounds an approval-bypassed run + to declared scope. It stays mandatory for ``bounded`` and optional + everywhere else. """ _START_TIMEOUT_SECONDS = 15.0 @@ -546,20 +584,34 @@ class _EmbeddedCuaDaemon: raise ValueError( "embedded permission override supports unrestricted or bounded only" ) - if permission_mode == "bounded": - manifest = str(capability_manifest or "").strip() - if not manifest: - raise ValueError( - "bounded permission mode requires computer_use.capability_manifest" - ) + self.capability_manifest: Optional[str] = None + manifest = str(capability_manifest or "").strip() + if not manifest and permission_mode == "bounded": + raise ValueError( + "bounded permission mode requires computer_use.capability_manifest" + ) + if manifest: manifest = os.path.abspath(os.path.expanduser(manifest)) if not os.path.isfile(manifest): raise ValueError( f"capability manifest not found: {manifest}" ) - self.capability_manifest: Optional[str] = manifest - else: - self.capability_manifest = None + self.capability_manifest = manifest + # bounded always forwards — the driver validates it there. Any other + # mode only accepts a v3 (mode-independent) manifest; forwarding a + # legacy one would abort startup instead of bounding the run. + self.manifest_applies = bool(self.capability_manifest) and ( + permission_mode == "bounded" + or _manifest_is_mode_independent(str(self.capability_manifest)) + ) + if self.capability_manifest and not self.manifest_applies: + logger.warning( + "computer_use.capability_manifest is a legacy (v1/v2) manifest, " + "which cua-driver only accepts in bounded mode — it will NOT " + "bound this %s session. Migrate the manifest to version 3 to " + "keep a ceiling on approval-bypassed runs.", + permission_mode, + ) self.permission_mode = permission_mode self._driver_cmd = driver_cmd self._command = driver_cmd @@ -618,7 +670,13 @@ class _EmbeddedCuaDaemon: ] if self.permission_mode == "unrestricted": command.append("--dangerously-bypass-approvals") - else: # bounded — manifest validated in __init__ + # A v3 manifest is a ceiling, not a mode: cua-driver accepts it + # alongside any permission mode and it "can narrow a profile but never + # widen it". Attaching it to unrestricted is what bounds an + # approval-bypassed run to declared scope, so pass it whenever it + # applies — not only for bounded, which used to drop it for every + # other mode. + if self.manifest_applies: command.extend( [ "--capability-manifest", @@ -2389,8 +2447,13 @@ class CuaDriverBackend(ComputerUseBackend): raise ValueError(f"unsupported cua-driver permission mode: {permission_mode}") self.permission_mode = permission_mode if permission_mode == "unrestricted": + # Carry the manifest into unrestricted too. It is optional here + # (unlike bounded), but when the user declared one it still caps + # what an approval-bypassed run may touch. self._embedded_daemon: Optional[_EmbeddedCuaDaemon] = _EmbeddedCuaDaemon( - resolve_cua_driver_cmd() or "", permission_mode + resolve_cua_driver_cmd() or "", + permission_mode, + capability_manifest=_cua_capability_manifest(), ) elif permission_mode == "bounded": # Manifest path comes from config.yaml; _EmbeddedCuaDaemon