Skip to content

Commit 5b76992

Browse files
LibGit2: respect NetworkOptions.verify_host (#38506)
1 parent fc2db3f commit 5b76992

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

stdlib/LibGit2/src/callbacks.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,26 @@ function fetchhead_foreach_callback(ref_name::Cstring, remote_url::Cstring,
359359
return Cint(0)
360360
end
361361

362+
function certificate_callback(
363+
cert_p :: Ptr{Cvoid},
364+
valid :: Cint,
365+
host_p :: Ptr{Cchar},
366+
user_p :: Ptr{Cvoid},
367+
)::Cint
368+
valid != 0 && return Consts.CERT_ACCEPT
369+
host = unsafe_string(host_p)
370+
cert_type = unsafe_load(convert(Ptr{Cint}, cert_p))
371+
transport = cert_type == Consts.CERT_TYPE_TLS ? "TLS" :
372+
cert_type == Consts.CERT_TYPE_SSH ? "SSH" : nothing
373+
verify = NetworkOptions.verify_host(host, transport)
374+
verify ? Consts.CERT_REJECT : Consts.CERT_ACCEPT
375+
end
376+
362377
"C function pointer for `mirror_callback`"
363378
mirror_cb() = @cfunction(mirror_callback, Cint, (Ptr{Ptr{Cvoid}}, Ptr{Cvoid}, Cstring, Cstring, Ptr{Cvoid}))
364379
"C function pointer for `credentials_callback`"
365380
credentials_cb() = @cfunction(credentials_callback, Cint, (Ptr{Ptr{Cvoid}}, Cstring, Cstring, Cuint, Any))
366381
"C function pointer for `fetchhead_foreach_callback`"
367382
fetchhead_foreach_cb() = @cfunction(fetchhead_foreach_callback, Cint, (Cstring, Cstring, Ptr{GitHash}, Cuint, Any))
383+
"C function pointer for `certificate_callback`"
384+
certificate_cb() = @cfunction(certificate_callback, Cint, (Ptr{Cvoid}, Cint, Ptr{Cchar}, Ptr{Cvoid}))

stdlib/LibGit2/src/consts.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@ const STATUS_OPT_UPDATE_INDEX = Cuint(1 << 13)
308308
const STATUS_OPT_INCLUDE_UNREADABLE = Cuint(1 << 14)
309309
const STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED = Cuint(1 << 15)
310310

311+
# certificate types from `enum git_cert_t` in `cert.h`.
312+
const CERT_TYPE_TLS = 1 # GIT_CERT_X509
313+
const CERT_TYPE_SSH = 2 # GIT_CERT_HOSTKEY_LIBSSH2
314+
315+
# certificate callback return values
316+
const CERT_REJECT = -1
317+
const CERT_ACCEPT = 0
318+
311319
@enum(GIT_SUBMODULE_IGNORE, SUBMODULE_IGNORE_UNSPECIFIED = -1, # use the submodule's configuration
312320
SUBMODULE_IGNORE_NONE = 1, # any change or untracked == dirty
313321
SUBMODULE_IGNORE_UNTRACKED = 2, # dirty if tracked files change

stdlib/LibGit2/src/types.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ Matches the [`git_remote_callbacks`](https://libgit2.org/libgit2/#HEAD/type/git_
222222
sideband_progress::Ptr{Cvoid} = C_NULL
223223
completion::Ptr{Cvoid} = C_NULL
224224
credentials::Ptr{Cvoid} = C_NULL
225-
certificate_check::Ptr{Cvoid} = C_NULL
225+
certificate_check::Ptr{Cvoid} = certificate_cb()
226226
transfer_progress::Ptr{Cvoid} = C_NULL
227227
update_tips::Ptr{Cvoid} = C_NULL
228228
pack_progress::Ptr{Cvoid} = C_NULL
@@ -310,7 +310,7 @@ julia> fetch(remote, "master", options=fo)
310310
proxytype::Consts.GIT_PROXY = Consts.PROXY_AUTO
311311
url::Cstring = Cstring(C_NULL)
312312
credential_cb::Ptr{Cvoid} = C_NULL
313-
certificate_cb::Ptr{Cvoid} = C_NULL
313+
certificate_cb::Ptr{Cvoid} = certificate_cb()
314314
payload::Any = nothing
315315
end
316316
@assert ProxyOptions.isinlinealloc

0 commit comments

Comments
 (0)