Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

14.0 KB
Raw Blame
#!/usr/bin/env node
/**
 * MCP market E2E (headless protocol-level).
 * Covers the market install path and the public-network boundary:
 *
 *   E2E-MCP-MARKET-INSTALL        builtin entry → mcp.upsert → record on disk
 *   E2E-MCP-MARKET-SEMANTICS      registry record → template keeps named
 *                                 arguments and required/optional envs
 *   E2E-MCP-MARKET-HEADER-SCOPE   header credential stays out of the URL
 *                                 through mapping, resolution and persistence; unbound
 *                                 headers stay literal with a partial binding map
 *   E2E-MCP-MARKET-NET-BOUNDARY   a user source may be loopback/private (and
 *                                 plaintext under the stored opt-in), while a
 *                                 registry remote, a catalog body endpoint and
 *                                 any cloud metadata host stay public-only
 *
 * Env: PI_DESKTOP_HOST_BIN (optional), DEBUG_HOST for tracing.
 * Deterministic: no live network access.
 */
import { spawn } from "node:child_process";
import { readNdjsonLines } from "../packages/shared/dist/ndjson.js";
import { randomUUID } from "node:crypto";
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { PROTOCOL_VERSION } from "../packages/shared/dist/protocol.js";
import {
  BUILTIN_MCP_CATALOG,
  GLOBAL_SCOPE,
  isPublicHttpsUrl,
  isPublicIpLiteral,
  isSafeMarketSourceUrl,
  mapRegistryServer,
  resolveCatalogEntry,
  validateMcpCatalogFile,
} from "../packages/shared/dist/index.js";

const __dirname = dirname(fileURLToPath(import.meta.url));
const root = join(__dirname, "..");
const hostBinCandidates = [];
const configuredHostBin = process.env.PI_DESKTOP_HOST_BIN?.trim();
if (configuredHostBin) {
  const configured = resolve(configuredHostBin);
  hostBinCandidates.push(configured);
  if (process.platform === "win32" && !configured.toLowerCase().endsWith(".exe")) {
    hostBinCandidates.push(`${configured}.exe`);
  }
}
const hostBinaryName = `pi-desktop-host-core${process.platform === "win32" ? ".exe" : ""}`;
hostBinCandidates.push(join(root, "target", "debug", hostBinaryName));
hostBinCandidates.push(join(root, "..", "..", "..", "target", "debug", hostBinaryName));
const hostBin = hostBinCandidates.find((candidate) => existsSync(candidate));

if (!hostBin) {
  console.error("host binary missing; tried:", hostBinCandidates.join(", "));
  process.exit(1);
}

const results = [];
function record(id, ok, detail = "") {
  results.push({ id, ok, detail });
  console.log(`${ok ? "PASS" : "FAIL"} ${id}${detail ? " — " + detail : ""}`);
}

class Host {
  constructor(bin, dataDir, home) {
    this.child = spawn(bin, [], {
      stdio: ["pipe", "pipe", "pipe"],
      env: {
        ...process.env,
        HOME: home,
        PI_DESKTOP_DATA_DIR: dataDir,
      },
    });
    this.pending = new Map();
    this.child.stderr.on("data", () => {});
    readNdjsonLines(this.child.stdout, (line) => {
      let msg;
      try {
        msg = JSON.parse(line);
      } catch {
        return;
      }
      if (msg.id != null && this.pending.has(String(msg.id))) {
        const pending = this.pending.get(String(msg.id));
        this.pending.delete(String(msg.id));
        if (msg.error) pending.reject(new Error(JSON.stringify(msg.error)));
        else pending.resolve(msg.result);
      }
    });
    this.child.on("exit", (code) => {
      for (const pending of this.pending.values()) {
        pending.reject(new Error(`host exited code=${code}`));
      }
      this.pending.clear();
    });
  }
  call(method, params = {}) {
    const id = randomUUID();
    return new Promise((resolve, reject) => {
      this.pending.set(String(id), { resolve, reject });
      this.child.stdin.write(JSON.stringify({ jsonrpc: "2.0", id, method, params }) + "\n");
      setTimeout(() => {
        if (this.pending.has(String(id))) {
          this.pending.delete(String(id));
          reject(new Error(`timeout ${method}`));
        }
      }, 30_000);
    });
  }
  dispose() {
    this.child.kill("SIGTERM");
  }
}

