{"record":{"id":"666441af2f92defe","repo":"gchq/CyberChef","slug":"invalid-file-type-666441","errorCode":null,"errorMessage":"Invalid file type.","messagePattern":"Invalid file type\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ConvertImageFormat.mjs","lineNumber":130,"sourceCode":"            return buffer.buffer;\n        } catch (err) {\n            throw new OperationError(`Error converting image format. (${err})`);\n        }\n    }\n\n    /**\n     * Displays the converted image using HTML for web apps\n     *\n     * @param {ArrayBuffer} data\n     * @returns {html}\n     */\n    present(data) {\n        if (!data.byteLength) return \"\";\n        const dataArray = new Uint8Array(data);\n\n        const type = isImage(dataArray);\n        if (!type) {\n            throw new OperationError(\"Invalid file type.\");\n        }\n\n        return `<img src=\"data:${type};base64,${toBase64(dataArray)}\">`;\n    }\n}\n\nexport default ConvertImageFormat;\n","sourceCodeStart":112,"sourceCodeEnd":138,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ConvertImageFormat.mjs#L112-L138","documentation":"present() renders the converted output as an <img> for the web UI and re-validates it with isImage() before base64-encoding. If the converted buffer is not recognized as an image, `if (!type)` throws 'Invalid file type.' Reaching here means the transcode produced bytes isImage cannot classify.","triggerScenarios":"Target format produced a container/mime outside isImage's recognized set; encode produced empty/garbage bytes; output mime not in the detector table.","commonSituations":"Converting to an obscure format; partial/failed encode that still returned a buffer; detector/table mismatch between input and output.","solutions":["Convert to a standard format (PNG/JPEG) that isImage reliably recognizes.","Inspect the output with Detect File Type to see the actual mime.","In the Node API, consume the ArrayBuffer directly if display isn't required."],"exampleFix":"// before: converting to an unrecognized target, present() fails\n// after: target PNG so output is reliably detected\nrecipe: [Convert Image Format (PNG)]","handlingStrategy":"type-guard","validationCode":"import { isImage } from \"src/core/lib/FileType.mjs\";\nconst bytes = new Uint8Array(outputBuffer);\nif (!isImage(bytes)) {\n  throw new Error(\"Converted output is not a recognized image; target a standard format.\");\n}","typeGuard":"import { isImage } from \"src/core/lib/FileType.mjs\";\nfunction isRecognizedOutput(buf) {\n  return isImage(buf instanceof ArrayBuffer ? new Uint8Array(buf) : buf) !== false;\n}","tryCatchPattern":null,"preventionTips":["Target PNG or JPEG so the converted output is reliably recognized.","Inspect output with Detect File Type if present() fails.","In the Node API, consume the ArrayBuffer directly and skip present()."],"tags":["cyberchef","image","jimp","file-type","present","operation-error"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}