Fixed XF\Util\Ip::binaryToString breaks IPv6 addresses that contain a double colon

Steffen

Well-known member
Affected version
2.3.6
How to reproduce:
PHP:
var_dump(\XF\Util\Ip::binaryToString("*\x02\x124\x124\0\0\0\0\0\0\0\0\0\0", $shorten = false));

What should be returned:
string(39) "2a02:1234:1234:0000:0000:0000:0000:0000"

What is actually returned:
string(24) "2a02:1234:1234:0000:0000"

This is an invalid IPv6 address.

Possible fix (found at https://stackoverflow.com/a/58577916/444959):
Diff:
--- a/src/XF/Util/Ip.php
+++ b/src/XF/Util/Ip.php
@@ -37,13 +37,8 @@ class Ip
 
        if (strlen($ip) === 16 && !$shorten)
        {
-           $output = implode(':', array_map(
-               function ($block)
-               {
-                   return str_pad($block, 4, '0', STR_PAD_LEFT);
-               },
-               explode(':', $output)
-           ));
+           $hex = bin2hex(inet_pton($output));
+           $output = implode(':', str_split($hex, 4));
        }
 
        return $output;
 
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.3.7).

Change log:
Fix IPv6 binary to string expansion
There may be a delay before changes are rolled out to the XenForo Community.
 
Back
Top Bottom