// ── E2E-MCP-MARKET-NET-BOUNDARY ──────────────────────────────────────────
{
  // A market source URL is an address the user typed into a settings field, so
  // their own machine and their own LAN are reachable there, and a plaintext
  // source is reachable once the stored `networkPolicy` accepts it. Everything
  // the source *returns* — a registry record, a catalog body, a redirect target
  // — keeps the public-only rule, because that is the input an attacker holds.
  const userAccepted = [
    "https://127.0.0.1/x",
    "https://10.1.2.3/x",
    "https://192.168.1.5:8443/x",
    "https://localhost./x",
    "https://nas.local/x",
    "https://[::1]/x",
    "https://[::ffff:127.0.0.1]/x",
    "https://[fd00::1]/x",
    "https://[fe80::1]/x",
    "https://[fec0::1]/x",
  ];
  const userRefused = [
    "https://169.254.169.254/x",
    "https://169.254.169.254./x",
    "https://100.100.100.200/x",
    "https://[fd00:ec2::254]/x",
    "https://metadata.google.internal/x",
    "https://metadata./x",
    "https://instance-data/x",
    "https://0.0.0.0/x",
    "https://[::]/x",
    "https://239.1.2.3/x",
    "https://240.0.0.1/x",
    "https://192.0.2.1/x",
    "https://[2001:db8::1]/x",
    "https://user:[email protected]/x",
    "file:///etc/passwd",
    "not a url",
  ];
  const userPlaintext = [
    "http://registry.example/x",
    "http://10.0.0.7:8080/x",
    "http://nas.local/x",
  ];
  const userGateOk =
    userAccepted.every((url) => isSafeMarketSourceUrl(url) === true) &&
    userRefused.every((url) => isSafeMarketSourceUrl(url) === false) &&
    userPlaintext.every((url) => isSafeMarketSourceUrl(url) === false) &&
    userPlaintext.every((url) => isSafeMarketSourceUrl(url, { allowInsecureHttp: true }) === true) &&
    isSafeMarketSourceUrl("http://169.254.169.254/x", { allowInsecureHttp: true }) === false;

  // The third-party positions: a registry record's remote URL and a catalog
  // body's endpoint, neither of which the user typed.
  const thirdPartyRefused = [
    "https://127.0.0.1/x",
    "https://10.1.2.3/x",
    "https://192.168.1.5/x",
    "https://[fd00::1]/x",
    "https://[fe80::1]/x",
    "https://169.254.169.254/x",
    "https://nas.local/x",
    "http://registry.example/x",
  ];
  const accepted = "https://registry.modelcontextprotocol.io/v0/servers";
  const publicOk =
    isSafeMarketSourceUrl(accepted) &&
    isSafeMarketSourceUrl("https://[2606:4700::1]/x") &&
    isPublicIpLiteral("2606:4700:4700::1111") &&
    isPublicHttpsUrl(accepted);
  const thirdPartyOk =
    thirdPartyRefused.every((url) => isPublicHttpsUrl(url) === false) &&
    mapRegistryServer({
      server: {
        name: "io.example/private-remote",
        remotes: [{ type: "streamable-http", url: "https://192.168.1.5/mcp" }],
      },
    }) === null;
  // A catalog body that names an internal endpoint is dropped, not installed.
  const body = validateMcpCatalogFile({
    schemaVersion: 1,
    servers: [
      { id: "body-private", name: "Body private", transport: "http", url: "https://10.0.0.8/mcp" },
      { id: "body-public", name: "Body public", transport: "http", url: "https://mcp.example/mcp" },
    ],
  });
  const bodyOk =
    body.catalog.servers.map((server) => server.id).join(",") === "body-public" &&
    body.warnings.some((warning) => warning.includes("public https address"));

  const ok = userGateOk && publicOk && thirdPartyOk && bodyOk;
  record(
    "E2E-MCP-MARKET-NET-BOUNDARY",
    ok,
    ok
      ? `${userAccepted.length} user forms accepted (${userRefused.length} refused, ${userPlaintext.length} plaintext gated), ${thirdPartyRefused.length} third-party forms rejected`
      : "guard misclassification"
  );
}

// ── E2E-MCP-MARKET-SEMANTICS ─────────────────────────────────────────────
{
  const semanticsRecord = {
    server: {
      name: "io.github.example/semantics",
      description: "semantics probe",
      packages: [
        {
          registryType: "npm",
          identifier: "semantics-mcp",
          version: "1.2.3",
          runtimeHint: "npx",
          runtimeArguments: [{ type: "positional", value: "-y" }],
          packageArguments: [
            { type: "named", name: "--port", value: "8080" },
            { type: "named", name: "--verbose" },
          ],
          environmentVariables: [
            { name: "REQUIRED_KEY", description: "needed", isRequired: true },
            { name: "OPT_KEY", isRequired: false, default: "off" },
          ],
        },
      ],
    },
  };
  const entry = mapRegistryServer(semanticsRecord);
  const argsOk =
    JSON.stringify(entry?.args) ===
    JSON.stringify(["-y", "[email protected]", "--port", "8080", "--verbose"]);
  const specs = entry?.requiredEnv ?? [];
  const envOk =
    specs.find((s) => s.name === "REQUIRED_KEY") &&
    !specs.find((s) => s.name === "REQUIRED_KEY")?.optional &&
    specs.find((s) => s.name === "OPT_KEY")?.optional === true &&
    specs.find((s) => s.name === "OPT_KEY")?.defaultValue === "off";
  record("E2E-MCP-MARKET-SEMANTICS", !!entry && argsOk && envOk, JSON.stringify({ args: entry?.args, env: specs }));
}

