Skip to content

Commit b9b6a35

Browse files
authored
Merge pull request github#4629 from pwntester/improve_bean_validation_query
Java: add some improvements to the bean validation query
2 parents 7f0ad2d + 30d8dce commit b9b6a35

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

java/ql/src/Security/CWE/CWE-094/InsecureBeanValidation.ql

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,48 @@ import semmle.code.java.dataflow.TaintTracking
1414
import semmle.code.java.dataflow.FlowSources
1515
import DataFlow::PathGraph
1616

17+
/**
18+
* A message interpolator Type that perform Expression Language (EL) evaluations
19+
*/
20+
class ELMessageInterpolatorType extends RefType {
21+
ELMessageInterpolatorType() {
22+
this
23+
.getASourceSupertype*()
24+
.hasQualifiedName("org.hibernate.validator.messageinterpolation",
25+
["ResourceBundleMessageInterpolator", "ValueFormatterMessageInterpolator"])
26+
}
27+
}
28+
29+
/**
30+
* A method call that sets the application's default message interpolator.
31+
*/
32+
class SetMessageInterpolatorCall extends MethodAccess {
33+
SetMessageInterpolatorCall() {
34+
exists(Method m, RefType t |
35+
this.getMethod() = m and
36+
m.getDeclaringType().getASourceSupertype*() = t and
37+
(
38+
t.hasQualifiedName("javax.validation", ["Configuration", "ValidatorContext"]) and
39+
m.getName() = "messageInterpolator"
40+
or
41+
t
42+
.hasQualifiedName("org.springframework.validation.beanvalidation",
43+
["CustomValidatorBean", "LocalValidatorFactoryBean"]) and
44+
m.getName() = "setMessageInterpolator"
45+
)
46+
)
47+
}
48+
49+
/**
50+
* The message interpolator is likely to be safe, because it does not process Java Expression Language expressions.
51+
*/
52+
predicate isSafe() { not this.getAnArgument().getType() instanceof ELMessageInterpolatorType }
53+
}
54+
55+
/**
56+
* A method named `buildConstraintViolationWithTemplate` declared on a subtype
57+
* of `javax.validation.ConstraintValidatorContext`.
58+
*/
1759
class BuildConstraintViolationWithTemplateMethod extends Method {
1860
BuildConstraintViolationWithTemplateMethod() {
1961
this
@@ -24,6 +66,10 @@ class BuildConstraintViolationWithTemplateMethod extends Method {
2466
}
2567
}
2668

69+
/**
70+
* Taint tracking BeanValidationConfiguration describing the flow of data from user input
71+
* to the argument of a method that builds constraint error messages.
72+
*/
2773
class BeanValidationConfig extends TaintTracking::Configuration {
2874
BeanValidationConfig() { this = "BeanValidationConfig" }
2975

@@ -38,5 +84,12 @@ class BeanValidationConfig extends TaintTracking::Configuration {
3884
}
3985

4086
from BeanValidationConfig cfg, DataFlow::PathNode source, DataFlow::PathNode sink
41-
where cfg.hasFlowPath(source, sink)
42-
select sink, source, sink, "Custom constraint error message contains unsanitized user data"
87+
where
88+
(
89+
not exists(SetMessageInterpolatorCall c)
90+
or
91+
exists(SetMessageInterpolatorCall c | not c.isSafe())
92+
) and
93+
cfg.hasFlowPath(source, sink)
94+
select sink.getNode(), source, sink,
95+
"Custom constraint error message contains unsanitized user data"

0 commit comments

Comments
 (0)