Skip to content

Commit

Permalink
(chocolatey#3565) Do case insensitive comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
corbob committed Nov 25, 2024
1 parent 667fbc7 commit b7a345f
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public Task<CredentialResponse> GetAsync(Uri uri, IWebProxy proxy, CredentialReq

// If the user has specified --source with a *named* source and not a URL, try to find the matching one
// with the correct URL for this credential request.
var namedExplicitSources = _config.ExplicitSources?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
// Lower case all of the explicitly named sources so that we can use .Contains to compare them.
var namedExplicitSources = _config.ExplicitSources?.ToLower().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Where(s => !Uri.IsWellFormedUriString(s, UriKind.Absolute))
.ToList();

Expand All @@ -101,7 +102,9 @@ public Task<CredentialResponse> GetAsync(Uri uri, IWebProxy proxy, CredentialReq
// Uri.Equals() and == operator compare hostnames case-insensitively and the remainder of the url case-sensitively
// while ignoring #fragments on the URLs, but does care about trailing slashes, which we do not here.
source = _config.MachineSources
.Where(s => namedExplicitSources.Contains(s.Name) && new Uri(s.Key.TrimEnd('/')) == trimmedTargetUri)
.Where(s => namedExplicitSources.Contains(s.Name.ToLower())
&& Uri.TryCreate(s.Key.TrimEnd('/'), UriKind.Absolute, out var trimmedSourceUri)
&& Uri.Compare(trimmedSourceUri, trimmedTargetUri, UriComponents.HttpRequestUrl, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase) == 0)
.FirstOrDefault();
}

Expand All @@ -113,11 +116,12 @@ public Task<CredentialResponse> GetAsync(Uri uri, IWebProxy proxy, CredentialReq
// Note: This behaviour remains as removing it would be a breaking change, but we may want
// to remove this in a future version, as specifying an explicit URL should potentially
// not go looking in the configuration file for saved credentials anyway.
// See GitHub Issue: https://github.com/chocolatey/choco/issues/3573
var candidateSources = _config.MachineSources
.Where(s => !string.IsNullOrWhiteSpace(s.Username)
&& !string.IsNullOrWhiteSpace(s.EncryptedPassword)
&& Uri.TryCreate(s.Key.TrimEnd('/'), UriKind.Absolute, out var trimmedSourceUri)
&& trimmedSourceUri == trimmedTargetUri)
&& Uri.Compare(trimmedSourceUri, trimmedTargetUri, UriComponents.HttpRequestUrl, UriFormat.Unescaped, StringComparison.OrdinalIgnoreCase) == 0)
.ToList();

if (candidateSources.Count == 1)
Expand Down

0 comments on commit b7a345f

Please sign in to comment.