Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 47 additions & 2 deletions src/main/java/com/maxmind/minfraud/request/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.commons.validator.routines.DomainValidator;
import org.apache.commons.validator.routines.EmailValidator;

/**
Expand All @@ -28,6 +27,9 @@ public final class Email extends AbstractModel {
private static final Map<String, String> equivalentDomains;
private static final Map<String, Boolean> fastmailDomains;
private static final Map<String, Boolean> yahooDomains;
private static final Pattern DOMAIN_LABEL_PATTERN = Pattern.compile(
"^[a-zA-Z0-9]$|^[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9]$"
);
private static final Pattern DOT_PATTERN = Pattern.compile("\\.");
private static final Pattern TRAILING_DOT_PATTERN = Pattern.compile("\\.+$");
private static final Pattern REPEAT_COM_PATTERN = Pattern.compile("(?:\\.com){2,}$");
Expand Down Expand Up @@ -373,7 +375,7 @@ public Email.Builder hashAddress() {
* @throws IllegalArgumentException when domain is not a valid domain.
*/
public Email.Builder domain(String domain) {
if (enableValidation && !DomainValidator.getInstance().isValid(domain)) {
if (enableValidation && !isValidDomain(domain)) {
throw new IllegalArgumentException("The email domain " + domain + " is not valid.");
}
this.domain = domain;
Expand Down Expand Up @@ -491,6 +493,49 @@ private String cleanDomain(String domain) {
return domain;
}

private static boolean isValidDomain(String domain) {
if (domain == null || domain.isEmpty()) {
return false;
}

String asciiDomain;
try {
asciiDomain = IDN.toASCII(domain);
} catch (IllegalArgumentException e) {
return false;
}

if (domain.endsWith(".")) {
domain = domain.substring(0, domain.length() - 1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we want to use asciiDomain on these two lines?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I fixed this and the other issue.

}

if (asciiDomain.length() > 255) {
return false;
}

String[] labels = asciiDomain.split("\\.");

if (labels.length < 2) {
return false;
}

for (String label : labels) {
if (!isValidDomainLabel(label)) {
return false;
}
}

return true;
}

private static boolean isValidDomainLabel(String label) {
if (label == null || label.isEmpty() || label.length() > 63) {
return false;
}

return DOMAIN_LABEL_PATTERN.matcher(label).matches();
}

/**
* @return The domain of the email address used in the transaction.
*/
Expand Down
29 changes: 26 additions & 3 deletions src/test/java/com/maxmind/minfraud/request/EmailTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class EmailTest {

Expand Down Expand Up @@ -264,11 +266,32 @@ public void testDomainWithoutValidation() {
assertEquals(domain, email.getDomain());
}

@Test
public void testInvalidDomain() {
@ParameterizedTest(name = "Run #{index}: domain = \"{0}\"")
@ValueSource(strings = {
"example",
"",
" ",
" domain.com",
"domain.com ",
"domain com.com",
"domain_name.com",
"domain$.com",
"-domain.com",
"domain-.com",
"domain..com",
".domain.com",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com",
"a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a" +
".a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a" +
".a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a" +
".a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a.a" +
".com",
"xn--.com"
})
void testInvalidDomains(String invalidDomain) {
assertThrows(
IllegalArgumentException.class,
() -> new Builder().domain(" domain.com").build()
() -> new Builder().domain(invalidDomain).build()
);
}
}