Skip to content

Make Kestrel config case-insensitive for certificates #23268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public ConfigurationReader(IConfiguration configuration)

private IDictionary<string, CertificateConfig> ReadCertificates()
{
var certificates = new Dictionary<string, CertificateConfig>(0);
var certificates = new Dictionary<string, CertificateConfig>(0, StringComparer.OrdinalIgnoreCase);

var certificatesConfig = _configuration.GetSection(CertificatesKey).GetChildren();
foreach (var certificateConfig in certificatesConfig)
Expand Down
33 changes: 33 additions & 0 deletions src/Servers/Kestrel/Kestrel/test/ConfigurationReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ public void ReadCertificatesSection_ReturnsCollection()
Assert.True(storeCert.AllowInvalid);
}

[Fact]
public void ReadCertificatesSection_IsCaseInsensitive()
{
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>("Certificates:filecert:Path", "/path/cert.pfx"),
new KeyValuePair<string, string>("Certificates:FILECERT:Password", "certpassword"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still pass if the entire key is UPPERCASE, right? I know this PR only changes the dictionary comparer, but I see no reason not to verify the rest of it case insensitive while we're at it.

}).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<ArgumentException>(() =>
new ConfigurationBuilder().AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>("Certificates:filecert:Password", "certpassword"),
new KeyValuePair<string, string>("Certificates:FILECERT:Password", "certpassword"),
}).Build());

Assert.Contains("An item with the same key has already been added", exception.Message);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This string is in Kestrel/Core/src/CoreStrings.resx - would it better to add a reference to that resource file to the .csproj?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CoreStrings is already InternalsVisibleTo to this project you just need to add a using for Microsoft.AspNetCore.Server.Kestrel.Core.

}

[Fact]
public void ReadEndpointsWhenNoEndpointsSection_ReturnsEmptyCollection()
{
Expand Down