JuliusBrussee/caveman · error

--pubkey JSON is malformed

Error message

--pubkey JSON is malformed

What it means

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.

Solutions

  1. Validate the file with `jq . keys.json` and fix the JSON syntax
  2. Re-download/re-copy the keyring file in full
  3. If you meant to pin a single key, pass the raw key string instead of JSON

Example fix

# before (truncated)
{"public_keys":[{"key_id":"k1"
# after
{"public_keys":[{"key_id":"k1","key":"AAA"}],"public_key":{"key_id":"k1","key":"AAA"}}
Defensive patterns

Strategy: validation

Validate before calling

let parsed: unknown;
try { parsed = JSON.parse(fs.readFileSync(pubkeyPath, 'utf8')); } catch { throw new Error('--pubkey JSON is malformed'); }

Type guard

const isKeyringJson = (v: unknown): v is { public_key?: ReceiptPublicKey; public_keys?: ReceiptPublicKey[] } =>
  typeof v === 'object' && v !== null;
const ok = isKeyringJson(JSON.parse(src));

Try / catch

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 }

Prevention

When it happens

Trigger: `caveman receipts verify bundle.json --pubkey keys.json` where keys.json does not contain valid JSON (empty file, concatenated objects, BOM, truncation).

Common situations: 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 '{'.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/f72643b87da947e9. Report an issue: GitHub.

Appendix: source

Thrown at packages/cli/src/index.ts:17889

  if (bundle.public_keys !== undefined && !Array.isArray(bundle.public_keys)) throw new Error("public_keys must be an array");
  if (bundle.schema === RECEIPT_BUNDLE_V2 && (!Array.isArray(bundle.public_keys) || bundle.public_keys.length === 0)) throw new Error("v2 bundle requires public_keys");
  const keys = decodeUniqueKeyring(bundle.public_keys ?? [], "public_keys");
  const currentInRing = keys.get(current.info.key_id);
  if (currentInRing && !currentInRing.raw.equals(current.raw)) throw new Error(`public_key conflicts with public_keys entry ${current.info.key_id}`);
  if (bundle.schema === RECEIPT_BUNDLE_V2 && !currentInRing) throw new Error("v2 public_keys must include public_key");
  if (!currentInRing) keys.set(current.info.key_id, current);
  return { current, keys };
}

async function pinnedReceiptKeys(file: string, current: DecodedReceiptKey): Promise<{ keys: Map<string, DecodedReceiptKey>; trust: string }> {
  const source = (await readFile(file, "utf8")).trim();
  if (!source.startsWith("{")) {
    const pinned = decodeReceiptKey({ ...current.info, key: source }, "--pubkey");
    if (!pinned.raw.equals(current.raw)) throw new Error("bundle public key does not match the published --pubkey");
    return { keys: new Map([[current.info.key_id, pinned]]), trust: "pinned_public_key" };
  }
  let parsed: { public_key?: ReceiptPublicKey; public_keys?: ReceiptPublicKey[] };
  try { parsed = JSON.parse(source); } catch { throw new Error("--pubkey JSON is malformed"); }
  const infos = Array.isArray(parsed.public_keys) ? parsed.public_keys : parsed.public_key ? [parsed.public_key] : [];
  if (infos.length === 0) throw new Error("--pubkey JSON must contain public_key or public_keys");
  const keys = decodeUniqueKeyring(infos, "--pubkey public_keys");
  const pinnedCurrent = keys.get(current.info.key_id);
  if (!pinnedCurrent || !pinnedCurrent.raw.equals(current.raw)) throw new Error("trusted --pubkey keyring does not contain the bundle public key");
  return { keys, trust: "pinned_keyring" };
}

// receiptsVerify validates a signed receipt bundle offline (no network). A raw
// --pubkey pins the current key; JSON may independently pin a full rotation
// keyring. Without either, embedded keys prove self-consistency, not publisher
// authenticity. Exits non-zero on any included content, signature, or
// scope-chain break. Tail/scope omission needs separately trusted head manifest;
// bundle output states completeness is not attested.
//   caveman receipts verify <bundle.json> [--pubkey <file>]
async function receiptsVerify(argv: string[]) {
  const file = positionalAfterOptions(argv.slice(1), new Set(["--pubkey"]));
  if (!file) throw new Error(`usage: ${invokedCommand("receipts")} verify <bundle.json> [--pubkey <file>]`);

View on GitHub (pinned to 3ee70a1026)