Skip to content

Commit 19a94a5

Browse files
committed
Move InsecureBeanValidation configuration to Query.qll
1 parent 367042b commit 19a94a5

File tree

2 files changed

+64
-60
lines changed

2 files changed

+64
-60
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/** Provides classes and a taint tracking configuration to reason about insecure bean validation. */
2+
3+
import java
4+
import semmle.code.java.dataflow.TaintTracking
5+
import semmle.code.java.dataflow.FlowSources
6+
private import semmle.code.java.dataflow.ExternalFlow
7+
8+
/**
9+
* A message interpolator Type that perform Expression Language (EL) evaluations
10+
*/
11+
class ELMessageInterpolatorType extends RefType {
12+
ELMessageInterpolatorType() {
13+
this.getASourceSupertype*()
14+
.hasQualifiedName("org.hibernate.validator.messageinterpolation",
15+
["ResourceBundleMessageInterpolator", "ValueFormatterMessageInterpolator"])
16+
}
17+
}
18+
19+
/**
20+
* A method call that sets the application's default message interpolator.
21+
*/
22+
class SetMessageInterpolatorCall extends MethodAccess {
23+
SetMessageInterpolatorCall() {
24+
exists(Method m, RefType t |
25+
this.getMethod() = m and
26+
m.getDeclaringType().getASourceSupertype*() = t and
27+
(
28+
t.hasQualifiedName("javax.validation", ["Configuration", "ValidatorContext"]) and
29+
m.getName() = "messageInterpolator"
30+
or
31+
t.hasQualifiedName("org.springframework.validation.beanvalidation",
32+
["CustomValidatorBean", "LocalValidatorFactoryBean"]) and
33+
m.getName() = "setMessageInterpolator"
34+
)
35+
)
36+
}
37+
38+
/**
39+
* Holds if the message interpolator is likely to be safe, because it does not process Java Expression Language expressions.
40+
*/
41+
predicate isSafe() { not this.getAnArgument().getType() instanceof ELMessageInterpolatorType }
42+
}
43+
44+
/**
45+
* Taint tracking BeanValidationConfiguration describing the flow of data from user input
46+
* to the argument of a method that builds constraint error messages.
47+
*/
48+
module BeanValidationConfig implements DataFlow::ConfigSig {
49+
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
50+
51+
predicate isSink(DataFlow::Node sink) { sink instanceof BeanValidationSink }
52+
}
53+
54+
/** Tracks flow from user input to the argument of a method that builds constraint error messages. */
55+
module BeanValidationFlow = TaintTracking::Global<BeanValidationConfig>;
56+
57+
/**
58+
* A bean validation sink, such as method `buildConstraintViolationWithTemplate`
59+
* declared on a subtype of `javax.validation.ConstraintValidatorContext`.
60+
*/
61+
private class BeanValidationSink extends DataFlow::Node {
62+
BeanValidationSink() { sinkNode(this, "bean-validation") }
63+
}

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

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,9 @@
1111
*/
1212

1313
import java
14-
import semmle.code.java.dataflow.TaintTracking
15-
import semmle.code.java.dataflow.FlowSources
16-
private import semmle.code.java.dataflow.ExternalFlow
17-
18-
/**
19-
* A message interpolator Type that perform Expression Language (EL) evaluations
20-
*/
21-
class ELMessageInterpolatorType extends RefType {
22-
ELMessageInterpolatorType() {
23-
this.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.hasQualifiedName("org.springframework.validation.beanvalidation",
42-
["CustomValidatorBean", "LocalValidatorFactoryBean"]) and
43-
m.getName() = "setMessageInterpolator"
44-
)
45-
)
46-
}
47-
48-
/**
49-
* The message interpolator is likely to be safe, because it does not process Java Expression Language expressions.
50-
*/
51-
predicate isSafe() { not this.getAnArgument().getType() instanceof ELMessageInterpolatorType }
52-
}
53-
54-
/**
55-
* Taint tracking BeanValidationConfiguration describing the flow of data from user input
56-
* to the argument of a method that builds constraint error messages.
57-
*/
58-
module BeanValidationConfig implements DataFlow::ConfigSig {
59-
predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
60-
61-
predicate isSink(DataFlow::Node sink) { sink instanceof BeanValidationSink }
62-
}
63-
64-
module BeanValidationFlow = TaintTracking::Global<BeanValidationConfig>;
65-
14+
import semmle.code.java.security.InsecureBeanValidationQuery
6615
import BeanValidationFlow::PathGraph
6716

68-
/**
69-
* A bean validation sink, such as method `buildConstraintViolationWithTemplate`
70-
* declared on a subtype of `javax.validation.ConstraintValidatorContext`.
71-
*/
72-
private class BeanValidationSink extends DataFlow::Node {
73-
BeanValidationSink() { sinkNode(this, "bean-validation") }
74-
}
75-
7617
from BeanValidationFlow::PathNode source, BeanValidationFlow::PathNode sink
7718
where
7819
(

0 commit comments

Comments
 (0)