Skip to content

Commit 8fcd557

Browse files
AlainODeaegulias
authored andcommitted
fix Accepts invalid domains (#7)
- 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
1 parent f23390a commit 8fcd557

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

src/main/java/emailvalidator4j/lexer/EmailLexer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class EmailLexer {
1616

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

src/main/java/emailvalidator4j/parser/DomainPart.java

100644100755
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ private void doParseDomainPart() throws InvalidEmail {
5555
int domainPartOpenedParenthesis = 0;
5656
boolean openBrackets = false;
5757
do {
58+
if (this.lexer.getCurrent().equals(Tokens.HYPHEN)) {
59+
throw new DomainHyphen("Found - in domain part");
60+
}
61+
5862
if (this.lexer.getCurrent().equals(Tokens.SEMICOLON)) {
5963
throw new ExpectedATEXT("Expected ATEXT");
6064
}
@@ -149,6 +153,11 @@ private void checkExceptions() throws InvalidEmail {
149153
if (this.lexer.getCurrent().equals(Tokens.BACKSLASH) && this.lexer.isNextToken(Tokens.get("GENERIC"))) {
150154
throw new ExpectedATEXT("Found BACKSLASH");
151155
}
156+
157+
if (this.lexer.getCurrent().equals(Tokens.get("GENERIC")) && this.lexer.isNextToken(Tokens.get("GENERIC"))) {
158+
this.lexer.next();
159+
throw new ConsecutiveGeneric("Found " + this.lexer.getCurrent().getText());
160+
}
152161
}
153162

154163
private void checkIPv6Tag(String literal) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package emailvalidator4j.parser.exception;
2+
3+
public class ConsecutiveGeneric extends InvalidEmail {
4+
public ConsecutiveGeneric(String message) {
5+
super(message);
6+
}
7+
}

src/test/java/emailvalidator4j/parser/DomainPartTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ public static Object[][] invalidDomainParts() {
4747
{ConsecutiveAT.class, "@@start"},
4848
{ExpectedATEXT.class, "@at[start"},
4949
{DomainHyphen.class, "@atstart-.com"},
50+
{DomainHyphen.class, "@bb.-cc"},
51+
{DomainHyphen.class, "@bb.-cc-"},
52+
{DomainHyphen.class, "@bb.cc-"},
5053
{DomainNotAllowedCharacter.class, "@atst\\art.com"},
5154
{DomainNotAllowedCharacter.class, "@example\\"},
5255
{DomainNotAllowedCharacter.class, "@exa\\mple"},
5356
{UnclosedDomainLiteral.class, "@example]"},
57+
{ConsecutiveGeneric.class, "@example'"},
5458
};
5559
}
5660

0 commit comments

Comments
 (0)