{"record":{"id":"98716d7235b67f06","repo":"gchq/CyberChef","slug":"invalid-file-type-98716d","errorCode":null,"errorMessage":"Invalid file type.","messagePattern":"Invalid file type\\.","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/GenerateQRCode.mjs","lineNumber":83,"sourceCode":"        const [format, size, margin, errorCorrection] = args;\n\n        return generateQrCode(input, format, size, margin, errorCorrection);\n    }\n\n    /**\n     * Displays the QR image using HTML for web apps\n     *\n     * @param {ArrayBuffer} data\n     * @returns {html}\n     */\n    present(data, args) {\n        if (!data.byteLength && !data.length) return \"\";\n        const dataArray = new Uint8Array(data),\n            [format] = args;\n        if (format === \"PNG\") {\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        return Utils.arrayBufferToStr(data);\n    }\n\n}\n\nexport default GenerateQRCode;\n","sourceCodeStart":65,"sourceCodeEnd":95,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/GenerateQRCode.mjs#L65-L95","documentation":"Thrown by GenerateQRCode.present() when the format is PNG and isImage() cannot identify the QR code buffer as a recognized image format. present() runs on the operation's PNG output, so this means the PNG bytes were not recognized — implying truncated or corrupted output from the encoding step.","triggerScenarios":"The QR encoder returned a non-PNG or malformed buffer, or present() receives data that has been altered between run() and present(). SVG format bypasses this check.","commonSituations":"An empty/failed QR generation upstream, or chaining through an operation that corrupts bytes before present().","solutions":["Ensure the QR code input string is non-empty and within the QR capacity for the selected error-correction level.","Use SVG format to bypass the image-type check if PNG encoding is problematic.","Verify the buffer has PNG magic bytes (\\x89PNG) before present()."],"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 (format === \"PNG\" && !isPng) {\n  // use SVG format or fix the upstream QR generation\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":["Ensure the QR input fits the capacity for the chosen error-correction level.","Use SVG format to bypass the PNG image-type check."],"tags":["qr-code","present","file-type","runtime"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}