Skip to content
Open
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'com.tngtech.java', name: 'junit-dataprovider', version: '1.9.2'
testCompile "org.mockito:mockito-core:1.+"
testCompile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'
}

def siteUrl = 'https://github.com/egulias/EmailValidator4J'
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/emailvalidator4j/lexer/EmailLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public class EmailLexer {

public void lex(String input) {
Pattern pattern = Pattern.compile(
"(([a-zA-Z0-9!#$%&'*+\\-/=?^_`{|}~]|[^\\u0000-\\u007F])+[46]?)|([0-9]+)|(\r\n)|(::)|(\\s+?)|(.)|(\\p{Cc}+)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE
"(([a-zA-Z0-9!#$%&'*+\\-/=?^_`{|}~]|[^\\u0000-\\u007F])+[46]?)|([0-9]+)|(\r\n)|(::)|(\\s+?)|(.)|(\\p{Cc}+)",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE
);

Matcher matcher = pattern.matcher(input);

this.reset();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emailvalidator4j/parser/DomainPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
final class DomainPart extends Parser {

private static final Pattern subDomainIllegalCharacters = Pattern.compile("[^a-zA-Z0-9\\-\\x00007F-\\u10FFFF]");
private static final int DOMAINPART_MAX_LENGTH = 255;
private static final int LABEL_MAX_LENGTH = 63;
public static final int DOMAINPART_MAX_LENGTH = 255;
private final HashSet<TokenInterface> notAllowedTokens = new HashSet<TokenInterface>(2) {{
add(Tokens.BACKSLASH);
add(Tokens.SLASH);
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/emailvalidator4j/parser/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ public Email(EmailLexer lexer) {
}

public void parse(String email) throws InvalidEmail {
this.lexer.lex(Optional.ofNullable(email).orElseThrow(() ->
new InvalidEmail("Empty email")
));

String nonEmptyEmail = Optional.ofNullable(email).orElseThrow(() ->
new InvalidEmail("Empty email"));
this.validateLength(nonEmptyEmail);

this.lexer.lex(nonEmptyEmail);

if (!this.lexer.find(Tokens.AT)) {
throw new NoLocalPart("No local part found");
Expand All @@ -40,6 +43,14 @@ public void parse(String email) throws InvalidEmail {
this.domainPartParser.parse(this.lexer.toString());
}

private void validateLength(String email) throws InvalidEmail {
int maxLength = LocalPart.RFC5321_LOCALPART_MAX_LENGTH + DomainPart.DOMAINPART_MAX_LENGTH + 1;

if (email.length() > maxLength) {
throw new InvalidEmail("email too long");
}
}

public List getWarnings() {
List<Warnings> warnings = this.localPartParser.getWarnings();
warnings.addAll(this.domainPartParser.getWarnings());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/emailvalidator4j/parser/LocalPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
final class LocalPart extends Parser {
private boolean closingQuote = false;
private boolean parseDQuote = true;
private static int RFC5321_LOCALPART_MAX_LENGTH = 64;
public static int RFC5321_LOCALPART_MAX_LENGTH = 64;

LocalPart (EmailLexer lexer) {
super(lexer);
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/emailvalidator4j/EmailValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -134,4 +135,11 @@ public void warningsAreExposed() {
validator.isValid("test@[127.0.0.0]");
Assert.assertFalse(validator.getWarnings().isEmpty());
}

@Test
public void controlStringOverflow() {
String filled = StringUtils.repeat("a", 2000);
EmailValidator validator = new EmailValidator();
Assert.assertFalse(validator.isValid(filled.concat("@example.com")));
}
}