Skip to content

Commit

Permalink
Fix local part length check (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
marmichalski authored Feb 13, 2020
1 parent 5065faf commit ade6887
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 6 additions & 4 deletions EmailValidator/Parser/LocalPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function parse($localPart)
$parseDQuote = true;
$closingQuote = false;
$openedParenthesis = 0;
$totalLength = 0;

while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) {
if ($this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type']) {
Expand All @@ -34,12 +35,13 @@ public function parse($localPart)
$this->parseComments();
$openedParenthesis += $this->getOpenedParenthesis();
}

if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
if ($openedParenthesis === 0) {
throw new UnopenedComment();
} else {
$openedParenthesis--;
}

$openedParenthesis--;
}

$this->checkConsecutiveDots();
Expand All @@ -57,11 +59,11 @@ public function parse($localPart)
$this->parseFWS();
}

$totalLength += strlen($this->lexer->token['value']);
$this->lexer->moveNext();
}

$prev = $this->lexer->getPrevious();
if (strlen($prev['value']) > LocalTooLong::LOCAL_PART_LENGTH) {
if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) {
$this->warnings[LocalTooLong::CODE] = new LocalTooLong();
}
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/EmailValidator/Validation/RFCValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ public function getInvalidEmailsWithWarnings()
[LocalTooLong::CODE,],
'too_long_localpart_too_long_localpart_too_long_localpart_too_long_localpart@invalid.example.com'
],
[
[LocalTooLong::CODE],
'too_long_localpart_too_long_localpart_123_too_long_localpart_too_long_localpart@example.com'
],
[
[LabelTooLong::CODE,],
'example@toolonglocalparttoolonglocalparttoolonglocalparttoolonglocalpart.co.uk'
Expand Down

0 comments on commit ade6887

Please sign in to comment.