Add empty check on host in other methods + add unit tests

Add empty on host in other methods where checkdnsrr is called and add unit tests to prevent future regressions
This commit is contained in:
apetitpa 2017-03-23 11:01:22 +01:00 committed by Fabien Potencier
parent 6dd023f255
commit 6b0702e52a
4 changed files with 13 additions and 2 deletions

View File

@ -138,7 +138,7 @@ class EmailValidator extends ConstraintValidator
*/
private function checkMX($host)
{
if (null === $host || '' === $host) {
if ('' === $host) {
return false;
}
@ -154,6 +154,10 @@ class EmailValidator extends ConstraintValidator
*/
private function checkHost($host)
{
if ('' === $host) {
return false;
}
return $this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'));
}
}

View File

@ -80,7 +80,7 @@ class UrlValidator extends ConstraintValidator
if ($constraint->checkDNS) {
$host = parse_url($value, PHP_URL_HOST);
if (!checkdnsrr($host, 'ANY')) {
if ('' === $host || !checkdnsrr($host, 'ANY')) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->dnsMessage)
->setParameter('{{ value }}', $this->formatValue($host))

View File

@ -159,4 +159,10 @@ class EmailValidatorTest extends AbstractConstraintValidatorTest
$this->assertNoViolation();
}
public function testEmptyHostReturnsFalse()
{
$this->assertFalse($this->validator->checkMX(''));
$this->assertFalse($this->validator->checkHost(''));
}
}

View File

@ -171,6 +171,7 @@ class UrlValidatorTest extends AbstractConstraintValidatorTest
array('http://example.com/exploit.html?<script>alert(1);</script>'),
array('http://example.com/exploit.html?hel lo'),
array('http://example.com/exploit.html?not_a%hex'),
array('http:/.com'),
);
}