Skip to content

Commit

Permalink
Validator\Ip should not allow newlines in any case.
Browse files Browse the repository at this point in the history
This issue was caused by ZF-10621
  • Loading branch information
sasezaki committed Apr 8, 2014
1 parent 2ec78a2 commit 6d4c84d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions library/Zend/Validator/Ip.php
Expand Up @@ -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));
Expand Down
6 changes: 6 additions & 0 deletions tests/ZendTest/Validator/IpTest.php
Expand Up @@ -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) {
Expand Down

0 comments on commit 6d4c84d

Please sign in to comment.