From ade6887fd9bd74177769645ab5c474824f8a418a Mon Sep 17 00:00:00 2001 From: Marcin Michalski <57528542+marmichalski@users.noreply.github.com> Date: Thu, 13 Feb 2020 23:36:52 +0100 Subject: [PATCH] Fix local part length check (#233) --- EmailValidator/Parser/LocalPart.php | 10 ++++++---- Tests/EmailValidator/Validation/RFCValidationTest.php | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/EmailValidator/Parser/LocalPart.php b/EmailValidator/Parser/LocalPart.php index 5f6b8c2..3c21f34 100644 --- a/EmailValidator/Parser/LocalPart.php +++ b/EmailValidator/Parser/LocalPart.php @@ -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']) { @@ -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(); @@ -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(); } } diff --git a/Tests/EmailValidator/Validation/RFCValidationTest.php b/Tests/EmailValidator/Validation/RFCValidationTest.php index fc4f0dd..a2dfa87 100644 --- a/Tests/EmailValidator/Validation/RFCValidationTest.php +++ b/Tests/EmailValidator/Validation/RFCValidationTest.php @@ -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'