Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

${AspNetRequestIp} added CheckForwardedForHeader to check for X-Forwarded-For header #325

Merged
merged 5 commits into from
Oct 31, 2018

Conversation

Giorgi
Copy link
Contributor

@Giorgi Giorgi commented Oct 27, 2018

No description provided.

@Giorgi
Copy link
Contributor Author

Giorgi commented Oct 27, 2018

Fixes #324

@codecov
Copy link

codecov bot commented Oct 27, 2018

Codecov Report

Merging #325 into master will increase coverage by 1%.
The diff coverage is 69%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #325   +/-   ##
=====================================
+ Coverage      62%    63%   +1%     
=====================================
  Files          32     32           
  Lines         454    466   +12     
  Branches       97    101    +4     
=====================================
+ Hits          282    294   +12     
+ Misses        135    132    -3     
- Partials       37     40    +3
Impacted Files Coverage Δ
...e/LayoutRenderers/AspNetRequestIpLayoutRenderer.cs 75% <69%> (+75%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ebb807...215e803. Read the comment docs.

#if !ASP_NET_CORE
if (CheckForwardedForHeader)
{
var forwardedHeader = httpContext.TryGetRequest().Headers[ForwardedForHeader];
Copy link
Contributor

@snakefoot snakefoot Oct 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling httpContext.TryGetRequest() twice, then do it once and reuse it for Headers[ForwardedForHeader] and ServerVariables["REMOTE_ADDR"] (Maybe also handle null value from TryGetRequest)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

var ip = httpContext.Connection?.RemoteIpAddress;
if (CheckForwardedForHeader)
{
var forwardedHeaders = httpContext.Request.Headers.GetCommaSeparatedValues(ForwardedForHeader);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe perform if (Headers.ContainsKey(ForwardedForHeader)) before doing the GetCommaSeparatedValues-call. To reduce allocation when nothing to parse.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the header is missing GetCommaSeparatedValues returns StringValues.Empty which is a static field so I think it shouldn't cause any extra allocation

Copy link
Contributor

@snakefoot snakefoot Oct 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will add that check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@snakefoot
Copy link
Contributor

snakefoot commented Oct 27, 2018

Maybe possible to create a unit-test similar to AspNetRequestReferrerRendererTests where Headers are mocked to include "X-Forwarded-For" with dummy-address ? (And verify it is returned)

@Giorgi
Copy link
Contributor Author

Giorgi commented Oct 28, 2018

Tests added

@snakefoot
Copy link
Contributor

snakefoot commented Oct 28, 2018

Thank you for the unit-tests. One last request from me. Could the logic be refactored so it becomes:

string ip = CheckForwardedForHeader ? TryLookupForwardHeader(httpContext) : string.Empty;
if (string.IsNullOrEmpty(ip))
{
#if !ASP_NET_CORE
      ip = httpContext.TryGetRequest()?.ServerVariables["REMOTE_ADDR"];
#else
      ip = httpContext.Connection?.RemoteIpAddress;
#endif
}
builder.Append(ip);

Then it would reduce the amount of code in one method. Will also skip double lookup of ip-address. And maybe make the "Better Code Hub" more happy.

@Giorgi
Copy link
Contributor Author

Giorgi commented Oct 28, 2018

What should be the type of context parameter in TryLookupForwardHeader method?

@snakefoot
Copy link
Contributor

snakefoot commented Oct 28, 2018

Maybe something like this

#if !ASP_NET_CORE
string TryLookupForwardHeader(System.Web.HttpContextBase httpContext)
{
    // Blah Blah
}
#else
string TryLookupForwardHeader(Microsoft.AspNetCore.Http.HttpContext httpContext)
{
    // Blah Blah
}
#endif

@Giorgi
Copy link
Contributor Author

Giorgi commented Oct 28, 2018

Do you think it's worth the hassle? I will have to call httpContext.TryGetRequest() twice again.

@snakefoot
Copy link
Contributor

snakefoot commented Oct 28, 2018

Alternative like this:

{
   var httpRequest = httpContext.TryGetRequest();
   if (httpRequest==null)
      return;

   string ip = CheckForwardedForHeader ? TryLookupForwardHeader(httpRequest) : string.Empty;
   if (string.IsNullOrEmpty(ip))
   {
#if !ASP_NET_CORE
      ip = httpRequest.ServerVariables["REMOTE_ADDR"];
#else
      ip = httpContext.Connection?.RemoteIpAddress;
#endif
   }
   builder.Append(ip);
}

#if !ASP_NET_CORE
string TryLookupForwardHeader(System.Web.HttpRequestBase httpRequest)
{
    // New logic
}
#else
string TryLookupForwardHeader(Microsoft.AspNetCore.Http.HttpRequest httpRequest)
{
    // New logic
}
#endif

@Giorgi
Copy link
Contributor Author

Giorgi commented Oct 29, 2018

OK, I don't fully agree with that but I can do it.

@snakefoot
Copy link
Contributor

Thank you. All lights have become green. Now just need the final approval stamp from @304NotModified

@304NotModified 304NotModified changed the title Added an option to AspNetRequestIpLayoutRenderer to check for X-Forwarded-For header ${AspNetRequestIp} added CheckForwardedForHeader to check for X-Forwarded-For header Oct 31, 2018
@304NotModified
Copy link
Member

Thanks for the contribution! Also thanks @snakefoot for the reviews! 👍 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ASP.NET Core ASP.NET Core - all versions ASP.NET 4 ASP.NET MVC Classic feature size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants