Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 0f45aee

Browse files
committed
Merge branch 'hotfix/67'
Close #67
2 parents b7b45e8 + a10e468 commit 0f45aee

File tree

3 files changed

+59
-17
lines changed

3 files changed

+59
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ All notable changes to this project will be documented in this file, in reverse
66

77
### Added
88

9-
- Nothing.
9+
- [#67](https://github.com/zendframework/zend-validator/pull/67) adds support
10+
for Punycoded top-level domains in the `Hostname` validator.
1011

1112
### Deprecated
1213

src/Hostname.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1772,8 +1772,18 @@ public function isValid($value)
17721772
// id-prefix: alpha / digit
17731773
// ldh: alpha / digit / dash
17741774

1775+
$this->tld = $matches[1];
1776+
// Decode Punycode TLD to IDN
1777+
if (strpos($this->tld, 'xn--') === 0) {
1778+
$this->tld = $this->decodePunycode(substr($this->tld, 4));
1779+
if ($this->tld === false) {
1780+
return false;
1781+
}
1782+
} else {
1783+
$this->tld = strtoupper($this->tld);
1784+
}
1785+
17751786
// Match TLD against known list
1776-
$this->tld = strtoupper($matches[1]);
17771787
if ($this->getTldCheck()) {
17781788
if (!in_array(strtolower($this->tld), $this->validTlds)
17791789
&& !in_array($this->tld, $this->validTlds)) {

test/HostnameTest.php

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -486,24 +486,55 @@ public function testIDNIL()
486486
}
487487
}
488488

489-
public function testAdditionalUTF8TLDs()
489+
/**
490+
* Ensures that the validator follows expected behavior for UTF-8 and Punycoded (ACE) TLDs
491+
*
492+
* @dataProvider validTLDHostnames
493+
*/
494+
public function testValidTLDHostnames($value)
490495
{
491-
$validator = new Hostname(Hostname::ALLOW_ALL);
496+
$this->assertTrue(
497+
$this->validator->isValid($value),
498+
sprintf(
499+
'%s failed validation: %s',
500+
$value,
501+
implode("\n", $this->validator->getMessages())
502+
)
503+
);
504+
}
492505

493-
// Check UTF-8 TLD matching
494-
$valuesExpected = [
495-
[true, ['test123.онлайн', 'тест.рф', 'туршилтын.мон']],
496-
[false, ['சோதனை3.இலங்கை', 'رات.мон']]
506+
public function validTLDHostnames()
507+
{
508+
// @codingStandardsIgnoreStart
509+
return [
510+
'ASCII label + UTF-8 TLD' => ['test123.онлайн'],
511+
'ASCII label + Punycoded TLD' => ['test123.xn--80asehdb'],
512+
'UTF-8 label + UTF-8 TLD (cyrillic)' => ['тест.рф'],
513+
'Punycoded label + Punycoded TLD (cyrillic)' => ['xn--e1aybc.xn--p1ai'],
497514
];
498-
foreach ($valuesExpected as $element) {
499-
foreach ($element[1] as $input) {
500-
$this->assertEquals(
501-
$element[0],
502-
$validator->isValid($input),
503-
implode("\n", $validator->getMessages()) .' - '. $input
504-
);
505-
}
506-
}
515+
// @codingStandardsIgnoreEnd
516+
}
517+
518+
/**
519+
* Ensures that the validator follows expected behavior for invalid UTF-8 and Punycoded (ACE) TLDs
520+
*
521+
* @dataProvider invalidTLDHostnames
522+
*/
523+
public function testInalidTLDHostnames($value)
524+
{
525+
$this->assertFalse($this->validator->isValid($value));
526+
}
527+
528+
public function invalidTLDHostnames()
529+
{
530+
// @codingStandardsIgnoreStart
531+
return [
532+
'Invalid mix of UTF-8 and ASCII in label' => ['சோதனை3.இலங்கை'],
533+
'Invalid mix of UTF-8 and ASCII in label (Punycoded)' => ['xn--3-owe4au9mpa.xn--xkc2al3hye2a'],
534+
'Invalid use of non-cyrillic characters with cyrillic TLD' => ['رات.мон'],
535+
'Invalid use of non-cyrillic characters with cyrillic TLD (Punycoded)' => ['xn--mgbgt.xn--l1acc'],
536+
];
537+
// @codingStandardsIgnoreEnd
507538
}
508539

509540
public function testIDNIT()

0 commit comments

Comments
 (0)