|
| 1 | +import java |
| 2 | +import semmle.code.java.dataflow.TaintTracking |
| 3 | +import semmle.code.java.dataflow.FlowSources |
| 4 | +import DataFlow::PathGraph |
| 5 | + |
| 6 | +class URLConstructor extends ClassInstanceExpr { |
| 7 | + URLConstructor() { this.getConstructor().getDeclaringType().getQualifiedName() = "java.net.URL" } |
| 8 | + |
| 9 | + Expr stringArg() { |
| 10 | + // Query only in URL's that were constructed by calling the single parameter string constructor. |
| 11 | + if |
| 12 | + this.getConstructor().getNumberOfParameters() = 1 and |
| 13 | + this.getConstructor().getParameter(0).getType().getName() = "String" |
| 14 | + then result = this.getArgument(0) |
| 15 | + else none() |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class URLOpenStreamMethod extends Method { |
| 20 | + URLOpenStreamMethod() { |
| 21 | + this.getDeclaringType().getQualifiedName() = "java.net.URL" and |
| 22 | + this.getName() = "openStream" |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +class RemoteURLToOpenStreamFlowConfig extends TaintTracking::Configuration { |
| 27 | + RemoteURLToOpenStreamFlowConfig() { this = "OpenStream::RemoteURLToOpenStreamFlowConfig" } |
| 28 | + |
| 29 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 30 | + |
| 31 | + override predicate isSink(DataFlow::Node sink) { |
| 32 | + exists(MethodAccess m | |
| 33 | + sink.asExpr() = m.getQualifier() and m.getMethod() instanceof URLOpenStreamMethod |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + override predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 38 | + exists(URLConstructor u | |
| 39 | + node1.asExpr() = u.stringArg() and |
| 40 | + node2.asExpr() = u |
| 41 | + ) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +from DataFlow::PathNode source, DataFlow::PathNode sink, MethodAccess call |
| 46 | +where |
| 47 | + sink.getNode().asExpr() = call.getQualifier() and |
| 48 | + any(RemoteURLToOpenStreamFlowConfig c).hasFlowPath(source, sink) |
| 49 | +select call, source, sink, |
| 50 | + "URL on which openStream is called may have been constructed from remote source" |
0 commit comments