{"record":{"id":"cb0ad9ee9c2028cc","repo":"gchq/CyberChef","slug":"invalid-file-type-cb0ad9","errorCode":null,"errorMessage":"Invalid file type.","messagePattern":"Invalid file type\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/DitherImage.mjs","lineNumber":41,"sourceCode":"\n        this.name = \"Dither Image\";\n        this.module = \"Image\";\n        this.description = \"Apply a dither effect to an image.\";\n        this.infoURL = \"https://wikipedia.org/wiki/Dither\";\n        this.inputType = \"ArrayBuffer\";\n        this.outputType = \"ArrayBuffer\";\n        this.presentType = \"html\";\n        this.args = [];\n    }\n\n    /**\n     * @param {ArrayBuffer} input\n     * @param {Object[]} args\n     * @returns {byteArray}\n     */\n    async run(input, args) {\n        if (!isImage(input)) {\n            throw new OperationError(\"Invalid file type.\");\n        }\n\n        let image;\n        try {\n            image = await Jimp.read(input);\n        } catch (err) {\n            throw new OperationError(`Error loading image. (${err})`);\n        }\n        try {\n            if (isWorkerEnvironment())\n                self.sendStatusMessage(\"Applying dither to image...\");\n            image.dither();\n\n            let imageBuffer;\n            if (image.mime === \"image/gif\") {\n                imageBuffer = await image.getBuffer(JimpMime.png);\n            } else {\n                imageBuffer = await image.getBuffer(image.mime);","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/DitherImage.mjs#L23-L59","documentation":"Thrown by Dither Image run() when isImage(input) returns falsy on the input ArrayBuffer - i.e. the bytes do not begin with a recognized image magic signature (PNG/JPEG/GIF/BMP/WebP). Dithering requires a decodable raster image; this guard runs before Jimp.read(). It fires on the run/crop path, distinct from the present() 'Invalid file type.' error which fires during rendering.","triggerScenarios":"Feeding non-image bytes into Dither Image: a text/document ArrayBuffer, raw pixel data without a header, a truncated image, or an unsupported image format that isImage does not recognize. The check happens before any Jimp processing.","commonSituations":"Piping output of a non-image operation into Dither Image; an image format Jimp/isImage does not support (e.g. HEIC, TIFF in some configs); a truncated/corrupted image upload; raw RGB pixel data missing a container header.","solutions":["Confirm the input is a supported raster image (use Detect File Type or check the magic bytes).","If the data is raw pixels, wrap it into a PNG/JPEG container first (e.g. via an image-encoding operation).","Convert unsupported formats (HEIC/TIFF) to PNG/JPEG before Dither Image.","Re-upload or re-extract the source image if it is truncated."],"exampleFix":"// before - raw RGB pixels fed in\ninput: <raw RGB bytes>\n\n// after - PNG-encoded image fed in\ninput: <PNG with header 89 50 4E 47 ...>","handlingStrategy":"validation","validationCode":"import { isImage } from \"src/core/lib/FileType.mjs\";\n\nfunction isImageBuffer(buf) {\n    return buf instanceof ArrayBuffer && buf.byteLength > 0 && Boolean(isImage(new Uint8Array(buf)));\n}","typeGuard":"/** @returns {boolean} */\nfunction isDecodableImage(buf) {\n    return buf instanceof ArrayBuffer\n        && buf.byteLength > 0\n        && Boolean(isImage(new Uint8Array(buf)));\n}","tryCatchPattern":"try {\n    out = await ditherImage.run(input, args);\n} catch (e) {\n    if (e instanceof OperationError && /Invalid file type/.test(e.message)) {\n        // convert/encode the input to PNG/JPEG before retrying\n    } else throw e;\n}","preventionTips":["Feed only supported raster image formats (PNG/JPEG/GIF/BMP/WebP).","Convert unsupported formats (HEIC/TIFF) to PNG/JPEG before Dither Image.","Wrap raw pixel data into a PNG container before dithering.","Use Detect File Type to confirm the MIME before the Image pipeline."],"tags":["image","file-type","dither","validation"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}