mirror of https://github.com/abpframework/abp.git
committed by
GitHub
4 changed files with 82 additions and 4 deletions
@ -0,0 +1,28 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.Http; |
|||
|
|||
public static class UrlHelpers |
|||
{ |
|||
private const string WildcardSubdomain = "*."; |
|||
|
|||
public static bool IsSubdomainOf(string subdomain, string domain) |
|||
{ |
|||
if (Uri.TryCreate(subdomain, UriKind.Absolute, out var subdomainUri) && |
|||
Uri.TryCreate(domain.Replace(WildcardSubdomain, string.Empty), UriKind.Absolute, out var domainUri)) |
|||
{ |
|||
return domainUri == subdomainUri || IsSubdomainOf(subdomainUri, domainUri); |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public static bool IsSubdomainOf(Uri subdomain, Uri domain) |
|||
{ |
|||
return subdomain.IsAbsoluteUri |
|||
&& domain.IsAbsoluteUri |
|||
&& subdomain.Scheme == domain.Scheme |
|||
&& subdomain.Port == domain.Port |
|||
&& subdomain.Host.EndsWith($".{domain.Host}", StringComparison.Ordinal); |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
using Shouldly; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Http; |
|||
|
|||
public class UrlHelpers_Tests |
|||
{ |
|||
[Theory] |
|||
[InlineData(null)] |
|||
[InlineData("null")] |
|||
[InlineData("http://")] |
|||
[InlineData("http://*")] |
|||
[InlineData("http://.domain")] |
|||
[InlineData("http://.domain/hello")] |
|||
public void IsSubdomainOf_ReturnsFalseIfDomainIsMalformedUri(string domain) |
|||
{ |
|||
var actual = UrlHelpers.IsSubdomainOf("http://*.domain", domain); |
|||
actual.ShouldBeFalse(); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData("http://sub.domain", "http://domain")] |
|||
[InlineData("http://sub.domain", "http://*.domain")] |
|||
[InlineData("http://sub.sub.domain", "http://*.domain")] |
|||
[InlineData("http://sub.sub.domain", "http://*.sub.domain")] |
|||
[InlineData("http://sub.domain:4567", "http://*.domain:4567")] |
|||
public void IsSubdomainOf_ReturnsTrue_WhenASubdomain(string subdomain, string domain) |
|||
{ |
|||
var actual = UrlHelpers.IsSubdomainOf(subdomain, domain); |
|||
actual.ShouldBeTrue(); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData("http://sub.domain:1234", "http://*.domain:5678")] |
|||
[InlineData("http://sub.domain", "http://domain.*")] |
|||
[InlineData("http://sub.domain.hacker", "http://*.domain")] |
|||
[InlineData("https://sub.domain", "http://*.domain")] |
|||
public void IsSubdomainOf_ReturnsFalse_WhenNotASubdomain(string subdomain, string domain) |
|||
{ |
|||
var actual = UrlHelpers.IsSubdomainOf(subdomain, domain); |
|||
actual.ShouldBeFalse(); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue