aspnetcore/fundamentals/middleware/request-decompression.md
By David Acker
Request decompression middleware:
Content-Encoding HTTP header to automatically identify and decompress requests which contain compressed content.When the Content-Encoding header value on a request matches one of the available decompression providers, the middleware:
Content-Encoding header, indicating that the request body is no longer compressed.Requests that don't include a Content-Encoding header are ignored by the request decompression middleware.
Decompression:
Content-Encoding, an exception is thrown. Brotli can throw xref:System.InvalidOperationException?displayProperty=fullName: :::no-loc text="Decoder ran into invalid data."::: Deflate and GZip can throw xref:System.IO.InvalidDataException?displayProperty=fullName: :::no-loc text="The archive entry was compressed using an unsupported compression method.":::If the middleware encounters a request with compressed content but is unable to decompress it, the request is passed to the next delegate in the pipeline. For example, a request with an unsupported Content-Encoding header value or multiple Content-Encoding header values is passed to the next delegate in the pipeline.
The following code uses xref:Microsoft.Extensions.DependencyInjection.RequestDecompressionServiceExtensions.AddRequestDecompression(Microsoft.Extensions.DependencyInjection.IServiceCollection) and xref:Microsoft.AspNetCore.Builder.RequestDecompressionBuilderExtensions.UseRequestDecompression%2A to enable request decompression for the default Content-Encoding types:
<a name="default"></a>
The Content-Encoding header values that the request decompression middleware supports by default are listed in the following table:
Content-Encoding header values | Description |
|---|---|
br | Brotli compressed data format |
deflate | DEFLATE compressed data format |
gzip | Gzip file format |
Support for custom encodings can be added by creating custom decompression provider classes that implement xref:Microsoft.AspNetCore.RequestDecompression.IDecompressionProvider:
Custom decompression providers are registered with xref:Microsoft.AspNetCore.RequestDecompression.RequestDecompressionOptions along with their corresponding Content-Encoding header values:
In order to protect against zip bombs or decompression bombs:
In order of precedence, the maximum request size for an endpoint is set by:
MaxRequestBodySize can be overridden per request with xref:Microsoft.AspNetCore.Http.Features.IHttpMaxRequestBodySizeFeature.MaxRequestBodySize?displayProperty=nameWithType, but defaults to the limit configured for the web server implementation.[!WARNING] Disabling the request body size limit poses a security risk in regards to uncontrolled resource consumption, particularly if the request body is being buffered. Ensure that safeguards are in place to mitigate the risk of denial-of-service (DoS) attacks.