Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/5900'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Mar 5, 2014
2 parents e21e883 + ebb79d7 commit 4c617fd
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 46 deletions.
123 changes: 81 additions & 42 deletions library/Zend/Validator/Hostname.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,41 @@ class Hostname extends AbstractValidator
* @var array
*/
protected $validTlds = array(
'ac', 'ad', 'ae', 'aero', 'af', 'ag', 'ai', 'al', 'am', 'an', 'ao', 'aq', 'ar', 'arpa',
'as', 'asia', 'at', 'au', 'aw', 'ax', 'az', 'ba', 'bb', 'bd', 'be', 'bf', 'bg', 'bh', 'bi',
'biz', 'bj', 'bm', 'bn', 'bo', 'br', 'bs', 'bt', 'bv', 'bw', 'by', 'bz', 'ca', 'cat', 'cc',
'cd', 'cf', 'cg', 'ch', 'ci', 'ck', 'cl', 'cm', 'cn', 'co', 'com', 'coop', 'cr', 'cu',
'cv', 'cx', 'cy', 'cz', 'de', 'dj', 'dk', 'dm', 'do', 'dz', 'ec', 'edu', 'ee', 'eg', 'er',
'es', 'et', 'eu', 'fi', 'fj', 'fk', 'fm', 'fo', 'fr', 'ga', 'gb', 'gd', 'ge', 'gf', 'gg',
'gh', 'gi', 'gl', 'gm', 'gn', 'gov', 'gp', 'gq', 'gr', 'gs', 'gt', 'gu', 'gw', 'gy', 'hk',
'hm', 'hn', 'hr', 'ht', 'hu', 'id', 'ie', 'il', 'im', 'in', 'info', 'int', 'io', 'iq',
'ir', 'is', 'it', 'je', 'jm', 'jo', 'jobs', 'jp', 'ke', 'kg', 'kh', 'ki', 'km', 'kn', 'kp',
'kr', 'kw', 'ky', 'kz', 'la', 'lb', 'lc', 'li', 'lk', 'lr', 'ls', 'lt', 'lu', 'lv', 'ly',
'ma', 'mc', 'md', 'me', 'mg', 'mh', 'mil', 'mk', 'ml', 'mm', 'mn', 'mo', 'mobi', 'mp',
'mq', 'mr', 'ms', 'mt', 'mu', 'museum', 'mv', 'mw', 'mx', 'my', 'mz', 'na', 'name', 'nc',
'ne', 'net', 'nf', 'ng', 'ni', 'nl', 'no', 'np', 'nr', 'nu', 'nz', 'om', 'org', 'pa', 'pe',
'pf', 'pg', 'ph', 'pk', 'pl', 'pm', 'pn', 'pr', 'pro', 'ps', 'pt', 'pw', 'py', 'qa', 're',
'ro', 'rs', 'ru', 'rw', 'sa', 'sb', 'sc', 'sd', 'se', 'sg', 'sh', 'si', 'sj', 'sk', 'sl',
'sm', 'sn', 'so', 'sr', 'st', 'su', 'sv', 'sy', 'sz', 'tc', 'td', 'tel', 'tf', 'tg', 'th',
'tj', 'tk', 'tl', 'tm', 'tn', 'to', 'tp', 'tr', 'travel', 'tt', 'tv', 'tw', 'tz', 'ua',
'ug', 'uk', 'um', 'us', 'uy', 'uz', 'va', 'vc', 've', 'vg', 'vi', 'vn', 'vu', 'wf', 'ws',
'xxx', 'ye', 'yt', 'yu', 'za', 'zm', 'zw'
'ac', 'academy', 'actor', 'ad', 'ae', 'aero', 'af', 'ag', 'agency', 'ai', 'al', 'am', 'an', 'ao', 'aq', 'ar',
'arpa', 'as', 'asia', 'at', 'au', 'aw', 'ax', 'az', 'ba', 'bar', 'bargains', 'bb', 'bd', 'be', 'berlin', 'best',
'bf', 'bg', 'bh', 'bi', 'bike', 'biz', 'bj', 'bl', 'blue', 'bm', 'bn', 'bo', 'boutique', 'bq', 'br', 'bs', 'bt',
'build', 'builders', 'buzz', 'bv', 'bw', 'by', 'bz', 'ca', 'cab', 'camera', 'camp', 'cards', 'careers', 'cat',
'catering', 'cc', 'cd', 'center', 'ceo', 'cf', 'cg', 'ch', 'cheap', 'christmas', 'ci', 'ck', 'cl', 'cleaning',
'clothing', 'club', 'cm', 'cn', 'co', 'codes', 'coffee', 'com', 'community', 'company', 'computer',
'construction', 'contractors', 'cool', 'coop', 'cr', 'cruises', 'cu', 'cv', 'cw', 'cx', 'cy', 'cz', 'dance',
'dating', 'de', 'democrat', 'diamonds', 'directory', 'dj', 'dk', 'dm', 'do', 'domains', 'dz', 'ec', 'edu',
'education', 'ee', 'eg', 'eh', 'email', 'enterprises', 'equipment', 'er', 'es', 'estate', 'et', 'eu', 'events',
'expert', 'exposed', 'farm', 'fi', 'fish', 'fj', 'fk', 'flights', 'florist', 'fm', 'fo', 'foundation', 'fr',
'futbol', 'ga', 'gallery', 'gb', 'gd', 'ge', 'gf', 'gg', 'gh', 'gi', 'gift', 'gl', 'glass', 'gm', 'gn', 'gov',
'gp', 'gq', 'gr', 'graphics', 'gs', 'gt', 'gu', 'guitars', 'guru', 'gw', 'gy', 'hk', 'hm', 'hn', 'holdings',
'holiday', 'house', 'hr', 'ht', 'hu', 'id', 'ie', 'il', 'im', 'immobilien', 'in', 'industries', 'info',
'institute', 'int', 'international', 'io', 'iq', 'ir', 'is', 'it', 'je', 'jm', 'jo', 'jobs', 'jp', 'kaufen',
'ke', 'kg', 'kh', 'ki', 'kim', 'kitchen', 'kiwi', 'km', 'kn', 'kp', 'kr', 'kred', 'kw', 'ky', 'kz', 'la',
'land', 'lb', 'lc', 'li', 'lighting', 'limo', 'link', 'lk', 'lr', 'ls', 'lt', 'lu', 'luxury', 'lv', 'ly', 'ma',
'management', 'mango', 'marketing', 'mc', 'md', 'me', 'menu', 'mf', 'mg', 'mh', 'mil', 'mk', 'ml', 'mm', 'mn',
'mo', 'mobi', 'moda', 'monash', 'mp', 'mq', 'mr', 'ms', 'mt', 'mu', 'museum', 'mv', 'mw', 'mx', 'my', 'mz',
'na', 'nagoya', 'name', 'nc', 'ne', 'net', 'neustar', 'nf', 'ng', 'ni', 'ninja', 'nl', 'no', 'np', 'nr', 'nu',
'nz', 'om', 'onl', 'org', 'pa', 'partners', 'parts', 'pe', 'pf', 'pg', 'ph', 'photo', 'photography', 'photos',
'pics', 'pink', 'pk', 'pl', 'plumbing', 'pm', 'pn', 'post', 'pr', 'pro', 'productions', 'properties', 'ps',
'pt', 'pub', 'pw', 'py', 'qa', 'qpon', 're', 'recipes', 'red', 'rentals', 'repair', 'report', 'reviews', 'rich',
'ro', 'rs', 'ru', 'ruhr', 'rw', 'sa', 'sb', 'sc', 'sd', 'se', 'sexy', 'sg', 'sh', 'shiksha', 'shoes', 'si',
'singles', 'sj', 'sk', 'sl', 'sm', 'sn', 'so', 'social', 'solar', 'solutions', 'sr', 'ss', 'st', 'su',
'supplies', 'supply', 'support', 'sv', 'sx', 'sy', 'systems', 'sz', 'tattoo', 'tc', 'td', 'technology', 'tel',
'tf', 'tg', 'th', 'tienda', 'tips', 'tj', 'tk', 'tl', 'tm', 'tn', 'to', 'today', 'tokyo', 'tools', 'tp', 'tr',
'training', 'travel', 'tt', 'tv', 'tw', 'tz', 'ua', 'ug', 'uk', 'um', 'uno', 'us', 'uy', 'uz', 'va',
'vacations', 'vc', 've', 'ventures', 'vg', 'vi', 'viajes', 'villas', 'vision', 'vn', 'voting', 'voyage', 'vu',
'wang', 'watch', 'wed', 'wf', 'wien', 'wiki', 'works', 'ws', '测试', 'परीक्षा', '集团', '在线', '한국', 'ভারত',
'বাংলা', '公益', '公司', '移动', '我爱你', 'испытание', 'қаз', 'онлайн', 'сайт', 'срб', '테스트', '삼성',
'சிங்கப்பூர்', 'дети', 'טעסט', '中文网', '中信', '中国', '中國', 'భారత్', 'ලංකා', '測試', 'ભારત', 'भारत',
'آزمایشی', 'பரிட்சை', '网络', 'укр', '香港', 'δοκιμή', 'إختبار', '台湾', '台灣', 'мон',
'الجزائر', 'عمان', 'ایران', 'امارات', 'بازار', 'پاکستان', 'الاردن', 'بھارت', 'المغرب', 'السعودية', 'سودان', 'مليسيا', 'شبكة', 'გე',
'ไทย', 'سورية', 'рф', 'تونس', 'みんな', 'ਭਾਰਤ', '游戏', 'مصر', 'قطر', 'இலங்கை', 'இந்தியா', '新加坡', 'فلسطين',
'テスト', '政务', 'xxx', 'xyz', 'ye', 'yt', 'za', 'zm', 'zone', 'zw'
);

