|
| 1 | +/** |
| 2 | + * @name Weak or direct parameter references are used |
| 3 | + * @description Directly checking request parameters without following a strong params pattern can lead to unintentional avenues for injection attacks. |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity error |
| 6 | + * @security-severity 5.0 |
| 7 | + * @precision medium |
| 8 | + * @id rb/weak-params |
| 9 | + * @tags security |
| 10 | + */ |
| 11 | + |
| 12 | +import ruby |
| 13 | +import codeql.ruby.Concepts |
| 14 | +import codeql.ruby.DataFlow |
| 15 | +import codeql.ruby.TaintTracking |
| 16 | +import codeql.ruby.frameworks.ActionController |
| 17 | +import DataFlow::PathGraph |
| 18 | + |
| 19 | +/** |
| 20 | + * A call to `request` in an ActionController controller class. |
| 21 | + * This probably refers to the incoming HTTP request object. |
| 22 | + */ |
| 23 | +class ActionControllerRequest extends DataFlow::Node { |
| 24 | + ActionControllerRequest() { |
| 25 | + exists(DataFlow::CallNode c | |
| 26 | + c.asExpr().getExpr().getEnclosingModule() instanceof ActionControllerControllerClass and |
| 27 | + c.getMethodName() = "request" |
| 28 | + | |
| 29 | + c.flowsTo(this) |
| 30 | + ) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * A direct parameters reference that happens inside a controller class. |
| 36 | + */ |
| 37 | +class WeakParams extends DataFlow::CallNode { |
| 38 | + WeakParams() { |
| 39 | + this.getReceiver() instanceof ActionControllerRequest and |
| 40 | + this.getMethodName() = |
| 41 | + ["path_parameters", "query_parameters", "request_parameters", "GET", "POST"] |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * A Taint tracking config where the source is a weak params access in a controller and the sink |
| 47 | + * is a method call of a model class |
| 48 | + */ |
| 49 | +class Configuration extends TaintTracking::Configuration { |
| 50 | + Configuration() { this = "WeakParamsConfiguration" } |
| 51 | + |
| 52 | + override predicate isSource(DataFlow::Node node) { node instanceof WeakParams } |
| 53 | + |
| 54 | + // the sink is an instance of a Model class that receives a method call |
| 55 | + override predicate isSink(DataFlow::Node node) { node = any(PersistentWriteAccess a).getValue() } |
| 56 | +} |
| 57 | + |
| 58 | +from Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink |
| 59 | +where config.hasFlowPath(source, sink) |
| 60 | +select sink.getNode(), source, sink, |
| 61 | + "By exposing all keys in request parameters or by blindy accessing them, unintended parameters could be used and lead to mass-assignment or have other unexpected side-effects. It is safer to follow the 'strong parameters' pattern in Rails, which is outlined here: https://api.rubyonrails.org/classes/ActionController/StrongParameters.html" |
0 commit comments