-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20628 from abpframework/UriHelpers
Support wildcard domain in `AppUrlProvider `.
- Loading branch information
Showing
4 changed files
with
82 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
framework/test/Volo.Abp.Core.Tests/Volo/Abp/Http/UrlHelpers_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters