UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

158 lines • 6.78 kB
/** * The anchored key-facts reader (custody design 3.1 "Read path", 15.10). * * Facts and revocations come from the key-facts verifier as verifier-signed statements * (`signed-statement.ts`) under a key this release pins. The endpoint is transport only: a TLS interposer can * withhold or replay bytes, never forge them, and a replay fails the nonce echo. Every failure is `"unknown"`. */ import { randomBytes } from "node:crypto"; import { performance } from "node:perf_hooks"; import { hasExactKeys, isRecord, verifySignedStatement, } from "./signed-statement.js"; import { acceptedRevocations, isKeyRevoked, isVouchRevoked, parseRevocationsStatement, readVerifierRevocations, recordVerifierRevocations, } from "./verifier-revocations.js"; export const MAX_FACTS_AGE_MS = 120_000; export const MAX_FACTS_FUTURE_SKEW_MS = 30_000; const MAX_STATEMENT_BYTES = 64 * 1024; const STATEMENT_TIMEOUT_MS = 10_000; const ORIGINS = ["AWS_KMS", "EXTERNAL", "AWS_CLOUDHSM", "EXTERNAL_KEY_STORE"]; const KEY_USAGES = ["KEY_AGREEMENT", "SIGN_VERIFY", "ENCRYPT_DECRYPT"]; const KEY_MANAGERS = ["CUSTOMER", "AWS"]; const FACT_KEYS = [ "spki", "origin", "keySpec", "keyUsage", "keyManager", "keyState", "multiRegion", "tags", "policy", "grantCount", ]; const STATEMENT_KEYS = ["v", "kind", "arn", "nonce", "readAt", "facts", "verifierKeyId"]; const isCount = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0; const oneOf = (values, value) => values.some((candidate) => candidate === value); function parseTags(value) { if (!isRecord(value)) return undefined; const tags = {}; for (const [key, tag] of Object.entries(value)) { if (typeof tag !== "string") return undefined; tags[key] = tag; } return tags; } function parseFacts(fields, readAt, verifierKeyId) { const { facts } = fields; if (!isRecord(facts) || !hasExactKeys(facts, FACT_KEYS)) return undefined; const { spki, origin, keySpec, keyUsage, keyManager, keyState, multiRegion, tags, policy, grantCount } = facts; const parsedTags = parseTags(tags); if (typeof spki !== "string" || typeof keySpec !== "string" || typeof keyState !== "string") return undefined; if (!oneOf(ORIGINS, origin) || !oneOf(KEY_USAGES, keyUsage) || !oneOf(KEY_MANAGERS, keyManager)) return undefined; if (typeof multiRegion !== "boolean" || typeof policy !== "string" || !isCount(grantCount) || !parsedTags) return undefined; return { spki, origin, keySpec, keyUsage, keyManager, keyState, multiRegion, tags: parsedTags, policy, grantCount, readAt, verifierKeyId, }; } async function boundedBody(response) { if (!response.body) return undefined; const reader = response.body.getReader(); const chunks = []; let size = 0; for (;;) { const { done, value } = await reader.read(); if (done) break; size += value.byteLength; if (size > MAX_STATEMENT_BYTES) { await reader.cancel(); return undefined; } chunks.push(value); } return Buffer.concat(chunks).toString("utf8"); } export function anchoredKeyFactsReader(options) { const now = options.now ?? Date.now; const monoNow = options.monoNow ?? (() => performance.now()); const request = options.fetch ?? fetch; const pins = options.verifierKeys; let freshness; async function get(path, nonce) { if (options.endpoint === undefined || pins.length === 0) return undefined; const sent = monoNow(); try { const response = await request(`${options.endpoint}${path}?nonce=${nonce}`, { redirect: "manual", signal: AbortSignal.timeout(STATEMENT_TIMEOUT_MS), }); const type = response.headers.get("content-type") ?? ""; if (response.status !== 200 || !/^application\/json(;|$)/.test(type)) return undefined; const body = await boundedBody(response); return monoNow() - sent < STATEMENT_TIMEOUT_MS ? body : undefined; } catch { return undefined; } } async function refreshRevocations() { const nonce = randomBytes(16).toString("base64url"); const fetchedMono = monoNow(); const body = await get("/v1/revocations", nonce); const held = await readVerifierRevocations(options.revocationsPath); if (body === undefined || held.state !== "ok") return false; const verified = await verifySignedStatement(body, pins, (pin) => !isKeyRevoked(held.revocations, pin.keyId)); const statement = verified && parseRevocationsStatement(verified.fields, nonce); if (!verified || !statement) return false; const accepted = acceptedRevocations(statement.revocations, verified.signer, pins, held.revocations); if (!(await recordVerifierRevocations(options.revocationsPath, accepted))) return false; freshness = { issuedAt: statement.issuedAt, fetchedMono }; return true; } async function describe(keyArn) { const nonce = randomBytes(16).toString("base64url"); const body = await get(`/v1/keys/${encodeURIComponent(keyArn)}`, nonce); const held = await readVerifierRevocations(options.revocationsPath); if (body === undefined || held.state !== "ok") return "unknown"; const verified = await verifySignedStatement(body, pins, (pin) => !isKeyRevoked(held.revocations, pin.keyId)); if (!verified || !hasExactKeys(verified.fields, STATEMENT_KEYS)) return "unknown"; const { kind, arn, readAt } = verified.fields; if (kind !== "facts" || arn !== keyArn || verified.fields.nonce !== nonce || !isCount(readAt)) return "unknown"; const at = now(); if (at - readAt > MAX_FACTS_AGE_MS || readAt - at > MAX_FACTS_FUTURE_SKEW_MS) return { refusal: "facts-stale", skewMs: at - readAt }; const facts = parseFacts(verified.fields, readAt, verified.signer.keyId); if (!facts || facts.keyState !== "Enabled" || !(await refreshRevocations())) return "unknown"; const after = await readVerifierRevocations(options.revocationsPath); if (after.state !== "ok" || isVouchRevoked(after.revocations, facts.verifierKeyId, readAt)) return "unknown"; return facts; } return { describe, refreshRevocations, revocationFreshness: () => freshness }; } //# sourceMappingURL=key-facts-client.js.map