Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

20.4 KB
Raw Blame
import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createHmac } from "node:crypto";
import { request as httpRequest, createServer as createHttpServer } from "node:http";
import type { AddressInfo } from "node:net";
import { createApp } from "../src/api/app.ts";
import { createInsecureTestServer } from "../src/api/server.ts";
import { createDeployStore } from "../src/deploy/deploy-store.ts";
import { createDeployService } from "../src/deploy/deploy-service.ts";
import { createAclStore } from "../src/acl/acl-store.ts";
import { createDirectoryStore } from "../src/directory/directory-store.ts";
import { createIdentityService } from "../src/identity/identity-service.ts";
import { createMemorySessionStore } from "../src/sessions/memory-session-store.ts";
import { PORTAL_IDENTITY_HEADER } from "../src/auth/portal-identity.ts";
import { portalSession } from "../src/deploy/viewer-session.ts";
import { scopeId } from "../src/types.ts";
import type { FeatureFlagStore } from "../src/feature-flags.ts";
import { singleProcessDeps } from "./support/single-process.ts";

let externalSharing = true;
const externalSharingOn = { enabled: async () => externalSharing } as unknown as FeatureFlagStore;

const auditLog = { record() {}, events: async () => [], tail: async () => [] };
const SESSION_SECRET = "portal-session-secret";
const LOGIN_URL = "https://portal.example.com";

function mintPortalSession(sub: string, expInSeconds = 3600, secret = SESSION_SECRET, appOnly?: true): string {
  const key = createHmac("sha256", secret).update("portal.session.v1").digest();
  const now = Math.floor(Date.now() / 1000);
  const body = Buffer.from(
    JSON.stringify({
      k: "session",
      sub,
      org: "acme",
      iat: now,
      exp: now + expInSeconds,
      ...(appOnly ? { appOnly } : {}),
    }),
  ).toString("base64url");
  const sig = createHmac("sha256", key).update(body).digest("base64url");
  return `${body}.${sig}`;
}

test("portalSession: verifies, and rejects tampering, expiry, wrong kind, wrong secret", () => {
  const good = mintPortalSession("[email protected]");
  assert.deepEqual(portalSession(`portal_session=${good}`, SESSION_SECRET), {
    sub: "[email protected]",
    appOnly: false,
  });
  assert.deepEqual(portalSession(`other=1; portal_session=${good}; x=2`, SESSION_SECRET), {
    sub: "[email protected]",
    appOnly: false,
  });
  assert.deepEqual(portalSession(`portal_session=${good}x`, SESSION_SECRET), null, "tampered signature");
  assert.deepEqual(
    portalSession(`portal_session=junk; portal_session=${good}`, SESSION_SECRET),
    { sub: "[email protected]", appOnly: false },
    "an app's junk same-named cookie cannot shadow the real session",
  );
  assert.deepEqual(portalSession(`portal_session=${good}`, "other-secret"), null, "wrong secret");
  assert.deepEqual(
    portalSession(`portal_session=${mintPortalSession("[email protected]", -10)}`, SESSION_SECRET),
    null,
    "expired",
  );
  assert.deepEqual(portalSession(undefined, SESSION_SECRET), null);
  assert.deepEqual(portalSession("portal_session=", SESSION_SECRET), null);
  const key = createHmac("sha256", SESSION_SECRET).update("portal.session.v1").digest();
  const body = Buffer.from(
    JSON.stringify({ k: "impersonate", sub: "eve", exp: Math.floor(Date.now() / 1000) + 60 }),
  ).toString("base64url");
  const sig = createHmac("sha256", key).update(body).digest("base64url");
  assert.deepEqual(portalSession(`portal_session=${body}.${sig}`, SESSION_SECRET), null, "non-session claims");
});

test("forwarded app hosts fail closed before core routes when gateway configuration is absent", async () => {
  for (const config of [
    {},
    { deployAppsDomain: "apps.example.com" },
    { deployAppsDomain: "apps.example.com", deployGateSecret: "gate" },
  ]) {
    const server = createInsecureTestServer({} as Parameters<typeof createInsecureTestServer>[0], config);
    server.listen(0);
    try {
      const result = await httpGet((server.address() as AddressInfo).port, "/healthz", {
        Host: "app.other.example.com",
        "x-qm-app-host": "1",
      });
      assert.equal(result.status, "deployGateSecret" in config ? 404 : 503);
    } finally {
      await new Promise<void>((resolve) => server.close(() => resolve()));
    }
  }
});

