]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: uri-util - Make construction of URI host name robust to IP literals.
authorStephan Bosch <stephan.bosch@open-xchange.com>
Sat, 20 Nov 2021 18:08:01 +0000 (19:08 +0100)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 17 Jan 2022 11:52:10 +0000 (13:52 +0200)
When only a host name (string) is set, it may still be an IP address. This
change makes sure it is correctly written to the URL when it is IPv6; don't
trust the caller to get it right.

src/lib/uri-util.c

index 8cd6d48170029c67be2ad612a0764a117c19bfcd..5713a1438d8dffcd9863ade1111f0b8dc3b45f25 100644 (file)
@@ -1291,15 +1291,16 @@ void uri_append_host_ip(string_t *out, const struct ip_addr *host_ip)
 
 void uri_append_host(string_t *out, const struct uri_host *host)
 {
+       struct ip_addr ip;
+
        if (host->ip.family != 0)
                uri_append_host_ip(out, &host->ip);
        else {
                i_assert(host->name != NULL);
-               /* Assume IPv6 literal if starts with '['; avoid encoding */
-               if (*host->name == '[')
-                       str_append(out, host->name);
-               else
+               if (net_addr2ip(host->name, &ip) < 0)
                        uri_append_host_name(out, host->name);
+               else
+                       uri_append_host_ip(out, &ip);
        }
 }