|
| 1 | +/** Provides taint tracking configurations to be used in XXE queries. */ |
| 2 | + |
| 3 | +import java |
| 4 | +import semmle.code.java.dataflow.FlowSources |
| 5 | +import semmle.code.java.dataflow.TaintTracking |
| 6 | +import semmle.code.java.dataflow.TaintTracking2 |
| 7 | +import semmle.code.java.security.XmlParsers |
| 8 | +import DataFlow::PathGraph |
| 9 | + |
| 10 | +/** |
| 11 | + * A taint-tracking configuration for unvalidated remote user input that is used in XML external entity expansion. |
| 12 | + */ |
| 13 | +class XxeConfig extends TaintTracking::Configuration { |
| 14 | + XxeConfig() { this = "XxeConfig" } |
| 15 | + |
| 16 | + override predicate isSource(DataFlow::Node src) { src instanceof RemoteFlowSource } |
| 17 | + |
| 18 | + override predicate isSink(DataFlow::Node sink) { sink instanceof UnsafeXxeSink } |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * A taint-tracking configuration for unvalidated local user input that is used in XML external entity expansion. |
| 23 | + */ |
| 24 | +class XxeLocalConfig extends TaintTracking::Configuration { |
| 25 | + XxeLocalConfig() { this = "XxeLocalConfig" } |
| 26 | + |
| 27 | + override predicate isSource(DataFlow::Node src) { src instanceof LocalUserInput } |
| 28 | + |
| 29 | + override predicate isSink(DataFlow::Node sink) { sink instanceof UnsafeXxeSink } |
| 30 | +} |
| 31 | + |
| 32 | +private class UnsafeXxeSink extends DataFlow::ExprNode { |
| 33 | + UnsafeXxeSink() { |
| 34 | + not exists(SafeSaxSourceFlowConfig safeSource | safeSource.hasFlowTo(this)) and |
| 35 | + exists(XmlParserCall parse | |
| 36 | + parse.getSink() = this.getExpr() and |
| 37 | + not parse.isSafe() |
| 38 | + ) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * A taint-tracking configuration for safe XML readers used to parse XML documents. |
| 44 | + */ |
| 45 | +private class SafeSaxSourceFlowConfig extends TaintTracking2::Configuration { |
| 46 | + SafeSaxSourceFlowConfig() { this = "XmlParsers::SafeSAXSourceFlowConfig" } |
| 47 | + |
| 48 | + override predicate isSource(DataFlow::Node src) { src.asExpr() instanceof SafeSaxSource } |
| 49 | + |
| 50 | + override predicate isSink(DataFlow::Node sink) { |
| 51 | + sink.asExpr() = any(XmlParserCall parse).getSink() |
| 52 | + } |
| 53 | + |
| 54 | + override int fieldFlowBranchLimit() { result = 0 } |
| 55 | +} |
0 commit comments