{"record":{"id":"f72643b87da947e9","repo":"JuliusBrussee/caveman","slug":"pubkey-json-is-malformed","errorCode":null,"errorMessage":"--pubkey JSON is malformed","messagePattern":"--pubkey JSON is malformed","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":17889,"sourceCode":"  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\");\n  return { keys, trust: \"pinned_keyring\" };\n}\n\n// receiptsVerify validates a signed receipt bundle offline (no network). A raw\n// --pubkey pins the current key; JSON may independently pin a full rotation\n// keyring. Without either, embedded keys prove self-consistency, not publisher\n// authenticity. Exits non-zero on any included content, signature, or\n// scope-chain break. Tail/scope omission needs separately trusted head manifest;\n// bundle output states completeness is not attested.\n//   caveman receipts verify <bundle.json> [--pubkey <file>]\nasync function receiptsVerify(argv: string[]) {\n  const file = positionalAfterOptions(argv.slice(1), new Set([\"--pubkey\"]));\n  if (!file) throw new Error(`usage: ${invokedCommand(\"receipts\")} verify <bundle.json> [--pubkey <file>]`);","sourceCodeStart":17871,"sourceCodeEnd":17907,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L17871-L17907","documentation":"If the --pubkey file content starts with `{`, pinnedReceiptKeys() parses it as JSON. Any JSON.parse failure (truncated file, trailing commas, wrong encoding) is reported as '--pubkey JSON is malformed' rather than crashing with a raw parse error.","triggerScenarios":"`caveman receipts verify bundle.json --pubkey keys.json` where keys.json does not contain valid JSON (empty file, concatenated objects, BOM, truncation).","commonSituations":"Saving a keyring via a shell redirect that mangled it, copying partial JSON from a browser or chat, or passing a binary key file that happens to start with '{'.","solutions":["Validate the file with `jq . keys.json` and fix the JSON syntax","Re-download/re-copy the keyring file in full","If you meant to pin a single key, pass the raw key string instead of JSON"],"exampleFix":"# before (truncated)\n{\"public_keys\":[{\"key_id\":\"k1\"\n# after\n{\"public_keys\":[{\"key_id\":\"k1\",\"key\":\"AAA\"}],\"public_key\":{\"key_id\":\"k1\",\"key\":\"AAA\"}}","handlingStrategy":"validation","validationCode":"let parsed: unknown;\ntry { parsed = JSON.parse(fs.readFileSync(pubkeyPath, 'utf8')); } catch { throw new Error('--pubkey JSON is malformed'); }","typeGuard":"const isKeyringJson = (v: unknown): v is { public_key?: ReceiptPublicKey; public_keys?: ReceiptPublicKey[] } =>\n  typeof v === 'object' && v !== null;\nconst ok = isKeyringJson(JSON.parse(src));","tryCatchPattern":"try { await verify(argv) } catch (e) { if (e.message === '--pubkey JSON is malformed') { console.error('Run: jq . keys.json  to locate the syntax error'); process.exit(1); } throw e }","preventionTips":["Validate keyring files with jq or a JSON linter before use","Copy keyrings with checksums (sha256sum) to detect truncation","Avoid shell heredocs/redirects that can mangle JSON"],"tags":["json","cli","parse-error","pubkey"],"backgroundTag":"json-parse-error","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"}