bug #22109 [Validator] check for empty host when calling checkdnsrr() (apetitpa)

This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #22109).

Discussion
----------

[Validator] check for empty host when calling checkdnsrr()

| Q | A |
| --- | --- |
| Branch? | master|
| Bug fix? | yes |
| New feature? | no |
| BC breaks? | no |
| Deprecations? | no |
| Tests pass? | yes |
| Fixed tickets | #22106 |
| License | MIT |
| Doc PR | n/a |

This should fix the email validator issue

Commits
-------

7104c4e849 move provider after test
c07fb416ff update dataProvider function name
1825d0f2ae cast substr result to string and remove empty function use
894127bf09 rename dataset provider
d973eb1f7d Add a test to prevent future regressions
6f24b05467 Switch to `empty` native function to check emptiness
f390f29173 remove non relevant test case
91b665c12a Switch to `is_string` native method
6071ff6c8f Remove unnecessary parentheses
9753a2773e Add a test case to prevent future regressions
4fcb24bacb Move empty condition in return statement
a090ef855a Use LF line separator
60392fdd43 fix coding standard to comply with fabbot
197d19f34c Remove malformed EmailValidatorTest + Update UrlValidator test
6b0702e52a Add empty check on host in other methods + add unit tests
6dd023f255 [Validator] Allow checkMX() to return false when $host is empty
This commit is contained in:
Fabien Potencier 2017-04-05 11:10:59 -07:00
commit 2adfb375c6
4 changed files with 33 additions and 4 deletions

View File

@ -93,7 +93,7 @@ class EmailValidator extends ConstraintValidator
return;
}
$host = substr($value, strrpos($value, '@') + 1);
$host = (string) substr($value, strrpos($value, '@') + 1);
// Check for host DNS resource records
if ($constraint->checkMX) {
@ -138,7 +138,7 @@ class EmailValidator extends ConstraintValidator
*/
private function checkMX($host)
{
return checkdnsrr($host, 'MX');
return '' !== $host && checkdnsrr($host, 'MX');
}
/**
@ -150,6 +150,6 @@ class EmailValidator extends ConstraintValidator
*/
private function checkHost($host)
{
return $this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'));
return '' !== $host && ($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 (!is_string($host) || !checkdnsrr($host, 'ANY')) {
if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->dnsMessage)
->setParameter('{{ value }}', $this->formatValue($host))

View File

@ -159,4 +159,32 @@ class EmailValidatorTest extends AbstractConstraintValidatorTest
$this->assertNoViolation();
}
/**
* @dataProvider provideCheckTypes
*/
public function testEmptyHostIsNotValid($checkType, $violation)
{
$this->validator->validate(
'foo@bar.fr@',
new Email(array(
'message' => 'myMessage',
$checkType => true,
))
);
$this
->buildViolation('myMessage')
->setParameter('{{ value }}', '"foo@bar.fr@"')
->setCode($violation)
->assertRaised();
}
public function provideCheckTypes()
{
return array(
array('checkMX', Email::MX_CHECK_FAILED_ERROR),
array('checkHost', Email::HOST_CHECK_FAILED_ERROR),
);
}
}

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://'),
);
}