{"record":{"id":"21b0324a1e0116a9","repo":"musistudio/claude-code-router","slug":"the-ccr-artifact-content-did-not-match-its-declare","errorCode":null,"errorMessage":"The CCR artifact content did not match its declared media type.","messagePattern":"The CCR artifact content did not match its declared media type\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/agents/codex/media-preview-bridge.ts","lineNumber":337,"sourceCode":"  const chunks: Buffer[] = [];\n  let total = 0;\n  while (true) {\n    const part = await reader.read();\n    if (part.done) break;\n    if (!part.value?.byteLength) continue;\n    total += part.value.byteLength;\n    if (total > maxBytes) {\n      await reader.cancel();\n      throw new Error(\"The CCR media artifact exceeds the inline preview size limit.\");\n    }\n    chunks.push(Buffer.from(part.value));\n  }\n  if (!total) throw new Error(\"The CCR artifact response was empty.\");\n  if (declaredLength && total !== declaredLength) throw new Error(\"The CCR artifact response length did not match its headers.\");\n  const bytes = Buffer.concat(chunks, total);\n  const detectedMimeType = detectMediaMimeType(bytes);\n  if (!detectedMimeType || mediaKind(detectedMimeType) !== declaredKind) {\n    throw new Error(\"The CCR artifact content did not match its declared media type.\");\n  }\n  return { bytes, mimeType: detectedMimeType };\n}\n\nfunction detectMediaMimeType(buffer: Buffer): string | undefined {\n  if (buffer.byteLength >= 8 && buffer.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) return \"image/png\";\n  if (buffer.byteLength >= 3 && buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) return \"image/jpeg\";\n  if (buffer.byteLength >= 12 && buffer.subarray(0, 4).toString(\"ascii\") === \"RIFF\" && buffer.subarray(8, 12).toString(\"ascii\") === \"WEBP\") return \"image/webp\";\n  if (buffer.byteLength >= 12 && buffer.subarray(4, 8).toString(\"ascii\") === \"ftyp\") {\n    const brand = buffer.subarray(8, 12).toString(\"ascii\");\n    if ([\"avif\", \"avis\", \"mif1\", \"msf1\"].includes(brand)) return \"image/avif\";\n    return \"video/mp4\";\n  }\n  if (buffer.byteLength >= 4 && buffer.subarray(0, 4).equals(Buffer.from([0x1a, 0x45, 0xdf, 0xa3]))) return \"video/webm\";\n  return undefined;\n}\n\nfunction mediaKind(mimeType: string): \"image\" | \"video\" | undefined {","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/musistudio/claude-code-router/blob/99f24806c6a2c660b16e53e95211c517448a6c90/packages/core/src/agents/codex/media-preview-bridge.ts#L319-L355","documentation":"A CCR (Codex Chat Representation) media artifact was downloaded, but sniffing the byte content (magic-number detection) either failed to identify a known media type or produced a type whose kind (image/audio/video/etc.) differs from the kind declared in the artifact metadata. The library validates artifact integrity by comparing detectedMediaMimeType's kind against declaredKind and rejects mismatches to prevent rendering garbage or mis-typed payloads.","triggerScenarios":"Calling loadCodexMediaArtifact for a CCR artifact whose declared media kind (e.g. image) does not match the actual bytes (e.g. an HTML error page, truncated download, or a format detectMediaMimeType doesn't recognize); also triggered by corrupted/empty-ish buffers that pass the length check but fail magic-byte sniffing.","commonSituations":"Proxy or gateway returns an HTML/text error body with 200 status; artifact truncated by streaming bug; new/unsupported media format not in the magic-number table; wrong artifact ID mapping to different content.","solutions":["Verify the artifact bytes directly (download and run `file`/`xxd` on them) to see what the payload actually is","Check that the declared media type in the CCR artifact metadata matches the real file kind; fix the producer if mismatched","If the format is legitimate but unsupported, extend detectMediaMimeType with the appropriate magic bytes and upstream a patch","If an interceptor/proxy is rewriting responses, bypass it or fix it so raw artifact bytes flow through"],"exampleFix":"// before\nconst { bytes } = await loadCodexMediaArtifact(artifact);\n\n// after\nif (mediaKind(artifact.mimeType) !== 'image' && mediaKind(artifact.mimeType) !== 'audio') {\n  throw new TypeError(`Unsupported declared kind: ${artifact.mimeType}`);\n}\nconst { bytes } = await loadCodexMediaArtifact(artifact);","handlingStrategy":"validation","validationCode":"const kind = mediaKind(artifact.mimeType);\nif (!kind) throw new TypeError(`Unknown declared media type: ${artifact.mimeType}`);\n// optionally pre-fetch a HEAD/first bytes to sanity check","typeGuard":"function isProbablyMediaKind(buf: Buffer, declared: string): boolean {\n  const detected = detectMediaMimeType(buf);\n  return !!detected && mediaKind(detected) === mediaKind(declared);\n}","tryCatchPattern":"try { const m = await loadCodexMediaArtifact(a); } catch (e) { if (String(e).includes('did not match its declared media type')) { /* re-fetch artifact or flag upstream producer */ } throw e; }","preventionTips":["Validate artifact.mimeType against an allowlist before loading","Log detected vs declared types on failure to pinpoint producer bugs","Keep detectMediaMimeType's magic table current when adding new formats"],"tags":["media","artifact","validation","content-sniffing"],"backgroundTag":"content-type-mismatch","analyzedSha":"99f24806c6a2c660b16e53e95211c517448a6c90","analyzedAt":"2026-08-27T04:11:01.184Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}