Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

19.2 KB
Raw Blame
import { test } from "node:test";
import assert from "node:assert/strict";
import { brokerCredentialCall, type BrokerFetch } from "../src/api/credential-broker.ts";
import type { CapabilityClaims } from "../src/auth/capability-token.ts";
import type { DecryptedServiceCredential, ServiceCredentialReader } from "../src/credentials/keychain.ts";
import { createCredentialUsageSink } from "../src/admin/credential-usage-sink.ts";

const SECRET = "super-secret-bearer";

function reader(rec: Partial<DecryptedServiceCredential> & { slug: string }): ServiceCredentialReader {
  const full: DecryptedServiceCredential = {
    name: rec.slug,
    secret: SECRET,
    delivery: "broker",
    host: "api.x.com",
    deployments: true,
    enabled: true,
    ...rec,
  };
  return { getServiceCredentialSecret: async (_org, slug) => (slug === full.slug ? full : null) };
}

const claims = (credentials: string[]): CapabilityClaims => ({
  actorId: "U1",
  scopeId: "personal:U1",
  aud: "credential-broker",
  credentials,
  exp: Date.now() + 60_000,
});

function captureFetch(): {
  fetch: BrokerFetch;
  calls: Array<{ url: string; method: string; headers: Record<string, string>; body?: string }>;
} {
  const calls: Array<{ url: string; method: string; headers: Record<string, string>; body?: string }> = [];
  const fetch: BrokerFetch = async (url, init) => {
    calls.push({
      url,
      method: init.method,
      headers: init.headers,
      ...(init.body !== undefined ? { body: init.body } : {}),
    });
    return { status: 200, contentType: "application/json", text: async () => '{"ok":true}' };
  };
  return { fetch, calls };
}

const base = (over: Partial<Parameters<typeof brokerCredentialCall>[0]> = {}) => ({
  claims: claims(["x-firehose"]),
  body: { credential: "x-firehose", method: "GET", url: "https://api.x.com/2/tweets/search/recent?query=ai" },
  orgScopeId: "org:default-org",
  reader: reader({ slug: "x-firehose", allowedMethods: ["GET"], allowedPathPrefixes: ["/2/tweets/search/"] }),
  fetchImpl: captureFetch().fetch,
  ...over,
});

test("happy path: injects the secret as Bearer, never exposes it in the result", async () => {
  const cap = captureFetch();
  const usage = createCredentialUsageSink();
  const r = await brokerCredentialCall(base({ fetchImpl: cap.fetch, usage }));
  assert.equal(r.status, 200);
  assert.deepEqual(r.json, { status: 200, contentType: "application/json", body: '{"ok":true}' });
  assert.doesNotMatch(JSON.stringify(r.json), new RegExp(SECRET));
  assert.equal(cap.calls.length, 1);
  assert.equal(cap.calls[0]!.headers["Authorization"], `Bearer ${SECRET}`);
  const used = await usage.list({ slug: "x-firehose" });
  assert.equal(used.length, 1);
  assert.equal(used[0]!.host, "api.x.com");
  assert.equal(used[0]!.status, "ok");
  assert.equal(used[0]!.principalId, "U1");
  assert.doesNotMatch(JSON.stringify(used), /tweets\/search|ai|super-secret/);
});

test("an env-delivery record is refused exactly like a missing credential — the broker door does not serve sandbox-env material", async () => {
  const cap = captureFetch();
  const r = await brokerCredentialCall(
    base({
      reader: reader({
        slug: "x-firehose",
        delivery: "env",
        envKey: "X_FIREHOSE_KEY",
        allowedMethods: ["GET"],
        allowedPathPrefixes: ["/2/tweets/search/"],
      }),
      fetchImpl: cap.fetch,
    }),
  );
  assert.equal(r.status, 404);
  assert.equal(cap.calls.length, 0, "no upstream fetch happens");
  assert.doesNotMatch(JSON.stringify(r.json), new RegExp(SECRET));
});

