-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HV-1773 Be more explicit about issues with EL injection and how to av…
…oid them
- Loading branch information
Showing
3 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...test/java/org/hibernate/validator/referenceguide/chapter06/elinjection/SafeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.hibernate.validator.referenceguide.chapter06.elinjection; | ||
|
||
import org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext; | ||
import org.hibernate.validator.referenceguide.chapter06.constraintvalidatorpayload.ZipCode; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
//tag::include[] | ||
public class SafeValidator implements ConstraintValidator<ZipCode, String> { | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if ( value == null ) { | ||
return true; | ||
} | ||
|
||
HibernateConstraintValidatorContext hibernateContext = context.unwrap( | ||
HibernateConstraintValidatorContext.class ); | ||
hibernateContext.disableDefaultConstraintViolation(); | ||
|
||
if ( isInvalid( value ) ) { | ||
hibernateContext | ||
.addExpressionVariable( "validatedValue", value ) | ||
.buildConstraintViolationWithTemplate( "${validatedValue} is not a valid ZIP code" ) | ||
.addConstraintViolation(); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private boolean isInvalid(String value) { | ||
// ... | ||
return false; | ||
} | ||
} | ||
// end::include[] |
35 changes: 35 additions & 0 deletions
35
...st/java/org/hibernate/validator/referenceguide/chapter06/elinjection/UnsafeValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.hibernate.validator.referenceguide.chapter06.elinjection; | ||
|
||
import org.hibernate.validator.referenceguide.chapter06.constraintvalidatorpayload.ZipCode; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
//tag::include[] | ||
public class UnsafeValidator implements ConstraintValidator<ZipCode, String> { | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if ( value == null ) { | ||
return true; | ||
} | ||
|
||
context.disableDefaultConstraintViolation(); | ||
|
||
if ( isInvalid( value ) ) { | ||
context | ||
.buildConstraintViolationWithTemplate( value + " is not a valid ZIP code" ) | ||
.addConstraintViolation(); | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private boolean isInvalid(String value) { | ||
// ... | ||
return false; | ||
} | ||
} | ||
// end::include[] |