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

Punycode decoding fails if encoded string has not hyphen #4636

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions library/Zend/Validator/Hostname.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,22 +635,19 @@ public function isValid($value)
*/
protected function decodePunycode($encoded)
{
$found = preg_match('/([^a-z0-9\x2d]{1,10})$/i', $encoded);
if (empty($encoded) || ($found > 0)) {
// no punycode encoded string, return as is
if (!preg_match('/^[a-z0-9-]+$/i', $encoded)) {
// no punycode encoded string
$this->error(self::CANNOT_DECODE_PUNYCODE);
return false;
}

$decoded = array();
$separator = strrpos($encoded, '-');
if ($separator > 0) {
for ($x = 0; $x < $separator; ++$x) {
// prepare decoding matrix
$decoded[] = ord($encoded[$x]);
}
} else {
$this->error(self::CANNOT_DECODE_PUNYCODE);
return false;
}

$lengthd = count($decoded);
Expand Down
2 changes: 1 addition & 1 deletion tests/ZendTest/Validator/HostnameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public function testPunycodeDecoding()

// Check TLD matching
$valuesExpected = array(
array(true, array('xn--brger-kva.com')),
array(true, array('xn--brger-kva.com', 'xn--eckwd4c7cu47r2wf.jp')),
array(false, array('xn--brger-x45d2va.com', 'xn--bürger.com', 'xn--'))
);
foreach ($valuesExpected as $element) {
Expand Down