|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for detecting "URL |
| 3 | + * redirection" vulnerabilities, as well as extension points for adding your |
| 4 | + * own. |
| 5 | + */ |
| 6 | + |
| 7 | +private import ruby |
| 8 | +private import codeql.ruby.DataFlow |
| 9 | +private import codeql.ruby.Concepts |
| 10 | +private import codeql.ruby.dataflow.RemoteFlowSources |
| 11 | +private import codeql.ruby.dataflow.BarrierGuards |
| 12 | + |
| 13 | +/** |
| 14 | + * Provides default sources, sinks and sanitizers for detecting |
| 15 | + * "URL redirection" vulnerabilities, as well as extension points for |
| 16 | + * adding your own. |
| 17 | + */ |
| 18 | +module UrlRedirect { |
| 19 | + /** |
| 20 | + * A data flow source for "URL redirection" vulnerabilities. |
| 21 | + */ |
| 22 | + abstract class Source extends DataFlow::Node { } |
| 23 | + |
| 24 | + /** |
| 25 | + * A data flow sink for "URL redirection" vulnerabilities. |
| 26 | + */ |
| 27 | + abstract class Sink extends DataFlow::Node { } |
| 28 | + |
| 29 | + /** |
| 30 | + * A sanitizer for "URL redirection" vulnerabilities. |
| 31 | + */ |
| 32 | + abstract class Sanitizer extends DataFlow::Node { } |
| 33 | + |
| 34 | + /** |
| 35 | + * A sanitizer guard for "URL redirection" vulnerabilities. |
| 36 | + */ |
| 37 | + abstract class SanitizerGuard extends DataFlow::BarrierGuard { } |
| 38 | + |
| 39 | + /** |
| 40 | + * Additional taint steps for "URL redirection" vulnerabilities. |
| 41 | + */ |
| 42 | + predicate isAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 43 | + taintStepViaMethodCallReturnValue(node1, node2) |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * A source of remote user input, considered as a flow source. |
| 48 | + */ |
| 49 | + class RemoteFlowSourceAsSource extends Source, RemoteFlowSource { } |
| 50 | + |
| 51 | + /** |
| 52 | + * A HTTP redirect response, considered as a flow sink. |
| 53 | + */ |
| 54 | + class RedirectLocationAsSink extends Sink { |
| 55 | + RedirectLocationAsSink() { |
| 56 | + exists(HTTP::Server::HttpRedirectResponse e | |
| 57 | + this = e.getRedirectLocation() and |
| 58 | + // As a rough heuristic, assume that methods with these names are handlers for POST/PUT/PATCH/DELETE requests, |
| 59 | + // which are not as vulnerable to URL redirection because browsers will not initiate them from clicking a link. |
| 60 | + not this.getEnclosingCallable() |
| 61 | + .(Method) |
| 62 | + .getName() |
| 63 | + .regexpMatch(".*(create|update|destroy).*") |
| 64 | + ) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * A comparison with a constant string, considered as a sanitizer-guard. |
| 70 | + */ |
| 71 | + class StringConstCompareAsSanitizerGuard extends SanitizerGuard, StringConstCompare { } |
| 72 | + |
| 73 | + /** |
| 74 | + * Some methods will propagate taint to their return values. |
| 75 | + * Here we cover a few common ones related to `ActionController::Parameters`. |
| 76 | + * TODO: use ApiGraphs or something to restrict these method calls to the correct receiver, rather |
| 77 | + * than matching on method name alone. |
| 78 | + */ |
| 79 | + predicate taintStepViaMethodCallReturnValue(DataFlow::Node node1, DataFlow::Node node2) { |
| 80 | + exists(MethodCall m | m = node2.asExpr().getExpr() | |
| 81 | + m.getReceiver() = node1.asExpr().getExpr() and |
| 82 | + (actionControllerTaintedMethod(m) or hashTaintedMethod(m)) |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * String interpolation is considered safe, provided the string is prefixed by a non-tainted value. |
| 88 | + * In most cases this will prevent the tainted value from controlling e.g. the host of the URL. |
| 89 | + * |
| 90 | + * For example: |
| 91 | + * |
| 92 | + * ```ruby |
| 93 | + * redirect_to "/users/#{params[:key]}" # safe |
| 94 | + * redirect_to "#{params[:key]}/users" # unsafe |
| 95 | + * ``` |
| 96 | + * |
| 97 | + * There are prefixed interpolations that are not safe, e.g. |
| 98 | + * |
| 99 | + * ```ruby |
| 100 | + * redirect_to "foo#{params[:key]}/users" # => "foo-malicious-site.com/users" |
| 101 | + * ``` |
| 102 | + * |
| 103 | + * We currently don't catch these cases. |
| 104 | + */ |
| 105 | + class StringInterpolationAsSanitizer extends Sanitizer { |
| 106 | + StringInterpolationAsSanitizer() { |
| 107 | + exists(StringlikeLiteral str, int n | str.getComponent(n) = this.asExpr().getExpr() and n > 0) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * These methods return a new `ActionController::Parameters` or a `Hash` containing a subset of |
| 113 | + * the original values. This may still contain user input, so the results are tainted. |
| 114 | + * TODO: flesh this out to cover the whole API. |
| 115 | + */ |
| 116 | + predicate actionControllerTaintedMethod(MethodCall m) { |
| 117 | + m.getMethodName() in ["to_unsafe_hash", "to_unsafe_h", "permit", "require"] |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * These `Hash` methods preserve taint because they return a new hash which may still contain keys |
| 122 | + * with user input. |
| 123 | + * TODO: flesh this out to cover the whole API. |
| 124 | + */ |
| 125 | + predicate hashTaintedMethod(MethodCall m) { m.getMethodName() in ["merge", "fetch"] } |
| 126 | +} |
0 commit comments