diff --git a/src/NLog.Web.AspNetCore/NLogRequestPostedBodyMiddleware.cs b/src/NLog.Web.AspNetCore/NLogRequestPostedBodyMiddleware.cs index 250da52b..43df43d1 100644 --- a/src/NLog.Web.AspNetCore/NLogRequestPostedBodyMiddleware.cs +++ b/src/NLog.Web.AspNetCore/NLogRequestPostedBodyMiddleware.cs @@ -19,14 +19,14 @@ public class NLogRequestPostedBodyMiddleware { private readonly RequestDelegate _next; - private readonly NLogRequestPostedBodyMiddlewareConfiguration _configuration; + private readonly NLogRequestPostedBodyMiddlewareOptions _configuration; /// /// Constructor that takes a configuration /// /// /// - public NLogRequestPostedBodyMiddleware(RequestDelegate next, NLogRequestPostedBodyMiddlewareConfiguration configuration) + public NLogRequestPostedBodyMiddleware(RequestDelegate next, NLogRequestPostedBodyMiddlewareOptions configuration) { _next = next; _configuration = configuration; @@ -117,7 +117,7 @@ private async Task GetString(Stream stream) using (var streamReader = new StreamReader( stream, Encoding.UTF8, - _configuration.DetectEncodingFromByteOrderMark, + true, 1024, leaveOpen: true)) { diff --git a/src/NLog.Web.AspNetCore/NLogRequestPostedBodyMiddlewareConfiguration.cs b/src/NLog.Web.AspNetCore/NLogRequestPostedBodyMiddlewareOptions.cs similarity index 59% rename from src/NLog.Web.AspNetCore/NLogRequestPostedBodyMiddlewareConfiguration.cs rename to src/NLog.Web.AspNetCore/NLogRequestPostedBodyMiddlewareOptions.cs index e6e83da5..715000f2 100644 --- a/src/NLog.Web.AspNetCore/NLogRequestPostedBodyMiddlewareConfiguration.cs +++ b/src/NLog.Web.AspNetCore/NLogRequestPostedBodyMiddlewareOptions.cs @@ -6,21 +6,27 @@ namespace NLog.Web /// /// Contains the configuration for the NLogRequestPostedBodyMiddleware /// - public class NLogRequestPostedBodyMiddlewareConfiguration + public class NLogRequestPostedBodyMiddlewareOptions { /// /// The default configuration /// - public static readonly NLogRequestPostedBodyMiddlewareConfiguration Default = new NLogRequestPostedBodyMiddlewareConfiguration(); + internal static readonly NLogRequestPostedBodyMiddlewareOptions Default = new NLogRequestPostedBodyMiddlewareOptions(); /// - /// Defaults to true + /// The default constructor /// - public bool DetectEncodingFromByteOrderMark { get; set; } = true; + public NLogRequestPostedBodyMiddlewareOptions() + { + ShouldCapture = DefaultCapture; + } /// /// The maximum request size that will be captured - /// Defaults to 30KB + /// Defaults to 30KB. This checks against the ContentLength. + /// HttpRequest.EnableBuffer() writes the request to TEMP files on disk if the request ContentLength is > 30KB + /// but uses memory otherwise if <= 30KB, so we should protect against "very large" + /// request post body payloads. /// public int MaximumRequestSize { get; set; } = 30 * 1024; @@ -31,16 +37,15 @@ public class NLogRequestPostedBodyMiddlewareConfiguration /// only certain hosts, only below a certain request body size, and so forth /// /// - public Predicate ShouldCapture { get; set; } = DefaultCapture; + public Predicate ShouldCapture { get; set; } /// /// The default predicate for ShouldCapture /// Returns true if content length <= 30KB /// - public static bool DefaultCapture(HttpContext context) + private bool DefaultCapture(HttpContext context) { - return context?.Request?.ContentLength != null && context?.Request?.ContentLength <= - new NLogRequestPostedBodyMiddlewareConfiguration().MaximumRequestSize; + return context?.Request?.ContentLength != null && context?.Request?.ContentLength <= MaximumRequestSize; } } } diff --git a/tests/NLog.Web.AspNetCore.Tests/NLogRequestPostedBodyMiddlewareConfigurationTests.cs b/tests/NLog.Web.AspNetCore.Tests/NLogRequestPostedBodyMiddlewareConfigurationTests.cs index 9d936a25..ea4294b6 100644 --- a/tests/NLog.Web.AspNetCore.Tests/NLogRequestPostedBodyMiddlewareConfigurationTests.cs +++ b/tests/NLog.Web.AspNetCore.Tests/NLogRequestPostedBodyMiddlewareConfigurationTests.cs @@ -10,32 +10,17 @@ public class NLogRequestPostedBodyMiddlewareConfigurationTests [Fact] public void SetMaximumRequestSizeTest() { - var config = new NLogRequestPostedBodyMiddlewareConfiguration(); + var config = new NLogRequestPostedBodyMiddlewareOptions(); var size = new Random().Next(); config.MaximumRequestSize = size; Assert.Equal(size, config.MaximumRequestSize); } - [Fact] - public void SetByteOrderMarkTest() - { - var config = new NLogRequestPostedBodyMiddlewareConfiguration(); - var bom = true; - config.DetectEncodingFromByteOrderMark = bom; - - Assert.Equal(bom, config.DetectEncodingFromByteOrderMark); - - bom = false; - config.DetectEncodingFromByteOrderMark = bom; - - Assert.Equal(bom, config.DetectEncodingFromByteOrderMark); - } - [Fact] public void GetDefault() { - var config = NLogRequestPostedBodyMiddlewareConfiguration.Default; + var config = NLogRequestPostedBodyMiddlewareOptions.Default; Assert.NotNull(config); } @@ -43,13 +28,13 @@ public void GetDefault() [Fact] public void DefaultCaptureTrue() { - var config = NLogRequestPostedBodyMiddlewareConfiguration.Default; + var config = NLogRequestPostedBodyMiddlewareOptions.Default; HttpContext httpContext = Substitute.For(); HttpRequest request = Substitute.For(); - request.ContentLength.Returns(NLogRequestPostedBodyMiddlewareConfiguration.Default.MaximumRequestSize - 1); + request.ContentLength.Returns(NLogRequestPostedBodyMiddlewareOptions.Default.MaximumRequestSize - 1); httpContext.Request.Returns(request); @@ -59,7 +44,7 @@ public void DefaultCaptureTrue() [Fact] public void DefaultCaptureFalseNullContentLength() { - var config = NLogRequestPostedBodyMiddlewareConfiguration.Default; + var config = NLogRequestPostedBodyMiddlewareOptions.Default; HttpContext httpContext = Substitute.For(); @@ -75,13 +60,13 @@ public void DefaultCaptureFalseNullContentLength() [Fact] public void DefaultCaptureExcessiveContentLength() { - var config = NLogRequestPostedBodyMiddlewareConfiguration.Default; + var config = NLogRequestPostedBodyMiddlewareOptions.Default; HttpContext httpContext = Substitute.For(); HttpRequest request = Substitute.For(); - request.ContentLength.Returns(NLogRequestPostedBodyMiddlewareConfiguration.Default.MaximumRequestSize + 1); + request.ContentLength.Returns(NLogRequestPostedBodyMiddlewareOptions.Default.MaximumRequestSize + 1); httpContext.Request.Returns(request); diff --git a/tests/NLog.Web.AspNetCore.Tests/NLogRequestPostedBodyMiddlewareTests.cs b/tests/NLog.Web.AspNetCore.Tests/NLogRequestPostedBodyMiddlewareTests.cs index 54eb9550..aeeaa76c 100644 --- a/tests/NLog.Web.AspNetCore.Tests/NLogRequestPostedBodyMiddlewareTests.cs +++ b/tests/NLog.Web.AspNetCore.Tests/NLogRequestPostedBodyMiddlewareTests.cs @@ -35,40 +35,7 @@ public void SuccessTest() long streamBeforePosition = defaultContext.Request.Body.Position; - var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next, NLogRequestPostedBodyMiddlewareConfiguration.Default); - middlewareInstance.Invoke(defaultContext).ConfigureAwait(false).GetAwaiter().GetResult(); - - long streamAfterPosition = defaultContext.Request.Body.Position; - - // Assert - Assert.NotNull(defaultContext.Items); - Assert.Single(defaultContext.Items); - Assert.NotNull(defaultContext.Items[AspNetRequestPostedBodyLayoutRenderer.NLogPostedRequestBodyKey]); - Assert.True(defaultContext.Items[AspNetRequestPostedBodyLayoutRenderer.NLogPostedRequestBodyKey] is string); - Assert.Equal("This is a test request body", defaultContext.Items[AspNetRequestPostedBodyLayoutRenderer.NLogPostedRequestBodyKey] as string); - Assert.Equal(streamBeforePosition, streamAfterPosition); - } - - [Fact] - public void SuccessWithCustomConfigurationTest() - { - // Arrange - DefaultHttpContext defaultContext = new DefaultHttpContext(); - defaultContext.Request.Body = new MemoryStream(); - byte[] bodyBytes = Encoding.ASCII.GetBytes("This is a test request body"); - defaultContext.Request.Body.Write(bodyBytes, 0, bodyBytes.Length); - defaultContext.Request.ContentLength = bodyBytes.Length; - - // Act - - long streamBeforePosition = defaultContext.Request.Body.Position; - - var configuration = new NLogRequestPostedBodyMiddlewareConfiguration - { - DetectEncodingFromByteOrderMark = false - }; - - var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next, configuration); + var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next, NLogRequestPostedBodyMiddlewareOptions.Default); middlewareInstance.Invoke(defaultContext).ConfigureAwait(false).GetAwaiter().GetResult(); long streamAfterPosition = defaultContext.Request.Body.Position; @@ -91,7 +58,7 @@ public void EmptyBodyTest() defaultContext.Request.ContentLength = 0; // Act - var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next, NLogRequestPostedBodyMiddlewareConfiguration.Default); + var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next, NLogRequestPostedBodyMiddlewareOptions.Default); middlewareInstance.Invoke(defaultContext).ConfigureAwait(false).GetAwaiter().GetResult(); // Assert @@ -107,7 +74,7 @@ public void NullContextTest() HttpContext defaultContext = null; // Act - var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next, NLogRequestPostedBodyMiddlewareConfiguration.Default); + var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next, NLogRequestPostedBodyMiddlewareOptions.Default); middlewareInstance.Invoke(defaultContext).ConfigureAwait(false).GetAwaiter().GetResult(); // Assert @@ -123,7 +90,7 @@ public void NullRequestTest() defaultContext.Request.ReturnsNull(); // Act - var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next, NLogRequestPostedBodyMiddlewareConfiguration.Default); + var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next, NLogRequestPostedBodyMiddlewareOptions.Default); middlewareInstance.Invoke(defaultContext).ConfigureAwait(false).GetAwaiter().GetResult(); // Assert @@ -139,7 +106,7 @@ public void NullBodyTest() defaultContext.Request.Body = null; // Act - var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next,NLogRequestPostedBodyMiddlewareConfiguration.Default); + var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next,NLogRequestPostedBodyMiddlewareOptions.Default); middlewareInstance.Invoke(defaultContext).ConfigureAwait(false).GetAwaiter().GetResult(); // Assert @@ -157,7 +124,7 @@ public void ContentLengthTooLargeTest() defaultContext.Request.ContentLength = 30 * 1024 + 1; // Act - var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next,NLogRequestPostedBodyMiddlewareConfiguration.Default); + var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next,NLogRequestPostedBodyMiddlewareOptions.Default); middlewareInstance.Invoke(defaultContext).ConfigureAwait(false).GetAwaiter().GetResult(); // Assert @@ -175,7 +142,7 @@ public void MissingContentLengthTest() defaultContext.Request.ContentLength = null; // Act - var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next,NLogRequestPostedBodyMiddlewareConfiguration.Default); + var middlewareInstance = new NLogRequestPostedBodyMiddleware(Next,NLogRequestPostedBodyMiddlewareOptions.Default); middlewareInstance.Invoke(defaultContext).ConfigureAwait(false).GetAwaiter().GetResult(); // Assert @@ -196,7 +163,7 @@ public void CannotReadLengthTest() // Act var middlewareInstance = - new NLogRequestPostedBodyMiddleware(Next,NLogRequestPostedBodyMiddlewareConfiguration.Default); + new NLogRequestPostedBodyMiddleware(Next,NLogRequestPostedBodyMiddlewareOptions.Default); middlewareInstance.Invoke(defaultContext).ConfigureAwait(false).GetAwaiter().GetResult(); // Assert @@ -217,7 +184,7 @@ public void CannotSeekLengthTest() // Act var middlewareInstance = - new NLogRequestPostedBodyMiddleware(Next,NLogRequestPostedBodyMiddlewareConfiguration.Default); + new NLogRequestPostedBodyMiddleware(Next,NLogRequestPostedBodyMiddlewareOptions.Default); middlewareInstance.Invoke(defaultContext).ConfigureAwait(false).GetAwaiter().GetResult(); // Assert