How to prevent ImageSharp Web from resizing to certain sizes? #139
-
I am new to ImageSharp.Web and want to resize images on my ASP.NET Core 5.0 website. (posted this question on SO as well: https://stackoverflow.com/questions/66284106/how-to-prevent-imagesharp-web-from-resizing-to-certain-sizes) To prevent DDoS (Distributed Denial of Service attacks), I would like to restrict the sizes that ImageSharp.Web can resize too. For example I have an image with an original size of 800x400 (100kb) that I am resizing using the following:
The problem is if an evil user decides to request the image with:
If that user request this image with 'high-numbered' pixel sizes (7000,7001,7002...8000} say 10,000 times the server will become non-responsive due to memory exhausting and bandwidth usage.
I don't see any configurable options for that in ImageSharp.Web (https://docs.sixlabors.com/articles/imagesharp.web/gettingstarted.html). My startup.cs:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi @shapeh The place your are looking for is actually on that page. It's the We actually do some default sanitation to help reduce potential attack vectors (and disallow that specific evil user) but you can implement custom rules you want there instead. Here's the default method. However this doesn't allow you to prevent upscaling since we haven't attempted to decode the image at this point so do not know anything about it. You would have to implement your own version of the ImageSharp.Web/src/ImageSharp.Web/Processors/ResizeWebProcessor.cs Lines 69 to 84 in b72064b Hope that makes it clear. P.S I transferred the discussion here. Each repository has its own distinct discussions channel. |
Beta Was this translation helpful? Give feedback.
Hi @shapeh
The place your are looking for is actually on that page. It's the
options.OnParseCommandsAsync
function.We actually do some default sanitation to help reduce potential attack vectors (and disallow that specific evil user) but you can implement custom rules you want there instead. Here's the default method.
ImageSharp.Web/src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs
Lines 20 to 44 in b72064b