Skip to content

Commit

Permalink
ValidatorHeuristics for @Email annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaleotti committed Oct 4, 2023
1 parent ef1d018 commit bdf54fe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.regex.Pattern;

/**
* Heuristics calculation for Java Beans, when dealing with javax.validation constraints
Expand All @@ -32,6 +31,7 @@ always get higher than this value (this can be achieved by using it as a base).
private static final String PREFIX_HIBERNATE = "org.hibernate.validator.constraints.";
private static final String PREFIX_JIRUKTA = "cz.jirutka.validator.collection.constraints.";

private static final String EMAIL_REGEX_PATTERN = "^(.+)@(.+)$";
/**
*
* @param validator A Object reference to a javax.validation.Validator instance.
Expand Down Expand Up @@ -122,7 +122,6 @@ private static double computeHeuristicToSolveFailedConstraint(Object violation)
DecimalMax
DecimalMin
Digits
Email
Future
FutureOrPresent
Past
Expand Down Expand Up @@ -213,21 +212,31 @@ private static double computeHeuristicToSolveFailedConstraint(Object violation)
return defaultFailed;
}

if (annotationType.endsWith(".Pattern")) {
if (annotationType.endsWith(".Pattern")
|| annotationType.endsWith(".Email")) {
/*
Quite expensive to handle, see RegexDistanceUtils.
so, for now, we just ensure we handle taint analysis for this
*/
assert invalidValue != null; // otherwise would had been valid
String value = invalidValue.toString();
if (ExecutionTracer.isTaintInput(value)) {
final String pattern;
if (annotationType.endsWith(".Pattern")) {
pattern = attributes.get("regexp").toString();
} else {
assert(annotationType.endsWith(".Email"));
pattern = EMAIL_REGEX_PATTERN;
}

ExecutionTracer.addStringSpecialization(value,
new StringSpecializationInfo(StringSpecialization.REGEX_WHOLE,
attributes.get("regexp").toString()));
pattern));
}

return defaultFailed;
}

}

SimpleLogger.warn("Not able to handle constrain type: " + annotationType);
Expand Down Expand Up @@ -370,9 +379,9 @@ private static Integer computeSize(Object invalidValue){
if(invalidValue instanceof CharSequence){
size = ((CharSequence) invalidValue).length();
} else if(invalidValue instanceof Collection){
size = ((Collection) invalidValue).size();
size = ((Collection<?>) invalidValue).size();
} else if(invalidValue instanceof Map){
size = ((Map)invalidValue).size();
size = ((Map<?,?>)invalidValue).size();
} else if(invalidValue.getClass().isArray()){
size = Array.getLength(invalidValue);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ public void testHeuristicForString() {
assertTrue(t17.getOfTrue() > t16.getOfTrue());

assertTrue(t17.isTrue());

bean.m = " "; //@Email
Truthness t18 = ValidatorHeuristics.computeTruthness(validator, bean);
assertFalse(t18.isTrue());

bean.m = "[email protected]"; //@Email
Truthness t19 = ValidatorHeuristics.computeTruthness(validator, bean);
assertEquals(t19.getOfTrue(), t17.getOfTrue(), 0.00001); // null and valid email are the same
assertTrue(t19.getOfTrue() > t18.getOfTrue());

assertTrue(t19.isTrue());

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ public class StringBean {

@Size(min = 1, max = 3)
public Map<String,String> l;

@Email
public String m;

}

0 comments on commit bdf54fe

Please sign in to comment.