Skip to content
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
17 changes: 15 additions & 2 deletions src/Paket.Core/Versioning/ConfigFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,24 @@ let checkCredentials(url, cred) =
true
with _ -> false

let getSourceNodes (credentialsNode : XmlNode) (source : string) nodeType =
/// Normalizes a source URL for credential-lookup comparisons only (does not change what URL is
/// actually used for requests). Strips a trailing slash and a "www." host prefix so that e.g.
/// credentials stored for "https://www.nuget.org" are found when looking up "https://nuget.org"
/// and vice versa (see https://github.com/fsprojects/Paket/issues/3843).
let private normalizeSourceForComparison (source : string) =
let source = source.TrimEnd([|'/'|])
let wwwPrefix = "://www."
let idx = source.IndexOf(wwwPrefix, StringComparison.OrdinalIgnoreCase)
if idx >= 0 then
source.Remove(idx + 3, 4) // remove "www." while keeping the leading "://"
else
source

let getSourceNodes (credentialsNode : XmlNode) (source : string) nodeType =
let source = normalizeSourceForComparison source
sprintf "//%s" nodeType |> credentialsNode.SelectNodes
|> Seq.cast<XmlElement>
|> Seq.filter (fun n -> n.Attributes.["source"].Value.TrimEnd([|'/'|]) = source)
|> Seq.filter (fun n -> normalizeSourceForComparison n.Attributes.["source"].Value = source)
|> Seq.toList

let private getCredentialsNode = lazy(getConfigNode "credentials" |> returnOrFail)
Expand Down
18 changes: 17 additions & 1 deletion tests/Paket.Tests/Versioning/ConfigFileSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,20 @@ let ``get token from node``() =
node.SetAttribute("value", "demotoken")
let (Token token) = getAuthFromNode node

token |> shouldEqual "demotoken"
token |> shouldEqual "demotoken"

[<Test>]
let ``get source nodes ignores www prefix mismatch for nuget.org``() =
let doc = sampleDoc()
let node = doc.CreateElement("credential")
node.SetAttribute("source", "https://www.nuget.org")
doc.DocumentElement.AppendChild(node) |> ignore

// a token stored for https://www.nuget.org (as documented via `paket config add-token`)
// should be found when looking up the default push URL https://nuget.org (no www)
// and vice versa. See https://github.com/fsprojects/Paket/issues/3843
let nodesWithoutWww = getSourceNodes doc "https://nuget.org" "credential"
let nodesWithWww = getSourceNodes doc "https://www.nuget.org" "credential"

nodesWithoutWww.Length |> shouldEqual 1
nodesWithWww.Length |> shouldEqual 1
Loading