Nguon: Microsoft Learn · .NET 8.0

Giải nén Request trong ASP.NET Core

Nguồn: Request decompression in ASP.NET Core

Bởi David Acker

Middleware (phần mềm trung gian) giải nén request:

Khi giá trị header Content-Encoding trong một request khớp với một trong các decompression provider (nhà cung cấp giải nén) có sẵn, middleware:

Các request không bao gồm header Content-Encoding sẽ bị middleware giải nén request bỏ qua.

Quá trình giải nén:

Nếu middleware gặp request có nội dung được nén nhưng không thể giải nén, request được chuyển đến delegate tiếp theo trong pipeline. Ví dụ, một request có giá trị header Content-Encoding không được hỗ trợ hoặc nhiều giá trị header Content-Encoding được chuyển đến delegate tiếp theo trong pipeline.

Cấu hình

Code sau sử dụng AddRequestDecompression(IServiceCollection)UseRequestDecompression để bật giải nén request cho các loại Content-Encoding mặc định:

csharp
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRequestDecompression();

var app = builder.Build();

app.UseRequestDecompression();

app.MapPost("/", (HttpRequest request) => Results.Stream(request.Body));

app.Run();

Các decompression provider mặc định

Các giá trị header Content-Encoding mà middleware giải nén request hỗ trợ theo mặc định được liệt kê trong bảng sau:

Giá trị header Content-EncodingMô tả
brĐịnh dạng dữ liệu nén Brotli
deflateĐịnh dạng dữ liệu nén DEFLATE
gzipĐịnh dạng file Gzip

Các decompression provider tùy chỉnh

Hỗ trợ cho các encoding tùy chỉnh có thể được thêm bằng cách tạo các class decompression provider tùy chỉnh cài đặt IDecompressionProvider:

csharp
public class CustomDecompressionProvider : IDecompressionProvider
{
    public Stream GetDecompressionStream(Stream stream)
    {
        // Perform custom decompression logic here
        return stream;
    }
}

Các decompression provider tùy chỉnh được đăng ký với RequestDecompressionOptions cùng với các giá trị header Content-Encoding tương ứng:

csharp
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRequestDecompression(options =>
{
    options.DecompressionProviders.Add("custom", new CustomDecompressionProvider());
});

var app = builder.Build();

app.UseRequestDecompression();

app.MapPost("/", (HttpRequest request) => Results.Stream(request.Body));

app.Run();

Giới hạn kích thước request

Để bảo vệ chống lại zip bombs hoặc decompression bombs:

Theo thứ tự ưu tiên, kích thước request tối đa cho một endpoint được đặt bởi:

  1. IRequestSizeLimitMetadata.MaxRequestBodySize, chẳng hạn như RequestSizeLimitAttribute hoặc DisableRequestSizeLimitAttribute cho các MVC endpoint.
  2. Giới hạn kích thước server toàn cục IHttpMaxRequestBodySizeFeature.MaxRequestBodySize. MaxRequestBodySize có thể được ghi đè theo từng request với IHttpMaxRequestBodySizeFeature.MaxRequestBodySize, nhưng mặc định theo giới hạn được cấu hình cho cài đặt web server.
Cài đặt web serverCấu hình MaxRequestBodySize
HTTP.sysHttpSysOptions.MaxRequestBodySize
IISIISServerOptions.MaxRequestBodySize
KestrelKestrelServerLimits.MaxRequestBodySize

Vô hiệu hóa giới hạn kích thước request body đặt ra rủi ro bảo mật liên quan đến việc tiêu thụ tài nguyên không kiểm soát, đặc biệt nếu request body đang được buffer. Đảm bảo rằng các biện pháp bảo vệ được thiết lập để giảm thiểu rủi ro của các cuộc tấn công từ chối dịch vụ (DoS).