Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

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

wants to merge 16 commits into from

Conversation

apetitpa
Copy link

@apetitpa apetitpa commented Mar 22, 2017

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

@fabpot
Copy link
Member

fabpot commented Mar 22, 2017

The regex should not be changed. But the condition should indeed be added in other methods using checkdnsrr.

@apetitpa
Copy link
Author

apetitpa commented Mar 23, 2017

thank you @fabpot and @jordscream I'll make some modifications and add unit tests.

@@ -118,6 +118,10 @@ public function validate($value, Constraint $constraint)
*/
private function checkMX($host)
{
if (null === $host || '' === $host) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$host can never be null, can it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right, I'll fix this. Thank you

Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please also add a test to prevent future regressions?

Copy link
Contributor

@jsamouh jsamouh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about unit test for the main issue ? ^^

if ('' === $host) {
return false;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preferred empty host condition in return statement

if ('' === $host) {
return false;
}

return $this->checkMX($host) || (checkdnsrr($host, 'A') || checkdnsrr($host, 'AAAA'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preferred empty host condition in return statement

@apetitpa
Copy link
Author

apetitpa commented Mar 23, 2017

About the unit test for the main issue, since checkMX() and checkHost() are both private how can I test it ? There is already a test case that gives an empty host array('example@'),

@jsamouh
Copy link
Contributor

jsamouh commented Mar 23, 2017

@apetitpa just test a use case as "test@test@" or "test@" in the EmailValidator. It should return the appropriate violation

@apetitpa
Copy link
Author

Thank you @jordscream I forgot that the original issue was with emails containing @ twice

@apetitpa apetitpa changed the title [Validator] Allow checkMX() to return false when $host is empty [Validator] check for empty host when calling checkdnsrr() Mar 23, 2017
@@ -118,7 +118,7 @@ public function validate($value, Constraint $constraint)
*/
private function checkMX($host)
{
return checkdnsrr($host, 'MX');
return ('' !== $host) && checkdnsrr($host, 'MX');
Copy link
Contributor

@Nek- Nek- Mar 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not !empty() check?

Copy link
Author

@apetitpa apetitpa Mar 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well there is a similar check line 45 if (null === $value || '' === $value) that does not use the native empty function so I assumed I should do the same but maybe I was wrong ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to avoid using empty in Symfony.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabpot interesting, why? (is the reason documented somewhere?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty is just too broad. For instance, it includes the 0 string as being false. Sometimes, that not desirable. So, we prefer to be very explicit about our expectations.

@@ -118,7 +118,7 @@ public function validate($value, Constraint $constraint)
*/
private function checkMX($host)
{
return checkdnsrr($host, 'MX');
return ('' !== $host) && checkdnsrr($host, 'MX');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parentheses can be removed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix this in a minute thank you.

@jsamouh
Copy link
Contributor

jsamouh commented Mar 23, 2017

@apetitpa , it seems your unit test fails cause you do not check the appropriate violation

@apetitpa
Copy link
Author

@jordscream I think you're right I should check for MX_CHECK_FAILED_ERROR or HOST_CHECK_FAILED_ERROR

@@ -72,7 +72,7 @@ public function validate($value, Constraint $constraint)
if ($constraint->checkDNS) {
$host = parse_url($value, PHP_URL_HOST);

if (!checkdnsrr($host, 'ANY')) {
if ('' === $host || !checkdnsrr($host, 'ANY')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps switch to !is_string($host) due possible false or null return values.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good idea. I'll do it thank you.

@jsamouh
Copy link
Contributor

jsamouh commented Mar 24, 2017

@apetitpa . When you think to do it ? ^^. Need this fix for a customer :-P

@apetitpa
Copy link
Author

@jordscream I hope this week-end but to be honest I'm facing difficulties creating an effective and working test for this issue. Some help would be appreciated.

@ro0NL
Copy link
Contributor

ro0NL commented Mar 24, 2017

@apetitpa have a look at https://symfony.com/doc/current/components/phpunit_bridge.html#dns-sensitive-tests

I would add a new , dns-senstive, test which covers exactly this case, so you can assert the expected violation code; or tweak a current test so you can provide the expected code.

edit: Dont think it needs to be dns-senstive though, as there will be no network traffic for foo@bar@.

@xabbuh
Copy link
Member

xabbuh commented Mar 24, 2017

Dont think it needs to be dns-senstive though, as there will be no network traffic for foo@bar@.

Agreed, the issue to fix is that the DNS functions should in fact not be called anymore.

@@ -118,7 +118,7 @@ public function validate($value, Constraint $constraint)
*/
private function checkMX($host)
{
return checkdnsrr($host, 'MX');
return !empty($host) && checkdnsrr($host, 'MX');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid empty :) We expect a string, and only a empty string ($host === '') should not be passed to checkdnsrr.

Copy link
Author

@apetitpa apetitpa Mar 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know but substr($value, strrpos($value, '@') + 1) with $value = 'foo@bar.fr@' returns "" with PHP 7.* and false with PHP 5.6. So only checking for string emptiness is not enough.

Copy link
Contributor

@ro0NL ro0NL Mar 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's cast the substring result above. $host = (string) ...;

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's done, thank you

@@ -229,4 +229,32 @@ public function testHostnameIsProperlyParsed()

$this->assertNoViolation();
}

public function getCheckTypes()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be provideCheckTypes i guess, but it's a tiny semantic ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make the modification thank you

@apetitpa
Copy link
Author

@xabbuh is it ok now ?

@@ -229,4 +229,32 @@ public function testHostnameIsProperlyParsed()

$this->assertNoViolation();
}

public function provideCheckTypes()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the provider after the test (that's the convention we are using in Symfony).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, will do in a minute.

apetitpa added 6 commits March 27, 2017 16:54
| 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 |
Add empty on host in other methods where checkdnsrr is called and add unit tests to prevent future regressions
Remove malformed EmailValidatorTest + Update UrlValidator test
@nicolas-grekas nicolas-grekas added this to the 3.3 milestone Mar 28, 2017
Copy link
Member

@xabbuh xabbuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@fabpot
Copy link
Member

fabpot commented Apr 5, 2017

Thank you @apetitpa.

fabpot added a commit that referenced this pull request Apr 5, 2017
… (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
@fabpot fabpot closed this Apr 5, 2017
@apetitpa apetitpa deleted the fix-email-validator branch April 14, 2017 18:50
This was referenced May 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.