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

Commit

Permalink
Merge pull request #67 from BreyndotEchse/patch-3
Browse files Browse the repository at this point in the history
Validate Punycoded TLDs
  • Loading branch information
weierophinney committed May 12, 2016
2 parents b7b45e8 + 6e43a59 commit 74e2382
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 16 deletions.
12 changes: 11 additions & 1 deletion src/Hostname.php
Original file line number Diff line number Diff line change
Expand Up @@ -1772,8 +1772,18 @@ public function isValid($value)
// id-prefix: alpha / digit
// ldh: alpha / digit / dash

$this->tld = $matches[1];
// Decode Punycode TLD to IDN
if (strpos($this->tld, 'xn--') === 0) {
$this->tld = $this->decodePunycode(substr($this->tld, 4));
if ($this->tld === false) {
return false;
}
} else {
$this->tld = strtoupper($this->tld);
}

// Match TLD against known list
$this->tld = strtoupper($matches[1]);
if ($this->getTldCheck()) {
if (!in_array(strtolower($this->tld), $this->validTlds)
&& !in_array($this->tld, $this->validTlds)) {
Expand Down
61 changes: 46 additions & 15 deletions test/HostnameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,24 +486,55 @@ public function testIDNIL()
}
}

public function testAdditionalUTF8TLDs()
/**
* Ensures that the validator follows expected behavior for UTF-8 and Punycoded (ACE) TLDs
*
* @dataProvider validTLDHostnames
*/
public function testValidTLDHostnames($value)
{
$validator = new Hostname(Hostname::ALLOW_ALL);
$this->assertTrue(
$this->validator->isValid($value),
sprintf(
'%s failed validation: %s',
$value,
implode("\n", $this->validator->getMessages())
)
);
}

// Check UTF-8 TLD matching
$valuesExpected = [
[true, ['test123.онлайн', 'тест.рф', 'туршилтын.мон']],
[false, ['சோதனை3.இலங்கை', 'رات.мон']]
public function validTLDHostnames()
{
// @codingStandardsIgnoreStart
return [
'ASCII label + UTF-8 TLD' => ['test123.онлайн'],
'ASCII label + Punycoded TLD' => ['test123.xn--80asehdb'],
'UTF-8 label + UTF-8 TLD (cyrillic)' => ['тест.рф'],
'Punycoded label + Punycoded TLD (cyrillic)' => ['xn--e1aybc.xn--p1ai'],
];
foreach ($valuesExpected as $element) {
foreach ($element[1] as $input) {
$this->assertEquals(
$element[0],
$validator->isValid($input),
implode("\n", $validator->getMessages()) .' - '. $input
);
}
}
// @codingStandardsIgnoreEnd
}

/**
* Ensures that the validator follows expected behavior for invalid UTF-8 and Punycoded (ACE) TLDs
*
* @dataProvider invalidTLDHostnames
*/
public function testInalidTLDHostnames($value)
{
$this->assertFalse($this->validator->isValid($value));
}

public function invalidTLDHostnames()
{
// @codingStandardsIgnoreStart
return [
'Invalid mix of UTF-8 and ASCII in label' => ['சோதனை3.இலங்கை'],
'Invalid mix of UTF-8 and ASCII in label (Punycoded)' => ['xn--3-owe4au9mpa.xn--xkc2al3hye2a'],
'Invalid use of non-cyrillic characters with cyrillic TLD' => ['رات.мон'],
'Invalid use of non-cyrillic characters with cyrillic TLD (Punycoded)' => ['xn--mgbgt.xn--l1acc'],
];
// @codingStandardsIgnoreEnd
}

public function testIDNIT()
Expand Down

0 comments on commit 74e2382

Please sign in to comment.