/**
Expand All @@ -115,6 +131,7 @@ class Hostname extends AbstractValidator
* (.FI) Finland http://www.ficora.fi/en/index/palvelut/fiverkkotunnukset/aakkostenkaytto.html
* (.GR) Greece https://grweb.ics.forth.gr/CharacterTable1_en.jsp
* (.HU) Hungary http://www.domain.hu/domain/English/szabalyzat/szabalyzat.html
* (.IL) Israel http://www.isoc.org.il/domains/il-domain-rules.html
* (.INFO) International http://www.nic.info/info/idn
* (.IO) British Indian Ocean Territory http://www.nic.io/IO-IDN-Policy.pdf
* (.IR) Iran http://www.nic.ir/Allowable_Characters_dot-iran
Expand Down Expand Up @@ -172,6 +189,8 @@ class Hostname extends AbstractValidator
'GR' => array(1 => '/^[\x{002d}0-9a-zΆΈΉΊΌΎ-ΡΣ-ώἀ-ἕἘ-Ἕἠ-ὅὈ-Ὅὐ-ὗὙὛὝὟ-ώᾀ-ᾴᾶ-ᾼῂῃῄῆ-ῌῐ-ΐῖ-Ίῠ-Ῥῲῳῴῶ-ῼ]{1,63}$/iu'),
'HK' => 'Hostname/Cn.php',
'HU' => array(1 => '/^[\x{002d}0-9a-záéíóöúüőű]{1,63}$/iu'),
'IL' => array(1 => '/^[\x{002d}0-9\x{05D0}-\x{05EA}]{1,63}$/iu',
2 => '/^[\x{002d}0-9a-z]{1,63}$/i'),
'INFO'=> array(1 => '/^[\x{002d}0-9a-zäåæéöøü]{1,63}$/iu',
2 => '/^[\x{002d}0-9a-záéíóöúüőű]{1,63}$/iu',
3 => '/^[\x{002d}0-9a-záæéíðóöúýþ]{1,63}$/iu',
Expand Down Expand Up @@ -251,6 +270,10 @@ class Hostname extends AbstractValidator
'UA' => array(1 => '/^[\x{002d}0-9a-zабвгдежзийклмнопрстуфхцчшщъыьэюяѐёђѓєѕіїјљњћќѝўџґӂʼ]{1,63}$/iu'),
'VE' => array(1 => '/^[\x{002d}0-9a-záéíóúüñ]{1,63}$/iu'),
'VN' => array(1 => '/^[ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚÝàáâãèéêìíòóôõùúýĂăĐđĨĩŨũƠơƯư\x{1EA0}-\x{1EF9}]{1,63}$/iu'),
'мон' => array(1 => '/^[\x{002d}0-9\x{0430}-\x{044F}]{1,63}$/iu'),
'срб' => array(1 => '/^[\x{002d}0-9а-ик-шђјљњћџ]{1,63}$/iu'),
'сайт' => array(1 => '/^[\x{002d}0-9а-яёіїѝйўґг]{1,63}$/iu'),
'онлайн' => array(1 => '/^[\x{002d}0-9а-яёіїѝйўґг]{1,63}$/iu'),
'中国' => 'Hostname/Cn.php',
'中國' => 'Hostname/Cn.php',
'ලංකා' => array(1 => '/^[\x{0d80}-\x{0dff}]{1,63}$/iu'),
Expand All @@ -266,6 +289,7 @@ class Hostname extends AbstractValidator
'مصر' => array(1 => '/^[\x{0621}-\x{0624}\x{0626}-\x{063A}\x{0641}\x{0642}\x{0644}-\x{0648}\x{067E}\x{0686}\x{0698}\x{06A9}\x{06AF}\x{06CC}\x{06F0}-\x{06F9}]{1,30}$/iu'),
'இலங்கை' => array(1 => '/^[\x{0b80}-\x{0bff}]{1,63}$/iu'),
'فلسطين' => array(1 => '/^[\x{0621}-\x{0624}\x{0626}-\x{063A}\x{0641}\x{0642}\x{0644}-\x{0648}\x{067E}\x{0686}\x{0698}\x{06A9}\x{06AF}\x{06CC}\x{06F0}-\x{06F9}]{1,30}$/iu'),
'شبكة' => array(1 => '/^[\x{0621}-\x{0624}\x{0626}-\x{063A}\x{0641}\x{0642}\x{0644}-\x{0648}\x{067E}\x{0686}\x{0698}\x{06A9}\x{06AF}\x{06CC}\x{06F0}-\x{06F9}]{1,30}$/iu'),
);

protected $idnLength = array(
Expand All @@ -284,6 +308,7 @@ class Hostname extends AbstractValidator
'تونس' => array(1 => 30),
'مصر' => array(1 => 30),
'فلسطين' => array(1 => 30),
'شبكة' => array(1 => 30),
'中国' => array(1 => 20),
'中國' => array(1 => 20),
'香港' => array(1 => 20),
Expand Down Expand Up @@ -352,6 +377,7 @@ public function getIpValidator()
}

/**
*
* @param Ip $ipValidator OPTIONAL
* @return Hostname;
*/
Expand Down Expand Up @@ -452,8 +478,9 @@ public function isValid($value)

