Skip to content

Commit

Permalink
If IP address is missing behaviour is according to general block rule
Browse files Browse the repository at this point in the history
  • Loading branch information
msmolka committed Feb 26, 2017
1 parent df6d3e7 commit a67e9a7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ When you install the package, it should be added to your `package.json`. Alterna
```json
{
"dependencies" : {
"ZNetCS.AspNetCore.IPFiltering": "1.0.0"
"ZNetCS.AspNetCore.IPFiltering": "1.0.1"
}
}
```
Expand Down
19 changes: 11 additions & 8 deletions src/ZNetCS.AspNetCore.IPFiltering/Internal/IPAddressChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ public class IPAddressChecker : IIPAddressChecker
/// <inheritdoc />
public virtual bool IsAllowed(IPAddress ipAddress, IPFilteringOptions options)
{
var whitelist = options.Whitelist.Select(IPAddressRange.Parse).ToList();
var blacklist = options.Blacklist.Select(IPAddressRange.Parse).ToList();

switch (options.DefaultBlockLevel)
if (ipAddress != null)
{
case DefaultBlockLevel.All:
return whitelist.Any(r => r.Contains(ipAddress)) && !blacklist.Any(r => r.Contains(ipAddress));
var whitelist = options.Whitelist.Select(IPAddressRange.Parse).ToList();
var blacklist = options.Blacklist.Select(IPAddressRange.Parse).ToList();

switch (options.DefaultBlockLevel)
{
case DefaultBlockLevel.All:
return whitelist.Any(r => r.Contains(ipAddress)) && !blacklist.Any(r => r.Contains(ipAddress));

case DefaultBlockLevel.None:
return !blacklist.Any(r => r.Contains(ipAddress)) || whitelist.Any(r => r.Contains(ipAddress));
case DefaultBlockLevel.None:
return !blacklist.Any(r => r.Contains(ipAddress)) || whitelist.Any(r => r.Contains(ipAddress));
}
}

return options.DefaultBlockLevel == DefaultBlockLevel.None;
Expand Down
2 changes: 1 addition & 1 deletion src/ZNetCS.AspNetCore.IPFiltering/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.0.0",
"version": "1.0.1",
"packOptions": {
"owners": [ "Marcin Smółka" ],
"licenseUrl": "https://raw.githubusercontent.com/msmolka/ZNetCS.AspNetCore.IPFiltering/master/LICENSE",
Expand Down
37 changes: 37 additions & 0 deletions test/ZNetCS.AspNetCore.IPFilteringTest/CodeFilteringTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ public async Task CodeAllowForwardedForOnWhitelistTest()
}
}

/// <summary>
/// The code allow no IP test.
/// </summary>
[TestMethod]
public async Task CodeAllowNoIPTest()
{
using (var server = new TestServer(WebHostBuilderHelper.CreateCodeBuilder(new IPFilteringOptions { DefaultBlockLevel = DefaultBlockLevel.None })))
{
// Act
RequestBuilder request = server.CreateRequest("/");

HttpResponseMessage response = await request.SendAsync("PUT");

// Arrange
response.EnsureSuccessStatusCode();
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "StatusCode != OK");
}
}

/// <summary>
/// The allow all test.
/// </summary>
Expand Down Expand Up @@ -335,6 +354,24 @@ public async Task CodeBlockForwarderForNotOnWhitelistTest()
}
}

/// <summary>
/// The code block no IP test.
/// </summary>
[TestMethod]
public async Task CodeBlockNoIPTest()
{
using (var server = new TestServer(WebHostBuilderHelper.CreateCodeBuilder()))
{
// Act
RequestBuilder request = server.CreateRequest("/");

HttpResponseMessage response = await request.SendAsync("PUT");

// Arrange
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode, "StatusCode != NotFound");
}
}

/// <summary>
/// The code block not on whitelist 1 test.
/// </summary>
Expand Down

0 comments on commit a67e9a7

Please sign in to comment.