tonic icon indicating copy to clipboard operation
tonic copied to clipboard

Configuring TLS doesn't work if IPv6 IP address is passed in

Open bouk opened this issue 3 months ago • 1 comments

I have some code like the following:


use tonic::transport::{Channel, Endpoint, ClientTlsConfig, Error};

fn connect_endpoint(url: String) -> Result<Channel, Error> {
    let channel = Endpoint::try_from(url)?
        .tls_config(ClientTlsConfig::new().with_enabled_roots())?
        .connect_lazy();
    Ok(channel)
}

This works fine if I pass a URL like https://example.com or http://127.0.0.1:1234 but it fails if I pass http://[::1]:1234 which is a valid URL.

The error is:

Error: 
   0: transport error
   1: invalid dns name

Which comes from this line:

https://github.com/hyperium/tonic/blob/ff7b54045c943172de4c65cf949011492db185ee/tonic/src/transport/channel/service/tls.rs#L102

bouk avatar Oct 09 '25 12:10 bouk

Ah, figured it out: it's because http::uri::Uri::host returns ipv6 addresses surrounded by square brackets but ServerName expects them without square brackets.

bouk avatar Oct 09 '25 12:10 bouk