Selaa lähdekoodia

[ZF-8312] Zend_Validate_Hostname:

- fixed multitagged IDN validation

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18999 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 vuotta sitten
vanhempi
commit
f8d173ea04
2 muutettua tiedostoa jossa 18 lisäystä ja 13 poistoa
  1. 10 13
      library/Zend/Validate/Hostname.php
  2. 8 0
      tests/Zend/Validate/HostnameTest.php

+ 10 - 13
library/Zend/Validate/Hostname.php

@@ -488,7 +488,6 @@ class Zend_Validate_Hostname extends Zend_Validate_Abstract
         }
 
         $this->_setValue($value);
-
         // Check input against IP address schema
         if (preg_match('/^[0-9.a-e:.]*$/i', $value) &&
             $this->_options['ip']->setTranslator($this->getTranslator())->isValid($value)) {
@@ -546,9 +545,8 @@ class Zend_Validate_Hostname extends Zend_Validate_Abstract
                     }
 
                     // Check each hostname part
-                    $valid = true;
+                    $check = 0;
                     foreach ($domainParts as $domainPart) {
-
                         // Decode Punycode domainnames to IDN
                         if (strpos($domainPart, 'xn--') === 0) {
                             $domainPart = $this->decodePunycode(substr($domainPart, 4));
@@ -561,13 +559,13 @@ class Zend_Validate_Hostname extends Zend_Validate_Abstract
                         if ((strpos($domainPart, '-') === 0)
                             || ((strlen($domainPart) > 2) && (strpos($domainPart, '-', 2) == 2) && (strpos($domainPart, '-', 3) == 3))
                             || (strpos($domainPart, '-') === (strlen($domainPart) - 1))) {
-                            $this->_error(self::INVALID_DASH);
+                                $this->_error(self::INVALID_DASH);
                             $status = false;
                             break 2;
                         }
 
                         // Check each domain part
-                        $check = false;
+                        $checked = false;
                         foreach($regexChars as $regexKey => $regexChar) {
                             $status = @preg_match($regexChar, $domainPart);
                             if ($status > 0) {
@@ -580,23 +578,22 @@ class Zend_Validate_Hostname extends Zend_Validate_Abstract
                                 if (iconv_strlen($domainPart, 'UTF-8') > $length) {
                                     $this->_error(self::INVALID_HOSTNAME);
                                 } else {
-                                    $check = true;
-                                    break 2;
+                                    $checked = true;
+                                    break;
                                 }
                             }
                         }
 
-                        if (!$check) {
-                            $valid = false;
+                        if ($checked) {
+                            ++$check;
                         }
                     }
 
-                    // If all labels didn't match, the hostname is invalid
-                    if (!$valid) {
+                    // If one of the labels doesn't match, the hostname is invalid
+                    if ($check !== count($domainParts)) {
                         $this->_error(self::INVALID_HOSTNAME_SCHEMA);
                         $status = false;
                     }
-
                 } else {
                     // Hostname not long enough
                     $this->_error(self::UNDECIPHERABLE_TLD);
@@ -610,7 +607,7 @@ class Zend_Validate_Hostname extends Zend_Validate_Abstract
             if ($status && ($this->_options['allow'] & self::ALLOW_DNS)) {
                 return true;
             }
-        } else {
+        } else if ($this->_options['allow'] & self::ALLOW_DNS) {
             $this->_error(self::INVALID_HOSTNAME);
         }
 

+ 8 - 0
tests/Zend/Validate/HostnameTest.php

@@ -381,4 +381,12 @@ class Zend_Validate_HostnameTest extends PHPUnit_Framework_TestCase
             }
         }
     }
+
+    /**
+     * @ZF-8312
+     */
+    public function testInvalidDoubledIdn()
+    {
+        $this->assertFalse($this->_validator->isValid('test.com / http://www.test.com'));
+    }
 }