// ── E2E-MCP-MARKET-INSTALL ───────────────────────────────────────────────
const home = mkdtempSync(join(tmpdir(), "pi-desktop-mcp-e2e-"));
const dataDir = mkdtempSync(join(tmpdir(), "pi-desktop-mcp-e2e-data-"));
let host = null;
try {
  const { catalog, warnings } = validateMcpCatalogFile(BUILTIN_MCP_CATALOG);
  const catalogOk = warnings.length === 0 && catalog.servers.length >= 8;
  if (!catalogOk) {
    record("E2E-MCP-MARKET-INSTALL", false, `builtin catalog invalid: ${warnings.join("; ")}`);
  } else {
    host = new Host(hostBin, dataDir, home);
    await host.call("app.handshake", { protocolVersion: PROTOCOL_VERSION });

    const entry = catalog.servers.find((server) => server.id === "memory");
    const input = resolveCatalogEntry(entry);
    await host.call("mcp.upsert", { server: { ...input, level: "global", scope: GLOBAL_SCOPE } });
    const listed = await host.call("mcp.list", { level: "global" });
    const row = (listed.servers ?? []).find((server) => server.id === "memory");

    const recordPath = join(home, ".agents", "servers", "memory.json");
    const onDisk = existsSync(recordPath)
      ? JSON.parse(readFileSync(recordPath, "utf8"))
      : null;
    const diskOk =
      onDisk?.command === "npx" &&
      JSON.stringify(onDisk?.args) === JSON.stringify(["-y", "@modelcontextprotocol/server-memory"]);
    record(
      "E2E-MCP-MARKET-INSTALL",
      !!row && row.enabled === true && diskOk,
      row ? JSON.stringify({ command: row.command, args: row.args, enabled: row.enabled }) : "not listed",
    );

    const scopedUrl = "https://example.com/{token}?token={token}";
    const scopedEntry = mapRegistryServer({ server: {
      name: "io.example/header-scope",
      remotes: [{ type: "streamable-http", url: scopedUrl, headers: [{
        name: "Authorization", value: "Bearer {token}",
        variables: { token: { isRequired: true } },
      }] }],
    } });
    const scopedInput = resolveCatalogEntry(scopedEntry, { token: "synthetic-header-secret" });
    await host.call("mcp.upsert", { server: {
      ...scopedInput, enabled: false, level: "global", scope: GLOBAL_SCOPE,
    } });
    const scopedList = await host.call("mcp.list", { level: "global" });
    const scopedRow = scopedList.servers.find((server) => server.id === scopedEntry.id);
    const scopedDisk = JSON.parse(readFileSync(join(home, ".agents", "servers", `${scopedEntry.id}.json`), "utf8"));
    record(
      "E2E-MCP-MARKET-HEADER-SCOPE",
      scopedInput.url === scopedUrl && scopedRow?.url === scopedUrl && scopedDisk.url === scopedUrl
        && scopedDisk.headers?.Authorization === "Bearer synthetic-header-secret"
        && scopedRow.enabled === false,
      "header resolves while the same-named URL token remains literal in host configuration",
    );

    // A partial binding map must not authorize tokens in a different header.
    const partialEntry = {
      ...scopedEntry, id: "partial-header-scope", name: "Partial header scope",
      headers: {
        Authorization: "Bearer {token}",
        "X-Unbound": "{token}/${token}",
        "X-Undeclared": "${UNBOUND}",
      },
      headerBindings: { Authorization: { "{token}": { input: "token" } } },
    };
    const partialInput = resolveCatalogEntry(partialEntry, { token: "synthetic-header-secret" });
    await host.call("mcp.upsert", { server: {
      ...partialInput, enabled: false, level: "global", scope: GLOBAL_SCOPE,
    } });
    const partialList = await host.call("mcp.list", { level: "global" });
    const partialRow = partialList.servers.find((server) => server.id === partialEntry.id);
    const partialDisk = JSON.parse(readFileSync(join(home, ".agents", "servers", `${partialEntry.id}.json`), "utf8"));
    const partialScopeOk = [partialInput, partialRow, partialDisk].every((server) =>
      server?.url === scopedUrl
      && server.headers?.Authorization === "Bearer synthetic-header-secret"
      && server.headers?.["X-Unbound"] === "{token}/${token}"
      && server.headers?.["X-Undeclared"] === "${UNBOUND}");
    record(
      "E2E-MCP-MARKET-partial-header-bindings-stay-literal",
      partialScopeOk && partialRow?.enabled === false,
      "partial bindings leave other headers literal through resolution, host upsert/list and persistence",
    );
  }
} catch (error) {
  record("E2E-MCP-MARKET-INSTALL", false, error.message);
} finally {
  host?.dispose();
  setTimeout(() => rmSync(home, { recursive: true, force: true }), 200);
  rmSync(dataDir, { recursive: true, force: true });
}

const failed = results.filter((result) => !result.ok);
console.log(`\nSummary: ${results.length - failed.length}/${results.length} passed`);
process.exit(failed.length ? 1 : 0);