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 testc07fb416ff
update dataProvider function name1825d0f2ae
cast substr result to string and remove empty function use894127bf09
rename dataset providerd973eb1f7d
Add a test to prevent future regressions6f24b05467
Switch to `empty` native function to check emptinessf390f29173
remove non relevant test case91b665c12a
Switch to `is_string` native method6071ff6c8f
Remove unnecessary parentheses9753a2773e
Add a test case to prevent future regressions4fcb24bacb
Move empty condition in return statementa090ef855a
Use LF line separator60392fdd43
fix coding standard to comply with fabbot197d19f34c
Remove malformed EmailValidatorTest + Update UrlValidator test6b0702e52a
Add empty check on host in other methods + add unit tests6dd023f255
[Validator] Allow checkMX() to return false when $host is empty
This commit is contained in:
commit
2adfb375c6
@ -93,7 +93,7 @@ class EmailValidator extends ConstraintValidator
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$host = substr($value, strrpos($value, '@') + 1);
|
$host = (string) substr($value, strrpos($value, '@') + 1);
|
||||||
|
|
||||||
// Check for host DNS resource records
|
// Check for host DNS resource records
|
||||||
if ($constraint->checkMX) {
|
if ($constraint->checkMX) {
|
||||||
@ -138,7 +138,7 @@ class EmailValidator extends ConstraintValidator
|
|||||||
*/
|
*/
|
||||||
private function checkMX($host)
|
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)
|
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')));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,7 +80,7 @@ class UrlValidator extends ConstraintValidator
|
|||||||
if ($constraint->checkDNS) {
|
if ($constraint->checkDNS) {
|
||||||
$host = parse_url($value, PHP_URL_HOST);
|
$host = parse_url($value, PHP_URL_HOST);
|
||||||
|
|
||||||
if (!checkdnsrr($host, 'ANY')) {
|
if (!is_string($host) || !checkdnsrr($host, 'ANY')) {
|
||||||
if ($this->context instanceof ExecutionContextInterface) {
|
if ($this->context instanceof ExecutionContextInterface) {
|
||||||
$this->context->buildViolation($constraint->dnsMessage)
|
$this->context->buildViolation($constraint->dnsMessage)
|
||||||
->setParameter('{{ value }}', $this->formatValue($host))
|
->setParameter('{{ value }}', $this->formatValue($host))
|
||||||
|
@ -159,4 +159,32 @@ class EmailValidatorTest extends AbstractConstraintValidatorTest
|
|||||||
|
|
||||||
$this->assertNoViolation();
|
$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),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -171,6 +171,7 @@ class UrlValidatorTest extends AbstractConstraintValidatorTest
|
|||||||
array('http://example.com/exploit.html?<script>alert(1);</script>'),
|
array('http://example.com/exploit.html?<script>alert(1);</script>'),
|
||||||
array('http://example.com/exploit.html?hel lo'),
|
array('http://example.com/exploit.html?hel lo'),
|
||||||
array('http://example.com/exploit.html?not_a%hex'),
|
array('http://example.com/exploit.html?not_a%hex'),
|
||||||
|
array('http://'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user