diff --git a/library/Zend/Validator/Ip.php b/library/Zend/Validator/Ip.php index 10ecf5f92e0..9253a8189e6 100644 --- a/library/Zend/Validator/Ip.php +++ b/library/Zend/Validator/Ip.php @@ -97,15 +97,15 @@ public function isValid($value) */ protected function validateIPv4($value) { - if (preg_match('/^([01]{8}.){3}[01]{8}$/i', $value)) { + if (preg_match('/^([01]{8}.){3}[01]{8}\z/i', $value)) { // binary format 00000000.00000000.00000000.00000000 $value = bindec(substr($value, 0, 8)) . '.' . bindec(substr($value, 9, 8)) . '.' . bindec(substr($value, 18, 8)) . '.' . bindec(substr($value, 27, 8)); - } elseif (preg_match('/^([0-9]{3}.){3}[0-9]{3}$/i', $value)) { + } elseif (preg_match('/^([0-9]{3}.){3}[0-9]{3}\z/i', $value)) { // octet format 777.777.777.777 $value = (int) substr($value, 0, 3) . '.' . (int) substr($value, 4, 3) . '.' . (int) substr($value, 8, 3) . '.' . (int) substr($value, 12, 3); - } elseif (preg_match('/^([0-9a-f]{2}.){3}[0-9a-f]{2}$/i', $value)) { + } elseif (preg_match('/^([0-9a-f]{2}.){3}[0-9a-f]{2}\z/i', $value)) { // hex format ff.ff.ff.ff $value = hexdec(substr($value, 0, 2)) . '.' . hexdec(substr($value, 3, 2)) . '.' . hexdec(substr($value, 6, 2)) . '.' . hexdec(substr($value, 9, 2)); diff --git a/tests/ZendTest/Validator/IpTest.php b/tests/ZendTest/Validator/IpTest.php index 6c023f171e6..123ba6b9d59 100644 --- a/tests/ZendTest/Validator/IpTest.php +++ b/tests/ZendTest/Validator/IpTest.php @@ -283,6 +283,12 @@ public function testIPv4AddressNotations() 'a0.b0.c0.d0' => true, 'g0.00.00.00' => false, 'g0.00.00.00:80' => false, + + // new lines should not accept + "00000001.00000010.00000011.00000100\n" => false, + "001.002.003.004\n" => false, + "a0.b0.c0.d0\n" => false, + ); foreach ($ips as $ip => $expectedOutcome) {