Skip to content

Commit e344694

Browse files
authored
Make Kestrel config case-insensitive for certificates (dotnet#23268)
* Make Kestrel config case-insensitive for certificates * Move tests to ConfigurationReaderTests
1 parent e73e3a8 commit e344694

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Servers/Kestrel/Core/src/Internal/ConfigurationReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public ConfigurationReader(IConfiguration configuration)
3939

4040
private IDictionary<string, CertificateConfig> ReadCertificates()
4141
{
42-
var certificates = new Dictionary<string, CertificateConfig>(0);
42+
var certificates = new Dictionary<string, CertificateConfig>(0, StringComparer.OrdinalIgnoreCase);
4343

4444
var certificatesConfig = _configuration.GetSection(CertificatesKey).GetChildren();
4545
foreach (var certificateConfig in certificatesConfig)

src/Servers/Kestrel/Kestrel/test/ConfigurationReaderTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,39 @@ public void ReadCertificatesSection_ReturnsCollection()
6868
Assert.True(storeCert.AllowInvalid);
6969
}
7070

71+
[Fact]
72+
public void ReadCertificatesSection_IsCaseInsensitive()
73+
{
74+
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
75+
{
76+
new KeyValuePair<string, string>("Certificates:filecert:Path", "/path/cert.pfx"),
77+
new KeyValuePair<string, string>("Certificates:FILECERT:Password", "certpassword"),
78+
}).Build();
79+
var reader = new ConfigurationReader(config);
80+
var certificates = reader.Certificates;
81+
Assert.NotNull(certificates);
82+
Assert.Equal(1, certificates.Count);
83+
84+
var fileCert = certificates["FiLeCeRt"];
85+
Assert.True(fileCert.IsFileCert);
86+
Assert.False(fileCert.IsStoreCert);
87+
Assert.Equal("/path/cert.pfx", fileCert.Path);
88+
Assert.Equal("certpassword", fileCert.Password);
89+
}
90+
91+
[Fact]
92+
public void ReadCertificatesSection_ThrowsOnCaseInsensitiveDuplicate()
93+
{
94+
var exception = Assert.Throws<ArgumentException>(() =>
95+
new ConfigurationBuilder().AddInMemoryCollection(new[]
96+
{
97+
new KeyValuePair<string, string>("Certificates:filecert:Password", "certpassword"),
98+
new KeyValuePair<string, string>("Certificates:FILECERT:Password", "certpassword"),
99+
}).Build());
100+
101+
Assert.Contains("An item with the same key has already been added", exception.Message);
102+
}
103+
71104
[Fact]
72105
public void ReadEndpointsWhenNoEndpointsSection_ReturnsEmptyCollection()
73106
{

0 commit comments

Comments
 (0)