{"record":{"id":"25800f64fda8223f","repo":"paperclipai/paperclip","slug":"invalid-heif-box-length","errorCode":null,"errorMessage":"Invalid HEIF box length","messagePattern":"Invalid HEIF box length","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/src/services/photon/media.ts","lineNumber":30,"sourceCode":"  \"image/heif-sequence\",\n]);\n\n/** Validate bounded ISO-BMFF structure before invoking any native decoder. */\nexport function validateHeifDimensions(body: Buffer): void {\n  let boxes = 0;\n  let dimensions = 0;\n  let totalPixels = 0;\n  let branded = false;\n  const visit = (start: number, end: number, depth: number) => {\n    if (depth > 8) throw new Error(\"HEIF metadata nesting is too deep\");\n    for (let at = start; at < end; ) {\n      if (++boxes > 4096 || end - at < 8)\n        throw new Error(\"Invalid HEIF box structure\");\n      let size = body.readUInt32BE(at);\n      const type = body.toString(\"ascii\", at + 4, at + 8);\n      let header = 8;\n      if (size === 1) {\n        if (end - at < 16) throw new Error(\"Invalid HEIF box length\");\n        const extended = body.readBigUInt64BE(at + 8);\n        if (extended > BigInt(body.length))\n          throw new Error(\"HEIF box exceeds file bounds\");\n        size = Number(extended);\n        header = 16;\n      } else if (size === 0) size = end - at;\n      if (size < header || at + size > end)\n        throw new Error(\"HEIF box exceeds file bounds\");\n      const content = at + header;\n      if (type === \"ftyp\") {\n        if (size < header + 8) throw new Error(\"HEIF file type is missing\");\n        const brands = body.toString(\"ascii\", content, at + size);\n        branded = /heic|heix|hevc|hevx|mif1|msf1/.test(brands);\n      } else if (type === \"ispe\") {\n        if (size !== header + 12)\n          throw new Error(\"Invalid HEIF image dimensions\");\n        const width = body.readUInt32BE(content + 4);\n        const height = body.readUInt32BE(content + 8);","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/paperclipai/paperclip/blob/3f1d897a7c018d76563a21c6e39c3c9b03933622/server/src/services/photon/media.ts#L12-L48","documentation":"When an ISO-BMFF box uses the 64-bit extended size encoding (32-bit size field === 1), the walker requires at least 16 bytes (8-byte header + 8-byte largesize) to be present. If fewer remain it throws \"Invalid HEIF box length\" — the box header claims an extended size that cannot fit in the buffer.","triggerScenarios":"A box with size field 1 located fewer than 16 bytes from the end of the current container range (media.ts:29-30), typically a truncated or hand-crafted buffer.","commonSituations":"Truncated HEIC download where the last box header was cut; maliciously crafted file with a size===1 box at the tail; byte-level corruption flipping a normal size field to 1.","solutions":["Obtain an intact copy of the image and re-upload; the file is structurally truncated.","Check the transfer path (proxy, resumable upload) that cut the file short and fix it.","Validate locally with `ffprobe`/`mp4dump` before submitting to confirm box integrity.","Do not bypass: a size===1 box near EOF is invalid per ISO 14496-12."],"exampleFix":"// before: uploading partial file.heic (10 bytes left, size==1 box)\n// after: re-download and verify\nffprobe file.heic  # must parse cleanly before upload","handlingStrategy":"validation","validationCode":"// ensure no size==1 box sits within the last 16 bytes\nfunction noTruncatedExtendedBox(buf: Buffer): boolean {\n  for (let at = 0; at + 8 <= buf.length; ) {\n    const size = buf.readUInt32BE(at);\n    if (size === 1 && buf.length - at < 16) return false;\n    if (size === 0) break;\n    if (size < 8) return false;\n    at += size;\n  }\n  return true;\n}","typeGuard":"function hasCompleteHeader(b: Buffer, at: number): boolean {\n  const size = b.readUInt32BE(at);\n  return size !== 1 || b.length - at >= 16;\n}","tryCatchPattern":"try {\n  validateHeifDimensions(body);\n} catch (e) {\n  if (e instanceof Error && e.message === \"Invalid HEIF box length\") {\n    return rejectUpload(\"File appears truncated; re-download and retry\");\n  }\n  throw e;\n}","preventionTips":["Use resumable/integrity-checked transfers so partial files never reach validation.","Compare uploaded byte length against expected size when the source provides it.","Run ffprobe on files client-side before upload where possible.","Never hand-edit binary media; re-export instead."],"tags":["heif","isobmff","corrupt-file","input-validation"],"backgroundTag":"invalid-argument-format","analyzedSha":"3f1d897a7c018d76563a21c6e39c3c9b03933622","analyzedAt":"2026-09-18T08:03:59.046Z","contentChangedAt":"2026-09-18T08:03:59.046Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}