简体   繁体   English

如何防止 ImageSharp Web 调整到某些尺寸?

[英]How to prevent ImageSharp Web from resizing to certain sizes?

I am using ImageSharp.Web to resize images on my ASP.NET Core 5.0 website.我正在使用 ImageSharp.Web 来调整我的 ASP.NET Core 5.0 网站上的图像大小。

To prevent DDoS (Distributed Denial of Service attacks), I would like to restrict the sizes that ImageSharp.Web can resize too.为了防止 DDoS(分布式拒绝服务攻击),我想限制 ImageSharp.Web 也可以调整大小。

For example I have an image with an original size of 800x400 (100kb) that I am resizing using the following:例如,我有一个原始大小为 800x400 (100kb) 的图像,我正在使用以下方法调整其大小:

<img src="image.jpg?width=300&height=300" alt="..." /> // image will be 40 kb, bandwidth saved yay!

The problem is if an evil user decides to request the image with:问题是,如果一个邪恶的用户决定使用以下方式请求图像:

<img src="image.jpg?width=8000&height=4000" alt="..." /> // 8,000 x 4,000 => image is now 2mb

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.如果该用户请求此图像具有“高编号”像素大小(7000,7001,7002...8000} 说 10,000 次,则由于 memory 耗尽和带宽使用,服务器将变得无响应。

  1. How can I restrict ImageSharp.Web to not resize images above their original size?如何限制 ImageSharp.Web 不将图像大小调整为高于原始大小?
  2. How can I restrict ImageSharp.Web to only resize images to eg 300x300, and 300x600?如何将 ImageSharp.Web 限制为仅将图像大小调整为例如 300x300 和 300x600?

I don't see any configurable options for that in ImageSharp.Web ( https://docs.sixlabors.com/articles/imagesharp.web/gettingstarted.html ).我在 ImageSharp.Web ( https://docs.sixlabors.com/articles/imagesharp.web/gettingstarted.ZFC35FDC70D5FC69D2698Z3A822C )中没有看到任何可配置的选项

My startup.cs :我的startup.cs

    public void ConfigureServices(IServiceCollection services)
    {

        // ....

        services.AddImageSharp();

    }

The place your are looking for is actually on that page.您正在寻找的地方实际上是在该页面上。 It's the options.OnParseCommandsAsync function.这是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.这是默认方法。

https://github.com/SixLabors/ImageSharp.Web/blob/b72064b3b8cb8b883f8310c86b6d7e5643d80ad3/src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs#L20-L44 https://github.com/SixLabors/ImageSharp.Web/blob/b72064b3b8cb8b883f8310c86b6d7e5643d80ad3/src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs#L20-L44

private Func<ImageCommandContext, Task> onParseCommandsAsync = c => 
{ 
    if (c.Commands.Count == 0) 
    { 
        return Task.CompletedTask; 
    } 

    // It's a good idea to have this to provide very basic security. 
    // We can safely use the static resize processor properties. 
    uint width = c.Parser.ParseValue<uint>( 
        c.Commands.GetValueOrDefault(ResizeWebProcessor.Width), 
        c.Culture); 

    uint height = c.Parser.ParseValue<uint>( 
        c.Commands.GetValueOrDefault(ResizeWebProcessor.Height), 
        c.Culture); 

    if (width > 4000 && height > 4000) 
    { 
        c.Commands.Remove(ResizeWebProcessor.Width); 
        c.Commands.Remove(ResizeWebProcessor.Height); 
    } 

    return Task.CompletedTask; 
};  

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 ResizeWebProcessor class ( inheriting should be fine since it's not sealed ) and override this method.您必须实现自己的ResizeWebProcessor class 版本(继承应该没问题,因为它不是密封的)并覆盖此方法。 You can remove the original and register your own as described in the documentation.您可以删除原始文件并按照文档中的说明注册自己的文件。

https://github.com/SixLabors/ImageSharp.Web/blob/b72064b3b8cb8b883f8310c86b6d7e5643d80ad3/src/ImageSharp.Web/Processors/ResizeWebProcessor.cs#L69-L84 https://github.com/SixLabors/ImageSharp.Web/blob/b72064b3b8cb8b883f8310c86b6d7e5643d80ad3/src/ImageSharp.Web/Processors/ResizeWebProcessor.cs#L69-L84

public FormattedImage Process( 
     FormattedImage image, 
     ILogger logger, 
     IDictionary<string, string> commands, 
     CommandParser parser, 
     CultureInfo culture) 
{ 
    ResizeOptions options = GetResizeOptions(commands, parser, culture); 

    if (options != null) 
    { 
        image.Image.Mutate(x => x.Resize(options)); 
    } 

    return image; 
} 

Hope that makes it clear.希望这说明清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM