|
| 1 | +// Adapted from: https://github.com/dotnet/aspnetcore/blob/c18e93a9a2e2949e1a9c880da16abf0837aa978f/src/Middleware/RequestDecompression/src/RequestDecompressionMiddleware.cs |
| 2 | + |
| 3 | +// // Licensed to the .NET Foundation under one or more agreements. |
| 4 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 5 | + |
| 6 | +using Microsoft.AspNetCore.Http; |
| 7 | +using Microsoft.AspNetCore.Http.Features; |
| 8 | +using Microsoft.AspNetCore.Http.Metadata; |
| 9 | +using Microsoft.AspNetCore.RequestDecompression; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | + |
| 12 | +namespace Sentry.AspNetCore.RequestDecompression; |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Enables HTTP request decompression. |
| 16 | +/// </summary> |
| 17 | +internal sealed partial class RequestDecompressionMiddleware |
| 18 | +{ |
| 19 | + private readonly RequestDelegate _next; |
| 20 | + private readonly ILogger<RequestDecompressionMiddleware> _logger; |
| 21 | + private readonly IRequestDecompressionProvider _provider; |
| 22 | + private readonly IHub _hub; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Initialize the request decompression middleware. |
| 26 | + /// </summary> |
| 27 | + /// <param name="next">The delegate representing the remaining middleware in the request pipeline.</param> |
| 28 | + /// <param name="logger">The logger.</param> |
| 29 | + /// <param name="provider">The <see cref="IRequestDecompressionProvider"/>.</param> |
| 30 | + /// <param name="hub">The Sentry Hub</param> |
| 31 | + public RequestDecompressionMiddleware( |
| 32 | + RequestDelegate next, |
| 33 | + ILogger<RequestDecompressionMiddleware> logger, |
| 34 | + IRequestDecompressionProvider provider, |
| 35 | + IHub hub) |
| 36 | + { |
| 37 | + ArgumentNullException.ThrowIfNull(next); |
| 38 | + ArgumentNullException.ThrowIfNull(logger); |
| 39 | + ArgumentNullException.ThrowIfNull(provider); |
| 40 | + ArgumentNullException.ThrowIfNull(hub); |
| 41 | + |
| 42 | + _next = next; |
| 43 | + _logger = logger; |
| 44 | + _provider = provider; |
| 45 | + _hub = hub; |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Invoke the middleware. |
| 50 | + /// </summary> |
| 51 | + /// <param name="context">The <see cref="HttpContext"/>.</param> |
| 52 | + /// <returns>A task that represents the execution of this middleware.</returns> |
| 53 | + public Task Invoke(HttpContext context) |
| 54 | + { |
| 55 | + Stream? decompressionStream = null; |
| 56 | + try |
| 57 | + { |
| 58 | + decompressionStream = _provider.GetDecompressionStream(context); |
| 59 | + } |
| 60 | + catch (Exception e) |
| 61 | + { |
| 62 | + HandleException(e); |
| 63 | + } |
| 64 | + return decompressionStream is null |
| 65 | + ? _next(context) |
| 66 | + : InvokeCore(context, decompressionStream); |
| 67 | + } |
| 68 | + |
| 69 | + private async Task InvokeCore(HttpContext context, Stream decompressionStream) |
| 70 | + { |
| 71 | + var request = context.Request.Body; |
| 72 | + try |
| 73 | + { |
| 74 | + try |
| 75 | + { |
| 76 | + var sizeLimit = |
| 77 | + context.GetEndpoint()?.Metadata?.GetMetadata<IRequestSizeLimitMetadata>()?.MaxRequestBodySize |
| 78 | + ?? context.Features.Get<IHttpMaxRequestBodySizeFeature>()?.MaxRequestBodySize; |
| 79 | + |
| 80 | + context.Request.Body = new SizeLimitedStream(decompressionStream, sizeLimit, static (long sizeLimit) => throw new BadHttpRequestException( |
| 81 | + $"The decompressed request body is larger than the request body size limit {sizeLimit}.", |
| 82 | + StatusCodes.Status413PayloadTooLarge)); |
| 83 | + } |
| 84 | + catch (Exception e) |
| 85 | + { |
| 86 | + HandleException(e); |
| 87 | + } |
| 88 | + |
| 89 | + await _next(context).ConfigureAwait(false); |
| 90 | + } |
| 91 | + finally |
| 92 | + { |
| 93 | + context.Request.Body = request; |
| 94 | + await decompressionStream.DisposeAsync().ConfigureAwait(false); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private void HandleException(Exception e) |
| 99 | + { |
| 100 | + const string description = |
| 101 | + "An exception was captured and then re-thrown, when attempting to decompress the request body." + |
| 102 | + "The web server likely returned a 5xx error code as a result of this exception."; |
| 103 | + e.SetSentryMechanism(nameof(RequestDecompressionMiddleware), description, handled: false); |
| 104 | + _hub.CaptureException(e); |
| 105 | + ExceptionDispatchInfo.Capture(e).Throw(); |
| 106 | + } |
| 107 | +} |
0 commit comments