Skip to content
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

Fix labels length check #278

Merged
merged 3 commits into from
Dec 29, 2020
Merged
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
36 changes: 30 additions & 6 deletions src/Parser/DomainPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
class DomainPart extends Parser
{
const DOMAIN_MAX_LENGTH = 254;
const LABEL_MAX_LENGTH = 63;

/**
* @var string
Expand Down Expand Up @@ -160,6 +161,7 @@ public function checkIPV6Tag($addressLiteral, $maxGroups = 8)
protected function doParseDomainPart()
{
$domain = '';
$label = '';
$openedParenthesis = 0;
do {
$prev = $this->lexer->getPrevious();
Expand Down Expand Up @@ -190,7 +192,12 @@ protected function doParseDomainPart()
$this->parseDomainLiteral();
}

$this->checkLabelLength($prev);
if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
$this->checkLabelLength($label);
$label = '';
} else {
$label .= $this->lexer->token['value'];
}

if ($this->isFWS()) {
$this->parseFWS();
Expand All @@ -203,6 +210,8 @@ protected function doParseDomainPart()
}
} while (null !== $this->lexer->token['type']);

$this->checkLabelLength($label);

return $domain;
}

Expand Down Expand Up @@ -386,16 +395,31 @@ protected function hasBrackets()
return true;
}

protected function checkLabelLength(array $prev)
/**
* @param string $label
*/
protected function checkLabelLength($label)
{
if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
$prev['type'] === EmailLexer::GENERIC &&
strlen($prev['value']) > 63
) {
if ($this->isLabelTooLong($label)) {
$this->warnings[LabelTooLong::CODE] = new LabelTooLong();
}
}

/**
* @param string $label
* @return bool
*/
private function isLabelTooLong($label)
{
if (preg_match('/[^\x00-\x7F]/', $label)) {
idn_to_ascii($label, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo);

return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG);
}

return strlen($label) > self::LABEL_MAX_LENGTH;
}

protected function parseDomainComments()
{
$this->isUnclosedComment();
Expand Down
2 changes: 1 addition & 1 deletion src/Warning/Warning.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function message()
*/
public function code()
{
return self::CODE;
return static::CODE;
}

/**
Expand Down
15 changes: 13 additions & 2 deletions tests/EmailValidator/Validation/NoRFCWarningsValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@ public function testEmailWithWarningsIsInvalid()
$this->assertInstanceOf(RFCWarnings::class, $validation->getError());
}

public function testEmailWithoutWarningsIsValid()
/**
* @dataProvider getValidEmailsWithoutWarnings
*/
public function testEmailWithoutWarningsIsValid($email)
{
$validation = new NoRFCWarningsValidation();

$this->assertTrue($validation->isValid('example@example.com', new EmailLexer()));
$this->assertTrue($validation->isValid($email, new EmailLexer()));
$this->assertNull($validation->getError());
}

public function getValidEmailsWithoutWarnings()
{
return [
['example@example.com',],
[sprintf('example@%s.com', str_repeat('ъ', 40)),],
];
}
}
21 changes: 17 additions & 4 deletions tests/EmailValidator/Validation/RFCValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Egulias\EmailValidator\Warning\Comment;
use Egulias\EmailValidator\Warning\DomainLiteral;
use Egulias\EmailValidator\Warning\DomainTooLong;
use Egulias\EmailValidator\Warning\EmailTooLong;
use Egulias\EmailValidator\Warning\IPV6BadChar;
use Egulias\EmailValidator\Warning\IPV6ColonEnd;
use Egulias\EmailValidator\Warning\IPV6ColonStart;
Expand Down Expand Up @@ -226,12 +227,12 @@ public function testInvalidEmailsWithWarningsCheck($expectedWarnings, $email)
$this->assertTrue($this->validator->isValid($email, $this->lexer));
$warnings = $this->validator->getWarnings();
$this->assertCount(
count($warnings), $expectedWarnings,
count($expectedWarnings), $warnings,
egulias marked this conversation as resolved.
Show resolved Hide resolved
"Expected: " . implode(",", $expectedWarnings) . " and got " . implode(",", $warnings)
);

foreach ($warnings as $warning) {
$this->assertArrayHasKey($warning->code(), $expectedWarnings);
$this->assertContains($warning->code(), $expectedWarnings);
}
}

Expand Down Expand Up @@ -278,17 +279,29 @@ public function getInvalidEmailsWithWarnings()
'example@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart.co.uk'
],
[
[DomainTooLong::CODE, LabelTooLong::CODE,],
[DomainTooLong::CODE, LabelTooLong::CODE, EmailTooLong::CODE],
'example2@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocal'.
'parttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'.
'toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'
],
[
[DomainTooLong::CODE, LabelTooLong::CODE,],
[DomainTooLong::CODE, LabelTooLong::CODE, EmailTooLong::CODE],
'example@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocal'.
'parttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart'.
'toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpar'
],
[
[LabelTooLong::CODE,],
sprintf('example@%s.com', str_repeat('ъ', 60)),
Copy link
Owner

Choose a reason for hiding this comment

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

@vajexal while porting to v3, why this should rise the warning? it is a 60 char label long

Copy link
Author

Choose a reason for hiding this comment

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

because if we convert label to idn, it's length will be > 63

var_dump(idn_to_ascii(str_repeat('ъ', 60), IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo), $idnaInfo);
// bool(false)
// array(3) {
//   ["result"]=> string(66) "xn--z1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
//   ["isTransitionalDifferent"]=> bool(false)
//   ["errors"]=> int(2)
// }

],
[
[LabelTooLong::CODE,],
sprintf('example@%s.com', str_repeat('a4t', 22)),
],
[
[LabelTooLong::CODE,],
sprintf('example@%s', str_repeat('a4t', 22)),
],
];
}
}