|
| 1 | +import java |
| 2 | +import InjectionLib |
| 3 | +import semmle.code.java.dataflow.FlowSources |
| 4 | +import semmle.code.java.dataflow.TaintTracking |
| 5 | + |
| 6 | +/** |
| 7 | + * A taint-tracking configuration for unsafe user input |
| 8 | + * that is used to construct and evaluate a Java EE expression. |
| 9 | + */ |
| 10 | +class JavaEEExpressionInjectionConfig extends TaintTracking::Configuration { |
| 11 | + JavaEEExpressionInjectionConfig() { this = "JavaEEExpressionInjectionConfig" } |
| 12 | + |
| 13 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 14 | + |
| 15 | + override predicate isSink(DataFlow::Node sink) { sink instanceof ExpressionEvaluationSink } |
| 16 | + |
| 17 | + override predicate isAdditionalTaintStep(DataFlow::Node fromNode, DataFlow::Node toNode) { |
| 18 | + any(TaintPropagatingCall c).taintFlow(fromNode, toNode) or |
| 19 | + returnsDataFromBean(fromNode, toNode) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * A sink for Expresssion Language injection vulnerabilities, |
| 25 | + * i.e. method calls that run evaluation of a Java EE expression. |
| 26 | + */ |
| 27 | +private class ExpressionEvaluationSink extends DataFlow::ExprNode { |
| 28 | + ExpressionEvaluationSink() { |
| 29 | + exists(MethodAccess ma, Method m, Expr taintFrom | |
| 30 | + ma.getMethod() = m and taintFrom = this.asExpr() |
| 31 | + | |
| 32 | + m.getDeclaringType() instanceof ValueExpression and |
| 33 | + m.hasName(["getValue", "setValue"]) and |
| 34 | + ma.getQualifier() = taintFrom |
| 35 | + or |
| 36 | + m.getDeclaringType() instanceof MethodExpression and |
| 37 | + m.hasName("invoke") and |
| 38 | + ma.getQualifier() = taintFrom |
| 39 | + or |
| 40 | + m.getDeclaringType() instanceof LambdaExpression and |
| 41 | + m.hasName("invoke") and |
| 42 | + ma.getQualifier() = taintFrom |
| 43 | + or |
| 44 | + m.getDeclaringType() instanceof ELProcessor and |
| 45 | + m.hasName(["eval", "getValue", "setValue"]) and |
| 46 | + ma.getArgument(0) = taintFrom |
| 47 | + ) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Defines method calls that propagate tainted expressions. |
| 53 | + */ |
| 54 | +private class TaintPropagatingCall extends Call { |
| 55 | + Expr taintFromExpr; |
| 56 | + |
| 57 | + TaintPropagatingCall() { |
| 58 | + taintFromExpr = this.getArgument(1) and |
| 59 | + exists(Method m | this.(MethodAccess).getMethod() = m | |
| 60 | + m.getDeclaringType() instanceof ExpressionFactory and |
| 61 | + m.hasName(["createValueExpression", "createMethodExpression"]) and |
| 62 | + taintFromExpr.getType() instanceof TypeString |
| 63 | + ) |
| 64 | + or |
| 65 | + exists(Constructor c | this.(ConstructorCall).getConstructor() = c | |
| 66 | + c.getDeclaringType() instanceof LambdaExpression and |
| 67 | + taintFromExpr.getType() instanceof ValueExpression |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Holds if `fromNode` to `toNode` is a dataflow step that propagates |
| 73 | + * tainted data. |
| 74 | + */ |
| 75 | + predicate taintFlow(DataFlow::Node fromNode, DataFlow::Node toNode) { |
| 76 | + fromNode.asExpr() = taintFromExpr and toNode.asExpr() = this |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +private class ELProcessor extends RefType { |
| 81 | + ELProcessor() { hasQualifiedName("javax.el", "ELProcessor") } |
| 82 | +} |
| 83 | + |
| 84 | +private class ExpressionFactory extends RefType { |
| 85 | + ExpressionFactory() { hasQualifiedName("javax.el", "ExpressionFactory") } |
| 86 | +} |
| 87 | + |
| 88 | +private class ValueExpression extends RefType { |
| 89 | + ValueExpression() { hasQualifiedName("javax.el", "ValueExpression") } |
| 90 | +} |
| 91 | + |
| 92 | +private class MethodExpression extends RefType { |
| 93 | + MethodExpression() { hasQualifiedName("javax.el", "MethodExpression") } |
| 94 | +} |
| 95 | + |
| 96 | +private class LambdaExpression extends RefType { |
| 97 | + LambdaExpression() { hasQualifiedName("javax.el", "LambdaExpression") } |
| 98 | +} |
0 commit comments