|
| 1 | +/** |
| 2 | + * @name Failure to use HTTPS URLs |
| 3 | + * @description Non-HTTPS connections can be intercepted by third parties. |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity warning |
| 6 | + * @precision medium |
| 7 | + * @id cpp/non-https-url |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-319 |
| 10 | + */ |
| 11 | + |
| 12 | +import cpp |
| 13 | +import semmle.code.cpp.dataflow.TaintTracking |
| 14 | +import DataFlow::PathGraph |
| 15 | + |
| 16 | +/** |
| 17 | + * A string matching private host names of IPv4 and IPv6, which only matches |
| 18 | + * the host portion therefore checking for port is not necessary. |
| 19 | + * Several examples are localhost, reserved IPv4 IP addresses including |
| 20 | + * 127.0.0.1, 10.x.x.x, 172.16.x,x, 192.168.x,x, and reserved IPv6 addresses |
| 21 | + * including [0:0:0:0:0:0:0:1] and [::1] |
| 22 | + */ |
| 23 | +class PrivateHostName extends string { |
| 24 | + bindingset[this] |
| 25 | + PrivateHostName() { |
| 26 | + this.regexpMatch("(?i)localhost(?:[:/?#].*)?|127\\.0\\.0\\.1(?:[:/?#].*)?|10(?:\\.[0-9]+){3}(?:[:/?#].*)?|172\\.16(?:\\.[0-9]+){2}(?:[:/?#].*)?|192.168(?:\\.[0-9]+){2}(?:[:/?#].*)?|\\[?0:0:0:0:0:0:0:1\\]?(?:[:/?#].*)?|\\[?::1\\]?(?:[:/?#].*)?") |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * A string containing an HTTP URL not in a private domain. |
| 32 | + */ |
| 33 | +class HttpStringLiteral extends StringLiteral { |
| 34 | + HttpStringLiteral() { |
| 35 | + exists(string s | this.getValue() = s | |
| 36 | + s = "http" |
| 37 | + or |
| 38 | + exists(string tail | |
| 39 | + tail = s.regexpCapture("http://(.*)", 1) and not tail instanceof PrivateHostName |
| 40 | + ) and |
| 41 | + not TaintTracking::localExprTaint(any(StringLiteral p | |
| 42 | + p.getValue() instanceof PrivateHostName |
| 43 | + ), this.getParent*()) |
| 44 | + ) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Taint tracking configuration for HTTP connections. |
| 50 | + */ |
| 51 | +class HttpStringToUrlOpenConfig extends TaintTracking::Configuration { |
| 52 | + HttpStringToUrlOpenConfig() { this = "HttpStringToUrlOpenConfig" } |
| 53 | + |
| 54 | + override predicate isSource(DataFlow::Node src) { |
| 55 | + // Sources are strings containing an HTTP URL not in a private domain. |
| 56 | + src.asExpr() instanceof HttpStringLiteral |
| 57 | + } |
| 58 | + |
| 59 | + override predicate isSink(DataFlow::Node sink) { |
| 60 | + // Sinks can be anything that demonstrates the string is likely to be |
| 61 | + // accessed as a URL, for example using it in a network access. Some |
| 62 | + // URLs are only ever displayed or used for data processing. |
| 63 | + exists(FunctionCall fc | |
| 64 | + fc.getTarget().hasGlobalOrStdName(["system", "gethostbyname", "getaddrinfo"]) and |
| 65 | + sink.asExpr() = fc.getArgument(0) |
| 66 | + or |
| 67 | + fc.getTarget().hasGlobalOrStdName(["send", "URLDownloadToFile", "URLDownloadToCacheFile"]) and |
| 68 | + sink.asExpr() = fc.getArgument(1) |
| 69 | + or |
| 70 | + fc.getTarget().hasGlobalOrStdName(["curl_easy_setopt", "getnameinfo"]) and |
| 71 | + sink.asExpr() = fc.getArgument(2) |
| 72 | + or |
| 73 | + fc.getTarget().hasGlobalOrStdName(["ShellExecute", "ShellExecuteA", "ShellExecuteW"]) and |
| 74 | + sink.asExpr() = fc.getArgument(3) |
| 75 | + ) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +from |
| 80 | + HttpStringToUrlOpenConfig config, DataFlow::PathNode source, DataFlow::PathNode sink, |
| 81 | + HttpStringLiteral str |
| 82 | +where |
| 83 | + config.hasFlowPath(source, sink) and |
| 84 | + str = source.getNode().asExpr() |
| 85 | +select str, source, sink, "A URL may be constructed with the HTTP protocol." |
0 commit comments