Skip to content

Commit be167aa

Browse files
committed
Fix segfault with SSH agent and null username
1 parent 6ab861f commit be167aa

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

base/libgit2/callbacks.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function authenticate_ssh(libgit2credptr::Ptr{Ptr{Void}}, p::CredentialPayload,
5757
end
5858

5959
# first try ssh-agent if credentials support its usage
60-
if p.use_ssh_agent == 'Y'
60+
if p.use_ssh_agent == 'Y' && username_ptr != Cstring(C_NULL)
6161
err = ccall((:git_cred_ssh_key_from_agent, :libgit2), Cint,
6262
(Ptr{Ptr{Void}}, Cstring), libgit2credptr, username_ptr)
6363
if err == 0

test/libgit2.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,39 @@ mktempdir() do dir
19021902
@test auth_attempts == 2
19031903
end
19041904

1905+
@testset "SSH agent username" begin
1906+
url = "github.com:test/package.jl"
1907+
1908+
valid_key = joinpath(KEY_DIR, "valid")
1909+
username = "git"
1910+
1911+
ssh_empty_ex = quote
1912+
include($LIBGIT2_HELPER_PATH)
1913+
valid_cred = LibGit2.SSHCredentials($username, "", $valid_key, $(valid_key * ".pub"))
1914+
credential_loop(valid_cred, $url, Nullable(""), use_ssh_agent=true)
1915+
end
1916+
1917+
ssh_null_ex = quote
1918+
include($LIBGIT2_HELPER_PATH)
1919+
valid_cred = LibGit2.SSHCredentials($username, "", $valid_key, $(valid_key * ".pub"))
1920+
credential_loop(valid_cred, $url, Nullable(), use_ssh_agent=true)
1921+
end
1922+
1923+
challenges = [
1924+
"Username for 'github.com':" => "\x04",
1925+
]
1926+
1927+
err, auth_attempts = challenge_prompt(ssh_empty_ex, challenges)
1928+
@test err == abort_prompt # TODO: `eauth_error` when we can disable prompting
1929+
@test auth_attempts == 2
1930+
1931+
# A null username_ptr passed into `git_cred_ssh_key_from_agent` can cause a
1932+
# segfault.
1933+
err, auth_attempts = challenge_prompt(ssh_null_ex, challenges)
1934+
@test err == abort_prompt # TODO: `eauth_error` when we can disable prompting
1935+
@test auth_attempts == 1
1936+
end
1937+
19051938
@testset "SSH explicit credentials" begin
19061939
url = "git@github.com:test/package.jl"
19071940

0 commit comments

Comments
 (0)