Skip to content

Commit b01e41b

Browse files
committed
Add :verify_peer config option and handle expired certificates better. Fixes #116
1 parent 1b2da16 commit b01e41b

5 files changed

Lines changed: 37 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# Changelog for Money v5.2.0
2+
3+
This is the changelog for Money v5.2.0 released on May 30th, 2020. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags)
4+
5+
### Enhancements
6+
7+
* Adds a configuration option `:verify_peer` which is a boolean that determines whether to verify the client certificate for any exchange rate service API call. The default is `true`. This option should not be changed without a very clear understanding of the security implications. This option will remain undocumented but supported for now.
8+
9+
### Bug fixes
10+
11+
* Handle expired certificate errors on the exchange rates API service and log them. Thanks to @coladarci. Fixes #116
12+
113
# Changelog for Money v5.1.0
214

315
This is the changelog for Money v5.1.0 released on May 26th, 2020. For older changelogs please consult the release tag on [GitHub](https://github.com/kipcole9/money/tags)

lib/money/exchange_rates.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ defmodule Money.ExchangeRates do
187187
log_levels: map(),
188188
preload_historic_rates: Date.t() | Date.Range.t() | {Date.t(), Date.t()} | nil,
189189
retriever_options: map() | nil,
190-
cache_module: module() | nil
190+
cache_module: module() | nil,
191+
verify_peer: boolean()
191192
}
192193

193194
defstruct retrieve_every: nil,
@@ -196,7 +197,8 @@ defmodule Money.ExchangeRates do
196197
log_levels: %{},
197198
preload_historic_rates: nil,
198199
retriever_options: nil,
199-
cache_module: nil
200+
cache_module: nil,
201+
verify_peer: true
200202
end
201203

202204
@doc """
@@ -214,7 +216,8 @@ defmodule Money.ExchangeRates do
214216
success: Money.get_env(:log_success, nil),
215217
failure: Money.get_env(:log_failure, :warn),
216218
info: Money.get_env(:log_info, :info)
217-
}
219+
},
220+
verify_peer: Money.get_env(:verify_peer, true, :boolean)
218221
}
219222
end
220223

lib/money/exchange_rates/exchange_rates_retriever.ex

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ defmodule Money.ExchangeRates.Retriever do
195195
def retrieve_rates(url, config) when is_list(url) do
196196
headers = if_none_match_header(url)
197197

198-
:httpc.request(:get, {url, headers}, https_opts(), [])
198+
:httpc.request(:get, {url, headers}, https_opts(config), [])
199199
|> process_response(url, config)
200200
end
201201

@@ -222,6 +222,10 @@ defmodule Money.ExchangeRates.Retriever do
222222
{:error, sys_message}
223223
end
224224

225+
defp process_response({:error, {:tls_alert, {:certificate_expired, _message}}}, url, _config) do
226+
{:error, "Certificate for #{inspect url} has expired"}
227+
end
228+
225229
defp if_none_match_header(url) do
226230
case get_etag(url) do
227231
{etag, date} ->
@@ -562,16 +566,23 @@ defmodule Money.ExchangeRates.Retriever do
562566
file
563567
end
564568

565-
defp https_opts do
566-
[ssl:
569+
defp https_opts(%Money.ExchangeRates.Config{verify_peer: true}) do
570+
[
571+
ssl:
567572
[
568573
verify: :verify_peer,
569574
cacertfile: certificate_store(),
570575
depth: 99,
576+
log_level: :alert,
577+
log_alert: false,
571578
customize_hostname_check: [
572579
match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
573580
]
574581
]
575582
]
576583
end
584+
585+
defp https_opts(%Money.ExchangeRates.Config{verify_peer: false}) do
586+
[]
587+
end
577588
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Money.Mixfile do
22
use Mix.Project
33

4-
@version "5.1.0"
4+
@version "5.2.0"
55

66
def project do
77
[

test/money_exchange_rates_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ defmodule Money.ExchangeRates.Test do
7676
config = Money.ExchangeRates.OpenExchangeRates.init(Money.ExchangeRates.default_config())
7777
config = Map.put(config, :log_levels, %{failure: nil, info: nil, success: nil})
7878

79+
# Testing only, should not be used in production
80+
# config = Map.put(config, :verify_peer, false)
81+
7982
case Money.ExchangeRates.OpenExchangeRates.get_latest_rates(config) do
8083
{:ok, rates} -> assert is_map(rates)
8184
{:error, :nxdomain} -> :no_network
85+
{:error, other} -> IO.warn(inspect(other))
8286
end
8387
end
8488
end

0 commit comments

Comments
 (0)