From 56f08463781793651e53f236c46f88149570bbe9 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 26 Jul 2023 17:21:43 +0200 Subject: [PATCH] [ticket/11765] Support IPv4 embedded IPv6 addresses in short_ipv6() PHPBB3-11765 --- phpBB/includes/functions.php | 10 +++++- tests/functions/short_ipv6__test.php | 49 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/functions/short_ipv6__test.php diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 1234e1aa7b..e3ce50fe43 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -2918,7 +2918,7 @@ function get_censor_preg_expression($word) /** * Returns the first block of the specified IPv6 address and as many additional -* ones as specified in the length paramater. +* ones as specified in the length parameter. * If length is zero, then an empty string is returned. * If length is greater than 3 the complete IP will be returned */ @@ -2929,6 +2929,14 @@ function short_ipv6($ip, $length) return ''; } + // Handle IPv4 embedded IPv6 addresses + if (preg_match('/(?:\d{1,3}\.){3}\d{1,3}$/i', $ip)) + { + $binary_ip = inet_pton($ip); + $ip_v6 = $binary_ip ? inet_ntop($binary_ip) : $ip; + $ip = $ip_v6 ?: $ip; + } + // extend IPv6 addresses $blocks = substr_count($ip, ':') + 1; if ($blocks < 9) diff --git a/tests/functions/short_ipv6__test.php b/tests/functions/short_ipv6__test.php new file mode 100644 index 0000000000..8fa9cd7420 --- /dev/null +++ b/tests/functions/short_ipv6__test.php @@ -0,0 +1,49 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class short_ipv6__test extends phpbb_test_case +{ + public function data_short_ipv6(): array + { + return [ + ['::1', 0, ''], + ['::1', 1, '0000:0000'], + ['::1', 2, '0000:0000:0000'], + ['::1', 3, '0000:0000:0000:0000'], + ['::1', 4, '0000:0000:0000:0000:0000:0000:0000:1'], + ['2001:db8:3333:4444:5555:6666:7777:8888', 0, ''], + ['2001:db8:3333:4444:5555:6666:7777:8888', 1, '2001:db8'], + ['2001:db8:3333:4444:5555:6666:7777:8888', 2, '2001:db8:3333'], + ['2001:db8:3333:4444:5555:6666:7777:8888', 3, '2001:db8:3333:4444'], + ['2001:db8:3333:4444:5555:6666:7777:8888', 4, '2001:db8:3333:4444:5555:6666:7777:8888'], + ['::ffff:192.168.1.1', 0, ''], + ['::ffff:192.168.1.1', 1, '0000:0000'], + ['::ffff:192.168.1.1', 2, '0000:0000:0000'], + ['::ffff:192.168.1.1', 3, '0000:0000:0000:0000'], + ['::ffff:192.168.1.1', 4, '0000:0000:0000:0000:0000:0000:ffff:192.168.1.1'], + ['FADE:BAD::192.168.0.1', 0, ''], + ['FADE:BAD::192.168.0.1', 1, 'fade:bad'], + ['FADE:BAD::192.168.0.1', 2, 'fade:bad:0000'], + ['FADE:BAD::192.168.0.1', 3, 'fade:bad:0000:0000'], + ['FADE:BAD::192.168.0.1', 4, 'fade:bad:0000:0000:0000:0000:c0a8:1'], + ]; + } + + /** + * @dataProvider data_short_ipv6 + */ + public function test_short_ipv6($ip, $length, $expected) + { + $this->assertEquals($expected, short_ipv6($ip, $length)); + } +}