Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NetworkOptions stdlib for default value of host verification. #678

Merged
merged 1 commit into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
IniFile = "83e8ac13-25f8-5344-8a64-a9f2b223428f"
MbedTLS = "739be429-bea8-5141-9913-cc70e7f3736d"
NetworkOptions = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"

Expand Down
7 changes: 4 additions & 3 deletions src/ConnectionPool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ using ..IOExtras, ..Sockets
import ..@debug, ..@debugshow, ..DEBUG_LEVEL, ..taskid
import ..@require, ..precondition_error, ..@ensure, ..postcondition_error
using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake!
import NetworkOptions

const default_connection_limit = 8
const default_pipeline_limit = 16
Expand Down Expand Up @@ -543,7 +544,7 @@ function getconnection(::Type{Transaction{T}},
pipeline_limit::Int=default_pipeline_limit,
idle_timeout::Int=0,
reuse_limit::Int=nolimit,
require_ssl_verification::Bool=true,
require_ssl_verification::Bool=NetworkOptions.verify_host(host, "SSL"),
kw...)::Transaction{T} where T <: IO
pod = getpod(POOL, hashconn(T, host, port, pipeline_limit, require_ssl_verification, true))
@v1_3 lock(pod.conns)
Expand Down Expand Up @@ -722,7 +723,7 @@ function getconnection(::Type{SSLContext},
end

function sslconnection(tcp::TCPSocket, host::AbstractString;
require_ssl_verification::Bool=true,
require_ssl_verification::Bool=NetworkOptions.verify_host(host, "SSL"),
sslconfig::SSLConfig=nosslconfig,
kw...)::SSLContext

Expand All @@ -740,7 +741,7 @@ end

function sslupgrade(t::Transaction{TCPSocket},
host::AbstractString;
require_ssl_verification::Bool=true,
require_ssl_verification::Bool=NetworkOptions.verify_host(host, "SSL"),
kw...)::Transaction{SSLContext}
tls = sslconnection(t.c.io, host;
require_ssl_verification=require_ssl_verification,
Expand Down
2 changes: 1 addition & 1 deletion src/HTTP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Status Exception options

SSLContext options

- `require_ssl_verification = false`, pass `MBEDTLS_SSL_VERIFY_REQUIRED` to
- `require_ssl_verification = NetworkOptions.verify_host(host)`, pass `MBEDTLS_SSL_VERIFY_REQUIRED` to
the mbed TLS library.
["... peer must present a valid certificate, handshake is aborted if
verification failed."](https://tls.mbed.org/api/ssl_8h.html#a5695285c9dbfefec295012b566290f37)
Expand Down
37 changes: 37 additions & 0 deletions test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,43 @@ end
end
end

import NetworkOptions, MbedTLS
@testset "NetworkOptions for host verification" begin
# Set up server with self-signed cert
server = listen(IPv4(0), 8443)
cert, key = joinpath.(@__DIR__, "resources", ("cert.pem", "key.pem"))
sslconfig = MbedTLS.SSLConfig(cert, key)
tsk = @async HTTP.listen("0.0.0.0", 8443; server=server, sslconfig=sslconfig) do http
HTTP.setstatus(http, 200)
HTTP.startwrite(http)
HTTP.write(http, "hello, world")
end
url = "https://localhost:8443"
env = ["JULIA_NO_VERIFY_HOSTS" => nothing, "JULIA_SSL_NO_VERIFY_HOSTS" => nothing, "JULIA_ALWAYS_VERIFY_HOSTS" => nothing]
withenv(env...) do
@test NetworkOptions.verify_host(url)
@test NetworkOptions.verify_host(url, "SSL")
@test_throws HTTP.IOError HTTP.get(url; retries=1)
@test_throws HTTP.IOError HTTP.get(url; require_ssl_verification=true, retries=1)
@test HTTP.get(url; require_ssl_verification=false).status == 200
end
withenv(env..., "JULIA_NO_VERIFY_HOSTS" => "localhost") do
@test !NetworkOptions.verify_host(url)
@test !NetworkOptions.verify_host(url, "SSL")
@test HTTP.get(url).status == 200
@test_throws HTTP.IOError HTTP.get(url; require_ssl_verification=true, retries=1)
@test HTTP.get(url; require_ssl_verification=false).status == 200
end
withenv(env..., "JULIA_SSL_NO_VERIFY_HOSTS" => "localhost") do
@test NetworkOptions.verify_host(url)
@test !NetworkOptions.verify_host(url, "SSL")
@test HTTP.get(url).status == 200
@test_throws HTTP.IOError HTTP.get(url; require_ssl_verification=true, retries=1)
@test HTTP.get(url; require_ssl_verification=false).status == 200
end
close(server)
end

@testset "Public entry point of HTTP.request and friends (e.g. issue #463)" begin
headers = Dict("User-Agent" => "HTTP.jl")
query = Dict("hello" => "world")
Expand Down