3

I am using .NET Core 3.0 MVC app with a file upload for users. These files assumed to be an image, but for obvious reasons, I validate the image before saving it to the server.

Now I have ran into an issue, when a user tries to upload a JPEG image (which seems to be fine as he had to email it to me for further inspection) and this image seems to be a WebP image or was converted from a WebP Image.

The issue is that one of the check for each image is

using (var bitmap = new Bitmap(postedFile.OpenReadStream()))
{
}

This threw an exception "Parameter is not valid" and because of this, validation fails.

If I read the buffer of the uploaded IFromFile, it starts with RIFFڶ\0\0WEBPVP8

This is how I knew, this is not a normal image.

Any way to decode or validate this image as any other?

Ian Kemp
  • 26,561
  • 17
  • 107
  • 129
Sugafree
  • 567
  • 2
  • 10
  • 27
  • 1
    Do you actually need to read the image? WebP is not supported by `Bitmap`. [There are libraries which can](https://stackoverflow.com/q/7566426/215552), but if you don't actually need to manipulate the image, I suggest just looking at the `Content-Type` HTTP header. – Heretic Monkey Dec 31 '19 at 14:51
  • I dont think `Content-Type` and `Header` would be enough to check if the file is valid. I check those libraries though. Thanks – Sugafree Dec 31 '19 at 14:53
  • From [here](https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/types-of-bitmaps?view=netframework-4.8), you can find the supported formats are BMP, GIF, EXIF, JPEG, PNG and TIFF. – Fei Han Jan 01 '20 at 06:40

0 Answers0