Skip to content

Commit

Permalink
fix Accepts invalid domains (#7)
Browse files Browse the repository at this point in the history
- Broaden GENERIC lexer regex to include non-ASCII for IDNs

 - Consider consecutive GENERIC tokens invalid in domain-part

 - Consider a leading hyphen in any part of a domain name an error
  • Loading branch information
AlainODea authored and egulias committed Jan 15, 2019
1 parent f23390a commit 8fcd557
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/emailvalidator4j/lexer/EmailLexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class EmailLexer {

public void lex(String input) {
Pattern pattern = Pattern.compile(
"([a-zA-Z_]+[46]?)|([0-9]+)|(\r\n)|(::)|(\\s+?)|(.)|(\\p{Cc}+)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE
"(([a-zA-Z_]|[^\\u0000-\\u007F])+[46]?)|([0-9]+)|(\r\n)|(::)|(\\s+?)|(.)|(\\p{Cc}+)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE
);
Matcher matcher = pattern.matcher(input);

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/emailvalidator4j/parser/DomainPart.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ private void doParseDomainPart() throws InvalidEmail {
int domainPartOpenedParenthesis = 0;
boolean openBrackets = false;
do {
if (this.lexer.getCurrent().equals(Tokens.HYPHEN)) {
throw new DomainHyphen("Found - in domain part");
}

if (this.lexer.getCurrent().equals(Tokens.SEMICOLON)) {
throw new ExpectedATEXT("Expected ATEXT");
}
Expand Down Expand Up @@ -149,6 +153,11 @@ private void checkExceptions() throws InvalidEmail {
if (this.lexer.getCurrent().equals(Tokens.BACKSLASH) && this.lexer.isNextToken(Tokens.get("GENERIC"))) {
throw new ExpectedATEXT("Found BACKSLASH");
}

if (this.lexer.getCurrent().equals(Tokens.get("GENERIC")) && this.lexer.isNextToken(Tokens.get("GENERIC"))) {
this.lexer.next();
throw new ConsecutiveGeneric("Found " + this.lexer.getCurrent().getText());
}
}

private void checkIPv6Tag(String literal) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package emailvalidator4j.parser.exception;

public class ConsecutiveGeneric extends InvalidEmail {
public ConsecutiveGeneric(String message) {
super(message);
}
}
4 changes: 4 additions & 0 deletions src/test/java/emailvalidator4j/parser/DomainPartTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ public static Object[][] invalidDomainParts() {
{ConsecutiveAT.class, "@@start"},
{ExpectedATEXT.class, "@at[start"},
{DomainHyphen.class, "@atstart-.com"},
{DomainHyphen.class, "@bb.-cc"},
{DomainHyphen.class, "@bb.-cc-"},
{DomainHyphen.class, "@bb.cc-"},
{DomainNotAllowedCharacter.class, "@atst\\art.com"},
{DomainNotAllowedCharacter.class, "@example\\"},
{DomainNotAllowedCharacter.class, "@exa\\mple"},
{UnclosedDomainLiteral.class, "@example]"},
{ConsecutiveGeneric.class, "@example'"},
};
}

Expand Down

0 comments on commit 8fcd557

Please sign in to comment.