|
| 1 | +/** Provides classes and predicates to reason about plaintext HTTP vulnerabilities. */ |
| 2 | + |
| 3 | +import java |
| 4 | +private import semmle.code.java.dataflow.DataFlow |
| 5 | +private import semmle.code.java.dataflow.ExternalFlow |
| 6 | +private import semmle.code.java.dataflow.TaintTracking |
| 7 | +private import semmle.code.java.frameworks.ApacheHttp |
| 8 | +private import semmle.code.java.frameworks.Networking |
| 9 | + |
| 10 | +/** |
| 11 | + * String of HTTP URLs not in private domains. |
| 12 | + */ |
| 13 | +class HttpStringLiteral extends StringLiteral { |
| 14 | + HttpStringLiteral() { |
| 15 | + exists(string s | this.getRepresentedString() = s | |
| 16 | + s = "http" |
| 17 | + or |
| 18 | + s.matches("http://%") and |
| 19 | + not s.substring(7, s.length()) instanceof PrivateHostName and |
| 20 | + not TaintTracking::localExprTaint(any(StringLiteral p | |
| 21 | + p.getRepresentedString() instanceof PrivateHostName |
| 22 | + ), this.getParent*()) |
| 23 | + ) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * A sink that represents a URL opening method call, such as a call to `java.net.URL.openConnection()`. |
| 29 | + */ |
| 30 | +abstract class UrlOpenSink extends DataFlow::Node { } |
| 31 | + |
| 32 | +private class DefaultUrlOpenSink extends UrlOpenSink { |
| 33 | + DefaultUrlOpenSink() { sinkNode(this, "open-url") } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * A unit class for adding additional taint steps. |
| 38 | + * |
| 39 | + * Extend this class to add additional taint steps that should apply |
| 40 | + * to configurations working with HTTP URLs. |
| 41 | + */ |
| 42 | +class HttpUrlsAdditionalTaintStep extends Unit { |
| 43 | + /** |
| 44 | + * Holds if the step from `node1` to `node2` should be considered a taint |
| 45 | + * step for taint tracking configurations working with HTTP URLs. |
| 46 | + */ |
| 47 | + abstract predicate step(DataFlow::Node n1, DataFlow::Node n2); |
| 48 | +} |
| 49 | + |
| 50 | +private class DefaultHttpUrlAdditionalTaintStep extends HttpUrlsAdditionalTaintStep { |
| 51 | + override predicate step(DataFlow::Node n1, DataFlow::Node n2) { |
| 52 | + apacheHttpRequestStep(n1, n2) or |
| 53 | + createUriStep(n1, n2) or |
| 54 | + createUrlStep(n1, n2) or |
| 55 | + urlOpenStep(n1, n2) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** Constructor of `ApacheHttpRequest` */ |
| 60 | +private predicate apacheHttpRequestStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 61 | + exists(ConstructorCall cc | |
| 62 | + cc.getConstructedType() instanceof ApacheHttpRequest and |
| 63 | + node2.asExpr() = cc and |
| 64 | + cc.getAnArgument() = node1.asExpr() |
| 65 | + ) |
| 66 | +} |
| 67 | + |
| 68 | +/** `URI` methods */ |
| 69 | +private predicate createUriStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 70 | + exists(UriConstructorCall cc | |
| 71 | + cc.getSchemeArg() = node1.asExpr() and |
| 72 | + node2.asExpr() = cc |
| 73 | + ) |
| 74 | +} |
| 75 | + |
| 76 | +/** Constructors of `URL` */ |
| 77 | +private predicate createUrlStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 78 | + exists(UrlConstructorCall cc | |
| 79 | + cc.getProtocolArg() = node1.asExpr() and |
| 80 | + node2.asExpr() = cc |
| 81 | + ) |
| 82 | +} |
| 83 | + |
| 84 | +/** Method call of `HttpURLOpenMethod` */ |
| 85 | +private predicate urlOpenStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 86 | + exists(MethodAccess ma | |
| 87 | + ma.getMethod() instanceof UrlOpenConnectionMethod and |
| 88 | + node1.asExpr() = ma.getQualifier() and |
| 89 | + ma = node2.asExpr() |
| 90 | + ) |
| 91 | +} |
0 commit comments