Skip to content

Commit 145ff43

Browse files
visrKristofferC
authored andcommitted
Don't error when initializing LibGit2 with CA roots path (#56924)
When SSL_CERT_FILE or SSL_CERT_DIR is set, it is [impossible to set this location](https://github.com/libgit2/libgit2/blob/4dcdb64c6844d76776745cdc25071a72c1af84d6/src/libgit2/settings.c#L206-L222) in LibGit2_jll on Apple and Windows because [it isn't built with support for that](https://github.com/JuliaPackaging/Yggdrasil/blob/7123a60a68102ba6cd953e13a4e45845dc37fd82/L/LibGit2/build_tarballs.jl#L67). Until now we've errored out with a message telling users to set JULIA_SSL_CA_ROOTS_PATH to an empty string, which is a somewhat problematic workaround because the Windows environment variables UI doesn't allow empty values, and [setting it to an empty string from PowerShell unsets it](https://discourse.julialang.org/t/how-to-fix-ssl-cert-issues-in-pkg/115495/7?u=visr). This PR changes the behavior to allow this expected error. Variables like SSL_CERT_FILE are for instance [set by the Conda OpenSSL package on environment activation](https://github.com/conda-forge/openssl-feedstock/blob/83b5e2a793bc95d19e6cc2d9d28068f1a6ff6b79/recipe/activate-win.ps1) used by e.g. Python, ensuring many people cannot use Pkg operations that use LibGit2, like `dev Example`, `add Example#master`. See more user reports [on Discourse](https://discourse.julialang.org/search?q=JULIA_SSL_CA_ROOTS_PATH). Together with JuliaLang/NetworkOptions.jl#37 this should improve the experience of users trying out Julia from a Conda environment. This should also be fine to backport. (cherry picked from commit 7fa969a)
1 parent da85988 commit 145ff43

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

stdlib/LibGit2/src/LibGit2.jl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,24 +1042,20 @@ function set_ssl_cert_locations(cert_loc)
10421042
else # files, /dev/null, non-existent paths, etc.
10431043
cert_file = cert_loc
10441044
end
1045-
ret = @ccall libgit2.git_libgit2_opts(
1045+
ret = @ccall libgit2.git_libgit2_opts(
10461046
Consts.SET_SSL_CERT_LOCATIONS::Cint;
10471047
cert_file::Cstring,
10481048
cert_dir::Cstring)::Cint
10491049
ret >= 0 && return ret
1050+
# On macOS and Windows LibGit2_jll is built without a TLS backend that supports
1051+
# certificate locations; don't throw on this expected error so we allow certificate
1052+
# location environment variables to be set for other purposes.
1053+
# We still try doing so to support other LibGit2 builds.
10501054
err = Error.GitError(ret)
10511055
err.class == Error.SSL &&
10521056
err.msg == "TLS backend doesn't support certificate locations" ||
10531057
throw(err)
1054-
var = nothing
1055-
for v in NetworkOptions.CA_ROOTS_VARS
1056-
haskey(ENV, v) && (var = v)
1057-
end
1058-
@assert var !== nothing # otherwise we shouldn't be here
1059-
msg = """
1060-
Your Julia is built with a SSL/TLS engine that libgit2 doesn't know how to configure to use a file or directory of certificate authority roots, but your environment specifies one via the $var variable. If you believe your system's root certificates are safe to use, you can `export JULIA_SSL_CA_ROOTS_PATH=""` in your environment to use those instead.
1061-
"""
1062-
throw(Error.GitError(err.class, err.code, chomp(msg)))
1058+
return ret
10631059
end
10641060

10651061
"""

stdlib/LibGit2/test/bad_ca_roots.jl

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,24 @@ const CAN_SET_CA_ROOTS_PATH = !Sys.isapple() && !Sys.iswindows()
1212
# Given this is a sub-processed test file, not using @testsets avoids
1313
# leaking the report print into the Base test runner report
1414
begin # empty CA roots file
15-
# these fail for different reasons on different platforms:
16-
# - on Apple & Windows you cannot set the CA roots path location
17-
# - on Linux & FreeBSD you you can but these are invalid files
15+
# different behavior on different platforms:
16+
# - on Apple & Windows you cannot set the CA roots path location; don't error
17+
# - on Linux & FreeBSD you can but these are invalid files
18+
1819
ENV["JULIA_SSL_CA_ROOTS_PATH"] = "/dev/null"
19-
@test_throws LibGit2.GitError LibGit2.ensure_initialized()
20+
if CAN_SET_CA_ROOTS_PATH
21+
@test_throws LibGit2.GitError LibGit2.ensure_initialized()
22+
else
23+
@test LibGit2.ensure_initialized() === nothing
24+
end
25+
2026
ENV["JULIA_SSL_CA_ROOTS_PATH"] = tempname()
21-
@test_throws LibGit2.GitError LibGit2.ensure_initialized()
22-
# test that it still fails if called a second time
23-
@test_throws LibGit2.GitError LibGit2.ensure_initialized()
24-
if !CAN_SET_CA_ROOTS_PATH
25-
# test that this doesn't work on macOS & Windows
26-
ENV["JULIA_SSL_CA_ROOTS_PATH"] = NetworkOptions.bundled_ca_roots()
27+
if CAN_SET_CA_ROOTS_PATH
28+
@test_throws LibGit2.GitError LibGit2.ensure_initialized()
29+
# test that it still fails if called a second time
2730
@test_throws LibGit2.GitError LibGit2.ensure_initialized()
28-
delete!(ENV, "JULIA_SSL_CA_ROOTS_PATH")
31+
else
32+
@test LibGit2.ensure_initialized() === nothing
2933
@test LibGit2.ensure_initialized() === nothing
3034
end
3135
end

0 commit comments

Comments
 (0)