172 lines
14 KiB
TypeScript
172 lines
14 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { randomUUID } from "node:crypto"
|
|
import { mkdtemp, rmdir, unlink } from "node:fs/promises"
|
|
import { tmpdir } from "node:os"
|
|
import { join } from "node:path"
|
|
import { encryptSecret, fingerprint, privateSecretFingerprint, redact } from "../modules/mcp/security.js"
|
|
import type { ProbeStorageSql } from "./server-probe-journal-maintenance.js"
|
|
import { collectLegacyServerProbeSnapshot, type LegacyServerProbeSnapshotOptions } from "./server-probe-legacy-snapshot.js"
|
|
|
|
type Row = Record<string, unknown>
|
|
const privateValue = "NEVER_SNAPSHOT_PLAINTEXT_OR_PROVIDER_ERROR"
|
|
const timestamp = new Date("2020-01-01T00:00:00Z")
|
|
function database() {
|
|
const anchors: Row[] = [], quarantines: Row[] = [], cancellations: Row[] = [], stages: Row[] = []
|
|
const sources = new Map<string, Row>(), calls: { text: string; values?: unknown[]; timeout?: number }[] = []
|
|
let isolation = "repeatable read", readOnly = "on", explicit = true, orphanStages = 0
|
|
let observe: ((text: string) => void | Promise<void>) | undefined
|
|
const sql: ProbeStorageSql = { async query(text, values, options) {
|
|
calls.push({ text, values, timeout: options?.timeoutMs })
|
|
assert(!/\bBEGIN\b|\bCOMMIT\b|\bROLLBACK\b|\bINSERT\b|\bDELETE\b|\bUPDATE\b|\bALTER\b|\bCREATE\b|FOR SHARE/.test(text))
|
|
assert(options && options.timeoutMs >= 1 && options.timeoutMs <= 10000)
|
|
await observe?.(text)
|
|
let rows: Row[]
|
|
const [kind, execution, receipt] = values ?? []
|
|
if (text.includes(":transaction")) rows = [{ isolation, read_only: readOnly, explicit_transaction: explicit }]
|
|
else if (text.includes(":orphan-stages")) rows = [{ count: String(orphanStages) }]
|
|
else if (text.includes(":identities")) {
|
|
const all = [...anchors, ...quarantines].map(row => ({ identity_kind: row.identity_kind, execution_id: row.execution_id }))
|
|
all.push(...cancellations.map(row => ({ identity_kind: "mcp", execution_id: row.operation_id })))
|
|
rows = [...new Map(all.map(row => [String(row.identity_kind) + ":" + String(row.execution_id), row])).values()]
|
|
.sort((a,b) => String(a.identity_kind).localeCompare(String(b.identity_kind)) || String(a.execution_id).localeCompare(String(b.execution_id)))
|
|
.filter(row => kind === null || String(row.identity_kind) > String(kind) || row.identity_kind === kind && String(row.execution_id) > String(execution))
|
|
.slice(0, Number(receipt))
|
|
} else if (text.includes(":anchor-receipt")) rows = anchors.filter(row => row.identity_kind === kind && row.execution_id === execution || row.receipt_id === receipt).slice(0, 2)
|
|
else if (text.includes(":anchor")) rows = anchors.filter(row => row.identity_kind === kind && row.execution_id === execution).slice(0, 2)
|
|
else if (text.includes(":quarantine")) rows = quarantines.filter(row => row.identity_kind === kind && row.execution_id === execution || receipt !== null && row.receipt_id === receipt).slice(0, 2)
|
|
else if (text.includes(":cancellation")) rows = cancellations.filter(row => kind === "mcp" && row.operation_id === execution).slice(0, 2)
|
|
else if (text.includes(":owner")) { const source = sources.get(String(kind)); rows = source ? [{ connection_id: source.connection_id, owner_id: source.owner_id }] : [] }
|
|
else if (text.includes("diagnostic-cancellation:source")) {
|
|
assert(!text.includes("FOR UPDATE")); const source = sources.get(String(kind)); rows = source ? [source] : []
|
|
} else if (text.includes(":stages")) rows = stages.filter(row => row.receipt_id === kind).sort((a,b) => Number(a.stage_index) - Number(b.stage_index)).slice(0, 12)
|
|
else throw Error(privateValue)
|
|
return { rows: structuredClone(rows) }
|
|
} }
|
|
return { sql, anchors, quarantines, cancellations, stages, sources, calls,
|
|
settings: (iso: string, ro: string, transaction = true) => { isolation = iso; readOnly = ro; explicit = transaction },
|
|
orphans: (count: number) => { orphanStages = count }, onQuery: (callback: typeof observe) => { observe = callback } }
|
|
}
|
|
type Database = ReturnType<typeof database>
|
|
async function addRest(db: Database) {
|
|
const receiptId = randomUUID(), executionId = randomUUID(), requestId = randomUUID(), owner = "private-owner-" + randomUUID()
|
|
const context = { identity: { kind: "rest", ownerId: owner, executionId, issuedAt: 1, expiresAt: timestamp.getTime(), requestId, authentication: "jwt" }, sourceServerId: 9, tool: "ping" }
|
|
const receipt = { receiptId, tool: "ping", serverId: 9, state: "completed", httpStatus: 200, output: privateValue,
|
|
data: { kind: "ping" }, attemptedStages: 1, completedStages: 1, automaticReplay: false, forwardingVerified: false }
|
|
const plaintext = JSON.stringify({ version: 1, context, bindingProof: "a".repeat(64), receipt })
|
|
db.anchors.push({ receipt_id: receiptId, binding_proof: "a".repeat(64), identity_kind: "rest", execution_id: executionId, actor_id: owner,
|
|
authentication: "jwt", mcp_connection_id: null, mcp_credential_id: null, request_id: requestId, authority_issued_at: "1", authority_expires_at: timestamp,
|
|
source_server_id: 9, destination_server_id: null, tool: "ping", state: "completed", attempted_stages: 1, completed_stages: 1,
|
|
receipt_ciphertext: await encryptSecret(plaintext), receipt_proof: await privateSecretFingerprint(fingerprint(plaintext), "server-probe-journal-v1:receipt"), created_at: timestamp, finished_at: timestamp })
|
|
db.stages.push({ receipt_id: receiptId, stage_index: 1, kind: "native-ping", timeout_ms: 1000, state: "received", outcome: "received", created_at: timestamp, received_at: timestamp })
|
|
return { executionId, receiptId, owner, requestId }
|
|
}
|
|
async function addMcpCancellation(db: Database) {
|
|
const operationId = randomUUID(), connectionId = randomUUID(), credentialId = randomUUID(), receiptId = randomUUID(), ownerId = "private-mcp-owner"
|
|
const binding = { version: 1, operation: "servers.probes.run", principal: { connectionId, credentialId, credentialExpiresAt: timestamp.toISOString() },
|
|
ownerId, issuedAt: 1, authorityExpiresAt: timestamp.getTime(), prepared: { receiptId, serverId: 9, tool: "ping", expiresAt: timestamp.toISOString(), bindingCiphertext: "private-inner-ciphertext" }, bindingProof: "b".repeat(64), stageBudget: 2 }
|
|
const input = { receiptId, serverId: 9, bindingCiphertext: await encryptSecret(JSON.stringify(binding)), inputDigest: await privateSecretFingerprint(fingerprint(binding), "server-probe-mcp-binding-v1") }
|
|
const step = { kind: "service", service: "servers.probes.run", input }
|
|
const evidence = { steps: [step], target: { scope: "servers", serverIds: [9] }, requiresHuman: true, verificationScope: "acknowledgement", validUntil: timestamp.toISOString(), mutationCost: 2 }
|
|
const source: Row = { operation_id: operationId, connection_id: connectionId, owner_id: ownerId, operation_state: "pending", operation_name: "servers.probes.run",
|
|
operation_version: 1, plan_state: "executing", approved_by: "real-human", evidence: { sealed: await encryptSecret(JSON.stringify(evidence)) },
|
|
step_index: null, step_state: null, intended_state: null }
|
|
db.sources.set(operationId, source)
|
|
db.cancellations.push({ operation_id: operationId, connection_id: connectionId, actor_id: ownerId })
|
|
return { source, operationId, step, input }
|
|
}
|
|
const collect = (db: Database, options: Partial<LegacyServerProbeSnapshotOptions> = {}) => collectLegacyServerProbeSnapshot(db.sql, { deadlineAt: Date.now() + 60000, ...options })
|
|
const directory = await mkdtemp(join(tmpdir(), "mcp-legacy-snapshot-")), previous = process.env.MCP_STATE_DIR
|
|
process.env.MCP_STATE_DIR = directory
|
|
let checks = 0
|
|
try {
|
|
{
|
|
const db = database(); const result = await collect(db)
|
|
assert.equal(result.complete, true); assert.equal(result.counts.identities, 0); assert.equal(result.migrationReady, false)
|
|
assert.equal(result.conversionAllowed, false); assert.equal(result.requiresDatabaseSourceAttestation, true)
|
|
assert.equal(result.requiresWriterDrain, true); assert.equal(result.requiresLockedReverification, true)
|
|
assert.deepEqual(db.calls.map(row => row.text.match(/probe-legacy-snapshot:([a-z-]+)/)?.[1]), ["transaction", "orphan-stages", "identities"]); checks++
|
|
}
|
|
{
|
|
const db = database(), rest = await addRest(db); await addMcpCancellation(db)
|
|
const result = await collect(db, { batchSize: 1 })
|
|
assert.equal(result.complete, true); assert.deepEqual(result.counts, { identities: 2, verified: 2, unresolved: 0, mcp: 1, rest: 1, anchors: 1, quarantines: 0, cancellations: 1, orphanStages: 0 })
|
|
assert.deepEqual(result.authenticationCounts, { receipt: 1, "approved-source": 1, "identity-context-only": 0 })
|
|
assert.deepEqual(result.reasons, {}); assert.match(result.aggregateDigest, /^[a-f0-9]{64}$/)
|
|
for (const value of [rest.executionId, rest.receiptId, rest.owner, rest.requestId, privateValue, "private-inner-ciphertext", "private-mcp-owner", String(db.anchors[0].receipt_ciphertext), String(db.anchors[0].receipt_proof)]) assert.equal(JSON.stringify(result).includes(value), false)
|
|
assert.equal(db.calls.filter(row => row.text.includes("diagnostic-cancellation:source")).length, 1)
|
|
assert(Object.isFrozen(result) && Object.isFrozen(result.counts) && Object.isFrozen(result.reasons))
|
|
const stable = await collect(db, { batchSize: 1 }); assert.equal(stable.aggregateDigest, result.aggregateDigest)
|
|
db.sources.values().next().value!.approved_by = "another-human"
|
|
const changed = await collect(db, { batchSize: 1 }); assert.notEqual(changed.aggregateDigest, result.aggregateDigest)
|
|
checks++
|
|
}
|
|
for (const [isolation, readOnly] of [["read committed", "on"], ["repeatable read", "off"]]) {
|
|
const db = database(); db.settings(isolation, readOnly)
|
|
const result = await collect(db); assert.equal(result.complete, false); assert.equal(result.reasons.SNAPSHOT_REQUIRED, 1); assert.equal(db.calls.length, 1); checks++
|
|
}
|
|
{
|
|
const db = database(); db.settings("repeatable read", "on", false)
|
|
const result = await collect(db); assert.equal(result.complete, false); assert.equal(result.reasons.SNAPSHOT_REQUIRED, 1)
|
|
assert.equal(db.calls.length, 1); assert(db.calls[0].text.includes("transaction_timestamp()<statement_timestamp()")); checks++
|
|
}
|
|
{
|
|
const db = database(); db.orphans(3)
|
|
const result = await collect(db); assert.equal(result.complete, false); assert.equal(result.counts.orphanStages, 3); assert.equal(result.reasons.ORPHAN_STAGES, 1)
|
|
assert.equal(db.calls.length, 2); checks++
|
|
}
|
|
{
|
|
const db = database()
|
|
db.quarantines.push({ identity_kind: "rest", execution_id: randomUUID(), receipt_id: randomUUID(), binding_proof: "a".repeat(64) })
|
|
const result = await collect(db); assert.equal(result.complete, true); assert.equal(result.counts.identities, 1); assert.equal(result.counts.unresolved, 1)
|
|
assert.equal(result.reasons.CONTEXT_UNAVAILABLE, 1); assert.equal(result.counts.quarantines, 1); checks++
|
|
}
|
|
{
|
|
const db = database(); await addRest(db); db.anchors.push(structuredClone(db.anchors[0]))
|
|
const result = await collect(db); assert.equal(result.complete, false); assert.equal(result.reasons.ROW_LIMIT, 1); checks++
|
|
}
|
|
{
|
|
const db = database(); await addRest(db)
|
|
for (let n = 2; n <= 12; n++) db.stages.push({ ...db.stages[0], stage_index: n })
|
|
const result = await collect(db); assert.equal(result.complete, false); assert.equal(result.reasons.ROW_LIMIT, 1); checks++
|
|
}
|
|
{
|
|
const db = database(); for (let n = 0; n < 3; n++) await addRest(db)
|
|
const result = await collect(db, { batchSize: 1, totalLimit: 2 })
|
|
assert.equal(result.complete, false); assert.equal(result.counts.identities, 2); assert.equal(result.counts.verified, 2); assert.equal(result.reasons.TOTAL_LIMIT, 1); checks++
|
|
}
|
|
{
|
|
const db = database(); await addRest(db)
|
|
db.quarantines.push({ ...db.anchors[0], identity_kind: "mcp", execution_id: randomUUID() })
|
|
const result = await collect(db); assert.equal(result.complete, false); assert.equal(result.reasons.SOURCE_UNAVAILABLE, 1); assert.equal(result.counts.verified, 0); checks++
|
|
}
|
|
{
|
|
const db = database(), mcp = await addMcpCancellation(db); mcp.source.intended_state = redact(mcp.step); mcp.source.step_index = 0; mcp.source.step_state = "intent"; mcp.source.operation_state = "running"
|
|
const result = await collect(db); assert.equal(result.counts.verified, 1)
|
|
mcp.source.intended_state = { service: "forged" }
|
|
const tampered = await collect(db); assert.equal(tampered.complete, true); assert.equal(tampered.counts.unresolved, 1); assert.equal(tampered.reasons.SOURCE_UNAVAILABLE, 1); checks++
|
|
}
|
|
{
|
|
const db = database(); db.onQuery(() => { throw Error(privateValue) })
|
|
const result = await collect(db); assert.equal(result.reasons.SQL_UNAVAILABLE, 1); assert.equal(JSON.stringify(result).includes(privateValue), false); checks++
|
|
}
|
|
{
|
|
const db = database(), controller = new AbortController(); controller.abort()
|
|
const result = await collect(db, { signal: controller.signal }); assert.equal(result.reasons.CANCELLED, 1); assert.equal(db.calls.length, 0); checks++
|
|
}
|
|
{
|
|
const db = database(), controller = new AbortController(); db.onQuery(() => controller.abort())
|
|
const result = await collect(db, { signal: controller.signal }); assert.equal(result.reasons.CANCELLED, 1); assert.equal(db.calls.length, 1); checks++
|
|
}
|
|
{
|
|
const db = database(), result = await collect(db, { deadlineAt: Date.now() - 1 })
|
|
assert.equal(result.reasons.DEADLINE_EXCEEDED, 1); assert.equal(db.calls.length, 0); checks++
|
|
}
|
|
for (const bad of [{ batchSize: 101 }, { totalLimit: 10001 }, { totalLimit: 0 }, { deadlineAt: NaN }]) {
|
|
const db = database(), result = await collect(db, bad); assert.equal(result.reasons.INVALID_OPTIONS, 1); assert.equal(db.calls.length, 0); checks++
|
|
}
|
|
console.log(`server-probe-legacy-snapshot: ${checks} fixed-SQL read-only checks passed; no database/native calls; conversion always false`)
|
|
} finally {
|
|
if (previous === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = previous
|
|
await unlink(join(directory, "encryption-key")); await rmdir(directory)
|
|
}
|