Skip to content

Commit

Permalink
Fix #19: Check TLD length before checking for the - character
Browse files Browse the repository at this point in the history
  • Loading branch information
RohanNagar committed Jun 29, 2021
1 parent c97765f commit dc838a5
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/sanctionco/jmail/JMail.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,16 +369,16 @@ private static Optional<Email> internalTryParse(String email) {
// Check that local-part does not end with '.'
if (localPart.charAt(localPart.length() - 1) == '.') return Optional.empty();

// Ensure the TLD is not empty or greater than 63 chars
if (currentDomainPart.length() <= 0 || currentDomainPart.length() > 63) return Optional.empty();

// Check that the final domain part does not start with '-'
// We already checked to make sure it doesn't end with '-'
if (currentDomainPart.charAt(0) == '-') return Optional.empty();

// Ensure the last domain part (TLD) is not all numeric
if (currentDomainPart.toString().chars().allMatch(Character::isDigit)) return Optional.empty();

// Ensure the TLD is not greater than 63 chars
if (currentDomainPart.length() > 63) return Optional.empty();

domainParts.add(currentDomainPart.toString());

return Optional.of(new Email(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public static Stream<String> provideInvalidWhitespaceEmails() {
"\"test\rblah\"@test.org",
"\"test\\\r\n blah\"@test.org",
"Invalid \\\n Folding \\\n Whitespace@test.org",
"test.\r\n\r\n obs@syntax.com");
"test.\r\n\r\n obs@syntax.com",
"ABC.DEF@GHI.JKL . ");
}

// Valid emails with quotes
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/invalid-addresses.csv
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,4 @@ Joe A Smith email@example.com ;
Joe A Smith <email@example.com-> ; The address part must be valid (cannot end with a dash)
Joe A Smith <email@-example.com-> ; The address part must be valid (domain cannot start with a dash)
Joe A Smith <email> ; The address part must be valid (missing @ character and domain)
ABC.DEF@GHI. (MNO) ; The domain cannot end with the . character even with an ending comment
1 change: 1 addition & 0 deletions src/test/resources/valid-addresses.csv
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,4 @@ abc\\@test.org,abc\\,test.org
Joe A Smith <email@example.com>,email,example.com
"John Michael" <tester@test.net>,tester,test.net
A <apple@pie.com>,apple,pie.com
ABC.DEF@GHI.JKL (MNO),ABC.DEF,GHI.JKL (MNO)

0 comments on commit dc838a5

Please sign in to comment.