$this->setValue($value);
// Check input against IP address schema
if (preg_match('/^[0-9a-f:.]*$/i', $value) &&
$this->getIpValidator()->setTranslator($this->getTranslator())->isValid($value)) {
if (preg_match('/^[0-9a-f:.]*$/i', $value) && $this->getIpValidator()
->setTranslator($this->getTranslator())
->isValid($value)) {
if (!($this->getAllow() & self::ALLOW_IP)) {
$this->error(self::IP_ADDRESS_NOT_ALLOWED);
return false;
Expand All @@ -477,21 +504,26 @@ public function isValid($value)
$domainParts = explode('.', $value);

// Prevent partial IP V4 addresses (ending '.')
if ((count($domainParts) == 4) && preg_match('/^[0-9.a-e:.]*$/i', $value) &&
$this->getIpValidator()->setTranslator($this->getTranslator())->isValid($value)) {
if ((count($domainParts) == 4) && preg_match('/^[0-9.a-e:.]*$/i', $value) && $this->getIpValidator()
->setTranslator($this->getTranslator())
->isValid($value)) {
$this->error(self::INVALID_LOCAL_NAME);
}

$utf8StrWrapper = StringUtils::getWrapper('UTF-8');

// Check input against DNS hostname schema
if ((count($domainParts) > 1) && (strlen($value) >= 4) && (strlen($value) <= 254)) {
$utf8StrWrapper = StringUtils::getWrapper('UTF-8');
$status = false;
if ((count($domainParts) > 1)
&& ($utf8StrWrapper->strlen($value) >= 4)
&& ($utf8StrWrapper->strlen($value) <= 254))
{
$status = false;

do {
// First check TLD
$matches = array();
if (preg_match('/([^.]{2,10})$/i', end($domainParts), $matches) ||
(array_key_exists(end($domainParts), $this->validIdns))) {
if (preg_match('/([^.]{2,10})$/iu', end($domainParts), $matches)
|| (array_key_exists(end($domainParts), $this->validIdns))) {
reset($domainParts);

// Hostname characters are: *(label dot)(label dot label); max 254 chars
Expand All @@ -500,26 +532,31 @@ public function isValid($value)
// ldh: alpha / digit / dash

// Match TLD against known list
$this->tld = strtolower($matches[1]);
$this->tld = strtoupper($matches[1]);
if ($this->getTldCheck()) {
if (!in_array($this->tld, $this->validTlds)) {
if (!in_array(strtolower($this->tld), $this->validTlds)
&& !in_array($this->tld, $this->validTlds)) {
$this->error(self::UNKNOWN_TLD);
$status = false;
break;
}
// We have already validated that the TLD is fine. We don't want it to go through the below
// checks as new UTF-8 TLDs will incorrectly fail if there is no IDN regex for it.
array_pop($domainParts);
}

/**
* Match against IDN hostnames
* Note: Keep label regex short to avoid issues with long patterns when matching IDN hostnames
*
* @see Hostname\Interface
*/
$regexChars = array(0 => '/^[a-z0-9\x2d]{1,63}$/i');
if ($this->getIdnCheck() && isset($this->validIdns[strtoupper($this->tld)])) {
if (is_string($this->validIdns[strtoupper($this->tld)])) {
$regexChars += include __DIR__ .'/'. $this->validIdns[strtoupper($this->tld)];
if ($this->getIdnCheck() && isset($this->validIdns[$this->tld])) {
if (is_string($this->validIdns[$this->tld])) {
$regexChars += include __DIR__ . '/' . $this->validIdns[$this->tld];
} else {
$regexChars += $this->validIdns[strtoupper($this->tld)];
$regexChars += $this->validIdns[$this->tld];
}
}

Expand All @@ -535,9 +572,9 @@ public function isValid($value)
}

// Check dash (-) does not start, end or appear in 3rd and 4th positions
if ((strpos($domainPart, '-') === 0)
|| ((strlen($domainPart) > 2) && (strpos($domainPart, '-', 2) == 2) && (strpos($domainPart, '-', 3) == 3))
|| (strpos($domainPart, '-') === (strlen($domainPart) - 1))) {
if (($utf8StrWrapper->strpos($domainPart, '-') === 0)
|| (($utf8StrWrapper->strlen($domainPart) > 2) && ($utf8StrWrapper->strpos($domainPart, '-', 2) == 2) && ($utf8StrWrapper->strpos($domainPart, '-', 3) == 3))
|| ($utf8StrWrapper->strpos($domainPart, '-') === ($utf8StrWrapper->strlen($domainPart) - 1))) {
$this->error(self::INVALID_DASH);
$status = false;
break 2;
Expand All @@ -551,13 +588,14 @@ public function isValid($value)
ErrorHandler::stop();
if ($status > 0) {
$length = 63;
if (array_key_exists(strtoupper($this->tld), $this->idnLength)
&& (array_key_exists($regexKey, $this->idnLength[strtoupper($this->tld)]))) {
$length = $this->idnLength[strtoupper($this->tld)];
if (array_key_exists($this->tld, $this->idnLength)
&& (array_key_exists($regexKey, $this->idnLength[$this->tld]))) {
$length = $this->idnLength[$this->tld];
}

if ($utf8StrWrapper->strlen($domainPart) > $length) {
$this->error(self::INVALID_HOSTNAME);
$status = false;
} else {
$checked = true;
break;
Expand Down Expand Up @@ -589,6 +627,7 @@ public function isValid($value)
}
} elseif ($this->getAllow() & self::ALLOW_DNS) {
$this->error(self::INVALID_HOSTNAME);
$status = false;
}

// Check for URI Syntax (RFC3986)
Expand Down Expand Up @@ -628,10 +667,10 @@ public function isValid($value)

/**
* Decodes a punycode encoded string to it's original utf8 string
* In case of a decoding failure the original string is returned
* Returns false in case of a decoding failure.
*
* @param string $encoded Punycode encoded string to decode
* @return string
* @return string|false
*/
protected function decodePunycode($encoded)
{
Expand Down
49 changes: 45 additions & 4 deletions tests/ZendTest/Validator/HostnameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,49 @@ public function testIDNSI()
$this->assertFalse($validator->isValid('رات.si'));
}

/**
* @group Issue #5894 - Add .il IDN domain checking; add new TLDs
*/
public function testIDNIL()
{
$validator = new Hostname(Hostname::ALLOW_ALL);

// Check .IL TLD matching
$valuesExpected = array(
array(true, array('xn----zhcbgfhe2aacg8fb5i.org.il', 'מבחן.il', 'מבחן123.il')),
array(false, array('tבדיקה123.il', 'رات.il')) // Can't mix Latin and Hebrew character sets (except digits)
);
foreach ($valuesExpected as $element) {
foreach ($element[1] as $input) {
$this->assertEquals(
$element[0],
$validator->isValid($input),
implode("\n", $validator->getMessages()) .' - '. $input
);
}
}
}

public function testAdditionalUTF8TLDs()
{
$validator = new Hostname(Hostname::ALLOW_ALL);

// Check UTF-8 TLD matching
$valuesExpected = array(
array(true, array('test123.δοκιμή', 'тест.рф', 'туршилтын.мон')),
array(false, array('சோதனை3.இலங்கை', 'رات.мон'))
);
foreach ($valuesExpected as $element) {
foreach ($element[1] as $input) {
$this->assertEquals(
$element[0],
$validator->isValid($input),
implode("\n", $validator->getMessages()) .' - '. $input
);
}
}
}

public function testIDNIT()
{
$validator = new Hostname(Hostname::ALLOW_ALL);
Expand All @@ -462,14 +505,12 @@ public function testIDNIT()
public function testEqualsMessageTemplates()
{
$validator = $this->validator;
$this->assertAttributeEquals($validator->getOption('messageTemplates'),
'messageTemplates', $validator);
$this->assertAttributeEquals($validator->getOption('messageTemplates'), 'messageTemplates', $validator);
}

public function testEqualsMessageVariables()
{
$validator = $this->validator;
$this->assertAttributeEquals($validator->getOption('messageVariables'),
'messageVariables', $validator);
$this->assertAttributeEquals($validator->getOption('messageVariables'), 'messageVariables', $validator);
}
}

0 comments on commit 4c617fd

Please sign in to comment.