Skip to content

Commit

Permalink
Make sure that "Email" only validates strings
Browse files Browse the repository at this point in the history
There shouldn't be possible to consider a non-string value as a valid
email anyways, but the real problem is that the "RFCValidation" from
"egulias/email-validator" casts the input as a string which makes PHP
trigger an error.

Co-authored-by: Henrique Moody <[email protected]>
  • Loading branch information
KonstantinKolodnitsky and henriquemoody committed Oct 9, 2018
1 parent 5a067fa commit c9850f4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"symfony/polyfill-mbstring": "^1.2"
},
"require-dev": {
"egulias/email-validator": "~1.2",
"egulias/email-validator": "~1.2 || ~2.1",
"mikey179/vfsStream": "^1.5",
"phpunit/phpunit": "~4.0",
"symfony/validator": "~2.6.9",
Expand Down
6 changes: 5 additions & 1 deletion library/Rules/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ public function getEmailValidator()

public function validate($input)
{
if (!is_string($input)) {
return false;
}

$emailValidator = $this->getEmailValidator();
if (!$emailValidator instanceof EmailValidator) {
return is_string($input) && filter_var($input, FILTER_VALIDATE_EMAIL);
return (bool) filter_var($input, FILTER_VALIDATE_EMAIL);
}

if (!class_exists('Egulias\\EmailValidator\\Validation\\RFCValidation')) {
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/Rules/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Respect\Validation\Rules;

use stdClass;

function class_exists($className)
{
if (isset($GLOBALS['class_exists'][$className])) {
Expand Down Expand Up @@ -142,6 +144,10 @@ public function providerForInvalidEmail()
['[email protected]'],
['[email protected].'],
['[email protected]'],
[[]],
[new stdClass()],
[null],
[tmpfile()],
];
}
}

0 comments on commit c9850f4

Please sign in to comment.