function httpGet(
  port: number,
  path: string,
  headers: Record<string, string>,
): Promise<{ status: number; headers: Record<string, string | string[] | undefined>; body: string }> {
  return new Promise((resolve, reject) => {
    const req = httpRequest({ host: "localhost", port, path, method: "GET", headers }, (res) => {
      let body = "";
      res.on("data", (c) => (body += c));
      res.on("end", () => resolve({ status: res.statusCode ?? 0, headers: res.headers, body }));
    });
    req.on("error", reject);
    req.end();
  });
}

test("app sign-in uses the configured trusted entry and preserves the app return address", async () => {
  const server = createInsecureTestServer(
    { getDeployment: async () => null } as unknown as Parameters<typeof createInsecureTestServer>[0],
    {
      deployAppsDomain: "apps.example.com",
      deployGateSecret: "gate",
      deployAppsSessionSecret: SESSION_SECRET,
      deployAppsLoginUrl: LOGIN_URL,
      deployAppsLoginPath: "/auth/trusted/login",
    },
  );
  server.listen(0);
  try {
    const result = await httpGet((server.address() as AddressInfo).port, "/counter?x=1", {
      Host: "counter.apps.example.com",
      Accept: "text/html",
    });
    assert.equal(result.status, 302);
    const redirect = new URL(result.headers.location as string);
    assert.equal(redirect.origin, LOGIN_URL);
    assert.equal(redirect.pathname, "/auth/trusted/login");
    assert.equal(redirect.searchParams.get("returnTo"), "https://counter.apps.example.com/counter?x=1&dpl_signin=1");
  } finally {
    await new Promise<void>((resolve) => server.close(() => resolve()));
  }
});

function httpPost(
  port: number,
  path: string,
  headers: Record<string, string>,
): Promise<{ status: number; body: string }> {
  return new Promise((resolve, reject) => {
    const req = httpRequest({ host: "localhost", port, path, method: "POST", headers }, (res) => {
      let body = "";
      res.on("data", (c) => (body += c));
      res.on("end", () => resolve({ status: res.statusCode ?? 0, body }));
    });
    req.on("error", reject);
    req.end();
  });
}

