Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

12.7 KB
Raw Blame
import { mcpSessionsJourney } from "./mcp-sessions.mjs";
import { mcpConnectionJourney } from "./mcp-connection.mjs";
// Real production Settings routes, with synthetic owner state and bounded fault injection.
import path from "node:path";
import { chmod } from "node:fs/promises";
import { expect } from "@playwright/test";
import AxeBuilder from "@axe-core/playwright";
import { createHash } from "node:crypto";

async function capture(page, name) {
  if (!process.env.MSO_SCREENSHOT_DIR) return;
  const file = path.join(process.env.MSO_SCREENSHOT_DIR, name + ".png");
  await page.screenshot({ path: file });
  await chmod(file, 0o600);
}
async function openMcp(page) {
  const sections = page.getByRole("button", { name: "Settings sections", exact: true });
  await expect.poll(async () => await sections.isVisible() || await page.getByRole("button", { name: "MCP", exact: true }).isVisible()).toBe(true);
  if (await sections.isVisible()) await sections.click();
  await page.getByRole("button", { name: "MCP", exact: true }).click();
}
export async function mcpPublicJourney(page) {
  let privateRequests = 0;
  const listener = request => { if (/\/api\/mcp\/tokens|\/api\/v1\/sys\/audit/.test(request.url())) privateRequests++; };
  page.on("request", listener);
  await openMcp(page);
  await expect(page.getByRole("link", { name: "Sign in to manage MCP" })).toBeVisible();
  expect(privateRequests).toBe(0);
  page.off("request", listener);
}
async function chooseProjectTarget(page, fixture) {
  let release = () => {};
  const held = new Promise(resolve => { release = resolve; });
  const initial = request => {
    if (!request.url().endsWith("/api/v1/project-mcp") || request.method() !== "POST") return false;
    const body = request.postDataJSON();
    return body?.action === "inspect" && body.project === "@host";
  };
  const hold = async route => { if (initial(route.request())) await held; await route.continue(); };
  await page.route("**/api/v1/project-mcp", hold);
  try {
    const request = page.waitForRequest(initial, { timeout: 10000 });
    await page.getByRole("tab", { name: /MSO to External/ }).click();
    await request;
    const input = page.getByLabel("Exact MCP target project");
    const useProject = page.getByRole("button", { name: "Use project", exact: true });
    await expect(input).toBeDisabled();
    await expect(useProject).toBeDisabled();
    release();
    await expect(input).toBeEnabled();
    await expect(page.getByText("Each project starts with no SI-Coder or Batonly binding.", { exact: false })).toBeVisible();
    await input.fill(fixture.dir);
    await expect(input).toHaveValue(fixture.dir);
    await expect(useProject).toBeEnabled();
    const inspected = page.waitForResponse(response => {
      if (!response.url().endsWith("/api/v1/project-mcp") || response.request().method() !== "POST") return false;
      const body = response.request().postDataJSON();
      return body?.action === "inspect" && body.project === fixture.dir;
    }, { timeout: 15000 });
    await useProject.click();
    expect((await inspected).status()).toBe(200);
    await expect(useProject).toBeEnabled();
  } finally { release(); await page.unroute("**/api/v1/project-mcp", hold); }
}
export async function mcpOwnerJourneys(page, fixture) {
  const errors = [];
  page.on("pageerror", error => errors.push(error.message));
  for (const viewport of [{ width: 1363, height: 936 }, { width: 390, height: 844 }, { width: 844, height: 390 }]) {
    await fixture.seedMcp();
    await page.setViewportSize(viewport);
    await page.goto(fixture.base + "/settings");
    await openMcp(page);
    const nav = page.getByRole("navigation", { name: "MCP navigation" });
    await expect(nav.getByRole("button", { name: /^Connected apps/ })).toBeVisible();
    await mcpConnectionJourney(page, fixture, capture);
    await capture(page, `mcp-overview-${viewport.width}`);
    await nav.getByRole("button", { name: /^Connect an app/ }).click();
    await expect(page.getByRole("heading", { name: "Which app do you want to connect?" })).toBeVisible();
    await capture(page, `mcp-client-picker-${viewport.width}`);
    await page.getByRole("button", { name: /^ChatGPT Apps/ }).click();
    await expect(page.getByText("Connect ChatGPT", { exact: true })).toBeVisible();
    await expect(page.getByText("Manual OAuth configuration", { exact: true })).toHaveCount(0);
    await capture(page, `mcp-guide-${viewport.width}`);
    await page.getByRole("button", { name: "Choose another app", exact: true }).click();
    await page.getByRole("button", { name: /^Cursor Remote/ }).click();
    await expect(page.getByText("Connect Cursor", { exact: true })).toBeVisible();
    await page.getByRole("navigation", { name: "Breadcrumb" }).getByRole("button", { name: "MCP", exact: true }).click();
    await nav.getByRole("button", { name: /^Connected apps/ }).click();
    await page.getByRole("button", { name: "Disconnect Fixture client", exact: true }).click();
    await page.getByRole("button", { name: "Cancel", exact: true }).click();
    await expect(page.getByRole("button", { name: "Disconnect Fixture client", exact: true })).toBeVisible();
    await page.getByRole("button", { name: "Disconnect Fixture client", exact: true }).click();
    await page.getByRole("button", { name: "Confirm disconnect", exact: true }).click();
    await expect(page.getByText("0 active connections", { exact: true })).toBeVisible();
    await page.getByRole("button", { name: "Show expired & disconnected", exact: true }).click();
    await expect(page.getByText("Read data · revoked", { exact: true })).toBeVisible();
    await page.getByRole("navigation", { name: "Breadcrumb" }).getByRole("button", { name: "MCP", exact: true }).click();
    await chooseProjectTarget(page, fixture);
    const siCoder = page.getByRole("article").filter({ has: page.getByRole("heading", { name: "SI-Coder", exact: true }) });
    await expect(siCoder.getByRole("button", { name: "Install", exact: true })).toBeEnabled();
    page.once("dialog", dialog => dialog.accept());
    await siCoder.getByRole("button", { name: "Install", exact: true }).click();
    const installed = page.getByRole("article").filter({ has: page.getByRole("heading", { name: "si-coder", exact: true }) });
    await expect(installed.getByRole("button", { name: "Uninstall", exact: true })).toBeVisible();
    await expect(installed.getByRole("button", { name: "Uninstall", exact: true })).toBeEnabled();
    page.once("dialog", dialog => dialog.accept());
    await installed.getByRole("button", { name: "Uninstall", exact: true }).click();
    await expect(page.getByText("No MCP installed on this target.", { exact: true })).toBeVisible();
    await page.getByRole("tab", { name: /Access MSO/ }).click();
    expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1)).toBe(true);
    const result = await new AxeBuilder({ page }).include('[data-slot="mcp-page"]').withTags(["wcag2a", "wcag2aa", "wcag21aa"]).analyze();
    expect(result.violations.map(v => ({ id: v.id, targets: v.nodes.map(n => n.target) }))).toEqual([]);
    await mcpSessionsJourney(page, fixture);
    console.log(`PASS MCP owner hierarchy, app guides, cancel/revoke and accessibility ${viewport.width}x${viewport.height}`);
  }
  // Server faults show a recoverable error, never an endless skeleton or false empty audit.
  await page.route("**/api/mcp/tokens", route => route.fulfill({ status: 503, body: '{}' }));
  await page.reload(); await openMcp(page);
  await expect(page.getByRole("alert").filter({ hasText: "MCP settings could not be loaded" })).toBeVisible();
  await page.unroute("**/api/mcp/tokens");
  await page.getByRole("button", { name: "Try again", exact: true }).click();
  const nav = page.getByRole("navigation", { name: "MCP navigation" });
  await expect(nav.getByRole("button", { name: /^Connect an app/ })).toBeVisible();
  await page.route("**/api/v1/sys/audit?*", route => route.fulfill({ status: 503, body: '{}' }));
  await nav.getByRole("button", { name: /^Recent activity/ }).click();
  await expect(page.getByRole("alert").filter({ hasText: "Activity could not be loaded" })).toBeVisible();
  await page.unroute("**/api/v1/sys/audit?*");
  await page.getByRole("button", { name: "Try again", exact: true }).click();
  await expect(page.getByText("Privileged MCP log", { exact: true })).toBeVisible();
  expect(errors).toEqual([]);
  console.log("PASS MCP failed-load recovery and activity error states");
}

export async function settingsAccessibilityJourney(page, base) {
  await page.goto(base + "/settings?section=appearance");
  await expect(page.getByRole("switch", { name: "Reduce transparency", exact: true })).toBeVisible();
  await expect(page.getByRole("group", { name: "Device preview", exact: true }).getByRole("button", { pressed: true })).toHaveCount(1);
  await page.goto(base + "/settings?section=theme");
  await expect(page.getByRole("switch", { name: "High contrast", exact: true })).toBeVisible();
  await expect(page.getByRole("group", { name: "Text size", exact: true }).getByRole("button", { pressed: true })).toHaveCount(1);
}

export async function oauthRegistrationJourney(page, fixture) {
  const callback = fixture.base + "/oauth-fixture-callback", verifier = "v".repeat(43);
  const challenge = createHash("sha256").update(verifier).digest("base64url");
  const consent = (clientId, redirectUri = callback) => fixture.base + "/oauth/authorize?" + new URLSearchParams({ client_id: clientId, redirect_uri: redirectUri, code_challenge: challenge, code_challenge_method: "S256", resource: fixture.base + "/mcp", state: "fixture-state", scope: "read" });
  // The synthetic OAuth client must receive the callback instead of mounting the MSO shell.
  const receiveCallback = route => route.fulfill({ status: 200, contentType: "text/html", body: "<!doctype html><title>OAuth fixture callback</title>" });
  await page.route(callback + "?*", receiveCallback);
  try {
  for (const viewport of [{ width: 1280, height: 900 }, { width: 390, height: 844 }]) {
    await page.setViewportSize(viewport);
    await page.goto(consent("manual:pat:another-owner", "https://attacker.invalid/callback"));
    await expect(page.getByRole("heading", { name: "Cannot connect this client" })).toBeVisible();
    await expect(page.getByText("This client must register before requesting authorization.")).toBeVisible();
    await expect(page.getByRole("button", { name: "Allow", exact: true })).toHaveCount(0);
    const registration = await page.request.post(fixture.base + "/oauth/register", { data: { client_name: "Security fixture", redirect_uris: [callback] } });
    expect(registration.status()).toBe(201);
    const { client_id: clientId } = await registration.json(); expect(clientId).toMatch(/^mcpc_/);
    await page.goto(consent(clientId, "https://attacker.invalid/callback"));
    await expect(page.getByText("That redirect target is not registered for this client.")).toBeVisible();
    await expect(page.getByRole("button", { name: "Allow", exact: true })).toHaveCount(0);
    await page.goto(consent(clientId));
    await expect(page.getByRole("heading", { name: "Connect Security fixture to your VPS" })).toBeVisible();
    await page.getByRole("radio", { name: /^Read only/ }).check();
    expect(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1)).toBe(true);
    await page.getByRole("button", { name: "Allow", exact: true }).click();
    await expect(page).toHaveURL(new RegExp("/oauth-fixture-callback\\?code="));
    const url = new URL(page.url()); expect(url.searchParams.get("state")).toBe("fixture-state");
    const exchanged = await page.request.post(fixture.base + "/oauth/token", { form: { grant_type: "authorization_code", code: url.searchParams.get("code"), code_verifier: verifier, client_id: clientId, redirect_uri: callback, resource: fixture.base + "/mcp" } });
    expect(exchanged.status()).toBe(200); expect((await exchanged.json()).token_type).toBe("Bearer");
    // Replacing the isolated test store simulates a swept registration, never owner
    // data. A self-declared public id may re-register on demand at its own callback
    // (consent renders, Allow is offered), but a reserved MSO principal fails closed.
    await fixture.seedMcp();
    await page.goto(consent(clientId));
    await expect(page.getByRole("heading", { name: /to your VPS$/ })).toBeVisible();
    await expect(page.getByRole("button", { name: "Allow", exact: true })).toHaveCount(1);
    await page.goto(consent("manual:pat:pruned"));
    await expect(page.getByText("This client must register before requesting authorization.")).toBeVisible();
    await expect(page.getByRole("button", { name: "Allow", exact: true })).toHaveCount(0);
  }
  console.log("PASS actual OAuth registered consent/exchange and manual/pruned callback denial on desktop/mobile");
  } finally { await page.unroute(callback + "?*", receiveCallback); }
}