I’ve got an Image as Stream coming in and I’m trying to convert the incoming Image from whatever it maybe (even if it already is a PNG) to a PNG.
I’m doing this as part of sanitizing the image as it comes from a source I do not trust.
By converting it I’m trying to get rid of any code that might have been injected into the image before I store it and return it to a frontend client at a later date.
I’m trying to do this in memory and to my knowledge it should be done like this:
Image image = Image.FromStream(stream, false, true);
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png);
return ValidateImage(path, ms);
};
What ive noticed however is that doing it in the above way has a different result from storing the image to disk and then reading it again after ive converted it.
Image image = Image.FromStream(stream, false, true);
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, ImageFormat.Png);
File.WriteAllBytes(path, ms.ToArray());
};
using (FileStream fs = new FileStream(path, FileMode.Open))
{
return ValidateImage(path, fs);
};
My ValidateImage method at some point does this:
byte[] bytes;
using (MemoryStream ms = new MemoryStream())
{
contentStream.CopyTo(ms);
bytes = ms.ToArray();
}
if(Validate(bytes)){
}
public override bool Validate(byte[] data)
{
try
{
// Try to open the image
Image img;
using (Stream ms = new MemoryStream(data))
{
img = Image.FromStream(ms);
}
// Check if the format matches the expected format
return img.RawFormat.Equals(_format);
}
catch (Exception)
{
// Return false on exception
return false;
}
}
The above Validate() returns false when I do the conversion solely in memory and I’m wondering why and was hoping to find a sage who could enlighten me and help me to do it properly.
Thank you in advance
DBerg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.