-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Follow up #1172 #1849: Fix cache issue. IP filtering helpers #1221
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
base: develop
Are you sure you want to change the base?
Changes from all commits
b4441c2
64ed0f3
0cf7e01
a2de83c
604f223
9892661
9a1ad24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,66 @@ | ||||||
using Microsoft.AspNetCore.Http; | ||||||
using Microsoft.Extensions.Primitives; | ||||||
|
||||||
namespace Ocelot.Security; | ||||||
|
||||||
public static class SecurityPolicyExtensions | ||||||
{ | ||||||
public static string GetClientIpAddress(this HttpContext httpContext, bool tryUseXForwardHeader = true) | ||||||
{ | ||||||
if (httpContext == null) | ||||||
{ | ||||||
return null; | ||||||
} | ||||||
|
||||||
string ip = null; | ||||||
|
||||||
// X-Forwarded-For => Using the First entry in the list | ||||||
if (string.IsNullOrWhiteSpace(ip) && tryUseXForwardHeader) | ||||||
{ | ||||||
ip = httpContext.GetHeaderValue("X-Forwarded-For").SplitCsv().FirstOrDefault(); | ||||||
} | ||||||
|
||||||
// RemoteIpAddress is always null in DNX RC1 Update1 (bug). | ||||||
if (string.IsNullOrWhiteSpace(ip) && httpContext.Connection?.RemoteIpAddress != null) | ||||||
{ | ||||||
ip = httpContext.Connection.RemoteIpAddress.ToString(); | ||||||
} | ||||||
|
||||||
if (string.IsNullOrWhiteSpace(ip)) | ||||||
{ | ||||||
ip = httpContext.GetHeaderValue("REMOTE_ADDR"); | ||||||
} | ||||||
|
||||||
if (ip == "::1") | ||||||
{ | ||||||
ip = "127.0.0.1"; | ||||||
} | ||||||
|
||||||
return ip; | ||||||
} | ||||||
|
||||||
public static string GetHeaderValue(this HttpContext httpContext, string headerName) | ||||||
{ | ||||||
if (httpContext?.Request?.Headers?.TryGetValue(headerName, out StringValues values) ?? false) | ||||||
{ | ||||||
return values.ToString(); | ||||||
} | ||||||
|
||||||
return string.Empty; | ||||||
} | ||||||
|
||||||
public static List<string> SplitCsv(this string csvList, bool nullOrWhitespaceInputReturnsNull = false) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be private There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...till the moment the author will write at least one unit test for this method. 🤣 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unless no one uses it outside of this class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensuring about strong safety by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this method is only use in this class, so, it must be private. There will always be time to make it public, if necessary |
||||||
{ | ||||||
if (string.IsNullOrWhiteSpace(csvList)) | ||||||
{ | ||||||
return nullOrWhitespaceInputReturnsNull ? null : new List<string>(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
return csvList | ||||||
.TrimEnd(',') | ||||||
.Split(',') | ||||||
.AsEnumerable() | ||||||
RaynaldM marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
.Select(s => s.Trim()) | ||||||
.ToList(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RaynaldM There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if @nurhat applies all these changes no problems compiling. I always recommend the use of Single-Dimensional Array because it is the most sober in memory and cpu. |
||||||
} | ||||||
} |
Uh oh!
There was an error while loading. Please reload this page.