| 1 | """Run with Cage's Python inside a prepared, network-authorized test VirtualMac. |
| 2 | |
| 3 | Use the ordinary guest account. The test gives Chrome and the official CLI a |
| 4 | fresh temporary home, opens the official sign-in page, then cancels. It never |
| 5 | submits an account, authorization code or model prompt. |
| 6 | """ |
| 7 | import json |
| 8 | import os |
| 9 | from pathlib import Path |
| 10 | import pty |
| 11 | import secrets |
| 12 | import select |
| 13 | import signal |
| 14 | import subprocess |
| 15 | import tempfile |
| 16 | import time |
| 17 | from urllib.parse import urlsplit |
| 18 | |
| 19 | |
| 20 | def browser_request(browser, route): |
| 21 | browser.stdin.write(json.dumps({"method": "GET", "path": route}).encode() + b"\n") |
| 22 | browser.stdin.flush() |
| 23 | if not select.select([browser.stdout], [], [], 30)[0]: |
| 24 | raise RuntimeError("Guest Chrome timed out") |
| 25 | value = json.loads(browser.stdout.readline()) |
| 26 | if value.get("error"): |
| 27 | raise RuntimeError("The guest Chrome controller could not serve " + route + " (" + str(value.get("code")) + ")") |
| 28 | return value |
| 29 | |
| 30 | |
| 31 | def authorized_tab(snapshot): |
| 32 | for tab in snapshot.get("tabs", []): |
| 33 | if not tab.get("login") or tab["id"] != snapshot.get("active"): |
| 34 | continue |
| 35 | if tab["url"] in ("about:blank", "null"): |
| 36 | continue |
| 37 | url = urlsplit(tab["url"]) |
| 38 | if url.query or url.fragment or url.path not in ("", "/"): |
| 39 | raise RuntimeError("The workbench snapshot exposed a login URL path or parameters") |
| 40 | return url.scheme == "https" and url.hostname in ( |
| 41 | "claude.ai", "claude.com", "platform.claude.com", "platform.anthropic.com", "console.anthropic.com") |
| 42 | return False |
| 43 | |
| 44 | |
| 45 | def check_login(browser, env): |
| 46 | master, slave = pty.openpty() |
| 47 | try: |
| 48 | account = secrets.token_hex(12) |
| 49 | with subprocess.Popen(["/usr/local/bin/cage-claude-account", "login", account], |
| 50 | stdin=slave, stdout=slave, stderr=slave, env=env, |
| 51 | start_new_session=True) as cli: |
| 52 | try: |
| 53 | deadline = time.monotonic() + 45 |
| 54 | while cli.poll() is None and time.monotonic() < deadline: |
| 55 | |
| 56 | if select.select([master], [], [], .25)[0]: |
| 57 | os.read(master, 65536) |
| 58 | if authorized_tab(browser_request(browser, "/tabs")): |
| 59 | break |
| 60 | time.sleep(.3) |
| 61 | else: |
| 62 | raise RuntimeError("The CLI did not activate an isolated official authorization tab") |
| 63 | finally: |
| 64 | if cli.poll() is None: |
| 65 | os.killpg(cli.pid, signal.SIGINT) |
| 66 | try: |
| 67 | cli.wait(timeout=10) |
| 68 | except subprocess.TimeoutExpired: |
| 69 | os.killpg(cli.pid, signal.SIGKILL) |
| 70 | finally: |
| 71 | os.close(master) |
| 72 | os.close(slave) |
| 73 | if any(tab.get("login") for tab in browser_request(browser, "/tabs").get("tabs", [])): |
| 74 | raise RuntimeError("Canceling the login left its private tab open") |
| 75 | |
| 76 | |
| 77 | def main(): |
| 78 | model = subprocess.check_output(["/usr/sbin/sysctl", "-n", "hw.model"], text=True).strip() |
| 79 | if os.geteuid() == 0 or not model.startswith("VirtualMac") or Path("/var/db/cage-guest-os").read_text().strip() != "macos": |
| 80 | raise RuntimeError("Run in a prepared test VirtualMac as its ordinary account") |
| 81 | base = Path("/usr/local/libexec/cage-workbench-browser-4.145.0-pw1.65.0") |
| 82 | with tempfile.TemporaryDirectory(dir="/tmp", prefix="cgc.") as home: |
| 83 | env = dict(os.environ, HOME=home, TERM="xterm-256color", |
| 84 | PATH="/usr/local/libexec/cage-cli/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin") |
| 85 | with subprocess.Popen([base / "editor/lib/node", base / "controller/macos-native-browser.mjs"], |
| 86 | env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 87 | stderr=subprocess.DEVNULL) as browser: |
| 88 | try: |
| 89 | deadline = time.monotonic() + 45 |
| 90 | while True: |
| 91 | try: |
| 92 | if browser_request(browser, "/health").get("ready"): |
| 93 | break |
| 94 | except RuntimeError as error: |
| 95 | if "browser_unavailable" not in str(error) or time.monotonic() >= deadline: |
| 96 | raise |
| 97 | if time.monotonic() >= deadline: |
| 98 | raise RuntimeError("Guest Chrome is unavailable") |
| 99 | time.sleep(1) |
| 100 | check_login(browser, env) |
| 101 | finally: |
| 102 | browser.stdin.close() |
| 103 | try: |
| 104 | browser.wait(timeout=12) |
| 105 | except subprocess.TimeoutExpired: |
| 106 | browser.kill() |
| 107 | print("Official Claude Code authorization opened in guest Chrome and closed on cancellation.") |
| 108 | |
| 109 | |
| 110 | if __name__ == "__main__": |
| 111 | main() |