|
| 1 | +import java |
| 2 | +import semmle.code.java.dataflow.FlowSources |
| 3 | +import DataFlow |
| 4 | +import DataFlow::PathGraph |
| 5 | + |
| 6 | +/** |
| 7 | + * A taint-tracking configuration for unvalidated user input that is used in OGNL EL evaluation. |
| 8 | + */ |
| 9 | +class OgnlInjectionFlowConfig extends TaintTracking::Configuration { |
| 10 | + OgnlInjectionFlowConfig() { this = "OgnlInjectionFlowConfig" } |
| 11 | + |
| 12 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 13 | + |
| 14 | + override predicate isSink(DataFlow::Node sink) { sink instanceof OgnlInjectionSink } |
| 15 | + |
| 16 | + override predicate isSanitizer(DataFlow::Node node) { |
| 17 | + node.getType() instanceof PrimitiveType or node.getType() instanceof BoxedType |
| 18 | + } |
| 19 | + |
| 20 | + override predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 21 | + parseCompileExpressionStep(node1, node2) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** The class `org.apache.commons.ognl.Ognl` or `ognl.Ognl`. */ |
| 26 | +class TypeOgnl extends Class { |
| 27 | + TypeOgnl() { |
| 28 | + this.hasQualifiedName("org.apache.commons.ognl", "Ognl") or |
| 29 | + this.hasQualifiedName("ognl", "Ognl") |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** The interface `org.apache.commons.ognl.Node` or `ognl.Node`. */ |
| 34 | +class TypeNode extends Interface { |
| 35 | + TypeNode() { |
| 36 | + this.hasQualifiedName("org.apache.commons.ognl", "Node") or |
| 37 | + this.hasQualifiedName("ognl", "Node") |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** The class `com.opensymphony.xwork2.ognl.OgnlUtil`. */ |
| 42 | +class TypeOgnlUtil extends Class { |
| 43 | + TypeOgnlUtil() { this.hasQualifiedName("com.opensymphony.xwork2.ognl", "OgnlUtil") } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * OGNL sink for OGNL injection vulnerabilities, i.e. 1st argument to `getValue` or `setValue` |
| 48 | + * method from `Ognl` or `getValue` or `setValue` method from `Node`. |
| 49 | + */ |
| 50 | +predicate ognlSinkMethod(Method m, int index) { |
| 51 | + ( |
| 52 | + m.getDeclaringType() instanceof TypeOgnl and index = 0 |
| 53 | + or |
| 54 | + m.getDeclaringType().getAnAncestor*() instanceof TypeNode |
| 55 | + ) and |
| 56 | + ( |
| 57 | + m.hasName("getValue") or |
| 58 | + m.hasName("setValue") |
| 59 | + ) and |
| 60 | + index = 0 |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Struts sink for OGNL injection vulnerabilities, i.e. 1st argument to `getValue`, `setValue` or |
| 65 | + * `callMethod` method from `OgnlUtil`. |
| 66 | + */ |
| 67 | +predicate strutsSinkMethod(Method m, int index) { |
| 68 | + m.getDeclaringType() instanceof TypeOgnlUtil and |
| 69 | + ( |
| 70 | + m.hasName("getValue") or |
| 71 | + m.hasName("setValue") or |
| 72 | + m.hasName("callMethod") |
| 73 | + ) and |
| 74 | + index = 0 |
| 75 | +} |
| 76 | + |
| 77 | +/** Holds if parameter at index `index` in method `m` is OGNL injection sink. */ |
| 78 | +predicate ognlInjectionSinkMethod(Method m, int index) { |
| 79 | + ognlSinkMethod(m, index) or |
| 80 | + strutsSinkMethod(m, index) |
| 81 | +} |
| 82 | + |
| 83 | +/** A data flow sink for unvalidated user input that is used in OGNL EL evaluation. */ |
| 84 | +class OgnlInjectionSink extends DataFlow::ExprNode { |
| 85 | + OgnlInjectionSink() { |
| 86 | + exists(MethodAccess ma, Method m, int index | |
| 87 | + ma.getMethod() = m and |
| 88 | + (ma.getArgument(index) = this.getExpr() or ma.getQualifier() = this.getExpr()) and |
| 89 | + ognlInjectionSinkMethod(m, index) |
| 90 | + ) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Holds if `n1` to `n2` is a dataflow step that converts between `String` and `Object` or `Node`, |
| 96 | + * i.e. `Ognl.parseExpression(tainted)` or `Ognl.compileExpression(tainted)`. |
| 97 | + */ |
| 98 | +predicate parseCompileExpressionStep(ExprNode n1, ExprNode n2) { |
| 99 | + exists(MethodAccess ma, Method m, int index | |
| 100 | + n1.asExpr() = ma.getArgument(index) and |
| 101 | + n2.asExpr() = ma and |
| 102 | + ma.getMethod() = m and |
| 103 | + m.getDeclaringType() instanceof TypeOgnl |
| 104 | + | |
| 105 | + m.hasName("parseExpression") and index = 0 |
| 106 | + or |
| 107 | + m.hasName("compileExpression") and index = 2 |
| 108 | + ) |
| 109 | +} |
0 commit comments