177 lines
13 KiB
TypeScript
177 lines
13 KiB
TypeScript
/** Real AES-GCM/HMAC component composition. The trusted source callback here
|
|
* tests codec ordering, not actual SQL source/terminal acceptance or authority. */
|
|
import assert from "node:assert/strict"
|
|
import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from "node:crypto"
|
|
import { test } from "node:test"
|
|
import { gzipSync } from "node:zlib"
|
|
import { canonicalPollArchiveTimestamp, createServerPollTerminalArchiveForTests, ServerPollArchiveRefused,
|
|
SERVER_POLL_ARCHIVE_MAX_BYTES, SERVER_POLL_ARCHIVE_MAX_DECODED_BYTES,
|
|
type ServerPollArchiveSource, type ServerPollArchiveSourceMetadata, type ServerPollTerminalArchive } from "./server-poll-terminal-archive.js"
|
|
|
|
const key = Buffer.from("4f".repeat(32), "hex"), macKey = Buffer.from("7c".repeat(32), "hex")
|
|
const sha = (value: Buffer | string) => createHash("sha256").update(value).digest("hex")
|
|
const canonical = (value: unknown): string => value && typeof value === "object" ? Array.isArray(value) ? "[" + value.map(canonical).join(",") + "]"
|
|
: "{" + Object.entries(value).sort(([a], [b]) => a.localeCompare(b)).map(([name, item]) => JSON.stringify(name) + ":" + canonical(item)).join(",") + "}" : JSON.stringify(value)
|
|
const proof = async (purpose: string, value: unknown) => createHmac("sha256", macKey).update(purpose + ":" + canonical(value)).digest("hex")
|
|
const encrypt = (plaintext: Buffer) => {
|
|
const iv = randomBytes(12), cipher = createCipheriv("aes-256-gcm", key, iv)
|
|
const encrypted = Buffer.concat([cipher.update(plaintext), cipher.final()])
|
|
return Buffer.concat([iv, cipher.getAuthTag(), encrypted])
|
|
}
|
|
const decrypt = (encrypted: Buffer) => {
|
|
const decipher = createDecipheriv("aes-256-gcm", key, encrypted.subarray(0, 12))
|
|
decipher.setAuthTag(encrypted.subarray(12, 28))
|
|
return Buffer.concat([decipher.update(encrypted.subarray(28)), decipher.final()])
|
|
}
|
|
const receiptId = "11111111-1111-4111-8111-111111111111", executionId = "22222222-2222-4222-8222-222222222222", connectionId = "33333333-3333-4333-8333-333333333333"
|
|
const privateText = "PRIVATE-PAYLOAD-MUST-NOT-ESCAPE"
|
|
const sample = (): ServerPollArchiveSource => ({
|
|
admission: { receipt_id: receiptId, opaque_future_column: { secret: privateText, unicode: "Настройки 🛰️ 東京\u0000", future_flag: false }, last_activity_at: "2026-10-29T03:24:15.123456+00:00" },
|
|
journal: { execution: { receipt_id: receiptId, created_at: "2026-10-29T03:20:00.654321Z", future_native_hint: [null, "雪"] },
|
|
intents: [{ stage: "native.identity", future_opaque_field: { payload: "unchanged" } }], acks: [{ stage: "native.identity", created_at: "2026-10-29T03:20:01.000009Z" }],
|
|
receipt: { receipt_ciphertext: "opaque-original-ciphertext", new_receipt_column: 17 }, quarantines: [] },
|
|
terminal: { operation: { id: executionId, unknown_result: { unicode: "Опрос завершён ✅", secret: privateText } },
|
|
plan: { id: "44444444-4444-4444-8444-444444444444", unknown_plan_config: [1, true, null] }, step: { step_index: 0, extra_column: "retained" } },
|
|
})
|
|
const metadata = (): ServerPollArchiveSourceMetadata => ({ receiptId, executionId, connectionId, ownerId: "owned-terminal-component",
|
|
bindingProof: "a".repeat(64), contextProof: "b".repeat(64), terminalDigest: "c".repeat(64), originalActivityAt: "2026-10-29T03:24:15.123456+00:00",
|
|
originalExpiresAt: "2026-10-29T03:25:00.000009Z", retentionDays: 30, safetyMs: 60000 })
|
|
function fixture(padding = 0) {
|
|
const events: string[] = [], counts = { verify: 0, encrypt: 0, decrypt: 0 }
|
|
const state = { failSource: false, failEncrypt: false, failProof: false, verified: metadata() }
|
|
const codec = createServerPollTerminalArchiveForTests({
|
|
encrypt: async value => { events.push("encrypt"); counts.encrypt++; if (state.failEncrypt) throw Error(privateText); return Buffer.concat([Buffer.alloc(padding), encrypt(value)]) },
|
|
decrypt: async value => { events.push("decrypt"); counts.decrypt++; return decrypt(value.subarray(padding)) },
|
|
proof: async (purpose, value) => { events.push("proof:" + purpose); if (state.failProof) throw Error(privateText); return proof(purpose, value) },
|
|
verifySource: async (source, now) => { events.push("verifySource"); counts.verify++; assert(Number.isSafeInteger(now) && now >= 0); assert(source.admission); if (state.failSource) throw Error(privateText); return structuredClone(state.verified) },
|
|
})
|
|
return { codec, events, counts, state }
|
|
}
|
|
const refusal = async (work: Promise<unknown>) => assert.rejects(work, (error: unknown) => {
|
|
assert(error instanceof ServerPollArchiveRefused)
|
|
assert.equal(error.automaticReplay, false)
|
|
assert.equal(String(error).includes(privateText), false)
|
|
assert.equal(JSON.stringify(error).includes(privateText), false)
|
|
return true
|
|
})
|
|
async function resign(row: ServerPollTerminalArchive): Promise<void> {
|
|
const { ciphertext: _ciphertext, archive_proof: _archive, ...fields } = row
|
|
row.archive_proof = await proof("server-poll-terminal-archive-record-v1", { ...fields, identity_kind: "mcp", storage_state: "compacted", admission_bytes: 32768, charged_rows: 2 })
|
|
}
|
|
async function replaceBinary(row: ServerPollTerminalArchive, compressed: Buffer, decoded: Buffer) {
|
|
row.ciphertext = encrypt(compressed).toString("base64")
|
|
row.compressed_bytes = compressed.length; row.ciphertext_bytes = Buffer.byteLength(row.ciphertext); row.ciphertext_sha256 = sha(row.ciphertext)
|
|
row.decoded_bytes = Math.min(decoded.length, SERVER_POLL_ARCHIVE_MAX_DECODED_BYTES)
|
|
row.source_sha256 = sha(decoded); row.source_digest = await proof("server-poll-terminal-archive-source-v1", row.source_sha256)
|
|
await resign(row)
|
|
}
|
|
|
|
test("real AES-GCM/HMAC preserves exact sourceText, opaque new raw columns, Unicode and six-digit SQL timestamps", async () => {
|
|
const f = fixture(), source = sample(), text = "\n " + JSON.stringify(source, null, 2) + " \n"
|
|
const archive = await f.codec.create(text, 1000); assert(archive)
|
|
assert.equal(archive.decoded_bytes, Buffer.byteLength(text)); assert.equal(archive.source_sha256, sha(text))
|
|
assert.equal(archive.ciphertext_bytes, Buffer.byteLength(archive.ciphertext)); assert.equal(archive.ciphertext_sha256, sha(archive.ciphertext))
|
|
assert.equal(archive.compressed_bytes, gzipSync(Buffer.from(text), { level: 6 }).length)
|
|
assert.equal(Buffer.from(archive.ciphertext, "base64").length, archive.compressed_bytes + 28)
|
|
assert.equal(archive.original_activity_at, "2026-10-29T03:24:15.123456Z"); assert.equal(archive.original_expires_at, "2026-10-29T03:25:00.000009Z")
|
|
assert.equal(f.events[0], "verifySource"); assert(f.events.indexOf("encrypt") > f.events.indexOf("verifySource"))
|
|
const charge = await f.codec.verifyCharge(archive)
|
|
assert.equal(charge.bytes, archive.ciphertext_bytes + 32768); assert.equal(charge.rows, 2)
|
|
assert.equal(f.counts.decrypt, 0, "Bounded charge verification never decrypts")
|
|
f.events.length = 0
|
|
const opened = await f.codec.open(archive, 2000)
|
|
assert.equal(opened.sourceText, text); assert.deepEqual(opened.source, source)
|
|
assert.deepEqual(Object.keys(opened).sort(), ["metadata", "source", "sourceText"])
|
|
assert.equal(f.events[0], "proof:server-poll-terminal-archive-record-v1")
|
|
assert(f.events.indexOf("decrypt") > 0); assert(f.events.indexOf("verifySource") > f.events.indexOf("decrypt"))
|
|
assert.equal(f.counts.verify, 2)
|
|
})
|
|
|
|
test("every metadata field and its MAC is authenticated before decrypt", async () => {
|
|
const f = fixture(), archive = await f.codec.create(JSON.stringify(sample()), 1); assert(archive)
|
|
const alternatives: Record<keyof Omit<ServerPollTerminalArchive, "ciphertext">, unknown> = {
|
|
version: 2, receipt_id: executionId, execution_id: receiptId, connection_id: receiptId, owner_id: "different-owner", binding_proof: "d".repeat(64), context_proof: "d".repeat(64),
|
|
source_digest: "d".repeat(64), source_sha256: "d".repeat(64), terminal_digest: "d".repeat(64), original_activity_at: "2026-10-29T03:24:15.123457Z", original_expires_at: "2026-10-29T03:25:00.000010Z",
|
|
retention_days: 31, safety_ms: 60001, decoded_bytes: archive.decoded_bytes + 1, compressed_bytes: archive.compressed_bytes + 1,
|
|
ciphertext_bytes: archive.ciphertext_bytes + 1, ciphertext_sha256: "d".repeat(64), archive_proof: "d".repeat(64),
|
|
}
|
|
for (const [field, value] of Object.entries(alternatives)) {
|
|
const altered = { ...archive, [field]: value }
|
|
await refusal(f.codec.verifyCharge(altered)); await refusal(f.codec.open(altered, 1))
|
|
}
|
|
assert.equal(f.counts.decrypt, 0); assert.equal(f.counts.verify, 1)
|
|
})
|
|
|
|
test("actual changed ciphertext digest/length or noncanonical base64 refuses before decryption", async () => {
|
|
const f = fixture(), archive = await f.codec.create(JSON.stringify(sample()), 1); assert(archive)
|
|
await refusal(f.codec.open({ ...archive, ciphertext: archive.ciphertext + "AAAA" }, 1))
|
|
await refusal(f.codec.open({ ...archive, ciphertext: archive.ciphertext.replace(/^./, value => value === "A" ? "B" : "A") }, 1))
|
|
for (const ciphertext of [archive.ciphertext + "\n", "%%%invalid-base64%%%", "AB=="]) {
|
|
const altered = { ...archive, ciphertext, ciphertext_bytes: Buffer.byteLength(ciphertext), ciphertext_sha256: sha(ciphertext) }
|
|
await resign(altered); await refusal(f.codec.open(altered, 1))
|
|
}
|
|
assert.equal(f.counts.decrypt, 0)
|
|
})
|
|
|
|
test("real AES authentication tag, gzip corruption and invalid UTF-8 cannot escape generic refusal", async () => {
|
|
for (const kind of ["tag", "gzip", "utf8"] as const) {
|
|
const f = fixture(), archive = await f.codec.create(JSON.stringify(sample()), 1); assert(archive)
|
|
if (kind === "tag") {
|
|
const binary = Buffer.from(archive.ciphertext, "base64"); binary[12] ^= 1
|
|
archive.ciphertext = binary.toString("base64"); archive.ciphertext_sha256 = sha(archive.ciphertext); await resign(archive)
|
|
} else if (kind === "gzip") await replaceBinary(archive, Buffer.from("not a gzip stream " + privateText), Buffer.from("unused"))
|
|
else { const invalid = Buffer.from([0xff, 0xfe, 0x80]); await replaceBinary(archive, gzipSync(invalid), invalid) }
|
|
await refusal(f.codec.open(archive, 1)); assert.equal(f.counts.decrypt, 1); assert.equal(f.counts.verify, 1)
|
|
}
|
|
})
|
|
|
|
test("incompressible or oversized encoded archives retain full history with undefined", async () => {
|
|
const incompressible = sample(); incompressible.admission.opaque = randomBytes(180000).toString("base64")
|
|
const f = fixture(); assert.equal(await f.codec.create(JSON.stringify(incompressible), 1), undefined)
|
|
assert.equal(f.counts.verify, 1)
|
|
const padded = fixture(12000), source = sample(); source.admission.opaque = randomBytes(90000).toString("base64")
|
|
const text = JSON.stringify(source), compressed = gzipSync(Buffer.from(text), { level: 6 })
|
|
assert(compressed.length < 96 * 1024 - 256)
|
|
assert(Buffer.byteLength(Buffer.concat([Buffer.alloc(12000), encrypt(compressed)]).toString("base64")) > SERVER_POLL_ARCHIVE_MAX_BYTES)
|
|
assert.equal(await padded.codec.create(text, 1), undefined); assert.equal(padded.counts.encrypt, 1)
|
|
})
|
|
|
|
test("authenticated compressed bomb stops at the16MiB decode cap before source verification", async () => {
|
|
const f = fixture(), archive = await f.codec.create(JSON.stringify(sample()), 1); assert(archive)
|
|
const bomb = Buffer.alloc(SERVER_POLL_ARCHIVE_MAX_DECODED_BYTES + 1, "x")
|
|
await replaceBinary(archive, gzipSync(bomb, { level: 6 }), bomb)
|
|
assert(archive.compressed_bytes < 96 * 1024 - 256)
|
|
await refusal(f.codec.open(archive, 1)); assert.equal(f.counts.decrypt, 1); assert.equal(f.counts.verify, 1)
|
|
await refusal(f.codec.create("x".repeat(SERVER_POLL_ARCHIVE_MAX_DECODED_BYTES + 1), 1))
|
|
})
|
|
|
|
test("UTC normalization retains microseconds and rejects invalid calendars/noncanonical timestamps", () => {
|
|
assert.equal(canonicalPollArchiveTimestamp("2024-02-29T12:34:56.123456+00:00"), "2024-02-29T12:34:56.123456Z")
|
|
assert.equal(canonicalPollArchiveTimestamp("2026-10-29T03:24:15.1Z"), "2026-10-29T03:24:15.100000Z")
|
|
for (const invalid of ["2026-02-29T00:00:00Z", "2026-02-30T00:00:00Z", "2026-04-31T00:00:00Z", "2026-10-29T24:00:00Z", "2026-10-29T00:00:60Z",
|
|
"2026-10-29T00:00:00.1234567Z", "2026-10-29 00:00:00Z", "2026-10-29T00:00:00+07:00", "2026-10-29", "2026-10-29T00:00:00Z ", "1969-12-31T23:59:59Z", 0, new Date()])
|
|
assert.throws(() => canonicalPollArchiveTimestamp(invalid), ServerPollArchiveRefused)
|
|
})
|
|
|
|
test("source validator must accept both seal and open; changed authenticated source metadata cannot be promoted", async () => {
|
|
const denied = fixture(); denied.state.failSource = true
|
|
await refusal(denied.codec.create(JSON.stringify(sample()), 1)); assert.equal(denied.counts.encrypt, 0)
|
|
const f = fixture(), archive = await f.codec.create(JSON.stringify(sample()), 1); assert(archive)
|
|
f.state.failSource = true; await refusal(f.codec.open(archive, 1))
|
|
f.state.failSource = false; f.state.verified.retentionDays++
|
|
await refusal(f.codec.open(archive, 1))
|
|
assert.equal(f.counts.verify, 3)
|
|
})
|
|
|
|
test("crypto/proof failures expose no private payload; invalid clock never reaches crypto/source", async () => {
|
|
for (const mode of ["failEncrypt", "failProof"] as const) {
|
|
const f = fixture(); f.state[mode] = true; await refusal(f.codec.create(JSON.stringify(sample()), 1))
|
|
}
|
|
const f = fixture(), archive = await f.codec.create(JSON.stringify(sample()), 1); assert(archive)
|
|
f.events.length = 0
|
|
for (const invalid of [NaN, Infinity, -1, 1.5, Number.MAX_SAFE_INTEGER + 1]) {
|
|
await refusal(f.codec.create(JSON.stringify(sample()), invalid)); await refusal(f.codec.open(archive, invalid))
|
|
}
|
|
assert.deepEqual(f.events, [])
|
|
})
|