Skip to content

Commit 3462106

Browse files
committed
Rely on cert stores provided by Erlang/OTP +25
Warning if no certificate is provided (older OTP version).
1 parent 7fa483d commit 3462106

3 files changed

Lines changed: 60 additions & 17 deletions

File tree

lib/rustler_precompiled.ex

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ defmodule RustlerPrecompiled do
110110
* `HTTPS_PROXY` or `https_proxy` - Sets the HTTPS proxy configuration.
111111
112112
* `HEX_CACERTS_PATH` - Sets the path for a custom CA certificates file.
113-
If unset, defaults to `CAStore.file_path/0`.
113+
If unset, defaults to `:public.cacerts_get/0` (OTP >= 25) if available.
114+
In case it's running on an old OTP version, a warning is emitted.
114115
115116
* `MIX_XDG` - If present, sets the OS as `:linux` for the `:filename.basedir/3` when getting
116117
an user cache dir.
@@ -926,21 +927,17 @@ defmodule RustlerPrecompiled do
926927
:httpc.set_options([{:https_proxy, {{String.to_charlist(host), port}, []}}])
927928
end
928929

929-
# https://erlef.github.io/security-wg/secure_coding_and_deployment_hardening/inets
930-
# respects the user provided ca certs via Hex env var
931-
cacertfile = System.get_env("HEX_CACERTS_PATH", CAStore.file_path())
932-
933930
http_options = [
934-
ssl: [
935-
verify: :verify_peer,
936-
cacertfile: cacertfile |> String.to_charlist(),
937-
# We need to increase depth because the default value is 1.
938-
# See: https://erlef.github.io/security-wg/secure_coding_and_deployment_hardening/ssl
939-
depth: 3,
940-
customize_hostname_check: [
941-
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
942-
]
943-
]
931+
ssl:
932+
[
933+
verify: :verify_peer,
934+
# We need to increase depth because the default value is 1.
935+
# See: https://erlef.github.io/security-wg/secure_coding_and_deployment_hardening/ssl
936+
depth: 3,
937+
customize_hostname_check: [
938+
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
939+
]
940+
] ++ cacerts_options()
944941
]
945942

946943
options = [body_format: :binary]
@@ -957,6 +954,52 @@ defmodule RustlerPrecompiled do
957954
end
958955
end
959956

957+
# https://erlef.github.io/security-wg/secure_coding_and_deployment_hardening/inets
958+
defp cacerts_options do
959+
cond do
960+
path = System.get_env("HEX_CACERTS_PATH") ->
961+
[cacertfile: path]
962+
963+
certs = otp_cacerts() ->
964+
[cacerts: certs]
965+
966+
true ->
967+
warn_no_cacerts()
968+
[]
969+
end
970+
end
971+
972+
defp otp_cacerts do
973+
if System.otp_release() >= "25" do
974+
# cacerts_get/0 raises if no certs found
975+
try do
976+
:public_key.cacerts_get()
977+
rescue
978+
_ -> nil
979+
end
980+
end
981+
end
982+
983+
defp warn_no_cacerts do
984+
Logger.warning("""
985+
No certificate trust store was found.
986+
987+
A certificate trust store is required in
988+
order to download locales for your configuration.
989+
Since rustler_precompiled could not detect a system
990+
installed certificate trust store one of the
991+
following actions may be taken:
992+
993+
1. Specify the location of a certificate trust store
994+
by configuring it in environment variable:
995+
996+
export HEX_CACERTS_PATH="/path/to/cacerts.pem"
997+
998+
2. Use OTP 25+ on an OS that has built-in certificate
999+
trust store.
1000+
""")
1001+
end
1002+
9601003
# Download a list of files from URLs and calculate its checksum.
9611004
# Returns a list with details of the download and the checksum of each file.
9621005
@doc false

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ defmodule RustlerPrecompiled.MixProject do
3535
defp deps do
3636
[
3737
{:rustler, "~> 0.23", optional: true},
38-
{:castore, "~> 0.1 or ~> 1.0"},
38+
{:castore, "~> 0.1 or ~> 1.0", optional: true},
3939
{:ex_doc, "~> 0.27", only: :dev},
4040
{:bypass, "~> 2.1", only: :test}
4141
]

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
%{
22
"bypass": {:hex, :bypass, "2.1.0", "909782781bf8e20ee86a9cabde36b259d44af8b9f38756173e8f5e2e1fabb9b1", [:mix], [{:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "d9b5df8fa5b7a6efa08384e9bbecfe4ce61c77d28a4282f79e02f1ef78d96b80"},
3-
"castore": {:hex, :castore, "1.0.8", "dedcf20ea746694647f883590b82d9e96014057aff1d44d03ec90f36a5c0dc6e", [:mix], [], "hexpm", "0b2b66d2ee742cb1d9cb8c8be3b43c3a70ee8651f37b75a8b982e036752983f1"},
3+
"castore": {:hex, :castore, "1.0.9", "5cc77474afadf02c7c017823f460a17daa7908e991b0cc917febc90e466a375c", [:mix], [], "hexpm", "5ea956504f1ba6f2b4eb707061d8e17870de2bee95fb59d512872c2ef06925e7"},
44
"cowboy": {:hex, :cowboy, "2.10.0", "ff9ffeff91dae4ae270dd975642997afe2a1179d94b1887863e43f681a203e26", [:make, :rebar3], [{:cowlib, "2.12.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "3afdccb7183cc6f143cb14d3cf51fa00e53db9ec80cdcd525482f5e99bc41d6b"},
55
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},
66
"cowlib": {:hex, :cowlib, "2.12.1", "a9fa9a625f1d2025fe6b462cb865881329b5caff8f1854d1cbc9f9533f00e1e1", [:make, :rebar3], [], "hexpm", "163b73f6367a7341b33c794c4e88e7dbfe6498ac42dcd69ef44c5bc5507c8db0"},

0 commit comments

Comments
 (0)