Skip to content

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

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

Closed
wants to merge 16 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function validate($value, Constraint $constraint)
return;
}

$host = substr($value, strrpos($value, '@') + 1);
$host = (string) substr($value, strrpos($value, '@') + 1);

// Check for host DNS resource records
if ($constraint->checkMX) {
Expand Down Expand Up @@ -118,7 +118,7 @@ public function validate($value, Constraint $constraint)
*/
private function checkMX($host)
{
return checkdnsrr($host, 'MX');
return '' !== $host && checkdnsrr($host, 'MX');
}

/**
Expand All @@ -130,6 +130,6 @@ private function checkMX($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')));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function validate($value, Constraint $constraint)
if ($constraint->checkDNS) {
$host = parse_url($value, PHP_URL_HOST);

if (!checkdnsrr($host, 'ANY')) {
if (!is_string($host) || !checkdnsrr($host, 'ANY')) {
$this->context->buildViolation($constraint->dnsMessage)
->setParameter('{{ value }}', $this->formatValue($host))
->setCode(Url::INVALID_URL_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,32 @@ public function testHostnameIsProperlyParsed()

$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),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public function getInvalidUrls()
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://'),
);
}

Expand Down