test("WHAT scheme normalization: a scheme missing its trailing space still emits `<scheme> <secret>`", async () => {
  const cap = captureFetch();
  await brokerCredentialCall(
    base({
      reader: reader({
        slug: "x-firehose",
        injection: { scheme: "Bearer" },
        allowedMethods: ["GET"],
        allowedPathPrefixes: ["/2/tweets/search/"],
      }),
      fetchImpl: cap.fetch,
    }),
  );
  assert.equal(cap.calls[0]!.headers["Authorization"], `Bearer ${SECRET}`);

  const cap2 = captureFetch();
  await brokerCredentialCall(
    base({
      reader: reader({
        slug: "x-firehose",
        injection: { header: "X-Api-Key", scheme: "" },
        allowedMethods: ["GET"],
        allowedPathPrefixes: ["/2/tweets/search/"],
      }),
      fetchImpl: cap2.fetch,
    }),
  );
  assert.equal(cap2.calls[0]!.headers["X-Api-Key"], SECRET);
});

test("WHO: a slug NOT in the token's signed set is refused (no record read, no fetch)", async () => {
  const cap = captureFetch();
  let readCount = 0;
  const r = await brokerCredentialCall(
    base({
      claims: claims([]),
      fetchImpl: cap.fetch,
      reader: {
        getServiceCredentialSecret: async () => {
          readCount++;
          return null;
        },
      },
    }),
  );
  assert.equal(r.status, 403);
  assert.match(JSON.stringify(r.json), /not_entitled/);
  assert.equal(cap.calls.length, 0);
  assert.equal(readCount, 0, "must reject on the token alone, before touching the store");
});

test("WHAT host-pin: a different host is refused — the bearer can't be pointed at an attacker (exfil)", async () => {
  const cap = captureFetch();
  const r = await brokerCredentialCall(
    base({ body: { credential: "x-firehose", url: "https://evil.example/steal" }, fetchImpl: cap.fetch }),
  );
  assert.equal(r.status, 403);
  assert.match(JSON.stringify(r.json), /host_not_allowed/);
  assert.equal(cap.calls.length, 0, "the secret must NEVER be sent to a non-pinned host");
});

test("WHAT host-pin: a userinfo trick (https://[email protected]) targets evil.com → refused", async () => {
  const cap = captureFetch();
  const r = await brokerCredentialCall(
    base({
      body: { credential: "x-firehose", url: "https://[email protected]/2/tweets/search/x" },
      fetchImpl: cap.fetch,
    }),
  );
  assert.equal(r.status, 403);
  assert.match(JSON.stringify(r.json), /host_not_allowed/);
  assert.equal(cap.calls.length, 0);
});

test("WHAT path allowlist matches on a path BOUNDARY (no /a/b → /a/b-evil bypass)", async () => {
  const mk = () => reader({ slug: "x-firehose", allowedPathPrefixes: ["/2/tweets/search"] });
  const call = (url: string) =>
    brokerCredentialCall(
      base({ reader: mk(), body: { credential: "x-firehose", url }, fetchImpl: captureFetch().fetch }),
    );
  const evil = await call("https://api.x.com/2/tweets/search-evil/x");
  assert.equal(evil.status, 403);
  assert.match(JSON.stringify(evil.json), /path_not_allowed/);
  assert.equal((await call("https://api.x.com/2/tweets/search")).status, 200);
  assert.equal((await call("https://api.x.com/2/tweets/search/recent")).status, 200);
});

test("WHAT host-pin: a subdomain of the pinned host is allowed", async () => {
  const cap = captureFetch();
  const r = await brokerCredentialCall(
    base({ body: { credential: "x-firehose", url: "https://api.x.com/2/tweets/search/recent" }, fetchImpl: cap.fetch }),
  );
  assert.equal(r.status, 200);
  assert.equal(cap.calls.length, 1);
});

test("WHAT: https only — an http target is refused", async () => {
  const cap = captureFetch();
  const r = await brokerCredentialCall(
    base({ body: { credential: "x-firehose", url: "http://api.x.com/2/tweets/search/x" }, fetchImpl: cap.fetch }),
  );
  assert.equal(r.status, 403);
  assert.match(JSON.stringify(r.json), /scheme_not_allowed/);
  assert.equal(cap.calls.length, 0);
});

test("WHAT method allowlist: a read bearer can't mutate", async () => {
  const cap = captureFetch();
  const r = await brokerCredentialCall(
    base({
      body: { credential: "x-firehose", method: "POST", url: "https://api.x.com/2/tweets/search/recent" },
      fetchImpl: cap.fetch,
    }),
  );
  assert.equal(r.status, 403);
  assert.match(JSON.stringify(r.json), /method_not_allowed/);
  assert.equal(cap.calls.length, 0);
});

