import assert from "node:assert/strict" import { randomUUID } from "node:crypto" import { pool } from "../db/index.js" import type { AuthUser } from "../lib/permissions.js" import type { ConnectionRow } from "../modules/mcp/store.js" import { McpError } from "../modules/mcp/security.js" import { createMcpServerProbeAuthority, createMcpServerProbeReadAuthority, createRestServerProbeAuthority, SERVER_PROBE_OPERATION, SERVER_PROBE_RECEIPT_READ_OPERATION, type ServerProbeAuthorityDependencies } from "./server-probe-authority.js" const originalQuery = pool.query, originalConnect = pool.connect pool.query = (() => { throw Error("Actual SQL forbidden") }) as never pool.connect = (() => { throw Error("Actual SQL forbidden") }) as never let cases = 0 function fixture() { let time = 1000000, revoked = false, reads = 0, portalReads = 0, executionChecks = 0 let protections: Record[] = [], afterRead: (() => void) | undefined const principal = { connectionId: randomUUID(), credentialId: randomUUID(), credentialExpiresAt: new Date(2000000).toISOString() } const connection = { id: principal.connectionId, owner_id: "owner", owner_permissions: ["mm:servers:write"], generation: "generation", owner_valid_until: new Date(2000000), policy: { status: "active", mode: "approval", expiresAt: principal.credentialExpiresAt, permissions: ["mm:servers:write"], scope: { serverIds: [2, 1], operationIds: [SERVER_PROBE_OPERATION], allowGlobal: false } } } as ConnectionRow const actor: AuthUser = { id: "portal-owner", email: "", name: "", apps: ["mm"], permissions: ["mm:servers:write"], issuedAt: 900000, expiresAt: 1900000 } const deps: ServerProbeAuthorityDependencies = { now: () => time, connection: async () => { if (revoked) throw new McpError("PERMISSION_DENIED", "revoked", 403); return structuredClone(connection) }, portal: async () => { portalReads++; if (revoked) throw new McpError("OWNER_CLAIMS_REVOKED", "revoked", 401) }, verified: async () => { executionChecks++; if (revoked) throw new McpError("PERMISSION_DENIED", "no execution", 403) }, query: async sql => { reads++; const rows = sql.includes("FROM mcp_protections") ? structuredClone(protections) : [{ id: 1 }, { id: 2 }]; afterRead?.(); return rows }, } return { deps, principal, connection, actor, counts: () => ({ reads, portalReads, executionChecks }), revoke: () => { revoked = true }, advance: (value: number) => { time += value }, protections: (value: Record[]) => { protections = value }, afterRead: (hook: () => void) => { afterRead = hook } } } async function denied(work: Promise) { await assert.rejects(work, error => error instanceof McpError); cases++ } try { { const f = fixture(), auth = createMcpServerProbeAuthority(f.principal, { executing: true }, f.deps) const value = await auth.readAuthority() assert.equal(value.transport, "mcp"); assert.deepEqual(value.scopeServerIds, [1, 2]); assert.equal(f.counts().executionChecks, 1) await auth.assertProtections([1, 2], value); cases++ f.connection.policy.scope.serverIds = [1] await denied(auth.assertProtections([1, 2], value)) } for (const mutate of [(f: ReturnType) => { f.connection.policy.mode = "observer" }, (f: ReturnType) => { f.connection.policy.scope.operationIds = [] }, (f: ReturnType) => { f.connection.owner_permissions = [] }, (f: ReturnType) => { f.connection.owner_valid_until = new Date(999999) }, (f: ReturnType) => f.revoke()]) { const f = fixture(); mutate(f) await denied(createMcpServerProbeAuthority(f.principal, {}, f.deps).readAuthority()) assert.equal(f.counts().reads, 0) } { const f = fixture() assert.throws(() => createRestServerProbeAuthority(undefined, { requestId: "http1", acceptedAt: 1000000, authRequired: true }, f.deps), McpError); cases++ const auth = createRestServerProbeAuthority(undefined, { requestId: "http1", acceptedAt: 1000000, authRequired: false }, f.deps), value = await auth.readAuthority() assert.equal(value.transport, "rest"); assert.equal(value.ownerId, "local-operator"); assert.equal(value.connectionId, undefined); assert.equal(value.credentialId, undefined) assert.equal(f.counts().portalReads, 0); assert.equal(value.requestId, "http1"); cases++ f.advance(300001); await denied(auth.readAuthority()) } { const f = fixture(), auth = createRestServerProbeAuthority(f.actor, { requestId: "http2", acceptedAt: 1000000, authRequired: true }, f.deps) const value = await auth.readAuthority() assert.equal(value.ownerId, f.actor.id); assert.equal(value.issuedAt, f.actor.issuedAt); assert.equal(value.expiresAt, 1300000); assert.equal(f.counts().portalReads, 2); cases++ f.afterRead(f.revoke) await denied(auth.readAuthority()) } for (const protection of ["deny", "human-approval-required", "unknown"]) { const f = fixture(), auth = createRestServerProbeAuthority(f.actor, { requestId: "http3", acceptedAt: 1000000, authRequired: true }, f.deps), value = await auth.readAuthority() f.protections([{ object_ref: { kind: "server", id: "1", protection } }]) await denied(auth.assertProtections([1], value)) } { const f = fixture(), auth = createMcpServerProbeAuthority(f.principal, {}, f.deps), value = await auth.readAuthority() f.protections([{ object_ref: { kind: "server", id: "1", protection: "human-approval-required" } }]) await auth.assertProtections([1], value); cases++ f.protections([{ object_ref: { kind: "server", id: "1", protection: "deny" } }]) await denied(auth.assertProtections([1], value)) } { const f = fixture(), controller = new AbortController(), auth = createMcpServerProbeAuthority(f.principal, { signal: controller.signal }, f.deps) controller.abort(); await denied(auth.readAuthority()); assert.equal(f.counts().reads, 0) } { const f = fixture() f.connection.policy.mode = "observer"; f.connection.policy.scope.operationIds = [SERVER_PROBE_RECEIPT_READ_OPERATION] f.connection.owner_permissions = ["mm:servers:read"]; f.connection.policy.permissions = ["mm:servers:read"] const auth = createMcpServerProbeReadAuthority(f.principal, {}, f.deps), value = await auth.readAuthority() await auth.assertProtections([1], value); cases++ await denied(createMcpServerProbeAuthority(f.principal, {}, f.deps).readAuthority()) f.connection.policy.scope.operationIds = [SERVER_PROBE_OPERATION] await denied(auth.readAuthority()) } console.log(`Server probe authority: ${cases} inert cases passed; live MCP scope/owner/execution and REST JWT/local request identities, protection/expiry/revoke fences; no actual SQL`) } finally { pool.query = originalQuery; pool.connect = originalConnect }