Skip to content

Commit fb4a489

Browse files
omusKristofferC
authored andcommitted
Fix issue where approved credentials were not sent to git credential helpers (#40161)
Due to the bug in the `GitCredential` constructor we were never writing credential data to the credential helper effectively making our use of git credential helpers read-only. (cherry picked from commit ac50ac6)
1 parent 270c982 commit fb4a489

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

stdlib/LibGit2/src/gitcredential.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ function GitCredential(cfg::GitConfig, url::AbstractString)
3030
fill!(cfg, parse(GitCredential, url))
3131
end
3232

33-
GitCredential(cred::UserPasswordCredential, url::AbstractString) = parse(GitCredential, url)
33+
function GitCredential(user_pass_cred::UserPasswordCredential, url::AbstractString)
34+
cred = parse(GitCredential, url)
35+
cred.username = user_pass_cred.user
36+
cred.password = deepcopy(user_pass_cred.pass)
37+
return cred
38+
end
3439

3540
Base.:(==)(c1::GitCredential, c2::GitCredential) = (c1.protocol, c1.host, c1.path, c1.username, c1.password, c1.use_http_path) ==
3641
(c2.protocol, c2.host, c2.path, c2.username, c2.password, c2.use_http_path)

stdlib/LibGit2/test/libgit2.jl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,23 @@ end
600600
github_regex_test("ssh://git@github.com/$user/$repo", user, repo)
601601
@test !occursin(LibGit2.GITHUB_REGEX, "git@notgithub.com/$user/$repo.git")
602602
end
603+
604+
@testset "UserPasswordCredential/url constructor" begin
605+
user_pass_cred = LibGit2.UserPasswordCredential("user", "*******")
606+
url = "https://github.com"
607+
expected_cred = LibGit2.GitCredential("https", "github.com", nothing, "user", "*******")
608+
609+
cred = LibGit2.GitCredential(user_pass_cred, url)
610+
@test cred == expected_cred
611+
612+
# Shredding the UserPasswordCredential shouldn't result in information being lost
613+
# inside of a GitCredential.
614+
Base.shred!(user_pass_cred)
615+
@test cred == expected_cred
616+
617+
Base.shred!(cred)
618+
Base.shred!(expected_cred)
619+
end
603620
end
604621

605622
mktempdir() do dir
@@ -2121,6 +2138,50 @@ mktempdir() do dir
21212138
end
21222139
end
21232140
end
2141+
2142+
@testset "approve/reject with UserPasswordCredential" begin
2143+
# In order to use the "store" credential helper `git` needs to be installed and
2144+
# on the path.
2145+
if GIT_INSTALLED
2146+
config_path = joinpath(dir, config_file)
2147+
isfile(config_path) && rm(config_path)
2148+
2149+
credential_path = joinpath(dir, ".git-credentials")
2150+
isfile(credential_path) && rm(credential_path)
2151+
2152+
LibGit2.with(LibGit2.GitConfig(config_path, LibGit2.Consts.CONFIG_LEVEL_APP)) do cfg
2153+
query = LibGit2.GitCredential("https", "mygithost")
2154+
filled = LibGit2.GitCredential("https", "mygithost", nothing, "alice", "1234")
2155+
user_pass_cred = LibGit2.UserPasswordCredential("alice", "1234")
2156+
url = "https://mygithost"
2157+
2158+
# Requires `git` to be installed and available on the path.
2159+
LibGit2.set!(cfg, "credential.helper", "store --file \"$credential_path\"")
2160+
helper = only(LibGit2.credential_helpers(cfg, query))
2161+
2162+
@test !isfile(credential_path)
2163+
2164+
Base.shred!(LibGit2.fill!(helper, deepcopy(query))) do result
2165+
@test result == query
2166+
end
2167+
2168+
LibGit2.approve(cfg, user_pass_cred, url)
2169+
@test isfile(credential_path)
2170+
Base.shred!(LibGit2.fill!(helper, deepcopy(query))) do result
2171+
@test result == filled
2172+
end
2173+
2174+
LibGit2.reject(cfg, user_pass_cred, url)
2175+
Base.shred!(LibGit2.fill!(helper, deepcopy(query))) do result
2176+
@test result == query
2177+
end
2178+
2179+
Base.shred!(query)
2180+
Base.shred!(filled)
2181+
Base.shred!(user_pass_cred)
2182+
end
2183+
end
2184+
end
21242185
end
21252186

21262187
# The following tests require that we can fake a TTY so that we can provide passwords

0 commit comments

Comments
 (0)