|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for reasoning about |
| 3 | + * download of sensitive file through insecure connection, as well as |
| 4 | + * extension points for adding your own. |
| 5 | + */ |
| 6 | + |
| 7 | +private import ruby |
| 8 | +private import codeql.ruby.DataFlow |
| 9 | +private import codeql.ruby.Concepts |
| 10 | +private import codeql.ruby.typetracking.TypeTracker |
| 11 | +private import codeql.ruby.frameworks.Files |
| 12 | + |
| 13 | +/** |
| 14 | + * Classes and predicates for reasoning about download of sensitive file through insecure connection vulnerabilities. |
| 15 | + */ |
| 16 | +module InsecureDownload { |
| 17 | + /** |
| 18 | + * A data flow source for download of sensitive file through insecure connection. |
| 19 | + */ |
| 20 | + abstract class Source extends DataFlow::Node { |
| 21 | + /** |
| 22 | + * Gets a flow-label for this source. |
| 23 | + */ |
| 24 | + abstract DataFlow::FlowState getALabel(); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * A data flow sink for download of sensitive file through insecure connection. |
| 29 | + */ |
| 30 | + abstract class Sink extends DataFlow::Node { |
| 31 | + /** |
| 32 | + * Gets the call that downloads the sensitive file. |
| 33 | + */ |
| 34 | + abstract DataFlow::Node getDownloadCall(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Gets a flow-label where this sink is vulnerable. |
| 38 | + */ |
| 39 | + abstract DataFlow::FlowState getALabel(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * A sanitizer for download of sensitive file through insecure connection. |
| 44 | + */ |
| 45 | + abstract class Sanitizer extends DataFlow::Node { } |
| 46 | + |
| 47 | + /** |
| 48 | + * Flow-labels for reasoning about download of sensitive file through insecure connection. |
| 49 | + */ |
| 50 | + module Label { |
| 51 | + /** |
| 52 | + * A flow-label for a URL that is downloaded over an insecure connection. |
| 53 | + */ |
| 54 | + class Insecure extends DataFlow::FlowState { |
| 55 | + Insecure() { this = "insecure" } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * A flow-label for a URL that is sensitive. |
| 60 | + */ |
| 61 | + class Sensitive extends DataFlow::FlowState { |
| 62 | + Sensitive() { this = "sensitive" } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * A flow-label for file URLs that are both sensitive and downloaded over an insecure connection. |
| 67 | + */ |
| 68 | + class SensitiveInsecure extends DataFlow::FlowState { |
| 69 | + SensitiveInsecure() { this = "sensitiveInsecure" } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * A HTTP or FTP url. |
| 75 | + */ |
| 76 | + class InsecureUrl extends DataFlow::Node { |
| 77 | + string str; |
| 78 | + |
| 79 | + InsecureUrl() { |
| 80 | + str = this.asExpr().getConstantValue().getString() and |
| 81 | + str.regexpMatch("http://.*|ftp://.*") |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * A HTTP or FTP URL that refers to a file with a sensitive file extension, |
| 87 | + * seen as a source for downloads of sensitive files through an insecure connection. |
| 88 | + */ |
| 89 | + class InsecureFileUrl extends Source, InsecureUrl { |
| 90 | + override DataFlow::FlowState getALabel() { |
| 91 | + result instanceof Label::Insecure |
| 92 | + or |
| 93 | + hasUnsafeExtension(str) and |
| 94 | + result instanceof Label::SensitiveInsecure |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * A string containing a sensitive file extension, |
| 100 | + * seen as a source for downloads of sensitive files through an insecure connection. |
| 101 | + */ |
| 102 | + class SensitiveFileUrl extends Source { |
| 103 | + string str; |
| 104 | + |
| 105 | + SensitiveFileUrl() { |
| 106 | + str = this.asExpr().getConstantValue().getString() and |
| 107 | + hasUnsafeExtension(str) |
| 108 | + } |
| 109 | + |
| 110 | + override DataFlow::FlowState getALabel() { result instanceof Label::Sensitive } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Holds if `str` is a string that ends with an unsafe file extension. |
| 115 | + */ |
| 116 | + bindingset[str] |
| 117 | + predicate hasUnsafeExtension(string str) { |
| 118 | + exists(string suffix | suffix = unsafeExtension() | |
| 119 | + str.suffix(str.length() - suffix.length() - 1).toLowerCase() = "." + suffix |
| 120 | + ) |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Gets a file-extension that can potentially be dangerous. |
| 125 | + * |
| 126 | + * Archives are included, because they often contain source-code. |
| 127 | + */ |
| 128 | + string unsafeExtension() { |
| 129 | + result = |
| 130 | + [ |
| 131 | + "exe", "dmg", "pkg", "tar.gz", "zip", "sh", "bat", "cmd", "app", "apk", "msi", "dmg", |
| 132 | + "tar.gz", "zip", "js", "py", "jar", "war" |
| 133 | + ] |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * A response from an outgoing HTTP request, considered as a flow sink for |
| 138 | + * downloading a sensitive file through an insecure connection. |
| 139 | + */ |
| 140 | + private class HttpResponseAsSink extends Sink { |
| 141 | + private HTTP::Client::Request req; |
| 142 | + |
| 143 | + HttpResponseAsSink() { |
| 144 | + this = req.getAUrlPart() and |
| 145 | + // If any part of the URL has an unsafe extension, we consider all parts of the URL to be sinks. |
| 146 | + hasUnsafeExtension(req.getAUrlPart().asExpr().getConstantValue().getString()) |
| 147 | + } |
| 148 | + |
| 149 | + override DataFlow::Node getDownloadCall() { result.asExpr().getExpr() = req } |
| 150 | + |
| 151 | + override DataFlow::FlowState getALabel() { |
| 152 | + result instanceof Label::SensitiveInsecure |
| 153 | + or |
| 154 | + any(req.getAUrlPart()) instanceof InsecureUrl and result instanceof Label::Sensitive |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Gets a node for the response from `request`, type-tracked using `t`. |
| 160 | + */ |
| 161 | + DataFlow::LocalSourceNode clientRequestResponse(TypeTracker t, HTTP::Client::Request request) { |
| 162 | + t.start() and |
| 163 | + result = request.getResponseBody() |
| 164 | + or |
| 165 | + exists(TypeTracker t2 | result = clientRequestResponse(t2, request).track(t2, t)) |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * A url that is downloaded through an insecure connection, where the result ends up being saved to a sensitive location. |
| 170 | + */ |
| 171 | + class FileWriteSink extends Sink { |
| 172 | + HTTP::Client::Request request; |
| 173 | + |
| 174 | + FileWriteSink() { |
| 175 | + // For example, in: |
| 176 | + // |
| 177 | + // ```rb |
| 178 | + // f = File.open("foo.exe") |
| 179 | + // f.write(Excon.get(...).body) # $BAD= |
| 180 | + // ``` |
| 181 | + // |
| 182 | + // `f` is the `FileSystemAccess` and the call `f.write` is the `IO::FileWriter`. |
| 183 | + // The call `Excon.get` is the `HTTP::Client::Request`. |
| 184 | + // |
| 185 | + // The `file = write` alternative models this case: |
| 186 | + // ```rb |
| 187 | + // File.write("foo.exe", Excon.get(...).body) |
| 188 | + // ``` |
| 189 | + exists(IO::FileWriter write, FileSystemAccess file | |
| 190 | + this = request.getAUrlPart() and |
| 191 | + clientRequestResponse(TypeTracker::end(), request).flowsTo(write.getADataNode()) and |
| 192 | + (file.(DataFlow::LocalSourceNode).flowsTo(write.getReceiver()) or file = write) and |
| 193 | + hasUnsafeExtension(file.getAPathArgument().asExpr().getConstantValue().getString()) |
| 194 | + ) |
| 195 | + } |
| 196 | + |
| 197 | + override DataFlow::FlowState getALabel() { result instanceof Label::Insecure } |
| 198 | + |
| 199 | + override DataFlow::Node getDownloadCall() { result.asExpr().getExpr() = request } |
| 200 | + } |
| 201 | +} |
0 commit comments