Added null check to CorsPolicyBuilder (#19831)
* Added null check to CorsPolicyBuilder * Replaced string.IsNullOrEmpty with null check * Added inner null check + updated unit test #19830 * Unit Tests: Reverted Theory to separate Facts * Strongly typed args Co-authored-by: Chris Pickford <chris.pickford@aldautomotive.com>
This commit is contained in:
parent
a57943a443
commit
3eb778fb41
|
|
@ -54,6 +54,11 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public CorsPolicyBuilder WithOrigins(params string[] origins)
|
public CorsPolicyBuilder WithOrigins(params string[] origins)
|
||||||
{
|
{
|
||||||
|
if (origins is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(origins));
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var origin in origins)
|
foreach (var origin in origins)
|
||||||
{
|
{
|
||||||
var normalizedOrigin = GetNormalizedOrigin(origin);
|
var normalizedOrigin = GetNormalizedOrigin(origin);
|
||||||
|
|
@ -65,6 +70,11 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
|
|
||||||
internal static string GetNormalizedOrigin(string origin)
|
internal static string GetNormalizedOrigin(string origin)
|
||||||
{
|
{
|
||||||
|
if (origin is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(origin));
|
||||||
|
}
|
||||||
|
|
||||||
if (Uri.TryCreate(origin, UriKind.Absolute, out var uri) &&
|
if (Uri.TryCreate(origin, UriKind.Absolute, out var uri) &&
|
||||||
(uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps) &&
|
(uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps) &&
|
||||||
!string.Equals(uri.IdnHost, uri.Host, StringComparison.Ordinal))
|
!string.Equals(uri.IdnHost, uri.Host, StringComparison.Ordinal))
|
||||||
|
|
|
||||||
|
|
@ -139,6 +139,28 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
|
||||||
Assert.Equal(new List<string>() { "http://www.example.com", "https://example2.com" }, corsPolicy.Origins);
|
Assert.Equal(new List<string>() { "http://www.example.com", "https://example2.com" }, corsPolicy.Origins);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WithOrigins_ThrowsIfArgumentNull()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var builder = new CorsPolicyBuilder();
|
||||||
|
string[] args = null;
|
||||||
|
|
||||||
|
// Act / Assert
|
||||||
|
Assert.Throws<ArgumentNullException>(() => builder.WithOrigins(args));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WithOrigins_ThrowsIfArgumentArrayContainsNull()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var builder = new CorsPolicyBuilder();
|
||||||
|
string[] args = new string[] { null };
|
||||||
|
|
||||||
|
// Act / Assert
|
||||||
|
Assert.Throws<ArgumentNullException>(() => builder.WithOrigins(args));
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void AllowAnyOrigin_AllowsAny()
|
public void AllowAnyOrigin_AllowsAny()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue