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
31 changes: 29 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.EmailValidator;

/**
* The email information for the transaction.
Expand Down Expand Up @@ -340,7 +339,7 @@ public Builder(boolean enableValidation) {
* @throws IllegalArgumentException when address is not a valid email address.
*/
public Email.Builder address(String address) {
if (enableValidation && !EmailValidator.getInstance().isValid(address)) {
if (enableValidation && !isValidEmail(address)) {
throw new IllegalArgumentException(
"The email address " + address + " is not valid.");
}
Expand Down Expand Up @@ -460,6 +459,34 @@ private String cleanAddress(String address) {
return localPart + "@" + domain;
}

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

// RFC 5321 the forward path limits the mailbox to 254 characters

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.

Are we missing a word here in referring to the RFC?

// even though a domain can be 255 and the local part 64
if (email.length() > 254) {
return false;
}

int atIndex = email.lastIndexOf('@');
if (atIndex <= 0) {
return false;
}

String localPart = email.substring(0, atIndex);
String domainPart = email.substring(atIndex + 1);

// The local-part has a maximum length of 64 characters.
if (localPart.length() > 64) {
return false;
}

return isValidDomain(domainPart);
}


private String cleanDomain(String domain) {
if (domain == null) {
return null;
Expand Down
22 changes: 19 additions & 3 deletions src/test/java/com/maxmind/minfraud/request/EmailTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;


import com.maxmind.minfraud.request.Email.Builder;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -244,11 +245,26 @@ private String toMD5(String s) throws NoSuchAlgorithmException {
return String.format("%032x", i);
}

@Test
public void testInvalidAddress() {
@ParameterizedTest(name = "Run #{index}: email = \"{0}\"")
@ValueSource(strings = {
"test.com",
"test@",
"@test.com",
"",
" ",
"test@test com.com",
"test@test_domain.com",
"test@-test.com",
"test@test-.com",
"test@.test.com",
"test@test..com",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@test.com",
"test@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com"
})
void testInvalidAddresses(String invalidAddress) {
assertThrows(
IllegalArgumentException.class,
() -> new Builder().address("a@test@test.org").build()
() -> new Builder().address(invalidAddress).build()
);
}

Expand Down