Skip to content

Commit

Permalink
fix(mail): lookup UTF-8 support config in static method to pick up ch…
Browse files Browse the repository at this point in the history
…anged value during tests

Also switch to safer lookup via codepoint comparison to 7bit = chars < 128 in favor over encoder
  • Loading branch information
poikilotherm committed Oct 10, 2023
1 parent b970eb5 commit 05870d1
Showing 1 changed file with 3 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@

import org.apache.commons.validator.routines.EmailValidator;

import java.nio.charset.StandardCharsets;

/**
*
* @author skraffmi
*/
public class EMailValidator implements ConstraintValidator<ValidateEmail, String> {

private static final boolean MTA_SUPPORTS_UTF8 = JvmSettings.MAIL_MTA_SUPPORT_UTF8.lookup(Boolean.class);

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
Expand All @@ -28,10 +24,12 @@ public boolean isValid(String value, ConstraintValidatorContext context) {
* @return true when valid, false when invalid (null = valid!)
*/
public static boolean isEmailValid(String value) {
// Must be looked up here - otherwise changes are not picked up (tests, live config, ...)
final boolean mtaSupportsUTF8 = JvmSettings.MAIL_MTA_SUPPORT_UTF8.lookup(Boolean.class);
return value == null || (EmailValidator.getInstance().isValid(value) &&
// If the MTA isn't able to handle UTF-8 mail addresses following RFC 6530/6531/6532, we can only declare
// mail addresses using 7bit ASCII (RFC 821) as valid.
// Beyond scope for Apache Commons Validator, see also https://issues.apache.org/jira/browse/VALIDATOR-487
(StandardCharsets.US_ASCII.newEncoder().canEncode(value) || MTA_SUPPORTS_UTF8) );
(value.codePoints().noneMatch(c -> c > 127) || mtaSupportsUTF8) );
}
}

0 comments on commit 05870d1

Please sign in to comment.