239 lines
21 KiB
TypeScript
239 lines
21 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { generateKeyPairSync, randomUUID, sign } from "node:crypto"
|
|
import { realpath, writeFile } from "node:fs/promises"
|
|
import { tmpdir } from "node:os"
|
|
import { basename, dirname, isAbsolute, relative, resolve } from "node:path"
|
|
import type { ServerProbeJournalContext } from "./server-probe-journal.js"
|
|
import type { ServerProbeReceipt } from "./server-probe-execution.js"
|
|
|
|
// A separate, disposable legacy-only profile. Guard before any application
|
|
// import can create the fixed production pool or access private state.
|
|
assert.equal(process.env.MMAPP_OWN_PROBE_LEGACY_ORPHANS, "true")
|
|
const url = new URL(process.env.DATABASE_URL ?? "")
|
|
assert.equal(url.hostname, "127.0.0.1"); assert.equal(url.pathname, "/postgres")
|
|
assert(process.env.MCP_STATE_DIR)
|
|
const state = resolve(process.env.MCP_STATE_DIR), temporary = relative(resolve(tmpdir()), state)
|
|
assert(temporary && !isAbsolute(temporary) && temporary !== ".." && !temporary.startsWith("..\\") && !temporary.startsWith("../"))
|
|
assert(basename(dirname(state)).startsWith("mm-mcp-test-"))
|
|
|
|
const { pool, closePool } = await import("../db/index.js")
|
|
const { applyLegacyProbeFixtureMigrations: applySqlMigrations } = await import("../db/legacy-probe-fixture.js")
|
|
const security = await import("../modules/mcp/security.js")
|
|
const { readServerProbeExecutionSource } = await import("../modules/mcp/diagnostic-cancellation-source.js")
|
|
const { effectiveMcpConnection } = await import("../modules/mcp/store.js")
|
|
const { connectProbeStorage } = await import("./server-probe-storage-connection.js")
|
|
const { inspectLegacyServerProbeSource } = await import("./server-probe-legacy-inspection.js")
|
|
const { captureLegacyProbeStorage } = await import("./server-probe-legacy-source-inventory.js")
|
|
const { sourceBindingBytes, sourceBindingCanonical, sourceBindingSha256, readDatabaseSourceBindingRegistry } = await import("../db/database-source-binding.js")
|
|
const { provisionDatabaseSourceBindingOffline } = await import("../../scripts/database-source-binding-operator.js")
|
|
const { LEGACY_PROBE_ARCHIVE_APPROVAL_DOMAIN } = await import("./server-probe-legacy-archive-approval.js")
|
|
const cases: string[] = []
|
|
const privateValue = "owned orphan profile synthetic receipt plaintext"
|
|
const ownerId = "owned-orphan-original-" + randomUUID(), connectionId = randomUUID(), credentialId = randomUUID()
|
|
const stamp = Date.now(), issuedAt = stamp - 7200000, expiresAt = stamp - 3600000
|
|
const oldExpiry = new Date(expiresAt).toISOString(), until = new Date(stamp + 3600000).toISOString()
|
|
let serverId: number, generation: string
|
|
const bindingProof = "a".repeat(64)
|
|
const actor = () => ({ id: "owned-orphan-operator-" + randomUUID(), email: "", name: "", apps: ["mm"], permissions: ["mm:settings:admin"],
|
|
issuedAt: Date.now() - 1000, expiresAt: Date.now() + 60000 })
|
|
|
|
async function legacySchema() {
|
|
const migrations = (await pool.query("SELECT id FROM schema_migrations ORDER BY id")).rows.map(value => String(value.id))
|
|
assert(migrations.includes("0035_server_probe_journal")); assert(migrations.includes("0036_diagnostic_cancellations"))
|
|
assert.equal(migrations.at(-1), "0036_diagnostic_cancellations")
|
|
const relations = (await pool.query(`SELECT n.nspname,c.relname FROM pg_catalog.pg_class c
|
|
JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace
|
|
WHERE c.relname IN ('server_probe_execution_payloads','server_probe_storage_admissions','server_probe_storage_usage')
|
|
ORDER BY n.nspname,c.relname`)).rows
|
|
assert.deepEqual(relations, [], "Legacy-only profile must never install the v2 proposal")
|
|
return migrations
|
|
}
|
|
async function tableFingerprint() {
|
|
const tables = ["server_probe_executions", "server_probe_execution_stages", "server_probe_execution_quarantines", "mcp_diagnostic_cancellations",
|
|
"mcp_connections", "mcp_credentials", "mcp_plans", "mcp_operations", "mcp_operation_steps", "mcp_owner_revocations", "mcp_settings", "schema_migrations"]
|
|
const result: Record<string, string> = {}
|
|
for (const table of tables) {
|
|
// Names are a source-owned constant list; no caller identifier is accepted.
|
|
const rows = (await pool.query(`SELECT COALESCE(jsonb_agg(to_jsonb(t) ORDER BY to_jsonb(t)::text),'[]'::jsonb) AS rows FROM ${table} t`)).rows[0].rows
|
|
result[table] = security.fingerprint(rows)
|
|
}
|
|
return result
|
|
}
|
|
async function mcpSource(forgedDigest = false) {
|
|
const operationId = randomUUID(), receiptId = randomUUID(), planId = randomUUID()
|
|
const context: ServerProbeJournalContext = { identity: { kind: "mcp", ownerId, executionId: operationId, issuedAt, expiresAt,
|
|
connectionId, credentialId, operationName: "servers.probes.run" }, sourceServerId: serverId, tool: "ping" }
|
|
const binding = { version: 1, operation: "servers.probes.run", principal: { connectionId, credentialId, credentialExpiresAt: oldExpiry },
|
|
ownerId, issuedAt, authorityExpiresAt: expiresAt, prepared: { receiptId, serverId, tool: "ping", expiresAt: oldExpiry,
|
|
bindingCiphertext: await security.encryptSecret("owned synthetic original provider request") }, bindingProof, stageBudget: 2 }
|
|
const input = { receiptId, serverId, inputDigest: forgedDigest ? "b".repeat(64)
|
|
: await security.privateSecretFingerprint(security.fingerprint(binding), "server-probe-mcp-binding-v1"),
|
|
bindingCiphertext: await security.encryptSecret(JSON.stringify(binding)) }
|
|
const step = { kind: "service", service: "servers.probes.run", input }
|
|
const evidence = { steps: [step], target: { scope: "servers", serverIds: [serverId] }, requiresHuman: true,
|
|
verificationScope: "acknowledgement", mutationCost: 2, validUntil: oldExpiry }
|
|
const sealed = await security.encryptSecret(JSON.stringify(evidence))
|
|
await pool.query(`INSERT INTO mcp_plans(id,connection_id,operation_id,operation_version,policy_version,generation,payload,evidence,state,expires_at,approved_by)
|
|
VALUES($1,$2,'servers.probes.run',1,1,$3,'{}',$4::jsonb,'completed',$5,$6)`, [planId, connectionId, generation, JSON.stringify({ sealed }), oldExpiry, ownerId])
|
|
await pool.query("INSERT INTO mcp_operations(id,plan_id,connection_id,idempotency_key,state,finished_at) VALUES($1,$2,$3,$4,'applied',clock_timestamp())", [operationId, planId, connectionId, randomUUID()])
|
|
await pool.query("INSERT INTO mcp_operation_steps(operation_id,step_index,state,intended_state) VALUES($1,0,'intent',$2::jsonb)", [operationId, JSON.stringify(security.redact(step))])
|
|
return { operationId, receiptId, context, sealed, input }
|
|
}
|
|
type Source = Awaited<ReturnType<typeof mcpSource>>
|
|
async function readActualSource(value: Source, reject = false) {
|
|
const before = await tableFingerprint()
|
|
const options = { deadlineAt: Date.now() + 10000 }, sql = await connectProbeStorage(options)
|
|
try {
|
|
await sql.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY")
|
|
await sql.query("SET LOCAL statement_timeout='10s'")
|
|
const load = readServerProbeExecutionSource(sql, value.operationId, { connectionId, ownerId })
|
|
if (reject) await assert.rejects(load)
|
|
else {
|
|
const actual = await load
|
|
assert.deepEqual(actual.originalContext, value.context)
|
|
assert.equal(actual.originalCredentialId, credentialId); assert.equal(actual.originalContext.identity.expiresAt, expiresAt)
|
|
assert(actual.originalContext.identity.expiresAt < Date.now()); assert.equal(actual.scope.receiptId, value.receiptId)
|
|
}
|
|
await sql.query("ROLLBACK")
|
|
} finally { sql.release(true) }
|
|
assert.deepEqual(await tableFingerprint(), before)
|
|
}
|
|
async function quarantine(value: Source, attempted = 0, invalidMac = false) {
|
|
const mac = invalidMac ? "c".repeat(64) : await security.privateSecretFingerprint(security.fingerprint(value.context), "server-probe-journal-v1:identity")
|
|
await pool.query(`INSERT INTO server_probe_execution_quarantines(receipt_id,binding_proof,identity_kind,execution_id,identity_proof,error_code,attempted_stages,completed_stages)
|
|
VALUES($1,$2,'mcp',$3,$4,'JOURNAL_UNKNOWN',$5,0)`, [value.receiptId, bindingProof, value.operationId, mac, attempted])
|
|
}
|
|
async function cancel(value: Source) {
|
|
await pool.query("INSERT INTO mcp_diagnostic_cancellations(operation_id,connection_id,actor_id) VALUES($1,$2,$3)", [value.operationId, connectionId, ownerId])
|
|
}
|
|
async function observe(identities: number, unresolved: number, reasons: Record<string, number> = {}) {
|
|
const before = await tableFingerprint(), schema = await legacySchema()
|
|
const result = await inspectLegacyServerProbeSource(actor(), { deadlineAt: Date.now() + 30000, batchSize: 1 })
|
|
assert.equal(result.summary.complete, true)
|
|
assert.equal(result.summary.counts.identities, identities); assert.equal(result.summary.counts.verified, 3); assert.equal(result.summary.counts.unresolved, unresolved)
|
|
assert.deepEqual(result.summary.authenticationCounts, { receipt: 1, "approved-source": 2, "identity-context-only": 0 })
|
|
assert.deepEqual(result.summary.reasons, reasons)
|
|
assert.equal(result.summary.migrationReady, false); assert.equal(result.summary.conversionAllowed, false); assert.equal(result.summary.automaticReplay, false)
|
|
assert.equal(result.sourceObservation.ready, false); assert.equal(result.sourceObservation.generationAuthenticated, false)
|
|
assert.equal(result.sourceObservation.requiresExternalSourceBinding, true); assert.equal(result.migrationReady, false)
|
|
assert.equal(result.sourceObservation.source.databaseName, "postgres")
|
|
for (const privateData of [privateValue, ownerId, credentialId, connectionId]) assert.equal(JSON.stringify(result.summary).includes(privateData), false)
|
|
assert.deepEqual(await tableFingerprint(), before, "Fixed host must not alter anchors, ledgers, cancellations, source evidence or schema")
|
|
assert.deepEqual(await legacySchema(), schema)
|
|
return result.summary
|
|
}
|
|
|
|
async function captureLegacyRows() {
|
|
const client = await pool.connect()
|
|
try {
|
|
await client.query("BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY")
|
|
return await captureLegacyProbeStorage({ query: (text, values) => client.query(text, values) })
|
|
} finally {
|
|
try { await client.query("ROLLBACK") } finally { client.release(true) }
|
|
}
|
|
}
|
|
|
|
try {
|
|
await applySqlMigrations(pool)
|
|
await pool.query("UPDATE traffic_settings SET enabled=false; UPDATE uptime_settings SET enabled=false,resources_enabled=false,ping_enabled=false,speed_enabled=false")
|
|
await pool.query("UPDATE mcp_settings SET enabled=true WHERE id=1")
|
|
const initialSchema = await legacySchema()
|
|
serverId = Number((await pool.query("INSERT INTO servers(name,host,port,enabled) VALUES('owned-legacy-orphans','127.0.0.1',9,true) RETURNING id")).rows[0].id)
|
|
generation = await security.accessGeneration()
|
|
const policy = { id: connectionId, version: 1, status: "active", mode: "approval", permissions: ["mm:servers:write"], expiresAt: until,
|
|
scope: { serverIds: [serverId], operationIds: ["servers.probes.run"], allowGlobal: false } }
|
|
await pool.query("INSERT INTO mcp_connections(id,name,owner_id,owner_permissions,policy,generation,owner_valid_until,owner_claims_issued_at) VALUES($1,'owned orphan source',$2,$3::jsonb,$4::jsonb,$5,$6,clock_timestamp())", [connectionId, ownerId, JSON.stringify(policy.permissions), JSON.stringify(policy), generation, until])
|
|
await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [credentialId, connectionId, randomUUID(), oldExpiry])
|
|
const completed = await mcpSource(), orphanQuarantine = await mcpSource(), orphanCancellation = await mcpSource()
|
|
for (const source of [completed, orphanQuarantine, orphanCancellation]) await readActualSource(source)
|
|
const currentCredentialId = randomUUID()
|
|
await pool.query("INSERT INTO mcp_credentials(id,connection_id,digest,kind,expires_at) VALUES($1,$2,$3,'bearer',$4)", [currentCredentialId, connectionId, randomUUID(), until])
|
|
assert.equal((await effectiveMcpConnection({ connectionId, credentialId: currentCredentialId, credentialExpiresAt: until })).owner_id, ownerId)
|
|
await assert.rejects(effectiveMcpConnection({ connectionId, credentialId, credentialExpiresAt: oldExpiry }))
|
|
const receipt: ServerProbeReceipt = { receiptId: completed.receiptId, tool: "ping", serverId, state: "completed", httpStatus: 200,
|
|
output: privateValue, data: { kind: "ping" }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false }
|
|
const plaintext = JSON.stringify({ version: 1, context: completed.context, bindingProof, receipt })
|
|
await pool.query(`INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,mcp_connection_id,mcp_credential_id,authority_issued_at,authority_expires_at,source_server_id,tool)
|
|
VALUES($1,$2,'mcp',$3,$4,'mcp',$5,$6,$7,$8,$9,'ping')`, [completed.receiptId, bindingProof, completed.operationId, ownerId, connectionId, credentialId, issuedAt, oldExpiry, serverId])
|
|
await pool.query("INSERT INTO server_probe_execution_stages(receipt_id,stage_index,kind,timeout_ms,state,outcome,received_at) VALUES($1,1,'native-ping',1000,'received','received',clock_timestamp())", [completed.receiptId])
|
|
await pool.query("UPDATE server_probe_executions SET state='completed',attempted_stages=1,completed_stages=1,receipt_ciphertext=$2,receipt_proof=$3,finished_at=clock_timestamp() WHERE receipt_id=$1", [completed.receiptId, await security.encryptSecret(plaintext), await security.privateSecretFingerprint(security.fingerprint(plaintext), "server-probe-journal-v1:receipt")])
|
|
await quarantine(orphanQuarantine); await cancel(orphanCancellation)
|
|
const positive = await observe(3, 0)
|
|
assert.deepEqual(positive.counts, { identities: 3, verified: 3, unresolved: 0, mcp: 3, rest: 0, anchors: 1, quarantines: 1, cancellations: 1, orphanStages: 0 })
|
|
const rawSnapshot = await captureLegacyRows()
|
|
assert.equal(rawSnapshot.rowCount, 4)
|
|
assert.deepEqual(await captureLegacyRows(), rawSnapshot)
|
|
for (const datum of [privateValue, completed.receiptId, ownerId]) assert.equal(JSON.stringify(rawSnapshot).includes(datum), false)
|
|
const compareSnapshot = (expectedStorageSha256: string) => inspectLegacyServerProbeSource(actor(),
|
|
{ deadlineAt: Date.now() + 30000 }, undefined, expectedStorageSha256)
|
|
const correlated = await compareSnapshot(rawSnapshot.sha256)
|
|
assert.deepEqual(correlated.storageSnapshot, rawSnapshot)
|
|
assert.equal(correlated.summary.counts.verified, 3)
|
|
assert.equal(correlated.generationAuthenticated, false)
|
|
assert.equal(correlated.conversionAllowed, false)
|
|
await assert.rejects(inspectLegacyServerProbeSource(actor(), { deadlineAt: Date.now() + 30000, totalLimit: 1 },
|
|
undefined, rawSnapshot.sha256), { code: "STATE_UNAVAILABLE" })
|
|
const key = generateKeyPairSync("ed25519")
|
|
const runnerDirectory = await realpath(dirname(state))
|
|
const privateKeyFile = resolve(runnerDirectory, "owned-archive-approval.pem")
|
|
await writeFile(privateKeyFile, key.privateKey.export({ type: "pkcs8", format: "pem" }), { flag: "wx", mode: 0o600 })
|
|
const request = { installationId: randomUUID(), sourceIdentityId: randomUUID(), generation: randomUUID(),
|
|
source: { ...correlated.sourceObservation.source, databaseName: "original-legacy", inRecovery: false as const } }
|
|
const { deploymentPin: originalPin } = await provisionDatabaseSourceBindingOffline({ request, privateKeyFile,
|
|
outputDirectory: resolve(runnerDirectory, "owned-original-archive-source") })
|
|
const original = await readDatabaseSourceBindingRegistry(originalPin)
|
|
const archive = Buffer.from("owned synthetic original archive bytes")
|
|
const statement = { format: "legacy-probe-archive-approval-v1", installationId: request.installationId,
|
|
sourceIdentityId: request.sourceIdentityId, generation: request.generation, ordinal: 1,
|
|
headSha256: original.headSha256, bindingSha256: original.head.bindingSha256,
|
|
archiveSha256: sourceBindingSha256(archive), storageSha256: rawSnapshot.sha256 }
|
|
const signedApproval = sourceBindingBytes({ body: statement, signature: sign(null,
|
|
Buffer.from(LEGACY_PROBE_ARCHIVE_APPROVAL_DOMAIN + sourceBindingCanonical(statement)), key.privateKey).toString("base64url") })
|
|
const archiveApproval = { archive, signedApproval, operatorPublicKeyPem: Buffer.from(key.publicKey.export({ type: "spki", format: "pem" })) }
|
|
const compareArchive = (evidence = archiveApproval, trustedPin = originalPin) => inspectLegacyServerProbeSource(actor(),
|
|
{ deadlineAt: Date.now() + 30000 }, undefined, evidence, trustedPin)
|
|
const approved = await compareArchive()
|
|
assert.equal(approved.approvedArchive?.signerAssertionVerified, true)
|
|
assert.equal(approved.approvedArchive?.archiveBytesMatched, true)
|
|
assert.equal(approved.approvedArchive?.generation, request.generation)
|
|
assert.equal(approved.generationAuthenticated, false)
|
|
assert.equal(approved.conversionAllowed, false)
|
|
await assert.rejects(compareArchive({ ...archiveApproval, archive: Buffer.from("different bytes") }), { code: "STATE_UNAVAILABLE" })
|
|
await assert.rejects(inspectLegacyServerProbeSource(actor(), { deadlineAt: Date.now() + 30000 }, undefined, archiveApproval),
|
|
{ code: "STATE_UNAVAILABLE" })
|
|
cases.push("actual completed MCP encrypted receipt and received ledger authenticated with expired original credential")
|
|
cases.push("actual orphan MCP quarantine zero/zero and context MAC authenticated by sealed approved source without anchor")
|
|
cases.push("actual orphan MCP cancellation authenticated by sealed approved source without anchor")
|
|
|
|
await pool.query("INSERT INTO server_probe_execution_quarantines(receipt_id,binding_proof,identity_kind,execution_id,identity_proof,error_code,attempted_stages,completed_stages) VALUES($1,$2,'rest',$3,$4,'JOURNAL_UNKNOWN',0,0)", [randomUUID(), bindingProof, randomUUID(), "d".repeat(64)])
|
|
const changedSnapshot = await captureLegacyRows()
|
|
assert.equal(changedSnapshot.rowCount, 5)
|
|
assert.notEqual(changedSnapshot.sha256, rawSnapshot.sha256)
|
|
assert.notEqual(changedSnapshot.tables[1].sha256, rawSnapshot.tables[1].sha256)
|
|
assert.equal(changedSnapshot.tables[3].sha256, rawSnapshot.tables[3].sha256)
|
|
await assert.rejects(compareSnapshot(rawSnapshot.sha256), { code: "STATE_UNAVAILABLE" })
|
|
await assert.rejects(compareArchive(), { code: "STATE_UNAVAILABLE" })
|
|
await assert.rejects(compareSnapshot("malformed"), { code: "STATE_UNAVAILABLE" })
|
|
assert.equal((await compareSnapshot(changedSnapshot.sha256)).summary.counts.unresolved, 1)
|
|
cases.push("raw legacy operator snapshot is stable across leases and detects an appended quarantine without exposing data")
|
|
cases.push("legacy operator correlates raw digest and verified classifications on one owned snapshot and refuses drift")
|
|
cases.push("legacy operator verifies independently pinned source signer and exact archive bytes before correlating snapshot")
|
|
await observe(4, 1, { CONTEXT_UNAVAILABLE: 1 }); cases.push("orphan REST quarantine without original context remains unresolved")
|
|
await pool.query(`INSERT INTO server_probe_executions(receipt_id,binding_proof,identity_kind,execution_id,actor_id,authentication,request_id,authority_issued_at,authority_expires_at,source_server_id,tool)
|
|
VALUES($1,$2,'rest',$3,$4,'jwt',$5,$6,$7,$8,'ping')`, [randomUUID(), bindingProof, randomUUID(), "unproved-rest-owner", randomUUID(), issuedAt, oldExpiry, serverId])
|
|
await observe(5, 2, { CONTEXT_UNAVAILABLE: 2 }); cases.push("claimed REST anchor without receipt or context MAC remains unresolved")
|
|
const invalidMac = await mcpSource(); await readActualSource(invalidMac); await quarantine(invalidMac, 0, true)
|
|
await observe(6, 3, { CONTEXT_UNAVAILABLE: 2, QUARANTINE_INVALID: 1 }); cases.push("actual sealed MCP source does not authenticate invalid orphan quarantine MAC")
|
|
const forged = await mcpSource(true); await readActualSource(forged, true); await cancel(forged)
|
|
await observe(7, 4, { CONTEXT_UNAVAILABLE: 2, QUARANTINE_INVALID: 1, SOURCE_UNAVAILABLE: 1 }); cases.push("actual encrypted MCP evidence with forged keyed input digest remains unresolved")
|
|
const noLedger = await mcpSource(); await readActualSource(noLedger); await quarantine(noLedger, 1)
|
|
const final = await observe(8, 5, { CONTEXT_UNAVAILABLE: 2, QUARANTINE_INVALID: 2, SOURCE_UNAVAILABLE: 1 })
|
|
cases.push("actual orphan MCP quarantine nonzero progress without ledger remains unresolved")
|
|
assert.deepEqual(await legacySchema(), initialSchema)
|
|
console.log("Owned legacy-only orphan/source acceptance:", JSON.stringify({ cases, actualSql: true, actualProductionCrypto: true,
|
|
actualFixedProductionSourcePairedHost: true, originalExpiredCredentialNotRevived: true, appendOnlyFixtures: true,
|
|
immutableRowsEdited: 0, triggersDisabled: 0, v2ProposalApplied: false, v2RelationsBefore: 0, v2RelationsAfter: 0, schemaVersionBefore: initialSchema.at(-1), schemaVersionAfter: initialSchema.at(-1),
|
|
finalCounts: final.counts, authenticationCounts: final.authenticationCounts, reasons: final.reasons,
|
|
sourceGenerationAuthenticated: false, migrationReady: false, conversionAllowed: false, nativeCalls: 0, realNetworkCalls: 0 }))
|
|
} finally { await closePool() }
|