| 1 | #!/usr/bin/env python3 |
| 2 | """computer.py against a local dashboard whose host socket is a stub with one VM.""" |
| 3 | import argparse |
| 4 | import base64 |
| 5 | import http.client |
| 6 | import json |
| 7 | import os |
| 8 | import socket |
| 9 | import socketserver |
| 10 | import sqlite3 |
| 11 | import struct |
| 12 | import subprocess |
| 13 | import sys |
| 14 | import tempfile |
| 15 | import threading |
| 16 | import time |
| 17 | import unittest |
| 18 | from pathlib import Path |
| 19 | from urllib.parse import urlsplit |
| 20 | from urllib.request import Request |
| 21 | |
| 22 | sys.path.insert(0, str(Path(__file__).parent)) |
| 23 | import computer # noqa: E402 |
| 24 | |
| 25 | PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Wl6qWQAAAAASUVORK5CYII=" |
| 26 | HOST = "snowglobe.paperclover.net" |
| 27 | |
| 28 | |
| 29 | class Host(socketserver.StreamRequestHandler): |
| 30 | owner = None |
| 31 | files = {} |
| 32 | |
| 33 | def handle(self): |
| 34 | request = json.loads(self.rfile.readline()) |
| 35 | try: |
| 36 | reply = {"value": self.answer(request["operation"], request.get("payload") or {})} |
| 37 | except KeyError as missing: |
| 38 | reply = {"error": f"missing {missing}", "status": 404} |
| 39 | body = json.dumps(reply).encode() |
| 40 | try: |
| 41 | self.wfile.write(struct.pack(">I", len(body)) + body) |
| 42 | except BrokenPipeError: |
| 43 | pass # The dashboard can drop a request before its reply. |
| 44 | |
| 45 | def answer(self, operation, payload): |
| 46 | if operation == "vm.computers": |
| 47 | return [{"name": "test-vm", "owner": self.owner, "capabilities": ["computer"], "platform": "linux", |
| 48 | "os": "Fedora", "arch": "x86_64", "state": "running", "aiOwned": False}] |
| 49 | if operation == "vm.owner": |
| 50 | return {"owner": self.owner} |
| 51 | if operation != "vm.computer": |
| 52 | raise KeyError(operation) |
| 53 | request, files = payload["request"], self.files |
| 54 | if request["action"] == "exec": |
| 55 | return {"exit": 0, "stdout": "ran " + request["script"], "png": PNG} |
| 56 | if request["action"] == "shot": |
| 57 | return {"png": PNG} |
| 58 | operation, path = request["operation"], request["path"] |
| 59 | if operation == "stat": |
| 60 | return {"directory": True} if path.endswith("/") else {"directory": False, "size": len(files[path])} |
| 61 | if operation == "write": |
| 62 | files[path] = files.get(path, b"")[:request["offset"]] + base64.b64decode(request["data"]) |
| 63 | elif operation == "commit": |
| 64 | files[request["destination"]] = files.pop(path) |
| 65 | elif operation == "read": |
| 66 | data = files[path][request["offset"]:request["offset"] + request["length"]] |
| 67 | return {"data": base64.b64encode(data).decode()} |
| 68 | elif operation == "remove": |
| 69 | files.pop(path, None) |
| 70 | return {} |
| 71 | |
| 72 | |
| 73 | class Client(unittest.TestCase): |
| 74 | @classmethod |
| 75 | def setUpClass(cls): |
| 76 | cls.temporary = tempfile.TemporaryDirectory(prefix="computer-client-") |
| 77 | data = Path(cls.temporary.name).resolve() |
| 78 | proof = os.urandom(32).hex() |
| 79 | (data / "proof").write_text(proof) |
| 80 | with socket.socket() as available: |
| 81 | available.bind(("127.0.0.1", 0)) |
| 82 | port = available.getsockname()[1] |
| 83 | host = socketserver.ThreadingUnixStreamServer(str(data / "host.sock"), Host) |
| 84 | threading.Thread(target=host.serve_forever, daemon=True).start() |
| 85 | environment = {**os.environ, "SNOWGLOBE_DOMAIN": "paperclover.net", "SNOWGLOBE_DATA_DIR": str(data), |
| 86 | "SNOWGLOBE_PUBLIC_ORIGIN": "https://" + HOST, "SNOWGLOBE_AUTH_RP_ID": "auth.paperclover.net", |
| 87 | "SNOWGLOBE_FILE_ORIGIN": "https://file.paperclover.net", "SNOWGLOBE_AUTH_REQUIRED": "1", |
| 88 | "SNOWGLOBE_WEB_DIR": str(Path("dashboard/dist").resolve()), "PORT": str(port), |
| 89 | "SNOWGLOBE_PROXY_TOKEN_FILE": str(data / "proof"), "SNOWGLOBE_HOST_SOCKET": str(data / "host.sock")} |
| 90 | account = json.loads(subprocess.run([BINARY, "--preview-account"], env=environment, check=True, |
| 91 | capture_output=True, text=True).stdout) |
| 92 | with sqlite3.connect(data / "accounts.sqlite") as db: |
| 93 | Host.owner = db.execute("SELECT id FROM users WHERE username='preview'").fetchone()[0] |
| 94 | cls.log = data / "server.log" |
| 95 | cls.server = subprocess.Popen([BINARY], env=environment, stdout=cls.log.open("wb"), stderr=subprocess.STDOUT) |
| 96 | deadline = time.monotonic() + 30 |
| 97 | while True: |
| 98 | try: |
| 99 | socket.create_connection(("127.0.0.1", port), timeout=0.1).close() |
| 100 | break |
| 101 | except OSError: |
| 102 | assert cls.server.poll() is None and time.monotonic() < deadline, cls.log.read_text()[-2000:] |
| 103 | time.sleep(0.05) |
| 104 | cls.headers = {"Host": HOST, "Snowglobe-Proxy-Token": proof, "X-Snowglobe-Client-IP": "127.0.0.1"} |
| 105 | cls.port = port |
| 106 | real = computer.urlopen |
| 107 | |
| 108 | def local(request, timeout=None): |
| 109 | request = Request(request) if isinstance(request, str) else request |
| 110 | url = urlsplit(request.full_url) |
| 111 | assert url.netloc == HOST, request.full_url |
| 112 | request.full_url = f"http://127.0.0.1:{port}{url.path}" + (f"?{url.query}" if url.query else "") |
| 113 | for key, value in cls.headers.items(): |
| 114 | request.add_header(key, value) |
| 115 | return real(request, timeout=timeout) |
| 116 | |
| 117 | computer.urlopen = local |
| 118 | computer.ORIGIN, computer.RESOURCE = f"https://{HOST}/", f"https://{HOST}/mcp/computer" |
| 119 | computer.STATE = data / "client/state.json" |
| 120 | cls.cookies = {} |
| 121 | csrf = cls.request("/auth/status")["csrf"] |
| 122 | cls.request("/auth/password", "POST", {"csrf": csrf, "username": account["username"], |
| 123 | "password": account["password"], "next": "/"}) |
| 124 | |
| 125 | @classmethod |
| 126 | def tearDownClass(cls): |
| 127 | cls.server.terminate() |
| 128 | cls.server.wait(timeout=10) |
| 129 | cls.temporary.cleanup() |
| 130 | |
| 131 | @classmethod |
| 132 | def request(cls, path, method="GET", body=None): |
| 133 | headers = {**cls.headers, "Cookie": "; ".join(f"{k}={v}" for k, v in cls.cookies.items())} |
| 134 | if body is not None: |
| 135 | headers.update({"Origin": "https://" + HOST, "Content-Type": "application/json"}) |
| 136 | connection = http.client.HTTPConnection("127.0.0.1", cls.port, timeout=30) |
| 137 | connection.request(method, path, json.dumps(body) if body is not None else None, headers) |
| 138 | response = connection.getresponse() |
| 139 | content = response.read() |
| 140 | for cookie in response.headers.get_all("set-cookie") or []: |
| 141 | key, value = cookie.split(";", 1)[0].split("=", 1) |
| 142 | cls.cookies[key] = value |
| 143 | assert response.status in (200, 302), (path, response.status, content[:300]) |
| 144 | return response.headers["location"] if response.status == 302 else json.loads(content) |
| 145 | |
| 146 | def connect(self): |
| 147 | link = urlsplit(computer.login()) |
| 148 | pending = self.request(f"{link.path}?{link.query}").removeprefix("/connect/") |
| 149 | self.assertEqual(self.request(f"/api/mcp/consent/{pending}")["scopes"], computer.SCOPES.split()) |
| 150 | return self.request(f"/api/mcp/consent/{pending}", "POST", {"resources": ["test-vm"]})["redirect"] |
| 151 | |
| 152 | def test_end_to_end(self): |
| 153 | with self.assertRaisesRegex(computer.ComputerError, "login"): |
| 154 | computer.Computer().call("computer_list") |
| 155 | redirected = self.connect() |
| 156 | with self.assertRaisesRegex(computer.ComputerError, "different login"): |
| 157 | computer.login(redirected.replace("state=", "state=x")) |
| 158 | redirected = self.connect() |
| 159 | self.assertIsNone(computer.login(redirected)) |
| 160 | self.assertEqual(computer.STATE.stat().st_mode & 0o777, 0o600) |
| 161 | |
| 162 | vm = computer.Computer() |
| 163 | self.assertIn("computer_exec", {tool["name"] for tool in vm.tools()}) |
| 164 | self.assertEqual([c["id"] for c in vm.call("computer_list")["computers"]], ["test-vm"]) |
| 165 | ran = vm.call("computer_exec", vm="test-vm", script="uname") |
| 166 | self.assertEqual((ran["stdout"], ran["png"]), ("ran uname", base64.b64decode(PNG))) |
| 167 | with self.assertRaisesRegex(computer.ComputerError, "granted"): |
| 168 | vm.call("computer_shot", vm="other-vm") |
| 169 | |
| 170 | source = Path(self.temporary.name) / "payload.bin" |
| 171 | source.write_bytes(os.urandom(200_000)) |
| 172 | self.assertEqual(vm.upload("test-vm", source, "/tmp/payload.bin")["bytes"], 200_000) |
| 173 | vm.download("test-vm", "/tmp/payload.bin", source.with_suffix(".back")) |
| 174 | self.assertEqual(source.with_suffix(".back").read_bytes(), source.read_bytes()) |
| 175 | |
| 176 | self.assertIn("png", vm.call("computer_shot", vm="test-vm")) |
| 177 | |
| 178 | state = json.loads(computer.STATE.read_text()) |
| 179 | computer.STATE.write_text(json.dumps({**state, "access_token": "x" * 43})) |
| 180 | self.assertTrue(computer.Computer().call("computer_list")["computers"]) |
| 181 | |
| 182 | # Concurrent renewals must not replay a refresh token, which would revoke the connection. |
| 183 | state = json.loads(computer.STATE.read_text()) |
| 184 | computer.STATE.write_text(json.dumps({**state, "expires_at": 0})) |
| 185 | results = [] |
| 186 | threads = [threading.Thread(target=lambda: results.append(computer.Computer().call("computer_list"))) |
| 187 | for _ in range(6)] |
| 188 | for thread in threads: |
| 189 | thread.start() |
| 190 | for thread in threads: |
| 191 | thread.join() |
| 192 | self.assertEqual(len(results), 6) |
| 193 | self.assertNotEqual(json.loads(computer.STATE.read_text())["refresh_token"], state["refresh_token"]) |
| 194 | self.assertTrue(computer.Computer().call("computer_list")["computers"]) |
| 195 | |
| 196 | |
| 197 | if __name__ == "__main__": |
| 198 | parser = argparse.ArgumentParser() |
| 199 | parser.add_argument("--binary", type=Path, default=Path("dashboard/target/debug/home-dashboard")) |
| 200 | args, rest = parser.parse_known_args() |
| 201 | BINARY = str(args.binary.resolve()) |
| 202 | unittest.main(argv=[sys.argv[0], *rest]) |