Skip to content

Commit 48fa4b8

Browse files
committed
More ssl
1 parent 9f38089 commit 48fa4b8

File tree

1 file changed

+16
-57
lines changed

1 file changed

+16
-57
lines changed

lib/hex/http/ssl.ex

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,6 @@ defmodule Hex.HTTP.SSL do
4040
Record.extract(:OTPTBSCertificate, from_lib: "public_key/include/OTP-PUB-KEY.hrl")
4141
)
4242

43-
def secure_ssl? do
44-
check? = not Hex.State.fetch!(:unsafe_https)
45-
46-
if check? and Hex.State.fetch!(:ssl_version) <= @secure_ssl_version do
47-
Mix.raise(
48-
"Insecure HTTPS request (peer verification disabled), " <>
49-
"please update to OTP 17.4 or later, or disable by setting " <>
50-
"the environment variable HEX_UNSAFE_HTTPS=1"
51-
)
52-
end
53-
54-
check?
55-
end
56-
5743
def get_ca_certs do
5844
case Hex.State.fetch!(:cacerts_path) do
5945
nil -> Certs.cacerts()
@@ -65,31 +51,20 @@ defmodule Hex.HTTP.SSL do
6551
hostname = String.to_charlist(URI.parse(url).host)
6652
ciphers = filter_ciphers(@default_ciphers)
6753

68-
if secure_ssl?() do
69-
partial_chain = &partial_chain(Certs.cacerts(), &1)
70-
71-
[
72-
verify: :verify_peer,
73-
depth: 4,
74-
partial_chain: partial_chain,
75-
cacerts: get_ca_certs(),
76-
server_name_indication: hostname,
77-
secure_renegotiate: true,
78-
reuse_sessions: true,
79-
versions: @default_versions,
80-
ciphers: ciphers
81-
]
82-
|> customize_hostname_check(hostname)
83-
else
84-
[
85-
verify: :verify_none,
86-
server_name_indication: hostname,
87-
secure_renegotiate: true,
88-
reuse_sessions: true,
89-
versions: @default_versions,
90-
ciphers: ciphers
91-
]
92-
end
54+
partial_chain = &partial_chain(Certs.cacerts(), &1)
55+
56+
[
57+
verify: :verify_peer,
58+
depth: 4,
59+
partial_chain: partial_chain,
60+
cacerts: get_ca_certs(),
61+
server_name_indication: hostname,
62+
secure_renegotiate: true,
63+
reuse_sessions: true,
64+
versions: @default_versions,
65+
ciphers: ciphers
66+
]
67+
|> customize_hostname_check()
9368
end
9469

9570
def partial_chain(cacerts, certs) do
@@ -125,23 +100,7 @@ defmodule Hex.HTTP.SSL do
125100
Enum.filter(allowed, &(&1 in available))
126101
end
127102

128-
defp customize_hostname_check(opts, hostname) do
129-
if ssl_major_version() >= 9 do
130-
# From OTP 20.0 use built-in support for custom hostname checks
131-
Keyword.put(opts, :customize_hostname_check, match_fun: &VerifyHostname.match_fun/2)
132-
else
133-
# Before OTP 20.0 use mint_shims for hostname check, from a custom verify_fun
134-
Keyword.put(opts, :verify_fun, {&VerifyHostname.verify_fun/3, check_hostname: hostname})
135-
end
136-
end
137-
138-
defp ssl_major_version do
139-
# Elixir 1.0.5 - 1.1.1 have no Application.spec/2
140-
case :application.get_key(:ssl, :vsn) do
141-
{:ok, value} -> value
142-
:undefined -> nil
143-
end
144-
|> :string.to_integer()
145-
|> elem(0)
103+
defp customize_hostname_check(opts) do
104+
Keyword.put(opts, :customize_hostname_check, match_fun: &VerifyHostname.match_fun/2)
146105
end
147106
end

0 commit comments

Comments
 (0)