99 lines
7.3 KiB
TypeScript
99 lines
7.3 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { createHmac, randomBytes, randomUUID } from "node:crypto"
|
|
import { mkdtemp, readFile, rm } from "node:fs/promises"
|
|
import { tmpdir } from "node:os"
|
|
import { join } from "node:path"
|
|
import { prepareRuntimeVrfTicket, withVrfClaimQuarantine, type VrfRuntimeTicketInput } from "./vrf-ticket-runtime.js"
|
|
import { VrfProductionClaimOutcomeUnknown, type PrepareVrfProductionTicketInput } from "../../services/vrf-production-ticket-store.js"
|
|
|
|
const sourceProof = "a".repeat(64)
|
|
let issued: PrepareVrfProductionTicketInput | undefined, keyUses = 0, authority = 0, sources = 0
|
|
const sql = { async query() { return { rows: [] } } }
|
|
const input: VrfRuntimeTicketInput = {
|
|
operationId: "ros.vrf.update", operationVersion: 4, planId: randomUUID(), connectionId: randomUUID(), ownerId: "owner",
|
|
evidence: { vrfProductionAdmission: true, target: { scope: "servers", serverIds: [7] }, objects: [], warnings: [], snapshots: [{ serverId: 7, resource: "vrf", rows: [], fingerprint: "snapshot", projectionVersion: 3, sourceProof }], steps: [{ kind: "ros", step: { serverId: 7, resource: "vrf", action: "update", id: "*1", fields: { name: "tenant" } } }] },
|
|
sql, async assertAuthority() { authority++ }, async assertCurrentSource(serverId, proof) { assert.equal(serverId, 7); assert.equal(proof, sourceProof); sources++ },
|
|
}
|
|
const dependencies = {
|
|
async withKey<T>(work: (key: Uint8Array) => Promise<T>) { keyUses++; return work(randomBytes(32)) },
|
|
async prepare(value: PrepareVrfProductionTicketInput) {
|
|
issued = value
|
|
await value.assertAuthority(); await value.assertCurrentSource()
|
|
await value.transaction(async transaction => { assert.equal(transaction, sql) })
|
|
return { ticketId: randomUUID(), expiresAt: new Date().toISOString(), executionAvailable: false as const }
|
|
},
|
|
}
|
|
const ticket = await prepareRuntimeVrfTicket(input, dependencies)
|
|
assert.equal(ticket?.executionAvailable, false)
|
|
assert.equal(keyUses, 1); assert.equal(authority, 1); assert.equal(sources, 1)
|
|
assert.equal(issued?.context.binding.planId, input.planId)
|
|
assert.equal(issued?.context.binding.actorId, input.ownerId)
|
|
assert.equal(issued?.context.connectionId, input.connectionId)
|
|
assert.equal(issued?.context.actionVersion, 4)
|
|
assert.deepEqual(issued?.request, { action: "patch", id: "*1", fields: { name: "tenant" } })
|
|
assert.deepEqual(Object.keys(ticket!).sort(), ["executionAvailable", "expiresAt", "ticketId"])
|
|
|
|
// A mislabeled, combined, cross-target or projection-v2 plan must never reach
|
|
// the raw reader/key provider, even if it resembles a VRF action.
|
|
for (const [mutate, refusal] of [
|
|
[(value: VrfRuntimeTicketInput) => { value.operationId = "ros.ethernet.update" }, /VRF production ticket refused: untyped VRF plan/],
|
|
[(value: VrfRuntimeTicketInput) => { value.operationId = "ros.vrf.delete" }, /VRF action binding refused: unmapped public\/native action/],
|
|
[(value: VrfRuntimeTicketInput) => { value.evidence.steps.push(structuredClone(value.evidence.steps[0])) }, /VRF production ticket refused: ambiguous typed plan/],
|
|
[(value: VrfRuntimeTicketInput) => { value.evidence.target = { scope: "servers", serverIds: [8] } }, /VRF production ticket refused: ambiguous typed plan/],
|
|
[(value: VrfRuntimeTicketInput) => { value.evidence.snapshots[0].projectionVersion = 2 }, /VRF production ticket refused: missing trusted source-v3 snapshot/],
|
|
[(value: VrfRuntimeTicketInput) => { delete value.evidence.snapshots[0].sourceProof }, /VRF production ticket refused: missing trusted source-v3 snapshot/],
|
|
] as const) {
|
|
const changed = { ...input, evidence: structuredClone(input.evidence) }
|
|
mutate(changed)
|
|
await assert.rejects(prepareRuntimeVrfTicket(changed, dependencies), refusal)
|
|
assert.equal(keyUses, 1)
|
|
}
|
|
assert.equal(await prepareRuntimeVrfTicket({ ...input, evidence: { ...input.evidence, vrfProductionAdmission: undefined } }, dependencies), undefined)
|
|
|
|
// Lost claim ACK must be quarantined in the permanent operation journal
|
|
// before control returns. It cannot return a mutation grant or retry claim.
|
|
let claims = 0, quarantines = 0
|
|
const unknown = new VrfProductionClaimOutcomeUnknown(randomUUID())
|
|
const journalInput = { operationId: randomUUID(), planId: input.planId, connectionId: input.connectionId, ownerId: input.ownerId,
|
|
journal: { async query(text: string, values?: unknown[]) { quarantines++; assert.match(text, /state='unknown'/); assert.match(text, /VRF_CLAIM_OUTCOME_UNKNOWN/); assert.deepEqual(values, [journalInput.operationId, input.planId, input.connectionId, input.ownerId]); return { rows: [{ id: journalInput.operationId }] } } },
|
|
async claim(): Promise<never> { claims++; throw unknown },
|
|
}
|
|
await assert.rejects(withVrfClaimQuarantine(journalInput), error => error === unknown)
|
|
assert.equal(claims, 1); assert.equal(quarantines, 1)
|
|
await assert.rejects(withVrfClaimQuarantine({ ...journalInput, journal: { async query() { return { rows: [] } } } }), /quarantine unavailable; execution forbidden/)
|
|
const ordinary = new Error("revoked")
|
|
await assert.rejects(withVrfClaimQuarantine({ ...journalInput, async claim() { throw ordinary } }), error => error === ordinary)
|
|
assert.equal(quarantines, 1)
|
|
|
|
// Wiring boundary: actual encrypted INSERT precedes ticket creation and audit
|
|
// within the existing transaction. It does not bypass action preparation or
|
|
// replace existing native companion refusal / simple VRF execution admission.
|
|
const runtime = await readFile(new URL("./runtime.ts", import.meta.url), "utf8")
|
|
const insert = runtime.indexOf('await client.query("INSERT INTO mcp_plans(')
|
|
const prepare = runtime.indexOf("const vrfTicket = await prepareRuntimeVrfTicket(")
|
|
const audit = runtime.indexOf('await audit(client, connection.owner_id, "plan.create"')
|
|
assert.ok(insert > 0 && prepare > insert && audit > prepare)
|
|
assert.ok(runtime.indexOf("const evidence = await action.prepare(input, call.context)") < insert)
|
|
assert.match(runtime, /\(vrfTicket \? \{ vrfTicket \} : \{\}\), executionAvailable: true/)
|
|
// Repeated server callbacks derive identical purpose material from durable
|
|
// storage; a different server state directory cannot validate these tickets.
|
|
const directory = await mkdtemp(join(tmpdir(), "mm-vrf-sealing-")), other = await mkdtemp(join(tmpdir(), "mm-vrf-sealing-")), previous = process.env.MCP_STATE_DIR
|
|
try {
|
|
const { withVrfProductionSealingKey } = await import("./security.js")
|
|
process.env.MCP_STATE_DIR = directory
|
|
let retained: Uint8Array | undefined
|
|
const digest = async () => withVrfProductionSealingKey(async key => { retained = key; return createHmac("sha256", key).update("private-test-vector").digest("hex") })
|
|
const first = await digest()
|
|
assert.ok(retained!.every(byte => byte === 0), "purpose key cleared after callback")
|
|
assert.equal(await digest(), first)
|
|
process.env.MCP_STATE_DIR = other
|
|
assert.notEqual(await digest(), first)
|
|
process.env.MCP_STATE_DIR = directory
|
|
const encryptionKey = await readFile(join(directory, "encryption-key"))
|
|
assert.notEqual(createHmac("sha256", encryptionKey).update("private-test-vector").digest("hex"), first)
|
|
} finally {
|
|
if (previous === undefined) delete process.env.MCP_STATE_DIR; else process.env.MCP_STATE_DIR = previous
|
|
await rm(directory, { recursive: true, force: true }); await rm(other, { recursive: true, force: true })
|
|
}
|
|
console.log("VRF ticket runtime: transaction wiring, typed scope, private callback and claim quarantine passed")
|