From e34469482c6424c5cd9cf3410489a65f6449430f Mon Sep 17 00:00:00 2001 From: William Godbe Date: Tue, 23 Jun 2020 23:40:38 -0700 Subject: [PATCH] Make Kestrel config case-insensitive for certificates (#23268) * Make Kestrel config case-insensitive for certificates * Move tests to ConfigurationReaderTests --- .../Core/src/Internal/ConfigurationReader.cs | 2 +- .../Kestrel/test/ConfigurationReaderTests.cs | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Servers/Kestrel/Core/src/Internal/ConfigurationReader.cs b/src/Servers/Kestrel/Core/src/Internal/ConfigurationReader.cs index 19ec02774a..ea9f9acec8 100644 --- a/src/Servers/Kestrel/Core/src/Internal/ConfigurationReader.cs +++ b/src/Servers/Kestrel/Core/src/Internal/ConfigurationReader.cs @@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal private IDictionary ReadCertificates() { - var certificates = new Dictionary(0); + var certificates = new Dictionary(0, StringComparer.OrdinalIgnoreCase); var certificatesConfig = _configuration.GetSection(CertificatesKey).GetChildren(); foreach (var certificateConfig in certificatesConfig) diff --git a/src/Servers/Kestrel/Kestrel/test/ConfigurationReaderTests.cs b/src/Servers/Kestrel/Kestrel/test/ConfigurationReaderTests.cs index a83e90818d..3328b08ce2 100644 --- a/src/Servers/Kestrel/Kestrel/test/ConfigurationReaderTests.cs +++ b/src/Servers/Kestrel/Kestrel/test/ConfigurationReaderTests.cs @@ -68,6 +68,39 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Tests Assert.True(storeCert.AllowInvalid); } + [Fact] + public void ReadCertificatesSection_IsCaseInsensitive() + { + var config = new ConfigurationBuilder().AddInMemoryCollection(new[] + { + new KeyValuePair("Certificates:filecert:Path", "/path/cert.pfx"), + new KeyValuePair("Certificates:FILECERT:Password", "certpassword"), + }).Build(); + var reader = new ConfigurationReader(config); + var certificates = reader.Certificates; + Assert.NotNull(certificates); + Assert.Equal(1, certificates.Count); + + var fileCert = certificates["FiLeCeRt"]; + Assert.True(fileCert.IsFileCert); + Assert.False(fileCert.IsStoreCert); + Assert.Equal("/path/cert.pfx", fileCert.Path); + Assert.Equal("certpassword", fileCert.Password); + } + + [Fact] + public void ReadCertificatesSection_ThrowsOnCaseInsensitiveDuplicate() + { + var exception = Assert.Throws(() => + new ConfigurationBuilder().AddInMemoryCollection(new[] + { + new KeyValuePair("Certificates:filecert:Password", "certpassword"), + new KeyValuePair("Certificates:FILECERT:Password", "certpassword"), + }).Build()); + + Assert.Contains("An item with the same key has already been added", exception.Message); + } + [Fact] public void ReadEndpointsWhenNoEndpointsSection_ReturnsEmptyCollection() {