{"record":{"id":"7e01491253fe9696","repo":"gchq/CyberChef","slug":"invalid-file-type-7e0149","errorCode":null,"errorMessage":"Invalid file type.","messagePattern":"Invalid file type\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/GenerateImage.mjs","lineNumber":196,"sourceCode":"            const result = await image.getBuffer(JimpMime.png);\n            return result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength);\n        } catch (err) {\n            throw new OperationError(`Error generating image. (${err})`);\n        }\n    }\n\n    /**\n     * Displays the generated image using HTML for web apps\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 GenerateImage;\n","sourceCodeStart":178,"sourceCodeEnd":204,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/GenerateImage.mjs#L178-L204","documentation":"Thrown by GenerateImage.present() when isImage() cannot identify the generated buffer as a known image format. present() runs on the output of run() (which produces a PNG), so this indicates the PNG buffer was not recognized — implying the encoding step produced invalid or empty data even though it did not throw.","triggerScenarios":"run() returned a buffer that is not a valid PNG (truncated, wrong magic bytes), or present() is called directly with arbitrary non-image data. A zero-length buffer is short-circuited earlier, so this is for non-empty-but-unrecognized data.","commonSituations":"Chaining GenerateImage output through another operation that corrupts bytes before present() runs, or calling present() on hand-supplied data in a custom recipe.","solutions":["Ensure present() receives the direct ArrayBuffer output of run() without intermediate corruption.","If the buffer is empty, note that present() returns '' for empty data rather than throwing.","Verify the upstream run() produced valid PNG bytes by checking the magic header (\\x89PNG)."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const PNG_MAGIC = [0x89, 0x50, 0x4e, 0x47];\nconst isPng = data.length >= 4 && PNG_MAGIC.every((b, i) => data[i] === b);\nif (!isPng) {\n  // do not pass to present(); the buffer is not a valid PNG\n}","typeGuard":"function looksLikePng(buf) {\n  return buf.length >= 4 && buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47;\n}","tryCatchPattern":null,"preventionTips":["Feed present() only the direct ArrayBuffer output of run().","Avoid chaining byte-altering operations between run() and present()."],"tags":["image","present","file-type","runtime"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}