|
| 1 | +/** |
| 2 | + * @name Database query in a loop |
| 3 | + * @description Database queries in a loop can lead to an unnecessary amount of database calls and poor performance. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity info |
| 6 | + * @precision high |
| 7 | + * @id rb/database-query-in-loop |
| 8 | + * @tags performance |
| 9 | + */ |
| 10 | + |
| 11 | +import ruby |
| 12 | +private import codeql.ruby.AST |
| 13 | +import codeql.ruby.ast.internal.Constant |
| 14 | +import codeql.ruby.Concepts |
| 15 | +import codeql.ruby.frameworks.ActiveRecord |
| 16 | +private import codeql.ruby.TaintTracking |
| 17 | +private import codeql.ruby.CFG |
| 18 | +private import codeql.ruby.controlflow.internal.Guards as Guards |
| 19 | + |
| 20 | +/** Gets the name of a built-in method that involves a loop operation. */ |
| 21 | +string getALoopMethodName() { |
| 22 | + result in [ |
| 23 | + "each", "reverse_each", "map", "map!", "foreach", "flat_map", "in_batches", "one?", "all?", |
| 24 | + "collect", "collect!", "select", "select!", "reject", "reject!" |
| 25 | + ] |
| 26 | +} |
| 27 | + |
| 28 | +/** A loop, represented by a call to a loop operation. */ |
| 29 | +class LoopingCall extends DataFlow::CallNode { |
| 30 | + Callable loopScope; |
| 31 | + |
| 32 | + LoopingCall() { |
| 33 | + this.getMethodName() = getALoopMethodName() and |
| 34 | + loopScope = this.getBlock().asCallable().asCallableAstNode() |
| 35 | + } |
| 36 | + |
| 37 | + /** Holds if `c` is executed as part of the body of this loop. */ |
| 38 | + predicate executesCall(DataFlow::CallNode c) { c.asExpr().getScope() = loopScope } |
| 39 | +} |
| 40 | + |
| 41 | +/** Holds if `ar` influences a guard that may control the execution of a loop. */ |
| 42 | +predicate usedInLoopControlGuard(ActiveRecordInstance ar) { |
| 43 | + exists(DataFlow::Node insideGuard, CfgNodes::ExprCfgNode guard | |
| 44 | + // For a guard like `cond && ar`, the whole guard will not be tainted |
| 45 | + // so we need to look at the taint of the individual parts. |
| 46 | + insideGuard.asExpr().getExpr() = guard.getExpr().getAChild*() |
| 47 | + | |
| 48 | + TaintTracking::localTaint(ar, insideGuard) and |
| 49 | + guardForLoopControl(guard, _) |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +/** Holds if `guard` controls `break` and `break` would break out of a loop. */ |
| 54 | +predicate guardForLoopControl(CfgNodes::ExprCfgNode guard, CfgNodes::AstCfgNode break) { |
| 55 | + Guards::guardControlsBlock(guard, break.getBasicBlock(), _) and |
| 56 | + ( |
| 57 | + break.(CfgNodes::ExprNodes::MethodCallCfgNode).getMethodName() = "raise" |
| 58 | + or |
| 59 | + break instanceof CfgNodes::ReturningCfgNode |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +from LoopingCall loop, ActiveRecordModelFinderCall call |
| 64 | +where |
| 65 | + loop.executesCall(call) and |
| 66 | + // Disregard loops over constants |
| 67 | + not isArrayConstant(loop.getReceiver().asExpr(), _) and |
| 68 | + // Disregard cases where the looping is influenced by the query result |
| 69 | + not usedInLoopControlGuard(call) and |
| 70 | + // Only report calls that are likely to be expensive |
| 71 | + not call.getMethodName() in ["new", "create"] |
| 72 | +select call, |
| 73 | + "This call to a database query operation happens inside $@, and could be hoisted to a single call outside the loop.", |
| 74 | + loop, "this loop" |
0 commit comments