Skip to content

Commit

Permalink
Fix issue with Git Credential Manager for Windows (#30195)
Browse files Browse the repository at this point in the history
- Ignores additional whitespace when reading in credentials.
- Warn when encountering unknown attributes. Should allow for more
  graceful failures in the future if the protocol is extended.
  • Loading branch information
omus authored and ararslan committed Nov 29, 2018
1 parent 3f8c311 commit 4b270b1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
7 changes: 6 additions & 1 deletion stdlib/LibGit2/src/gitcredential.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

const GIT_CRED_ATTRIBUTES = ("protocol", "host", "path", "username", "password", "url")

"""
GitCredential
Expand Down Expand Up @@ -111,16 +113,19 @@ function Base.read!(io::IO, cred::GitCredential)
else
value = readuntil(io, '\n')
end

if key == "url"
# Any components which are missing from the URL will be set to empty
# https://git-scm.com/docs/git-credential#git-credential-codeurlcode
Base.shred!(parse(GitCredential, value)) do urlcred
copy!(cred, urlcred)
end
else
elseif key in GIT_CRED_ATTRIBUTES
field = getproperty(cred, Symbol(key))
field !== nothing && Symbol(key) == :password && Base.shred!(field)
setproperty!(cred, Symbol(key), value)
elseif !all(isspace, key)
@warn "Unknown git credential attribute found: $(repr(key))"
end
end

Expand Down
39 changes: 39 additions & 0 deletions stdlib/LibGit2/test/libgit2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,45 @@ end
Base.shred!(expected_cred)
end

@testset "extra newline" begin
# The "Git for Windows" installer will also install the "Git Credential Manager for
# Windows" (https://github.com/Microsoft/Git-Credential-Manager-for-Windows) (also
# known as "manager" in the .gitconfig files). This credential manager returns an
# additional newline when returning the results.
str = """
protocol=https
host=example.com
path=
username=bob
password=*****
"""
expected_cred = LibGit2.GitCredential("https", "example.com", "", "bob", "*****")

cred = read!(IOBuffer(str), LibGit2.GitCredential())
@test cred == expected_cred
@test sprint(write, cred) * "\n" == str
Base.shred!(cred)
Base.shred!(expected_cred)
end

@testset "unknown attribute" begin
str = """
protocol=https
host=example.com
attribute=value
username=bob
password=*****
"""
expected_cred = LibGit2.GitCredential("https", "example.com", nothing, "bob", "*****")
expected_log = (:warn, "Unknown git credential attribute found: \"attribute\"")

cred = @test_logs expected_log read!(IOBuffer(str), LibGit2.GitCredential())
@test cred == expected_cred
Base.shred!(cred)
Base.shred!(expected_cred)
end

@testset "use http path" begin
cred = LibGit2.GitCredential("https", "example.com", "dir/file", "alice", "*****")
expected = """
Expand Down

0 comments on commit 4b270b1

Please sign in to comment.