ImageResizer: System.BadImageFormatException is causing Web app crash despite being in try ... catch

0

A user uploaded a PNG file which looks as if it is corrupted in some way. But the biggest problem is that any time our system tries to thumbnail it the entire app pool crashes and restarts. I've tried it using a direct URL and using code inside a try ... catch block.

Here's a copy of the image: https://drive.google.com/file/d/1GpmnIbia3rtqfKrhpg_nKHpjxdC-AxR3

If I load it into Paint.NET and save it out again it works fine so I'm content there is an issue with the image. But I need to stop any corrupt images from crashing my web app entirely. I'm happy to handle and log the errors, but I need everything to continue as normal.

For instance, running locally as a test, I can view the image directly without issue. The browser doesn't mind:

http://localhost:60148/Content/artwork/2019/06/img_0021-9.png

But if I try a thumbnailed version:

http://localhost:60148/Content/artwork/2019/06/img_0021-9.png?w=160&mode=max&otf=y&quality=90&format=jpg&bgcolor=white

Visual Studio will report the following and stop debugging:

System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)'

I have some legacy code that will also write the thumbnail as a response stream. Even though that's in a try catch VS still stops debugging after reporting the error. I have local and global error handling so I wouldn't expect a crash.

try
{
    ImageBuilder.Current.Build(pathname.StartsWith("/") ? "~" + pathname : pathname, outputStream,
                new ResizeSettings(resizeSettings));
}
catch (Exception ex)
{
    Console.WriteLine("Could not output to stream: " + ex.Message);
}

On this method I've tried adding the following attributes without any success:

[HandleProcessCorruptedStateExceptions]

and

[SecurityCritical]

I've tried putting it in its own Task in the hope it would localise it, but no joy. It's moot anyway. We use URLs for ImageResizer almost exclusively, and that's killing our web pool.

It's not just locally. It was noticed because we run this as a beta Web site and it was killing it.

png
imageresizer
asked on Stack Overflow Jun 18, 2019 by Steve Owen

1 Answer

1

System.BadImageFormatException never refers to images. It is an exception exclusively for .dll and .exe files, and refers to using the wrong bitness (like 64 vs 32) of file.

Something else is going on here. ImageResizer also is not known for killing the app pool. What plugins do you have installed? Posting the diagnostics page (/resizer.debug) would help.

answered on Stack Overflow Aug 13, 2019 by Lilith River

User contributions licensed under CC BY-SA 3.0