{"record":{"id":"49193a654402155e","repo":"gchq/CyberChef","slug":"invalid-file-type-49193a","errorCode":null,"errorMessage":"Invalid file type.","messagePattern":"Invalid file type\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/InvertImage.mjs","lineNumber":78,"sourceCode":"            }\n            return imageBuffer.buffer;\n        } catch (err) {\n            throw new OperationError(`Error inverting image. (${err})`);\n        }\n    }\n\n    /**\n     * Displays the inverted 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 InvertImage;\n","sourceCodeStart":60,"sourceCodeEnd":86,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/InvertImage.mjs#L60-L86","documentation":"In web apps InvertImage.present() re-runs isImage on the bytes it received and throws if they are not a recognized image (empty buffers return '' earlier). Indicates the presented data is not an image.","triggerScenarios":"present() receives non-image bytes: an upstream error replaced run()'s output, present() was called directly with arbitrary data, or getBuffer produced an atypical header.","commonSituations":"presentType wiring feeding an error buffer to present(); manual calls in tests; an encode path with a header isImage does not recognize.","solutions":["Make present() only receive a successful run() output.","Treat a present() failure as a run() failure and inspect leading bytes.","Re-encode run output to PNG before presenting."],"exampleFix":"// before\ninvertOp.present(textBuffer); // throws\n// after\nimport { isImage } from \"src/core/lib/FileType.mjs\";\nconst out = invertOp.run(pngBuffer, []);\nif (isImage(new Uint8Array(out)) === false) return '';\ninvertOp.present(out);","handlingStrategy":"validation","validationCode":"import { isImage } from \"src/core/lib/FileType.mjs\";\nfunction safePresent(op, data) {\n  if (!data || !data.byteLength) return '';\n  if (isImage(new Uint8Array(data)) === false) return '';\n  return op.present(data);\n}","typeGuard":"import { isImage } from \"src/core/lib/FileType.mjs\";\nfunction isPresentableImage(data) {\n  return data?.byteLength > 0 && typeof isImage(new Uint8Array(data)) === 'string';\n}","tryCatchPattern":null,"preventionTips":["Only call present() with a successful run() output.","Guard present() with isImage() so non-image output degrades to ''.","Treat present() failure as run() failure.","Re-encode run output to PNG before presenting."],"tags":["image","cyberchef","rendering","input-validation","web"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}