test("WHAT path allowlist: an out-of-allowlist path is refused", async () => {
  const cap = captureFetch();
  const r = await brokerCredentialCall(
    base({ body: { credential: "x-firehose", url: "https://api.x.com/2/dm/with/victim" }, fetchImpl: cap.fetch }),
  );
  assert.equal(r.status, 403);
  assert.match(JSON.stringify(r.json), /path_not_allowed/);
  assert.equal(cap.calls.length, 0);
});

test("WHAT path allowlist: percent-encoded parent traversal never escapes the prefix", async () => {
  const mk = () => reader({ slug: "x-firehose", allowedPathPrefixes: ["/2/tweets/search"] });
  const evil = [
    "https://api.x.com/2/tweets/search/../secrets", // literal
    "https://api.x.com/2/tweets/search/..%2fsecrets", // encoded slash
    "https://api.x.com/2/tweets/search/%2e%2e/secrets", // encoded dots (URL-normalized out of prefix)
    "https://api.x.com/2/tweets/search/%2e%2e%2fsecrets", // fully encoded
    "https://api.x.com/2/tweets/search/%252e%252e%252fsecrets", // double-encoded
    "https://api.x.com/2/tweets/search/..%5csecrets", // encoded backslash
    "https://api.x.com/2/tweets/search/%zz", // undecodable
  ];
  for (const url of evil) {
    const cap = captureFetch();
    const r = await brokerCredentialCall(
      base({ reader: mk(), body: { credential: "x-firehose", url }, fetchImpl: cap.fetch }),
    );
    assert.equal(r.status, 403, url);
    assert.match(JSON.stringify(r.json), /path_not_allowed/, url);
    assert.equal(cap.calls.length, 0, `no upstream fetch for ${url}`);
  }
  // benign lookalikes still pass: dots inside a segment are not dot segments
  const cap = captureFetch();
  const ok = await brokerCredentialCall(
    base({
      reader: mk(),
      body: { credential: "x-firehose", url: "https://api.x.com/2/tweets/search/v1..v2/some%20file" },
      fetchImpl: cap.fetch,
    }),
  );
  assert.equal(ok.status, 200);
  assert.equal(cap.calls.length, 1);
});

test("a disabled credential is refused even when the token still names it (live re-check)", async () => {
  const cap = captureFetch();
  const r = await brokerCredentialCall(
    base({ reader: reader({ slug: "x-firehose", enabled: false }), fetchImpl: cap.fetch }),
  );
  assert.equal(r.status, 404);
  assert.match(JSON.stringify(r.json), /credential_unavailable/);
  assert.equal(cap.calls.length, 0);
});

test("caller-supplied headers cannot override the injected auth header or smuggle others", async () => {
  const cap = captureFetch();
  await brokerCredentialCall(
    base({
      body: {
        credential: "x-firehose",
        url: "https://api.x.com/2/tweets/search/recent",
        headers: { Authorization: "Bearer ATTACKER", Host: "evil.example", Cookie: "x=y", Accept: "application/json" },
      },
      fetchImpl: cap.fetch,
    }),
  );
  const h = cap.calls[0]!.headers;
  assert.equal(h["Authorization"], `Bearer ${SECRET}`);
  assert.equal(h["Host"], undefined);
  assert.equal(h["Cookie"], undefined);
  assert.equal(h["Accept"], "application/json");
});

test("a custom injection header + scheme is honored", async () => {
  const cap = captureFetch();
  await brokerCredentialCall(
    base({
      reader: reader({
        slug: "x-firehose",
        injection: { header: "X-Api-Key", scheme: "" },
        allowedPathPrefixes: ["/"],
      }),
      fetchImpl: cap.fetch,
    }),
  );
  assert.equal(cap.calls[0]!.headers["X-Api-Key"], SECRET);
  assert.equal(cap.calls[0]!.headers["Authorization"], undefined);
});

test("an upstream fetch error is reported generically (never leaks the URL/headers) + recorded", async () => {
  const usage = createCredentialUsageSink();
  const r = await brokerCredentialCall(
    base({
      usage,
      fetchImpl: async () => {
        throw new Error("ECONNREFUSED https://api.x.com with Bearer super-secret-bearer");
      },
    }),
  );
  assert.equal(r.status, 502);
  assert.match(JSON.stringify(r.json), /upstream_unreachable/);
  assert.doesNotMatch(JSON.stringify(r.json), new RegExp(SECRET));
  assert.equal((await usage.list({ slug: "x-firehose" }))[0]!.status, "error");
});

test("a missing slug or url is a 400 bad_request", async () => {
  assert.equal(
    (await brokerCredentialCall(base({ body: { url: "https://api.x.com/2/tweets/search/x" } }))).status,
    400,
  );
  assert.equal((await brokerCredentialCall(base({ body: { credential: "x-firehose" } }))).status, 400);
});

test("actor attestation is opt-in and taken from capability claims", async () => {
  for (const actor of [undefined, false, true]) {
    const cap = captureFetch();
    const result = await brokerCredentialCall(
      base({
        claims: { ...claims(["x-firehose"]), actorId: "actor-42" },
        reader: reader({ slug: "x-firehose", injection: actor === undefined ? {} : { actor } }),
        fetchImpl: cap.fetch,
      }),
    );
    assert.equal(result.status, 200);
    assert.equal(cap.calls[0]!.headers["x-qm-actor"], actor ? "actor-42" : undefined);
    assert.equal(cap.calls[0]!.headers.Authorization, `Bearer ${SECRET}`);
  }
});

test("callers cannot supply an actor header even when attestation is disabled", async () => {
  for (const actor of [false, true]) {
    for (const key of ["x-qm-actor", "X-QM-Actor"]) {
      const cap = captureFetch();
      const result = await brokerCredentialCall(
        base({
          body: { credential: "x-firehose", url: "https://api.x.com/", headers: { [key]: "other-actor" } },
          reader: reader({ slug: "x-firehose", injection: { actor } }),
          fetchImpl: cap.fetch,
        }),
      );
      assert.equal(result.status, 400);
      assert.deepEqual(result.json, { error: "reserved_header", message: "x-qm-actor is set only by the broker" });
      assert.equal(cap.calls.length, 0);
    }
  }
});

test("actor attestation fails closed for malformed identities and injection configuration", async () => {
  for (const actorId of ["", "a\r\nb", "a b", "é", "a".repeat(257)]) {
    const cap = captureFetch();
    const result = await brokerCredentialCall(
      base({
        claims: { ...claims(["x-firehose"]), actorId },
        reader: reader({ slug: "x-firehose", injection: { actor: true } }),
        fetchImpl: cap.fetch,
      }),
    );
    assert.equal(result.status, 403);
    assert.equal(cap.calls.length, 0);
  }
  for (const injection of [{ actor: "yes" }, { header: "X-QM-Actor" }, { scheme: "Bearer\n" }]) {
    const cap = captureFetch();
    const result = await brokerCredentialCall(
      base({
        reader: reader({ slug: "x-firehose", injection: injection as never }),
        fetchImpl: cap.fetch,
      }),
    );
    assert.equal(result.status, 503);
    assert.equal(cap.calls.length, 0);
  }
});

test("actor-attested credentials require exact host while legacy subdomain matching remains", async () => {
  for (const actor of [false, true]) {
    const cap = captureFetch();
    const result = await brokerCredentialCall({
      claims: claims(["relay"]),
      body: { credential: "relay", url: "https://child.example.test/read" },
      orgScopeId: "org:default-org",
      reader: reader({ slug: "relay", host: "example.test", injection: { actor } }),
      fetchImpl: cap.fetch,
    });
    assert.equal(result.status, actor ? 403 : 200);
    assert.equal(cap.calls.length, actor ? 0 : 1);
  }
  const cap = captureFetch();
  const result = await brokerCredentialCall({
    claims: claims(["relay"]),
    body: { credential: "relay", url: "https://example.test/read" },
    orgScopeId: "org:default-org",
    reader: reader({ slug: "relay", host: "example.test", injection: { actor: true } }),
    fetchImpl: cap.fetch,
  });
  assert.equal(result.status, 200);
  assert.equal(cap.calls[0]!.headers["x-qm-actor"], "U1");
});

test("actor attestation rejects alternate ports before sending credentials", async () => {
  const cap = captureFetch();
  const result = await brokerCredentialCall({
    claims: claims(["relay"]),
    body: { credential: "relay", url: "https://example.test:8443/read" },
    orgScopeId: "org:default-org",
    reader: reader({ slug: "relay", host: "example.test", injection: { actor: true } }),
    fetchImpl: cap.fetch,
  });
  assert.equal(result.status, 403);
  assert.equal(cap.calls.length, 0);
});

test("a deployment token is refused when the credential is switched off for published apps, and audited with the app id when it is on", async () => {
  const cap = captureFetch();
  const audits: Array<{ action: string; detail?: string }> = [];
  const audit = (e: { action: string; detail?: string }) => audits.push(e);
  const asApp = { ...claims(["x-firehose"]), actorId: "publisher", deployment: "dpl-1" };
  const off = await brokerCredentialCall(
    base({
      claims: asApp,
      reader: reader({ slug: "x-firehose", deployments: false, allowedPathPrefixes: ["/2/tweets/search/"] }),
      fetchImpl: cap.fetch,
      audit,
    }),
  );
  assert.equal(off.status, 403);
  assert.equal((off.json as { error: string }).error, "not_available_to_deployments");
  assert.equal(cap.calls.length, 0, "nothing is fetched upstream");
  const on = await brokerCredentialCall(
    base({
      claims: asApp,
      reader: reader({ slug: "x-firehose", deployments: true, allowedPathPrefixes: ["/2/tweets/search/"] }),
      fetchImpl: cap.fetch,
      audit,
    }),
  );
  assert.equal(on.status, 200);
  assert.equal(cap.calls.length, 1);
  assert.deepEqual(
    audits.map((e) => [e.action, e.detail]),
    [
      ["credential.broker.denied", "not_available_to_deployments"],
      ["credential.broker.use", "deployment:dpl-1"],
    ],
  );
  const agent = await brokerCredentialCall(
    base({ reader: reader({ slug: "x-firehose", deployments: false, allowedPathPrefixes: ["/2/tweets/search/"] }) }),
  );
  assert.equal(agent.status, 200, "an agent turn's token is not affected by the published-apps switch");
});

test("generic credential broker cannot bypass Composio identity binding", async () => {
  const cap = captureFetch();
  const result = await brokerCredentialCall(
    base({
      reader: reader({ slug: "x-firehose", host: "backend.composio.dev", allowedMethods: ["POST"] }),
      body: {
        credential: "x-firehose",
        method: "POST",
        url: "https://backend.composio.dev/api/v3.1/tools/execute/GMAIL_FETCH_EMAILS",
      },
      fetchImpl: cap.fetch,
    }),
  );
  assert.equal(result.status, 403);
  assert.equal(cap.calls.length, 0);
});

test("a daily cap admits that many outbound calls per UTC day, then refuses with 429 without calling out", async () => {
  const cap = captureFetch();
  let clock = Date.UTC(2026, 9, 16, 23, 0);
  const usage = createCredentialUsageSink(() => clock);
  const capped = base({
    fetchImpl: cap.fetch,
    usage,
    reader: reader({ slug: "x-firehose", allowedPathPrefixes: ["/2/"], maxRequestsPerDay: 2 }),
  });
  assert.equal((await brokerCredentialCall(capped)).status, 200);
  assert.equal(
    (await brokerCredentialCall({ ...capped, body: { ...capped.body, url: "https://evil.example/" } })).status,
    403,
  );
  assert.equal((await brokerCredentialCall(capped)).status, 200);
  const refused = await brokerCredentialCall(capped);
  assert.equal(refused.status, 429);
  assert.equal((refused.json as { error: string }).error, "daily_cap_reached");
  assert.equal(cap.calls.length, 2);

  clock = Date.UTC(2026, 9, 17, 0, 1);
  assert.equal((await brokerCredentialCall(capped)).status, 200);
  assert.equal(cap.calls.length, 3);
});

test("a capped credential refuses closed when no usage ledger can count its calls", async () => {
  const capped = { reader: reader({ slug: "x-firehose", allowedPathPrefixes: ["/2/"], maxRequestsPerDay: 3 }) };
  assert.equal((await brokerCredentialCall(base(capped))).status, 503);
});