test("subdomain ingress: portal sign-in admits the owner, denies strangers, bounces the signed-out", async () => {
  let upstreamCookie: string | undefined = "unset";
  let upstreamIdentity: string | undefined = "unset";
  let upstreamUrl = "";
  const upstream = createHttpServer((req, res) => {
    upstreamCookie = req.headers.cookie as string | undefined;
    upstreamIdentity = req.headers[PORTAL_IDENTITY_HEADER] as string | undefined;
    upstreamUrl = req.url ?? "";
    res.writeHead(200, {
      "content-type": "text/plain",
      "set-cookie": ["portal_session=evil; Domain=example.com; Path=/", "dpl_access=evil", "app_pref=ok"],
    });
    res.end("UPSTREAM OK");
  });
  upstream.listen(0);
  const upstreamPort = (upstream.address() as AddressInfo).port;

  const deployStore = createDeployStore();
  const deploy = createDeployService({
    ...singleProcessDeps(),
    externalSharingAllowed: async () => true,
    deployStore,
    provider: {
      profile: { managedScaleToZero: false },
      apply: async () => ({ host: "127.0.0.1", port: upstreamPort }),
      destroy: async () => {},
    },
    auditLog,
    acl: createAclStore(),
    deployDir: mkdtempSync(join(tmpdir(), "signin-")),
  });
  const app = createApp({
    deploy,
    acl: createAclStore(),
    directory: createDirectoryStore(),
    sessions: createMemorySessionStore(),
    identity: createIdentityService(),
  } as unknown as Parameters<typeof createApp>[0]);
  await app.deploy({
    ownerScopeId: scopeId("personal", "[email protected]"),
    createdBy: "[email protected]",
    entrypoint: "x",
    files: [],
    name: "mysite",
  });
  const deliveries: { destination: unknown; text: string; idempotencyKey: string }[] = [];
  (app as unknown as Record<string, unknown>).enqueueDelivery = async (input: (typeof deliveries)[number]) => {
    deliveries.push(input);
  };
  const server = createInsecureTestServer(app, {
    featureFlags: externalSharingOn,
    deployAppsDomain: "apps.example.com",
    deployGateSecret: "gate-secret",
    deployAppsSessionSecret: SESSION_SECRET,
    deployAppsLoginUrl: LOGIN_URL,
  });
  server.listen(0);
  const port = (server.address() as AddressInfo).port;
  const host = "mysite.apps.example.com";

  try {
    const browser = await httpGet(port, "/consultants?x=1", { Host: host, Accept: "text/html,*/*" });
    assert.equal(browser.status, 302, "signed-out browser is bounced to sign-in");
    assert.equal(
      browser.headers.location,
      `${LOGIN_URL}/auth/login?returnTo=${encodeURIComponent("https://mysite.apps.example.com/consultants?x=1&dpl_signin=1")}`,
    );

    const stillSignedOut = await httpGet(port, "/consultants?x=1&dpl_signin=1", { Host: host, Accept: "text/html" });
    assert.equal(
      stillSignedOut.status,
      401,
      "coming back from sign-in still signed-out is an error page, not another bounce",
    );
    assert.match(stillSignedOut.body, /Sign-in didn&#39;t reach this app|Sign-in didn't reach this app/);

    const xhr = await httpGet(port, "/api/data", { Host: host, Accept: "application/json" });
    assert.equal(xhr.status, 401, "a non-HTML request never gets a login redirect");
    assert.match(xhr.body, /loginUrl/, "…but is told where sign-in lives");

    await app.setDeploymentPublic("mysite", true, { createdBy: "[email protected]" });
    const publicVisitor = await httpGet(port, "/consultants?x=1", { Host: host, Accept: "text/html,*/*" });
    assert.equal(publicVisitor.status, 200, "a public app is reachable without a portal session");
    assert.equal(publicVisitor.body, "UPSTREAM OK");
    assert.equal(upstreamCookie, undefined, "an anonymous request forwards no gateway cookies");
    assert.equal(upstreamIdentity, undefined, "an anonymous request forwards no viewer identity");

    const signedInPublicVisitor = await httpGet(port, "/consultants", {
      Host: host,
      Accept: "text/html",
      Cookie: `portal_session=${mintPortalSession("[email protected]")}`,
    });
    assert.equal(signedInPublicVisitor.status, 200);
    assert.equal(
      upstreamIdentity,
      undefined,
      "a link-only public visitor stays anonymous even when a portal session cookie is present",
    );
    externalSharing = false;
    const lockedDown = await httpGet(port, "/consultants?x=1", { Host: host, Accept: "text/html,*/*" });
    assert.equal(lockedDown.status, 302, "with external sharing off, a stored public link requires sign-in");
    const lockedXhr = await httpGet(port, "/api/data", { Host: host, Accept: "application/json" });
    assert.equal(lockedXhr.status, 401);
    externalSharing = true;
    await app.setDeploymentPublic("mysite", false, { createdBy: "[email protected]" });

    const backFromLogin = await httpGet(port, "/consultants?x=1&dpl_signin=1", {
      Host: host,
      Accept: "text/html",
      Cookie: `portal_session=${mintPortalSession("[email protected]")}`,
    });
    assert.equal(backFromLogin.status, 302, "landing back from sign-in redirects to the clean URL");
    assert.equal(backFromLogin.headers.location, "/consultants?x=1", "the dpl_signin marker leaves the address bar");

    const owner = await httpGet(port, "/consultants?x=1", {
      Host: host,
      Accept: "text/html",
      Cookie: `portal_session=${mintPortalSession("[email protected]")}`,
    });
    assert.equal(owner.status, 200, "the signed-in owner reaches the app with no capability link");
    assert.equal(owner.body, "UPSTREAM OK");
    assert.equal(upstreamCookie, undefined, "the portal session cookie never reaches the app");
    assert.equal(upstreamUrl, "/consultants?x=1", "the app sees the clean URL");
    for (const origin of ["https://sibling.apps.example.com", "https://portal.example.com", "null"]) {
      const denied = await httpPost(port, "/api/update", {
        Host: host,
        Cookie: `portal_session=${mintPortalSession("[email protected]")}`,
        Origin: origin,
      });
      assert.equal(denied.status, 403);
    }
    const sameApp = await httpPost(port, "/api/update", {
      Host: host,
      Cookie: `portal_session=${mintPortalSession("[email protected]")}`,
      Origin: `https://${host}`,
      "Sec-Fetch-Site": "same-origin",
    });
    assert.equal(sameApp.status, 200);
    for (const site of ["same-site", "cross-site"]) {
      const denied = await httpPost(port, "/api/update", {
        Host: host,
        Cookie: `portal_session=${mintPortalSession("[email protected]")}`,
        "Sec-Fetch-Site": site,
      });
      assert.equal(denied.status, 403);
    }
    const ownerSetCookies = ([] as string[]).concat(owner.headers["set-cookie"] ?? []);
    assert.deepEqual(
      ownerSetCookies,
      ["app_pref=ok"],
      "an app cannot set or clear the gateway's cookies on the visitor",
    );

    const stranger = await httpGet(port, "/consultants", {
      Host: host,
      Accept: "text/html",
      Cookie: `portal_session=${mintPortalSession("[email protected]")}`,
    });
    assert.equal(stranger.status, 403, "a signed-in stranger is denied");
    assert.match(stranger.body, /hasn&#39;t been shared with you|hasn't been shared with you/);
    assert.match(String(stranger.headers["content-type"]), /text\/html/, "denial is a friendly page, not bare JSON");

    const strangerXhr = await httpGet(port, "/consultants", {
      Host: host,
      Accept: "application/json",
      Cookie: `portal_session=${mintPortalSession("[email protected]")}`,
    });
    assert.equal(strangerXhr.status, 403, "non-HTML denial stays JSON");

    assert.match(stranger.body, /Request access/, "the denial page offers a request-access button");
    const asked = await httpPost(port, "/__claw__/request-access", {
      Host: host,
      Cookie: `portal_session=${mintPortalSession("[email protected]")}`,
    });
    assert.equal(asked.status, 200, "a denied visitor can ask the owner for access");
    const again = await httpPost(port, "/__claw__/request-access", {
      Host: host,
      Cookie: `portal_session=${mintPortalSession("[email protected]")}`,
    });
    assert.equal(again.status, 200, "asking twice is idempotent, not an error");
    assert.equal(deliveries.length, 2, "both posts enqueue (the outbox dedupes by idempotency key)");
    assert.equal(deliveries[0]!.idempotencyKey, deliveries[1]!.idempotencyKey, "same visitor+app+day dedupes");
    assert.match(deliveries[0]!.text, /mallory@example\.com is asking for access/);
    assert.match(deliveries[0]!.text, /mysite/);
    assert.deepEqual(deliveries[0]!.destination, {
      type: "principal",
      target: "[email protected]",
      audienceScopeId: "personal:[email protected]",
      onBehalfOf: "[email protected]",
      deploymentAccess: { deploymentId: (await app.getDeployment("mysite"))!.id, requesterId: "[email protected]" },
    });

    const signedOutAsk = await httpPost(port, "/__claw__/request-access", { Host: host });
    assert.equal(signedOutAsk.status, 401, "a signed-out visitor cannot send access requests");

    const forged = await httpGet(port, "/consultants", {
      Host: host,
      Accept: "text/html",
      Cookie: `portal_session=${mintPortalSession("[email protected]", 3600, "wrong-secret")}`,
    });
    assert.equal(forged.status, 302, "a forged session is just a signed-out visitor");

    const crafted = await httpGet(port, "//evil.com?dpl_signin=1", {
      Host: host,
      Accept: "text/html",
      Cookie: `portal_session=${mintPortalSession("[email protected]")}`,
    });
    assert.equal(crafted.status, 302);
    assert.equal(crafted.headers.location, "/", "a scheme-relative pathname never enters a gateway redirect");
  } finally {
    await new Promise<void>((resolve) => server.close(() => resolve()));
    await new Promise<void>((resolve) => upstream.close(() => resolve()));
  }
});

test("org-wide app access admits only signed-in org members", async () => {
  const upstream = createHttpServer((_req, res) => res.end("UPSTREAM OK"));
  upstream.listen(0);
  const acl = createAclStore();
  const identity = createIdentityService();
  const deploy = createDeployService({
    ...singleProcessDeps(),
    externalSharingAllowed: async () => false,
    deployStore: createDeployStore(),
    provider: {
      profile: { managedScaleToZero: false },
      apply: async () => ({ host: "127.0.0.1", port: (upstream.address() as AddressInfo).port }),
      destroy: async () => {},
    },
    auditLog,
    acl,
    deployDir: mkdtempSync(join(tmpdir(), "org-access-")),
  });
  const app = createApp({
    deploy,
    acl,
    directory: createDirectoryStore(),
    sessions: createMemorySessionStore(),
    identity,
  } as unknown as Parameters<typeof createApp>[0]);
  const d = await app.deploy({
    ownerScopeId: scopeId("personal", "[email protected]"),
    createdBy: "[email protected]",
    entrypoint: "x",
    files: [],
    name: "teamsite",
  });
  const server = createInsecureTestServer(app, {
    featureFlags: { enabled: async () => false } as unknown as FeatureFlagStore,
    identity,
    deployAppsDomain: "apps.example.com",
    deployGateSecret: "gate-secret",
    deployAppsSessionSecret: SESSION_SECRET,
    deployAppsLoginUrl: LOGIN_URL,
  });
  server.listen(0);
  const port = (server.address() as AddressInfo).port;
  const visit = (cookieFor?: string, appOnly?: true) =>
    httpGet(port, "/", {
      Host: "teamsite.apps.example.com",
      Accept: "application/json",
      ...(cookieFor ? { Cookie: `portal_session=${mintPortalSession(cookieFor, 3600, SESSION_SECRET, appOnly)}` } : {}),
    });
  try {
    assert.equal((await visit("[email protected]")).status, 403, "restricted: a signed-in member without a grant");
    await app.shareDeployment(d.id, scopeId("org", "acme"), "read", { createdBy: "[email protected]" });
    assert.equal((await visit()).status, 401, "org access still requires sign-in");
    assert.equal((await visit("[email protected]")).status, 200, "any signed-in org member can open it");
    assert.equal((await visit("[email protected]", true)).status, 403, "an app-only outside session is not the org");
    await identity.deactivate("[email protected]", "directory-sync");
    assert.equal((await visit("[email protected]")).status, 403, "someone who left the org is not the org");
    await app.shareDeployment(d.id, scopeId("org", "acme"), null, { createdBy: "[email protected]" });
    assert.equal((await visit("[email protected]")).status, 403, "back to restricted closes it");
  } finally {
    await new Promise<void>((resolve) => server.close(() => resolve()));
    await new Promise<void>((resolve) => upstream.close(() => resolve()));
  }
});

test("subdomain ingress: without the session config the domain has no door — a 503, not a bounce", async () => {
  const deployStore = createDeployStore();
  const deploy = createDeployService({
    ...singleProcessDeps(),
    externalSharingAllowed: async () => true,
    deployStore,
    provider: {
      profile: { managedScaleToZero: false },
      apply: async () => ({ host: "127.0.0.1", port: 1 }),
      destroy: async () => {},
    },
    auditLog,
    acl: createAclStore(),
    deployDir: mkdtempSync(join(tmpdir(), "signin-off-")),
  });
  const app = createApp({
    deploy,
    acl: createAclStore(),
    directory: createDirectoryStore(),
    sessions: createMemorySessionStore(),
    identity: createIdentityService(),
  } as unknown as Parameters<typeof createApp>[0]);
  await app.deploy({
    ownerScopeId: scopeId("personal", "[email protected]"),
    createdBy: "[email protected]",
    entrypoint: "x",
    files: [],
    name: "mysite",
  });
  const server = createInsecureTestServer(app, {
    featureFlags: externalSharingOn,
    deployAppsDomain: "apps.example.com",
    deployGateSecret: "gate-secret",
  });
  server.listen(0);
  const port = (server.address() as AddressInfo).port;
  try {
    const r = await httpGet(port, "/", {
      Host: "mysite.apps.example.com",
      Accept: "text/html",
      Cookie: `portal_session=${mintPortalSession("[email protected]")}`,
    });
    assert.equal(
      r.status,
      503,
      "no session secret ⇒ sign-in cannot work; surface the misconfiguration, not a dead-end bounce",
    );
  } finally {
    await new Promise<void>((resolve) => server.close(() => resolve()));
  }
});