Skip to content

Commit

Permalink
${aspnet-request-ip}: make ForwardedForHeader configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
304NotModified committed Apr 14, 2020
1 parent 85b35f6 commit 0d9f856
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/Shared/LayoutRenderers/AspNetRequestIpLayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System;
using System.ComponentModel;
using System.Text;

using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Layouts;
using NLog.Web.Internal;
#if ASP_NET_CORE
using Microsoft.AspNetCore.Http;

#else
using System.Web;
#endif

namespace NLog.Web.LayoutRenderers
Expand All @@ -22,10 +26,14 @@ namespace NLog.Web.LayoutRenderers
[ThreadSafe]
public class AspNetRequestIpLayoutRenderer : AspNetLayoutRendererBase
{
private const string ForwardedForHeader = "X-Forwarded-For";
/// <summary>
/// The header name to check for the Forwarded-For. Default "X-Forwarded-For". Needs <see cref="CheckForwardedForHeader"/>
/// </summary>
[DefaultValue("X-Forwarded-For")]
public Layout ForwardedForHeader { get; set; } = "X-Forwarded-For";

/// <summary>
/// Gets or sets whether the renderer should check value of X-Forwarded-For header
/// Gets or sets whether the renderer should check value of <see cref="ForwardedForHeader"/> header
/// </summary>
/// <docgen category='Rendering Options' order='10' />
public bool CheckForwardedForHeader { get; set; }
Expand All @@ -43,7 +51,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
return;
}

var ip = CheckForwardedForHeader ? TryLookupForwardHeader(request) : string.Empty;
var ip = CheckForwardedForHeader && ForwardedForHeader != null ? TryLookupForwardHeader(request, logEvent) : string.Empty;

if (string.IsNullOrEmpty(ip))
{
Expand All @@ -58,9 +66,10 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}

#if !ASP_NET_CORE
string TryLookupForwardHeader(System.Web.HttpRequestBase httpRequest)
string TryLookupForwardHeader(HttpRequestBase httpRequest, LogEventInfo logEvent)
{
var forwardedHeader = httpRequest.Headers[ForwardedForHeader];
var headerName = ForwardedForHeader.Render(logEvent);
var forwardedHeader = httpRequest.Headers[headerName];

if (!string.IsNullOrEmpty(forwardedHeader))
{
Expand All @@ -74,11 +83,12 @@ string TryLookupForwardHeader(System.Web.HttpRequestBase httpRequest)
return string.Empty;
}
#else
private string TryLookupForwardHeader(HttpRequest httpRequest)
private string TryLookupForwardHeader(HttpRequest httpRequest, LogEventInfo logEvent)
{
if (httpRequest.Headers?.ContainsKey(ForwardedForHeader) == true)
var headerName = ForwardedForHeader.Render(logEvent);
if (httpRequest.Headers?.ContainsKey(headerName) == true)
{
var forwardedHeaders = httpRequest.Headers.GetCommaSeparatedValues(ForwardedForHeader);
var forwardedHeaders = httpRequest.Headers.GetCommaSeparatedValues(headerName);
if (forwardedHeaders.Length > 0)
{
return forwardedHeaders[0];
Expand Down
25 changes: 25 additions & 0 deletions tests/Shared/LayoutRenderers/AspNetRequestIpLayoutRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,31 @@ public void ForwardedForHeaderContainsMultipleEntriesRenderFirstValue()
// Act
string result = renderer.Render(new LogEventInfo());

// Assert
Assert.Equal("127.0.0.1", result);
}

[Fact]
public void ForwardedForHeaderContainsMultipleEntriesRenderFirstValue_withCustomHeaderName()
{
// Arrange
var (renderer, httpContext) = CreateWithHttpContext();

#if !ASP_NET_CORE
httpContext.Request.ServerVariables.Returns(new NameValueCollection {{"REMOTE_ADDR", "192.0.0.0"}});
httpContext.Request.Headers.Returns(
new NameValueCollection {{"header2", "127.0.0.1, 192.168.1.1"}});
#else
var headers = new HeaderDict();
headers.Add("header2", new StringValues("127.0.0.1, 192.168.1.1"));
httpContext.Request.Headers.Returns(callinfo => headers);
#endif
renderer.CheckForwardedForHeader = true;
renderer.ForwardedForHeader = "header2";

// Act
string result = renderer.Render(new LogEventInfo());

// Assert
Assert.Equal("127.0.0.1", result);
}
Expand Down

0 comments on commit 0d9f856

Please sign in to comment.