Skip to content

Commit 9209bc5

Browse files
apetitpafabpot
authored andcommitted
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
1 parent 6fad799 commit 9209bc5

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

Constraints/EmailValidator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function validate($value, Constraint $constraint)
138138
*/
139139
private function checkMX($host)
140140
{
141-
if (null === $host || '' === $host) {
141+
if ('' === $host) {
142142
return false;
143143
}
144144

@@ -154,6 +154,10 @@ private function checkMX($host)
154154
*/
155155
private function checkHost($host)
156156
{
157+
if ('' === $host) {
158+
return false;
159+
}
160+
157161
return $this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'));
158162
}
159163
}

Constraints/UrlValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function validate($value, Constraint $constraint)
8080
if ($constraint->checkDNS) {
8181
$host = parse_url($value, PHP_URL_HOST);
8282

83-
if (!checkdnsrr($host, 'ANY')) {
83+
if ('' === $host || !checkdnsrr($host, 'ANY')) {
8484
if ($this->context instanceof ExecutionContextInterface) {
8585
$this->context->buildViolation($constraint->dnsMessage)
8686
->setParameter('{{ value }}', $this->formatValue($host))

Tests/Constraints/EmailValidatorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,10 @@ public function testHostnameIsProperlyParsed()
159159

160160
$this->assertNoViolation();
161161
}
162+
163+
public function testEmptyHostReturnsFalse()
164+
{
165+
$this->assertFalse($this->validator->checkMX(''));
166+
$this->assertFalse($this->validator->checkHost(''));
167+
}
162168
}

Tests/Constraints/UrlValidatorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public function getInvalidUrls()
171171
array('http://example.com/exploit.html?<script>alert(1);</script>'),
172172
array('http://example.com/exploit.html?hel lo'),
173173
array('http://example.com/exploit.html?not_a%hex'),
174+
array('http:/.com'),
174175
);
175176
}
176177

0 commit comments

Comments
 (0)