Merge branch 'fix/emudhra-ca-trust' (#77) into release/2.11

This commit is contained in:
Lain Soykaf 2026-10-22 16:37:08 +04:00
commit 2982472d00
No known key found for this signature in database
3 changed files with 42 additions and 16 deletions

View file

@ -15,16 +15,21 @@ defmodule Pleroma.HTTP.TLS do
@spec ssl_options(keyword()) :: keyword() @spec ssl_options(keyword()) :: keyword()
def ssl_options(overrides \\ []) do def ssl_options(overrides \\ []) do
Keyword.merge( defaults = [
[ verify: :verify_peer,
verify: :verify_peer, cacertfile: CAStore.file_path(),
cacertfile: CAStore.file_path(), depth: 20,
depth: 20, customize_hostname_check: [match_fun: :public_key.pkix_verify_hostname_match_fun(:https)],
customize_hostname_check: [match_fun: :public_key.pkix_verify_hostname_match_fun(:https)], partial_chain: &__MODULE__.reject_partial_chain/1
partial_chain: &__MODULE__.reject_partial_chain/1 ]
],
overrides # Hackney drops cacerts when cacertfile is set too.
) defaults =
if Keyword.has_key?(overrides, :cacerts),
do: Keyword.delete(defaults, :cacertfile),
else: defaults
Keyword.merge(defaults, overrides)
end end
@doc false @doc false
@ -32,10 +37,13 @@ defmodule Pleroma.HTTP.TLS do
@spec put_hackney_ssl_options(keyword()) :: keyword() @spec put_hackney_ssl_options(keyword()) :: keyword()
def put_hackney_ssl_options(opts) do def put_hackney_ssl_options(opts) do
# Hackney 4 ignores the top-level insecure option and falls back to certifi.
if opts[:insecure] do if opts[:insecure] do
opts Keyword.put(opts, :ssl_options, verify: :verify_none)
else else
Keyword.put(opts, :ssl_options, ssl_options(Keyword.get(opts, :ssl_options, []))) opts
|> Keyword.put(:ssl_options, ssl_options(Keyword.get(opts, :ssl_options, [])))
|> Keyword.put(:proxy_ssl_options, ssl_options(Keyword.get(opts, :proxy_ssl_options, [])))
end end
end end
end end

View file

@ -16,8 +16,9 @@ defmodule Pleroma.HTTP.AdapterHelper.TeslaAdapterTest do
test "adds the TLS options to Hackney" do test "adds the TLS options to Hackney" do
Application.put_env(:tesla, :adapter, Tesla.Adapter.Hackney) Application.put_env(:tesla, :adapter, Tesla.Adapter.Hackney)
assert AdapterHelper.tesla_adapter() == assert {Tesla.Adapter.Hackney, opts} = AdapterHelper.tesla_adapter()
{Tesla.Adapter.Hackney, ssl_options: TLS.ssl_options()} assert opts[:ssl_options] == TLS.ssl_options()
assert opts[:proxy_ssl_options] == TLS.ssl_options()
end end
test "turns on certificate checks for Gun" do test "turns on certificate checks for Gun" do

View file

@ -23,6 +23,13 @@ defmodule Pleroma.HTTP.TLSTest do
assert partial_chain.([:certificate]) == :unknown_ca assert partial_chain.([:certificate]) == :unknown_ca
end end
test "keeps CA certificates that the admin set" do
opts = TLS.ssl_options(cacerts: [:private_ca])
assert opts[:cacerts] == [:private_ca]
refute Keyword.has_key?(opts, :cacertfile)
end
test "keeps the CA bundle when other options are given" do test "keeps the CA bundle when other options are given" do
opts = TLS.ssl_options(versions: [:"tlsv1.2"]) opts = TLS.ssl_options(versions: [:"tlsv1.2"])
@ -38,6 +45,7 @@ defmodule Pleroma.HTTP.TLSTest do
assert opts[:pool] == :media assert opts[:pool] == :media
assert opts[:ssl_options] == TLS.ssl_options() assert opts[:ssl_options] == TLS.ssl_options()
assert opts[:proxy_ssl_options] == TLS.ssl_options()
end end
test "merges given ssl_options into the TLS options" do test "merges given ssl_options into the TLS options" do
@ -46,8 +54,17 @@ defmodule Pleroma.HTTP.TLSTest do
assert opts[:ssl_options] == TLS.ssl_options(versions: [:"tlsv1.2"]) assert opts[:ssl_options] == TLS.ssl_options(versions: [:"tlsv1.2"])
end end
test "leaves insecure requests alone" do test "turns insecure into an explicit verify_none" do
assert TLS.put_hackney_ssl_options(insecure: true) == [insecure: true] opts = TLS.put_hackney_ssl_options(insecure: true)
assert opts[:ssl_options] == [verify: :verify_none]
refute Keyword.has_key?(opts, :proxy_ssl_options)
end
test "secures the connection to an HTTPS proxy" do
opts = TLS.put_hackney_ssl_options(proxy_ssl_options: [versions: [:"tlsv1.3"]])
assert opts[:proxy_ssl_options] == TLS.ssl_options(versions: [:"tlsv1.3"])
end end
end end
end end