Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing tests for IDN TLD that contains uppercase #64

Open
wants to merge 2 commits into
base: 2.14.x
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"php": "^7.3 || ~8.0.0",
"container-interop/container-interop": "^1.1",
"laminas/laminas-stdlib": "^3.3",
"laminas/laminas-zendframework-bridge": "^1.0"
"laminas/laminas-zendframework-bridge": "^1.0",
"symfony/polyfill-mbstring": "^1.20"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO to be declared should be ext-mbstring, while we can decide to use symfony/polyfill-mbstring in require-dev

},
"require-dev": {
"laminas/laminas-cache": "^2.6.1",
Expand Down
162 changes: 81 additions & 81 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions src/Hostname.php
Original file line number Diff line number Diff line change
Expand Up @@ -1995,8 +1995,8 @@ public function isValid($value)
$this->setValue($value);
// Check input against IP address schema
if (
((preg_match('/^[0-9.]*$/', $value) && strpos($value, '.') !== false)
|| (preg_match('/^[0-9a-f:.]*$/i', $value) && strpos($value, ':') !== false))
((preg_match('/^[0-9.]*$/', $value) && mb_strpos($value, '.') !== false)
|| (preg_match('/^[0-9a-f:.]*$/i', $value) && mb_strpos($value, ':') !== false))
&& $this->getIpValidator()->setTranslator($this->getTranslator())->isValid($value)
) {
if (! ($this->getAllow() & self::ALLOW_IP)) {
Expand All @@ -2010,16 +2010,16 @@ public function isValid($value)
// Handle Regex compilation failure that may happen on .biz domain with has @ character, eg: [email protected]
// Technically, hostname with '@' character is invalid, so mark as invalid immediately
// @see https://github.com/laminas/laminas-validator/issues/8
if (strpos($value, '@') !== false) {
if (mb_strpos($value, '@') !== false) {
$this->error(self::INVALID_HOSTNAME);
return false;
}

// Local hostnames are allowed to be partial (ending '.')
if ($this->getAllow() & self::ALLOW_LOCAL) {
if (substr($value, -1) === '.') {
$value = substr($value, 0, -1);
if (substr($value, -1) === '.') {
if (mb_substr($value, -1) === '.') {
$value = mb_substr($value, 0, -1);
if (mb_substr($value, -1) === '.') {
// Empty hostnames (ending '..') are not allowed
$this->error(self::INVALID_LOCAL_NAME);
return false;
Expand Down Expand Up @@ -2063,20 +2063,20 @@ public function isValid($value)

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

// Match TLD against known list
$removedTld = false;
if ($this->getTldCheck()) {
if (
! in_array(strtolower($this->tld), $this->validTlds)
! in_array(mb_strtolower($this->tld), $this->validTlds)
&& ! in_array($this->tld, $this->validTlds)
) {
$this->error(self::UNKNOWN_TLD);
Expand Down Expand Up @@ -2112,8 +2112,8 @@ public function isValid($value)
}
foreach ($domainParts as $domainPart) {
// Decode Punycode domain names to IDN
if (strpos($domainPart, 'xn--') === 0) {
$domainPart = $this->decodePunycode(substr($domainPart, 4));
if (mb_strpos($domainPart, 'xn--') === 0) {
$domainPart = $this->decodePunycode(mb_substr($domainPart, 4));
if ($domainPart === false) {
return false;
}
Expand Down Expand Up @@ -2240,7 +2240,7 @@ protected function decodePunycode($encoded)
}

$decoded = [];
$separator = strrpos($encoded, '-');
$separator = mb_strrpos($encoded, '-');
if ($separator > 0) {
for ($x = 0; $x < $separator; ++$x) {
// prepare decoding matrix
Expand All @@ -2249,7 +2249,7 @@ protected function decodePunycode($encoded)
}

$lengthd = count($decoded);
$lengthe = strlen($encoded);
$lengthe = mb_strlen($encoded);

// decoding
$init = true;
Expand Down
2 changes: 2 additions & 0 deletions test/HostnameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,10 @@ public function validTLDHostnames(): array
// @codingStandardsIgnoreStart
return [
'ASCII label + UTF-8 TLD' => ['test123.онлайн'],
'ASCII label + UTF-8 TLD (Uppercase chars)' => ['test123.оНлАйН'],
'ASCII label + Punycoded TLD' => ['test123.xn--80asehdb'],
'UTF-8 label + UTF-8 TLD (cyrillic)' => ['тест.рф'],
'UTF-8 label + UTF-8 TLD (cyrillic) + CAPS' => ['ТЕСТ.РФ'],
'Punycoded label + Punycoded TLD (cyrillic)' => ['xn--e1aybc.xn--p1ai'],
];
// @codingStandardsIgnoreEnd
Expand Down