|
20 | 20 | import com.google.common.primitives.Bytes; |
21 | 21 | import inet.ipaddr.IPAddress; |
22 | 22 | import inet.ipaddr.IPAddressString; |
23 | | -import jakarta.mail.internet.AddressException; |
24 | | -import jakarta.mail.internet.InternetAddress; |
25 | 23 | import java.net.Inet4Address; |
26 | 24 | import java.net.Inet6Address; |
27 | 25 | import java.net.InetAddress; |
28 | 26 | import java.net.URI; |
29 | 27 | import java.net.URISyntaxException; |
30 | 28 | import java.util.HashSet; |
31 | 29 | import java.util.Set; |
| 30 | +import java.util.regex.Pattern; |
32 | 31 | import org.projectnessie.cel.common.types.BoolT; |
33 | 32 | import org.projectnessie.cel.common.types.Err; |
34 | 33 | import org.projectnessie.cel.common.types.IntT; |
@@ -58,6 +57,11 @@ final class CustomOverload { |
58 | 57 | private static final String OVERLOAD_IS_INF = "isInf"; |
59 | 58 | private static final String OVERLOAD_IS_HOST_AND_PORT = "isHostAndPort"; |
60 | 59 |
|
| 60 | + // See https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address |
| 61 | + private static final Pattern EMAIL_REGEX = |
| 62 | + Pattern.compile( |
| 63 | + "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"); |
| 64 | + |
61 | 65 | /** |
62 | 66 | * Create custom function overload list. |
63 | 67 | * |
@@ -514,27 +518,17 @@ private static Val uniqueList(Lister list) { |
514 | 518 | } |
515 | 519 |
|
516 | 520 | /** |
517 | | - * Validates if the input string is a valid email address. |
| 521 | + * validateEmail returns true if addr is a valid email address. |
| 522 | + * |
| 523 | + * <p>This regex conforms to the definition for a valid email address from the HTML standard. Note |
| 524 | + * that this standard willfully deviates from RFC 5322, which allows many unexpected forms of |
| 525 | + * email addresses and will easily match a typographical error. |
518 | 526 | * |
519 | 527 | * @param addr The input string to validate as an email address. |
520 | 528 | * @return {@code true} if the input string is a valid email address, {@code false} otherwise. |
521 | 529 | */ |
522 | 530 | private static boolean validateEmail(String addr) { |
523 | | - try { |
524 | | - InternetAddress emailAddr = new InternetAddress(addr); |
525 | | - emailAddr.validate(); |
526 | | - if (addr.contains("<") || !emailAddr.getAddress().equals(addr)) { |
527 | | - return false; |
528 | | - } |
529 | | - addr = emailAddr.getAddress(); |
530 | | - if (addr.length() > 254) { |
531 | | - return false; |
532 | | - } |
533 | | - String[] parts = addr.split("@", 2); |
534 | | - return parts[0].length() < 64 && validateHostname(parts[1]); |
535 | | - } catch (AddressException ex) { |
536 | | - return false; |
537 | | - } |
| 531 | + return EMAIL_REGEX.matcher(addr).matches(); |
538 | 532 | } |
539 | 533 |
|
540 | 534 | /** |
|
0 commit comments