|
| 1 | +/** |
| 2 | + * Provides classes and predicates for reasoning about the use of |
| 3 | + * non-HTTPS URLs in Rust code. |
| 4 | + */ |
| 5 | + |
| 6 | +import rust |
| 7 | +private import codeql.rust.dataflow.DataFlow |
| 8 | +private import codeql.rust.dataflow.FlowSink |
| 9 | +private import codeql.rust.Concepts |
| 10 | + |
| 11 | +/** |
| 12 | + * Provides default sources, sinks and barriers for detecting use of |
| 13 | + * non-HTTPS URLs, as well as extension points for adding your own. |
| 14 | + */ |
| 15 | +module UseOfHttp { |
| 16 | + /** |
| 17 | + * A data flow source for use of non-HTTPS URLs. |
| 18 | + */ |
| 19 | + abstract class Source extends DataFlow::Node { } |
| 20 | + |
| 21 | + /** |
| 22 | + * A data flow sink for use of non-HTTPS URLs. |
| 23 | + */ |
| 24 | + abstract class Sink extends QuerySink::Range { |
| 25 | + override string getSinkType() { result = "UseOfHttp" } |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * A barrier for use of non-HTTPS URLs. |
| 30 | + */ |
| 31 | + abstract class Barrier extends DataFlow::Node { } |
| 32 | + |
| 33 | + /** |
| 34 | + * A string containing an HTTP URL. |
| 35 | + */ |
| 36 | + class HttpStringLiteral extends StringLiteralExpr { |
| 37 | + HttpStringLiteral() { |
| 38 | + exists(string s | this.getTextValue() = s | |
| 39 | + // match HTTP URLs |
| 40 | + s.regexpMatch("(?i)\"http://.*\"") and |
| 41 | + // exclude private/local addresses: |
| 42 | + // - IPv4: localhost / 127.0.0.1, 192.168.x.x, 10.x.x.x, 172.16.x.x -> 172.31.x.x |
| 43 | + // - IPv6 (address inside []): ::1 (or 0:0:0:0:0:0:0:1), fc00::/7 (i.e. anything beginning `fcxx:` or `fdxx:`) |
| 44 | + not s.regexpMatch("(?i)\"http://(localhost|127\\.0\\.0\\.1|192\\.168\\.[0-9]+\\.[0-9]+|10\\.[0-9]+\\.[0-9]+\\.[0-9]+|172\\.(1[6-9]|2[0-9]|3[01])\\.[0-9]+|\\[::1\\]|\\[0:0:0:0:0:0:0:1\\]|\\[f[cd][0-9a-f]{2}:.*\\]).*\"") |
| 45 | + ) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * An HTTP string literal as a source. |
| 51 | + */ |
| 52 | + private class HttpStringLiteralAsSource extends Source { |
| 53 | + HttpStringLiteralAsSource() { this.asExpr().getExpr() instanceof HttpStringLiteral } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * A sink for use of HTTP URLs from model data. |
| 58 | + */ |
| 59 | + private class ModelsAsDataSink extends Sink { |
| 60 | + ModelsAsDataSink() { sinkNode(this, "request-url") } |
| 61 | + } |
| 62 | +} |
0 commit comments