8000 bug #22109 [Validator] check for empty host when calling checkdnsrr()… · symfony/symfony@2adfb37 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2adfb37

Browse files
committed
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 ------- 7104c4e move provider after test c07fb41 update dataProvider function name 1825d0f cast substr result to string and remove empty function use 894127b rename dataset provider d973eb1 Add a test to prevent future regressions 6f24b05 Switch to `empty` native function to check emptiness f390f29 remove non relevant test case 91b665c Switch to `is_string` native method 6071ff6 Remove unnecessary parentheses 9753a27 Add a test case to prevent future regressions 4fcb24b Move empty condition in return statement a090ef8 Use LF line separator 60392fd fix coding standard to comply with fabbot 197d19f Remove malformed EmailValidatorTest + Update UrlValidator test 6b0702e Add empty check on host in other methods + add unit tests 6dd023f [Validator] Allow checkMX() to return false when $host is empty
2 parents 933835c + 7104c4e commit 2adfb37

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/Symfony/Component/Validator/Constraints/EmailValidator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function validate($value, Constraint $constraint)
9393
return;
9494
}
9595

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

9898
// Check for host DNS resource records
9999
if ($constraint->checkMX) {
@@ -138,7 +138,7 @@ public function validate($value, Constraint $constraint)
138138
*/
139139
private function checkMX($host)
140140
{
141-
return checkdnsrr($host, 'MX');
141+
return '' !== $host && checkdnsrr($host, 'MX');
142142
}
143143

144144
/**
@@ -150,6 +150,6 @@ private function checkMX($host)
150150
*/
151151
private function checkHost($host)
152152
{
153-
return $this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'));
153+
return '' !== $host && ($this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA')));
154154
}
155155
}

src/Symfony/Component/Validator/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 (!is_string($host) || !checkdnsrr($host, 'ANY')) {
8484
if ($this->context instanceof ExecutionContextInterface) {
8585
$this->context->buildViolation($constraint->dnsMessage)
8686
->setParameter('{{ value }}', $this->formatValue($host))

src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php

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

160160
$this->assertNoViolation();
161161
}
162+
163+
/**
164+
* @dataProvider provideCheckTypes
165+
*/
166+
public function testEmptyHostIsNotValid($checkType, $violation)
167+
{
168+
$this->validator->validate(
169+
'foo@bar.fr@',
170+
new Email(array(
171+
'message' => 'myMessage',
172+
$checkType => true,
173+
))
174+
);
175+
176+
$this
177+
->buildViolation('myMessage')
178+
->setParameter('{{ value }}', '"foo@bar.fr@"')
179+
->setCode($violation)
180+
->assertRaised();
181+
}
182+
183+
public function provideCheckTypes()
184+
{
185+
return array(
186+
array('checkMX', Email::MX_CHECK_FAILED_ERROR),
187+
array('checkHost', Email::HOST_CHECK_FAILED_ERROR),
188+
);
189+
}
162190
}

src/Symfony/Component/Validator/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://'),
174175
);
175176
}
176177

0 commit comments

Comments
 (0)
0