diff --git a/app/assets/javascripts/discourse/app/lib/media-optimization-utils.js b/app/assets/javascripts/discourse/app/lib/media-optimization-utils.js index 2d18cbf98fe..ad581f01336 100644 --- a/app/assets/javascripts/discourse/app/lib/media-optimization-utils.js +++ b/app/assets/javascripts/discourse/app/lib/media-optimization-utils.js @@ -66,6 +66,14 @@ function isTransparent(type, imageData) { return false; } +function jpegDecodeFailure(type, imageData) { + if (!/(\.|\/)jpe?g$/i.test(type)) { + return false; + } + + return imageData.data[3] === 0; +} + export async function fileToImageData(file) { const drawable = await fileToDrawable(file); const imageData = drawableToimageData(drawable); @@ -74,5 +82,9 @@ export async function fileToImageData(file) { throw "Image has transparent pixels, won't convert to JPEG!"; } + if (jpegDecodeFailure(file.type, imageData)) { + throw "JPEG image has transparent pixel, decode failed!"; + } + return imageData; }