IP: Fix sanitization of IPv4 ranges
authorBrad Jorsch <bjorsch@wikimedia.org>
Tue, 22 May 2018 21:12:30 +0000 (17:12 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Wed, 23 May 2018 14:34:20 +0000 (10:34 -0400)
'000.000.000.000/24' should sanitize to '0.0.0.0/24', not
'0.0.0.000/24'.

Change-Id: I9364cb268dcc9b9b24aa1c627a87482978c4cf34

includes/libs/IP.php
tests/phpunit/includes/libs/IPTest.php

index f95bb1e..06589d2 100644 (file)
@@ -164,7 +164,7 @@ class IP {
                }
                if ( self::isIPv4( $ip ) ) {
                        // Remove leading 0's from octet representation of IPv4 address
-                       $ip = preg_replace( '/(?:^|(?<=\.))0+(?=[1-9]|0\.|0$)/', '', $ip );
+                       $ip = preg_replace( '!(?:^|(?<=\.))0+(?=[1-9]|0[./]|0$)!', '', $ip );
                        return $ip;
                }
                // Remove any whitespaces, convert to upper case
index 9702c82..9ec53c0 100644 (file)
@@ -325,6 +325,7 @@ class IPTest extends PHPUnit\Framework\TestCase {
                        [ '0.0.0.0', '0.0.0.0' ],
                        [ '0.0.0.0', '00.00.00.00' ],
                        [ '0.0.0.0', '000.000.000.000' ],
+                       [ '0.0.0.0/24', '000.000.000.000/24' ],
                        [ '141.0.11.253', '141.000.011.253' ],
                        [ '1.2.4.5', '1.2.4.5' ],
                        [ '1.2.4.5', '01.02.04.05' ],