From 2d6b382ce75bc551b3055c08e568825128ba3757 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Fri, 6 Aug 2021 10:41:21 +0200 Subject: [PATCH] Added support for Ip Ranges --- .../Services/Implement}/BasicAuthService.cs | 4 ++-- src/Umbraco.Infrastructure/Umbraco.Infrastructure.csproj | 1 + .../Umbraco.Core/Services/BasicAuthServiceTests.cs | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) rename src/{Umbraco.Core/Services => Umbraco.Infrastructure/Services/Implement}/BasicAuthService.cs (84%) diff --git a/src/Umbraco.Core/Services/BasicAuthService.cs b/src/Umbraco.Infrastructure/Services/Implement/BasicAuthService.cs similarity index 84% rename from src/Umbraco.Core/Services/BasicAuthService.cs rename to src/Umbraco.Infrastructure/Services/Implement/BasicAuthService.cs index 99a6f930c5..a685e4baca 100644 --- a/src/Umbraco.Core/Services/BasicAuthService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/BasicAuthService.cs @@ -2,7 +2,7 @@ using System.Net; using Microsoft.Extensions.Options; using Umbraco.Cms.Core.Configuration.Models; -namespace Umbraco.Cms.Core.Services +namespace Umbraco.Cms.Core.Services.Implement { public class BasicAuthService : IBasicAuthService { @@ -21,7 +21,7 @@ namespace Umbraco.Cms.Core.Services { foreach (var allowedIpString in _basicAuthSettings.AllowedIPs) { - if(IPAddress.TryParse(allowedIpString, out var allowedIp) && clientIpAddress.Equals(allowedIp)) + if(IPNetwork.TryParse(allowedIpString, out var allowedIp) && allowedIp.Contains(clientIpAddress)) { return true; }; diff --git a/src/Umbraco.Infrastructure/Umbraco.Infrastructure.csproj b/src/Umbraco.Infrastructure/Umbraco.Infrastructure.csproj index 712323656d..47af185872 100644 --- a/src/Umbraco.Infrastructure/Umbraco.Infrastructure.csproj +++ b/src/Umbraco.Infrastructure/Umbraco.Infrastructure.csproj @@ -19,6 +19,7 @@ + diff --git a/src/Umbraco.Tests.UnitTests/Umbraco.Core/Services/BasicAuthServiceTests.cs b/src/Umbraco.Tests.UnitTests/Umbraco.Core/Services/BasicAuthServiceTests.cs index f406acc03d..603ccfd9a1 100644 --- a/src/Umbraco.Tests.UnitTests/Umbraco.Core/Services/BasicAuthServiceTests.cs +++ b/src/Umbraco.Tests.UnitTests/Umbraco.Core/Services/BasicAuthServiceTests.cs @@ -4,7 +4,7 @@ using Microsoft.Extensions.Options; using Moq; using NUnit.Framework; using Umbraco.Cms.Core.Configuration.Models; -using Umbraco.Cms.Core.Services; +using Umbraco.Cms.Core.Services.Implement; namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Services { @@ -24,6 +24,8 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Services [TestCase("::1", "1.1.1.1, ::1", ExpectedResult = true)] [TestCase("127.0.0.1", "127.0.0.1, ::1", ExpectedResult = true)] [TestCase("127.0.0.1", "", ExpectedResult = false)] + [TestCase("125.125.125.1", "125.125.125.0/24", ExpectedResult = true)] + [TestCase("125.125.124.1", "125.125.125.0/24", ExpectedResult = false)] public bool IsBasicAuthEnabled(string clientIpAddress, string commaSeperatedAllowlist) { var allowedIPs = commaSeperatedAllowlist.Split(",").Select(x=>x.Trim()).ToArray();