Skip to content

Commit

Permalink
Merge remote-tracking branch 'CrumpledDog/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
ja0b committed Jul 8, 2022
2 parents fa3826d + 91b823f commit c982698
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
25 changes: 24 additions & 1 deletion src/Cogworks.SiteLock/Web/HttpModules/RequestProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public void ProcessRequest(HttpContextBase httpContext)
var urlReferrer = httpContext.Request.UrlReferrer;
var ipAddress = httpContext.Request.GetIpAddress();

if (!RequestHelper.IsLockedDomain(_config, requestUri.Host)) { return; }
if (RequestHelper.IsLockedDomain(_config, requestUri.Host))
{
if (RequestHelper.IsAllowedIP(_config, GetUserHostAddress(httpContext))) { return; }

if (RequestHelper.IsAllowedIP(_config, ipAddress)) { return; }

Expand All @@ -42,5 +44,26 @@ public void ProcessRequest(HttpContextBase httpContext)
httpContext.Response.StatusCode = 403;
throw new HttpException(403, "Locked by Cogworks.SiteLock Module");
}

/// <summary>
/// Attempt to get the IP address of the client (as a string)
/// </summary>
/// <returns></returns>
private static string GetUserHostAddress(HttpContextBase httpContext)
{
string ipAddress = httpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (!string.IsNullOrEmpty(ipAddress))
{
string[] ipAddresses = ipAddress.Split(',');

if (ipAddresses.Length != 0)
{
return ipAddresses[0];
}
}

return httpContext.Request.ServerVariables["REMOTE_ADDR"];
}
}
}
6 changes: 4 additions & 2 deletions src/Tests/Cogworks.SiteLock.Test/When_Processing_Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public When_Processing_Request()

_uriStub = new Uri("http://thecogworks.com" + AbsolutePath);
_httpRequestMock.Setup(x => x.Url).Returns(_uriStub);

_httpRequestMock.Setup(x => x.UserHostAddress).Returns("8.8.8.8");
_httpRequestMock.Setup(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection{
{ "HTTP_X_FORWARDED_FOR", "8.8.8.8, 4.4.4.4:18104" },
{ "REMOTE_ADDR", "8.8.8.8" }
});

_contextMock.Setup(x => x.Request).Returns(_httpRequestMock.Object);
_contextMock.Setup(x => x.Response).Returns(_httpResponseMock.Object);
Expand Down

0 comments on commit c982698

Please sign in to comment.