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.

4.6 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

const mocks = vi.hoisted(() => ({
  requireSession: vi.fn(),
  getClient: vi.fn(),
  ensureClient: vi.fn(),
  storeCode: vi.fn(),
  randomToken: vi.fn(),
}));

vi.mock("@/lib/auth/require-session", () => ({ requireSession: mocks.requireSession }));
vi.mock("@/lib/mcp/store", () => ({
  getClient: mocks.getClient,
  ensureClient: mocks.ensureClient,
  isAutoRegisterableClientId: (id: string) => /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(id) && !/^(manual:|pat:|mso_)/.test(id),
  storeCode: mocks.storeCode, CODE_TTL_MS: 60_000,
}));
vi.mock("@/lib/mcp/pkce", () => ({
  randomToken: mocks.randomToken,
  isAllowedRedirect: (raw: string) => {
    try {
      const url = new URL(raw);
      return url.protocol === "https:" || (url.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname));
    } catch { return false; }
  },
}));
vi.mock("@/lib/mcp/scope", () => ({
  mcpEnabled: () => true,
  parseScope: (value: string) => (["read", "write", "exec"].includes(value) ? value : "read"),
  clampScope: (value: string) => value,
}));

const { approve } = await import("./actions");

const form = (redirectUri = "https://chatgpt.com/connector/oauth/test") => {
  const data = new FormData();
  data.set("client_id", "chatgpt-client");
  data.set("redirect_uri", redirectUri);
  data.set("code_challenge", "challenge");
  data.set("code_challenge_method", "S256");
  data.set("state", "state-1");
  data.set("scope", "exec");
  data.set("resource", "https://mso.example.test/mcp");
  data.set("issuer", "https://mso.example.test");
  data.set("offline_access", "1");
  return data;
};

describe("OAuth consent approval", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.stubEnv("OS_PUBLIC_ORIGIN", "https://mso.example.test");
    mocks.requireSession.mockResolvedValue(true);
    mocks.randomToken.mockReturnValue("mso_code_test");
    mocks.getClient.mockResolvedValue({ name: "ChatGPT", profile: "chatgpt", redirectUris: ["https://chatgpt.com/connector/oauth/test"] });
    mocks.ensureClient.mockResolvedValue({ name: "ChatGPT", profile: "full", redirectUris: ["https://chatgpt.com/connector/oauth/test"] });
  });

  afterEach(() => vi.unstubAllEnvs());

  it("returns the validated callback for explicit top-level navigation", async () => {
    const before = Date.now();
    await expect(approve(form())).resolves.toEqual({
      ok: true,
      redirectTo: "https://chatgpt.com/connector/oauth/test?code=mso_code_test&state=state-1&iss=https%3A%2F%2Fmso.example.test",
    });
    expect(mocks.storeCode).toHaveBeenCalledWith("mso_code_test", expect.objectContaining({
      clientId: "chatgpt-client",
      redirectUri: "https://chatgpt.com/connector/oauth/test",
      codeChallenge: "challenge",
      scope: "exec",
      resource: "https://mso.example.test/mcp",
      profile: "chatgpt",
      offlineAccess: true,
      expiresAt: expect.any(Number),
    }));
    expect(mocks.storeCode.mock.calls[0][1].expiresAt).toBeGreaterThanOrEqual(before + 59_000);
  });

  it("does not mint a code after the owner session expires", async () => {
    mocks.requireSession.mockResolvedValue(false);
    await expect(approve(form())).resolves.toEqual({ ok: false, error: "Not signed in." });
    expect(mocks.storeCode).not.toHaveBeenCalled();
  });

  it.each(["manual:pat:another-owner", "pat:abc", "mso_code_x"])("rejects reserved MSO principal %s even with an HTTPS callback", async (id) => {
    mocks.getClient.mockResolvedValue(null);
    const data = form("https://attacker.example/callback"); data.set("client_id", id);
    expect(await approve(data)).toMatchObject({ ok: false, error: expect.stringContaining("register") });
    expect(mocks.storeCode).not.toHaveBeenCalled();
    expect(mocks.ensureClient).not.toHaveBeenCalled();
  });

  it("registers a self-declared public client id on demand and continues the flow", async () => {
    mocks.getClient.mockResolvedValue(null);
    const data = form();
    data.set("client_id", "chatgpt-mso");
    await expect(approve(data)).resolves.toMatchObject({ ok: true, redirectTo: expect.stringContaining("code=mso_code_test") });
    expect(mocks.ensureClient).toHaveBeenCalledWith("chatgpt-mso", ["https://chatgpt.com/connector/oauth/test"]);
    expect(mocks.storeCode).toHaveBeenCalledWith("mso_code_test", expect.objectContaining({ clientId: "chatgpt-mso", profile: "full" }));
  });

  it("does not authorize a registered client at another callback", async () => {
    expect(await approve(form("https://attacker.example/callback"))).toMatchObject({ ok: false });
    expect(mocks.storeCode).not.toHaveBeenCalled();
  });
});