{"record":{"id":"80448eed77627972","repo":"JuliusBrussee/caveman","slug":"v2-public-keys-must-include-public-key","errorCode":null,"errorMessage":"v2 public_keys must include public_key","messagePattern":"v2 public_keys must include public_key","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":17876,"sourceCode":"  for (const [index, info] of infos.entries()) {\n    const decoded = decodeReceiptKey(info, `${label}[${index}]`);\n    if (keys.has(decoded.info.key_id)) throw new Error(`${label} contains duplicate key_id ${decoded.info.key_id}`);\n    keys.set(decoded.info.key_id, decoded);\n  }\n  return keys;\n}\n\nfunction embeddedReceiptKeys(bundle: ReceiptBundle): { current: DecodedReceiptKey; keys: Map<string, DecodedReceiptKey> } {\n  if (bundle.schema !== RECEIPT_BUNDLE_V1 && bundle.schema !== RECEIPT_BUNDLE_V2) throw new Error(`unsupported bundle schema ${String(bundle.schema)}`);\n  if (bundle.verification_coverage !== undefined && bundle.verification_coverage !== INCLUDED_RECEIPTS_ONLY) throw new Error(`unsupported unsigned verification coverage ${String(bundle.verification_coverage)}`);\n  if (bundle.completeness_attested === true) throw new Error(\"bundle completeness cannot be attested by unsigned export metadata\");\n  const current = decodeReceiptKey(bundle.public_key, \"public_key\");\n  if (bundle.public_keys !== undefined && !Array.isArray(bundle.public_keys)) throw new Error(\"public_keys must be an array\");\n  if (bundle.schema === RECEIPT_BUNDLE_V2 && (!Array.isArray(bundle.public_keys) || bundle.public_keys.length === 0)) throw new Error(\"v2 bundle requires public_keys\");\n  const keys = decodeUniqueKeyring(bundle.public_keys ?? [], \"public_keys\");\n  const currentInRing = keys.get(current.info.key_id);\n  if (currentInRing && !currentInRing.raw.equals(current.raw)) throw new Error(`public_key conflicts with public_keys entry ${current.info.key_id}`);\n  if (bundle.schema === RECEIPT_BUNDLE_V2 && !currentInRing) throw new Error(\"v2 public_keys must include public_key\");\n  if (!currentInRing) keys.set(current.info.key_id, current);\n  return { current, keys };\n}\n\nasync function pinnedReceiptKeys(file: string, current: DecodedReceiptKey): Promise<{ keys: Map<string, DecodedReceiptKey>; trust: string }> {\n  const source = (await readFile(file, \"utf8\")).trim();\n  if (!source.startsWith(\"{\")) {\n    const pinned = decodeReceiptKey({ ...current.info, key: source }, \"--pubkey\");\n    if (!pinned.raw.equals(current.raw)) throw new Error(\"bundle public key does not match the published --pubkey\");\n    return { keys: new Map([[current.info.key_id, pinned]]), trust: \"pinned_public_key\" };\n  }\n  let parsed: { public_key?: ReceiptPublicKey; public_keys?: ReceiptPublicKey[] };\n  try { parsed = JSON.parse(source); } catch { throw new Error(\"--pubkey JSON is malformed\"); }\n  const infos = Array.isArray(parsed.public_keys) ? parsed.public_keys : parsed.public_key ? [parsed.public_key] : [];\n  if (infos.length === 0) throw new Error(\"--pubkey JSON must contain public_key or public_keys\");\n  const keys = decodeUniqueKeyring(infos, \"--pubkey public_keys\");\n  const pinnedCurrent = keys.get(current.info.key_id);\n  if (!pinnedCurrent || !pinnedCurrent.raw.equals(current.raw)) throw new Error(\"trusted --pubkey keyring does not contain the bundle public key\");","sourceCodeStart":17858,"sourceCodeEnd":17894,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L17858-L17894","documentation":"For v2 bundles, the `public_keys` keyring must contain the exact key that appears in `public_key` — the current signing key. If the keyring is valid and non-empty but does not include the current key's key_id, embeddedReceiptKeys() rejects the bundle.","triggerScenarios":"Verifying a v2 bundle whose `public_keys` array lists only old rotation keys and omits the key currently in `public_key`.","commonSituations":"Exporting after a key rotation but with a stale keyring snapshot, or hand-assembling the keyring from an old config.","solutions":["Add the current `public_key` object to the bundle's `public_keys` array","Re-export the bundle so the keyring includes the active key","Downgrade the schema to v1 if only single-key verification is intended"],"exampleFix":"// before\n{\"schema\":\"...v2\",\"public_key\":{\"key_id\":\"k2\",...},\"public_keys\":[{\"key_id\":\"k1\",...}]}\n// after\n{\"schema\":\"...v2\",\"public_key\":{\"key_id\":\"k2\",...},\"public_keys\":[{\"key_id\":\"k1\",...},{\"key_id\":\"k2\",...}]}","handlingStrategy":"validation","validationCode":"if (bundle.schema === 'caveman.receipt-bundle/v2' && !bundle.public_keys.some(k => k.key_id === bundle.public_key.key_id)) throw new Error('keyring missing current key');","typeGuard":"const ringIncludesCurrent = (b: ReceiptBundle): boolean =>\n  Array.isArray(b.public_keys) && b.public_keys.some(k => k.key_id === b.public_key.key_id);","tryCatchPattern":"try { verifyBundle(bundle) } catch (e) { if (e.message === 'v2 public_keys must include public_key') { /* request refreshed bundle after rotation */ } else throw e }","preventionTips":["After key rotation, re-export bundles rather than patching keyrings","Publish keyring and bundle atomically","Include the current key first in the keyring when exporting"],"tags":["validation","receipt-bundle","keyring","cli"],"backgroundTag":"schema-validation-failed","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}