From f8db7ce8f5d3255bc6f04617164de1351b8010ab Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Thu, 1 Jul 2021 20:37:33 -0600 Subject: Fix DnsPinMiddleware resolve pinning bug Libcurl expects the value of the CURLOPT_RESOLVE configurations to be an array of strings, those strings containing a comma delimited list of resolved IPs for each host:port combination. The original code here does create that array with the host:port:ip combination, but multiple ips for a single host:port result in additional array entries, rather than adding them to the end of the string with a comma. Per the libcurl docs, the `CURLOPT_RESOLVE` array entries should match the syntax `host:port:address[,address]`. This creates a function-scoped associative array which uses `host:port` as the key (which are supposed to be unique and this ensures that), and the value is an array containing IP strings (ipv4 or ipv6). Once the associative array is populated, it is then set to the CURLOPT_RESOLVE array, imploding the ip arrays using a comma delimiter so the array syntax matches the expected by libcurl. Note that this reorders the "foreach ip" and "foreach port" loops. Rather than looping over ips then ports, we now loop over ports then ips, since ports are part of the unique host:port map, and multiple ips can exist therein. Signed-off-by: Aaron Ball --- lib/private/Http/Client/DnsPinMiddleware.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/private/Http/Client/DnsPinMiddleware.php b/lib/private/Http/Client/DnsPinMiddleware.php index 019fde23723..900173bb506 100644 --- a/lib/private/Http/Client/DnsPinMiddleware.php +++ b/lib/private/Http/Client/DnsPinMiddleware.php @@ -112,15 +112,22 @@ class DnsPinMiddleware { $targetIps = $this->dnsResolve($hostName, 0); - foreach ($targetIps as $ip) { - $this->localAddressChecker->ThrowIfLocalIp($ip); + $curlResolves = []; - foreach ($ports as $port) { - $curlEntry = $hostName . ':' . $port . ':' . $ip; - $options['curl'][CURLOPT_RESOLVE][] = $curlEntry; + foreach ($ports as $port) { + $curlResolves["$hostName:$port"] = []; + + foreach ($targetIps as $ip) { + $this->localAddressChecker->ThrowIfLocalIp($ip); + $curlResolves["$hostName:$port"][] = $ip; } } + // Coalesce the per-host:port ips back into a comma separated list + foreach ($curlResolves as $hostport => $ips) { + $options['curl'][CURLOPT_RESOLVE][] = "$hostport:" . implode(',', $ips); + } + return $handler($request, $options); }; }; -- cgit v1.2.3