Skip to content

Commit c26b7ef

Browse files
author
Andrew Omondi
committed
Validate hosts in collection
1 parent ef0cdda commit c26b7ef

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [1.7.6] - 2024-01-24
11+
12+
### Changed
13+
14+
- Improve AllowedHost validator to throw an error if `https://` or `http://` prefix is present in a allowed host value.(https://github.com/microsoft/kiota-abstractions-dotnet/issues/165)
15+
1016
## [1.7.5] - 2024-01-11
1117

1218
### Changed

Microsoft.Kiota.Abstractions.Tests/Authentication/AuthenticationTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading;
@@ -112,4 +112,25 @@ public void AllowedHostValidatorAllowsAllUrls(string urlToTest)
112112
Assert.True(validationResult);
113113
Assert.Empty(validator.AllowedHosts);
114114
}
115+
116+
[Theory]
117+
[InlineData("https://graph.microsoft.com")] // https
118+
[InlineData("http://graph.microsoft.us")] // http
119+
[InlineData("HTTPS://TEST.MICROSOFT.COM")] // https with upperCase
120+
[InlineData("http://TEST.MICROSOFT.COM")] // http with upperCase
121+
[InlineData("http://developer.microsoft.com,graph.microsoft.com")] // a valid and an invalid together
122+
public void AllowedHostValidatorThrowsArgumentExceptionOnNonValidHost(string commaSeparatedHosts)
123+
{
124+
// Test through the constructor
125+
// Arrange
126+
var urlStrings = commaSeparatedHosts.Split(new char [] { ',' }, StringSplitOptions.RemoveEmptyEntries);
127+
128+
// Assert constructor throws
129+
var exception = Assert.Throws<ArgumentException>(() => new AllowedHostsValidator(urlStrings));
130+
Assert.Equal("host should not contain http or https prefix", exception.Message);
131+
// Assert setter throws
132+
var validator = new AllowedHostsValidator();
133+
Assert.Throws<ArgumentException>(() => validator.AllowedHosts = urlStrings);
134+
Assert.Equal("host should not contain http or https prefix", exception.Message);
135+
}
115136
}

src/Microsoft.Kiota.Abstractions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<PackageProjectUrl>https://aka.ms/kiota/docs</PackageProjectUrl>
1515
<EmbedUntrackedSources>true</EmbedUntrackedSources>
1616
<Deterministic>true</Deterministic>
17-
<VersionPrefix>1.7.5</VersionPrefix>
17+
<VersionPrefix>1.7.6</VersionPrefix>
1818
<VersionSuffix></VersionSuffix>
1919
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2020
<SignAssembly>false</SignAssembly>

src/authentication/AllowedHostsValidator.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public class AllowedHostsValidator
2121
/// <param name="validHosts"> Collection of valid Hosts</param>
2222
public AllowedHostsValidator(IEnumerable<string>? validHosts = null)
2323
{
24-
_allowedHosts = new HashSet<string>(validHosts ?? Array.Empty<string>(), StringComparer.OrdinalIgnoreCase);
24+
validHosts ??= Array.Empty<string>();
25+
ValidateHosts(validHosts);
26+
_allowedHosts = new HashSet<string>(validHosts, StringComparer.OrdinalIgnoreCase);
2527
}
2628

2729
/// <summary>
@@ -33,6 +35,7 @@ public IEnumerable<string> AllowedHosts
3335
set
3436
{
3537
if(value is null) throw new ArgumentNullException(nameof(value));
38+
ValidateHosts(value);
3639
_allowedHosts = new HashSet<string>(value.Where(x => !string.IsNullOrEmpty(x)), StringComparer.OrdinalIgnoreCase);
3740
}
3841
}
@@ -49,5 +52,17 @@ public bool IsUrlHostValid(Uri uri)
4952
{
5053
return !_allowedHosts.Any() || _allowedHosts.Contains(uri.Host);
5154
}
55+
56+
private static void ValidateHosts(IEnumerable<string> hostsToValidate)
57+
{
58+
if(hostsToValidate is null)
59+
throw new ArgumentNullException(nameof(hostsToValidate));
60+
61+
if (hostsToValidate.Any(static host => host.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
62+
|| host.StartsWith("https://", StringComparison.OrdinalIgnoreCase)))
63+
{
64+
throw new ArgumentException("host should not contain http or https prefix");
65+
}
66+
}
5267
}
5368
}

0 commit comments

Comments
 (0)