59 lines
4.5 KiB
TypeScript
59 lines
4.5 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import { randomUUID } from "node:crypto"
|
|
import { pool } from "../db/index.js"
|
|
import type { AuthUser } from "../lib/permissions.js"
|
|
import { createStorageServerProbeRestExecution } from "./server-probe-storage-rest.js"
|
|
import { createRestServerProbeAuthority } from "./server-probe-authority.js"
|
|
import { createProbeStorageHostIo } from "./server-probe-storage-host-io.js"
|
|
import { PROBE_STORAGE_LIMITS } from "../modules/mcp/diagnostic-cancellation-storage.js"
|
|
|
|
const context = () => ({ requestId: randomUUID(), acceptedAt: Date.now(), authRequired: true })
|
|
const request = { tool: "ping", target: "127.0.0.1" }
|
|
const actor = (permissions: string[]): AuthUser => ({ id: "owned-rest-user", email: "[email protected]", name: "Fixture", apps: ["mm"], permissions, issuedAt: Date.now() - 1000, expiresAt: Date.now() + 300000 })
|
|
|
|
test("REST opt-in construction and unauthenticated/read-only inputs cannot query a source or acquire a slot", async () => {
|
|
const originalQuery = pool.query, originalConnect = pool.connect
|
|
pool.query = (() => assert.fail("No SQL before REST authentication")) as typeof pool.query
|
|
pool.connect = (() => assert.fail("No checkout before REST authentication")) as typeof pool.connect
|
|
try {
|
|
const execute = createStorageServerProbeRestExecution()
|
|
assert.equal((await execute("1", { ...request, authRequired: false, ownerId: "admin", kind: "mcp" }, context())).statusCode, 403)
|
|
assert.equal((await execute("1", request, { ...context(), actor: actor(["mm:servers:read"]) })).statusCode, 403)
|
|
assert.equal((await execute("1", request, { ...context(), authRequired: false, actor: actor(["mm:servers:read"]) })).statusCode, 403)
|
|
const expired = { ...actor(["mm:servers:write"]), expiresAt: Date.now() - 1 }
|
|
assert.equal((await execute("1", request, { ...context(), actor: expired })).statusCode, 403)
|
|
} finally { pool.query = originalQuery; pool.connect = originalConnect }
|
|
})
|
|
test("authenticated local missing source retains 404 without fleet enumeration or credential reads", async () => {
|
|
const originalQuery = pool.query, originalConnect = pool.connect, queries: string[] = [], releases: boolean[] = []
|
|
pool.query = (() => assert.fail("Metadata must use bounded checkout")) as typeof pool.query
|
|
pool.connect = (async () => ({
|
|
async query(config: { text: string; values?: unknown[]; query_timeout: number }) {
|
|
queries.push(config.text); assert.equal(config.text, "SELECT id FROM servers WHERE id=$1 LIMIT 2"); assert.deepEqual(config.values, [37])
|
|
assert(config.query_timeout > 0 && config.query_timeout <= 10000)
|
|
return { rows: [], command: "SELECT" }
|
|
}, release(unsafe?: boolean) { releases.push(!!unsafe) },
|
|
})) as unknown as typeof pool.connect
|
|
try {
|
|
const response = await createStorageServerProbeRestExecution()("37", request, { ...context(), authRequired: false })
|
|
assert.deepEqual(response, { statusCode: 404, body: { error: "Server not found" } }); assert.equal(queries.length, 1); assert.deepEqual(releases, [false])
|
|
} finally { pool.query = originalQuery; pool.connect = originalConnect }
|
|
})
|
|
test("scoped REST authority uses trusted participant metadata and fences permission before resolving", async () => {
|
|
let reads = 0, selected = [9, 2]
|
|
const resolver = async () => { reads++; return selected }
|
|
const local = createRestServerProbeAuthority(undefined, { ...context(), authRequired: false }, { query: async () => assert.fail("No fleet enumeration") }, resolver)
|
|
const first = await local.readAuthority(); assert.deepEqual(first.scopeServerIds, [2, 9]); assert.equal(reads, 1)
|
|
selected = [9, 3]
|
|
const changed = await local.readAuthority(); assert.notEqual(changed.policyProof, first.policyProof)
|
|
assert.throws(() => createRestServerProbeAuthority(actor(["mm:servers:read"]), context(), {}, resolver))
|
|
const revoked = createRestServerProbeAuthority(actor(["mm:servers:write"]), context(), { portal: async () => { throw Error("revoked") } }, resolver)
|
|
const before = reads; await assert.rejects(revoked.readAuthority()); assert.equal(reads, before)
|
|
})
|
|
test("shared kernel fixes identity kind at construction and refuses cross-transport journals before I/O", () => {
|
|
assert.throws(() => createProbeStorageHostIo(PROBE_STORAGE_LIMITS, "caller-selected" as never))
|
|
const io = createProbeStorageHostIo(PROBE_STORAGE_LIMITS, "rest")
|
|
assert.throws(() => io.journal({ identity: { kind: "mcp" } } as never, { assertAuthority: async () => assert.fail("No authority for wrong transport") }))
|
|
})
|