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

Zend/Validator/Hostname.php trigger IP-check on pure numerical domain name. #6663

Closed
coding-angel opened this issue Sep 11, 2014 · 3 comments

Comments

@coding-angel
Copy link

I have tried a isolated test of hostname.php by including 8 Files in order to resolve dependencies:
'ErrorHandler.php',
'StringWrapperInterface.php',
'AbstractStringWrapper.php',
'MbString.php',
'StringUtils.php',
'ArrayUtils.php',
'AbstractValidator.php',
'Hostname.php'

I have run about 15 test cases which was successful, except this one:
"123456789012345678901234567890123456789012345678901234567890123.de"
The domain name has regular chars [a-zA-z0-9] and allowed length of 63 chars.

The test is run with default basic usage according:
http://unofficial-zf2.readthedocs.org/en/latest/modules/zend.validator.hostname.html

$validator = new Hostname();
if ($validator->isValid($hostname)) {
// hostname appears to be valid
} else {
// hostname is invalid; print the reasons
foreach ($validator->getMessages() as $message) {
echo "$message\n";
}
}

I got:
PHP Fatal error: Call to a member function setTranslator() on a non-object in /Zend/Validator/Hostname.php on line 487

The line is strange but near of it 482 is setTranslator().
$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 (!($this->getAllow() & self::ALLOW_IP)) {
$this->error(self::IP_ADDRESS_NOT_ALLOWED);
return false;
} else {
return true;
}
}
IpValidator is not given. It is defined as optional in __construct.

And it is not an IP at all.

@Tauop
Copy link
Contributor

Tauop commented Sep 13, 2014

Hi,

I can't reproduce your error.

In dependencies you have listed, you may have forgotten Ip.php.
When you instantiate Hostname without argument, the constructor set $options['ipValidator'], pass it to the parent constructor, call setOptions and setIpValidator() with null argument.
So $this->getIpValidator() returns a instance of Ip, and the error you have in line 487 must not be trigger.

Patrick

@coding-angel
Copy link
Author

Thank you for your answer, Patrick.

I understand the Error and the dependency to Ip.php, and i have resolved these.
My point is only that:

  1. IpValidator must be set, but according to the definition of __construct() is optional.
  2. more important: checking of valid hostnames like "12345.de" does not work, because it is recognized falsely to be an IP (hexadecimals.:), but it is not.
  3. there is no possibility to skip the IP-Check. Or is it? I see only ALLOW_IP.
  4. not relevant to this thread: for domain '.name' there are no regex rules.

For 3) my solution was a pre-check for hexadecimal hostnames first and not calling Zend/Validator/Hostname.php for them at all. Is there better way to do this? Something like SKIP_IP_CHECK?

Angel

@Tauop
Copy link
Contributor

Tauop commented Sep 13, 2014

1/ You can give your IpValidator. If no IpValidator is given, 'ipValidator' is set to null see _constructor. Then, parent::setOptions() calls setIpValidator() with null param, which instantiate a Ip() validator object

2/ it's not recognized as IP but as valid hostname Test case and html coverage in zip file

3/ You are right !

4/ Don't hesitate to contribute :)

For 3/, I agree with you that skiping IP checks options will be more convenient.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants