Skip to content

Commit ab5105c

Browse files
committed
Replace commons-validator email validation
This isn't quite as strict, but it is a good enough "looks like an email" check for this purpose.
1 parent cbfd4e8 commit ab5105c

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

src/main/java/com/maxmind/minfraud/request/Email.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.HashMap;
1414
import java.util.Map;
1515
import java.util.regex.Pattern;
16-
import org.apache.commons.validator.routines.EmailValidator;
1716

1817
/**
1918
* The email information for the transaction.
@@ -340,7 +339,7 @@ public Builder(boolean enableValidation) {
340339
* @throws IllegalArgumentException when address is not a valid email address.
341340
*/
342341
public Email.Builder address(String address) {
343-
if (enableValidation && !EmailValidator.getInstance().isValid(address)) {
342+
if (enableValidation && !isValidEmail(address)) {
344343
throw new IllegalArgumentException(
345344
"The email address " + address + " is not valid.");
346345
}
@@ -460,6 +459,34 @@ private String cleanAddress(String address) {
460459
return localPart + "@" + domain;
461460
}
462461

462+
private static boolean isValidEmail(String email) {
463+
if (email == null || email.isEmpty()) {
464+
return false;
465+
}
466+
467+
// RFC 5321 the forward path limits the mailbox to 254 characters
468+
// even though a domain can be 255 and the local part 64
469+
if (email.length() > 254) {
470+
return false;
471+
}
472+
473+
int atIndex = email.lastIndexOf('@');
474+
if (atIndex <= 0) {
475+
return false;
476+
}
477+
478+
String localPart = email.substring(0, atIndex);
479+
String domainPart = email.substring(atIndex + 1);
480+
481+
// The local-part has a maximum length of 64 characters.
482+
if (localPart.length() > 64) {
483+
return false;
484+
}
485+
486+
return isValidDomain(domainPart);
487+
}
488+
489+
463490
private String cleanDomain(String domain) {
464491
if (domain == null) {
465492
return null;

src/test/java/com/maxmind/minfraud/request/EmailTest.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertNull;
55
import static org.junit.jupiter.api.Assertions.assertThrows;
66

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

247-
@Test
248-
public void testInvalidAddress() {
248+
@ParameterizedTest(name = "Run #{index}: email = \"{0}\"")
249+
@ValueSource(strings = {
250+
"test.com",
251+
"test@",
252+
"@test.com",
253+
"",
254+
" ",
255+
"test@test com.com",
256+
"test@test_domain.com",
257+
"test@-test.com",
258+
"test@test-.com",
259+
"test@.test.com",
260+
"test@test..com",
261+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@test.com",
262+
"test@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com"
263+
})
264+
void testInvalidAddresses(String invalidAddress) {
249265
assertThrows(
250266
IllegalArgumentException.class,
251-
() -> new Builder().address("a@test@test.org").build()
267+
() -> new Builder().address(invalidAddress).build()
252268
);
253269
}
254270

0 commit comments

Comments
 (0)