Computer use for the Pi coding agent — Windows apps, browsers and desktops through native UI trees, screenshots and real input.
TypeScript accessibilityagent-toolsai-agentbrowser-automationcomputer-usecuadesktop-automationelectronpipi-coding-agentpi-extensionpi-packageplaywrighttypescriptui-automationvisionwebview2windowswindows-automationwsl
test: cover authenticated native identity lifetime and cleanup (LPT-1026)
alexshpunt-clanker[bot]419ca1648cd6
diff --git a/test/identity-auth.test.ts b/test/identity-auth.test.tsnew file mode 100644index 0000000..e8e6c88--- /dev/null+++ b/test/identity-auth.test.ts@@ -0,0 +1,55 @@+import { createServer, type AddressInfo, type Socket } from "node:net";+import { expect, test } from "vitest";+import { createInterface } from "node:readline";+import { acceptRelaySocket } from "../src/relay-auth.ts";+import { McpDriver } from "../src/driver.ts";++test("Sandbox identity uses its own credential and keeps one persistent peer until close", async () => {+ const auth = { session: "sandbox-test", key: "cd".repeat(32) };+ const sockets = new Set<Socket>(); let accepted = 0, requests = 0, closed = 0;+ let reportClosed: () => void;+ const peerClosed = new Promise<void>(resolve => { reportClosed = resolve; });+ const server = createServer(socket => {+ accepted++; sockets.add(socket); socket.on("error", () => {});+ socket.on("close", () => { sockets.delete(socket); closed++; reportClosed(); });+ void acceptRelaySocket(socket, auth, { channel: "identity", role: "client" }).then(() => {+ createInterface({ input: socket }).on("line", line => {+ const request = JSON.parse(line); requests++;+ socket.write(JSON.stringify({ identity: request.window_id === 123 && request.pid === 456 ? "private-marker" : null }) + "\n");+ });+ }, () => socket.destroy());+ });+ await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve));+ const driver = new McpDriver({ command: "unused.exe", endpoint: { url: "http://127.0.0.1:1/mcp", token: "unused",+ identityPort: (server.address() as AddressInfo).port, identityAuth: auth, relay: { session: auth.session, key: "ef".repeat(32) } } });+ try {+ expect(await driver.identity({ window_id: 123, pid: 456 }, true)).toBe("private-marker");+ expect(await driver.identity({ window_id: 123, pid: 456 }, false)).toBe("private-marker");+ expect(accepted).toBe(1); expect(requests).toBe(2);+ await driver.close(); await peerClosed; expect(closed).toBe(1);+ } finally {+ await driver.close(); sockets.forEach(s => s.destroy());+ await new Promise<void>(resolve => server.close(() => resolve()));+ }+});++test("a false Sandbox identity endpoint receives no window request before proving its identity", async () => {+ const sockets = new Set<Socket>(), received: Buffer[] = [];+ const server = createServer(socket => {+ sockets.add(socket); socket.on("close", () => sockets.delete(socket)); socket.on("error", () => {});+ socket.write(JSON.stringify({ version: 1, nonce: "ab".repeat(32) }) + "\n");+ socket.on("data", chunk => { received.push(chunk); socket.end(JSON.stringify({ proof: "00".repeat(32) }) + "\n"); });+ });+ await new Promise<void>(resolve => server.listen(0, "127.0.0.1", resolve));+ const port = (server.address() as AddressInfo).port;+ const driver = new McpDriver({ command: "unused.exe", endpoint: { url: "http://127.0.0.1:1/mcp", token: "unused", identityPort: port,+ identityAuth: { session: "sandbox-test", key: "cd".repeat(32) } } });+ try {+ await expect(driver.identity({ window_id: 123, pid: 456 }, true)).rejects.toBeInstanceOf(Error);+ expect(Buffer.concat(received).toString()).not.toContain('"window_id"');+ } finally {+ await driver.close();+ for (const socket of sockets) socket.destroy();+ await new Promise<void>(resolve => server.close(() => resolve()));+ }+});diff --git a/test/live/identity-service.live.ts b/test/live/identity-service.live.tsnew file mode 100644index 0000000..692d37c--- /dev/null+++ b/test/live/identity-service.live.ts@@ -0,0 +1,91 @@+import { randomBytes, randomUUID } from "node:crypto";+import { readFile } from "node:fs/promises";+import { connect, type Socket } from "node:net";+import { setTimeout as delay } from "node:timers/promises";+import { expect, test } from "vitest";+import { machineConfig } from "../../src/machine.ts";+import { connectRelay } from "../../src/relay-auth.ts";+import { localRunner, sshRunner } from "../../src/runner.ts";+import { runnerHost } from "../../src/sandbox.ts";+import { remoteIdentityPeer, WindowIdentity } from "../../src/window-identity.ts";++// Run explicitly on an already configured Windows target, under the shared project worker lock.+// No GUI or user window is touched: authorized protocol requests use HWND/PID zero.+test("the real PowerShell identity service rejects foreign clients and preserves its idle peer", async () => {+ const name = process.env.LIVE_MACHINE ?? "lab";+ const runner = name === "local" ? localRunner(process.env.WSL_INTEROP) : sshRunner((await machineConfig(process.cwd(), name)).ssh);+ const host = runnerHost(runner);+ const quote = (s: string) => "'" + s.replaceAll("'", "''") + "'";+ const local = (await host.powershell("$env:LOCALAPPDATA")).trim();+ const windows = `${local}\\PiComputerUse\\identity-checks\\${randomUUID()}`;+ const dir = await host.path(windows, "unix");+ const auth = { session: "identity-live", key: randomBytes(32).toString("hex") };+ let pid: number | undefined;+ let tunnel: { port: number; close(): void } | undefined;+ let identity: WindowIdentity | undefined;+ const sockets: Socket[] = [];+ const sources = new URL("../../scripts/", import.meta.url);+ const children = async () => JSON.parse(await host.powershell(`ConvertTo-Json -InputObject @(Get-CimInstance Win32_Process -Filter "ParentProcessId = ${pid}" | Where-Object { $_.Name -eq 'powershell.exe' } | ForEach-Object { [int]$_.ProcessId }) -Compress`)) as number[];+ try {+ await host.mkdir(dir);+ for (const name of ["identity-auth.ps1", "identity-channel.ps1", "identity-channel.cs", "window-identity.ps1"])+ await host.writeFile(`${dir}/${name}`, await readFile(new URL(name, sources), "utf8"));+ await host.writeFile(`${dir}/relay-auth.ps1`, await readFile(new URL("session/relay-auth.ps1", sources), "utf8"));+ await host.writeFile(`${dir}/guest-identity-bridge.ps1`, await readFile(new URL("sandbox/guest-identity-bridge.ps1", sources), "utf8"));+ await host.writeFile(`${dir}/identity.json`, JSON.stringify(auth));+ const port = Number((await host.powershell("$l = [Net.Sockets.TcpListener]::new([Net.IPAddress]::Loopback, 0); $l.Start(); $port = $l.LocalEndpoint.Port; $l.Stop(); $port")).trim());+ pid = Number((await host.powershell(`(Start-Process powershell.exe -WindowStyle Hidden -PassThru -ArgumentList ${quote(`-NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "${windows}\\guest-identity-bridge.ps1" -Address 127.0.0.1 -Port ${port} -Peer "${windows}\\window-identity.ps1" -Credentials "${windows}\\identity.json"`)}).Id`)).trim());+ expect(pid).toBeGreaterThan(0);+ tunnel = await host.forward(port);+ identity = new WindowIdentity(remoteIdentityPeer(tunnel.port, "127.0.0.1", auth));+ let ready = false;+ for (let i = 0; i < 20; i++) {+ try { await identity.identify({ window_id: 0, pid: 0 }, false); ready = true; break; }+ catch { await delay(250); }+ }+ expect(ready).toBe(true);+ const peers = await children(); expect(peers).toHaveLength(1);+ const idle = connect(tunnel.port, "127.0.0.1"); sockets.push(idle); idle.on("error", () => {}); idle.resume();+ await new Promise<void>((resolve, reject) => { idle.once("connect", resolve); idle.once("error", reject); });+ let idleClosed = false; idle.once("close", () => { idleClosed = true; });+ const peer = await connectRelay(tunnel.port, auth, { channel: "identity", role: "client" }); sockets.push(peer);+ expect(idleClosed).toBe(false);+ peer.end();+ for (const [credentials, context] of [+ [{ ...auth, key: "00".repeat(32) }, { channel: "identity", role: "client" }],+ [{ ...auth, session: "another-session" }, { channel: "identity", role: "client" }],+ [auth, { channel: "driver", role: "client" }],+ [auth, { channel: "identity", role: "session" }],+ ] as const) await expect(connectRelay(tunnel.port, credentials, context)).rejects.toBeInstanceOf(Error);+ // A foreign client need not verify the server's reply. Test server enforcement with a raw forged proof too.+ const foreign = connect(tunnel.port, "127.0.0.1"); sockets.push(foreign); foreign.on("error", () => {});+ let response = ""; foreign.on("data", chunk => { response += chunk.toString(); });+ const rejected = new Promise<void>((resolve, reject) => {+ const timer = setTimeout(() => reject(new Error("Unauthenticated connection stayed open")), 4500);+ foreign.once("close", () => { clearTimeout(timer); resolve(); });+ });+ foreign.once("connect", () => foreign.write(JSON.stringify({ version: 1, session: auth.session, channel: "identity", role: "client",+ nonce: "cd".repeat(32), proof: "00".repeat(32) }) + "\n" + JSON.stringify({ window_id: 0, pid: 0, create: false }) + "\n"));+ await rejected;+ expect(response).not.toContain('"proof"');+ expect(response).not.toContain('"identity"');+ await delay(4000);+ expect(await identity.identify({ window_id: 0, pid: 0 }, false)).toBeUndefined();+ expect(await children()).toEqual(peers);+ await identity.close();+ let remaining: number[] = [];+ for (let i = 0; i < 10; i++) { remaining = await children(); if (!remaining.length) break; await delay(250); }+ expect(remaining).toEqual([]);+ } finally {+ sockets.forEach(socket => socket.destroy());+ await identity?.close(); tunnel?.close();+ if (pid) {+ for (let i = 0; i < 10; i++) { if (!(await children()).length) break; await delay(250); }+ await host.powershell(`$p = Get-CimInstance Win32_Process -Filter "ProcessId = ${pid}"; if ($p -and $p.CommandLine.Contains(${quote(`${windows}\\guest-identity-bridge.ps1`)})) {+ Get-CimInstance Win32_Process -Filter "ParentProcessId = ${pid}" | Where-Object { $_.Name -eq 'powershell.exe' } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }+ Stop-Process -Id ${pid} -Force+}`);+ }+ await host.remove(dir);+ }+}, 90_000);diff --git a/test/sandbox.test.ts b/test/sandbox.test.tsindex 40220df..b4ad0fc 100644--- a/test/sandbox.test.ts+++ b/test/sandbox.test.ts@@ -32,9 +32,10 @@ test("the sandbox shares only the runtime, its own config, an output folder and});/** A host whose sandbox launch stops at `failAt`; every script is recorded. */-async function fakeHost(failAt: "session" | "id", running = false) {+async function fakeHost(failAt: "session" | "id" | "ready", running = false, failForward?: number) {const local = await mkdtemp(path.join(tmpdir(), "pi-sandbox-test-"));const scripts: string[] = [];+ const forwarded: number[] = [], closedForwards: number[] = [];const host: SandboxHost = {async path(value, to) {if (to === "windows") return value.startsWith("/mnt/c/") ? "C:\\" + value.slice(7).replaceAll("/", "\\") : "\\\\wsl.localhost\\Ubuntu" + value.replaceAll("/", "\\");@@ -44,23 +45,60 @@ async function fakeHost(failAt: "session" | "id", running = false) {async powershell(script) {scripts.push(script);if (script.includes("sandbox=")) return "sandbox=True;wsb=True;missing=;local=C:\\Local\n";- if (script.includes("did not start within")) {+ if (script.includes("Get-Process vmmemWindowsSandbox") && script.includes("Select-Object -First 1")) {if (failAt === "id") throw new Error("Cancelled while starting the sandbox.");return "11111111-2222-3333-4444-555555555555\n";}- if (script.includes("user session did not become available")) throw new Error("The sandbox user session did not become available within 120 s.");+ if (script.includes("cmd.exe /c exit 0")) {+ if (failAt !== "ready") throw new Error("The sandbox user session did not become available within 120 s.");+ return "";+ }+ if (script.includes(".IpV4Address")) return "172.20.0.2\n";if (script.includes("Get-Process WindowsSandboxRemoteSession -ErrorAction SilentlyContinue) { 'running' }")) return running ? "running\n" : "";return "";},mkdir: async dir => { await mkdir(dir, { recursive: true }); },writeFile: (file, content) => writeFile(file, content),- readFile: file => readFile(file, "utf8").catch(() => ""),+ readFile: file => {+ const relay = /relay-(47800|47802)\.txt$/.exec(file);+ if (failAt === "ready" && relay) return Promise.resolve(relay[1] === "47800" ? "12001 4242" : "12003 4243");+ return readFile(file, "utf8").catch(() => "");+ },remove: dir => rm(dir, { recursive: true, force: true }),- forward: async port => ({ port, close: () => {} }),+ forward: async port => {+ forwarded.push(port);+ if (forwarded.length === failForward) throw new Error("Fixture forward failure");+ return { port, close: () => { closedForwards.push(port); } };+ },};- return { host, scripts, local };+ return { host, scripts, local, forwarded, closedForwards };}+test("Sandbox provisions a separate identity key and returns it only for the identity endpoint", async () => {+ const f = await fakeHost("ready");+ let sandbox: WindowsSandbox | undefined;+ try {+ sandbox = await WindowsSandbox.open(f.host, "/mnt/c/runtime/cua-driver.exe", { shares: [] });+ const config = JSON.parse(await readFile(path.join(f.local, sandbox.ref, "config/identity.json"), "utf8"));+ expect(config.session).toBe(sandbox.ref);+ expect(config.key).toMatch(/^[a-f0-9]{64}$/);+ expect(config.key).not.toBe(sandbox.endpoint?.token);+ expect(sandbox.endpoint?.identityAuth).toEqual(config);+ expect(sandbox.endpoint?.relay).toBeUndefined();+ expect(f.forwarded).toEqual([12001, 12003]);+ } finally { await sandbox?.close(); await rm(f.local, { recursive: true, force: true }); }+ expect(f.closedForwards).toEqual([12001, 12003]);+});++test("a failed identity forward releases the already opened driver tunnel", async () => {+ const f = await fakeHost("ready", false, 2);+ try {+ await expect(WindowsSandbox.open(f.host, "/mnt/c/runtime/cua-driver.exe", { shares: [] })).rejects.toBeInstanceOf(Error);+ expect(f.closedForwards).toEqual([12001]);+ expect(await readdir(f.local)).toEqual([]);+ } finally { await rm(f.local, { recursive: true, force: true }); }+});+test("a sandbox that fails after launch is stopped and its host folder removed", async () => {const { host, scripts, local } = await fakeHost("session");await expect(WindowsSandbox.open(host, "/mnt/c/runtime/cua-driver.exe", sandboxOptions({ action: "open_sandbox" }))).rejects.toThrow(/user session/);