|
| 1 | +/** |
| 2 | + * @name User-controlled bypass of security check |
| 3 | + * @description Conditions controlled by the user are not suited for making security-related decisions. |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity error |
| 6 | + * @security-severity 7.8 |
| 7 | + * @precision medium |
| 8 | + * @id rb/user-controlled-bypass |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-807 |
| 11 | + * external/cwe/cwe-290 |
| 12 | + */ |
| 13 | + |
| 14 | +import ruby |
| 15 | +import codeql.ruby.DataFlow |
| 16 | +import codeql.ruby.dataflow.internal.DataFlowPublic |
| 17 | +import codeql.ruby.security.ConditionalBypassQuery |
| 18 | +import codeql.ruby.security.SensitiveActions |
| 19 | +import DataFlow::PathGraph |
| 20 | + |
| 21 | +/** |
| 22 | + * Holds if the value of `nd` flows into `guard`. |
| 23 | + */ |
| 24 | +predicate flowsToGuardExpr(DataFlow::Node nd, SensitiveActionGuardConditional guard) { |
| 25 | + nd = guard |
| 26 | + or |
| 27 | + exists(DataFlow::Node succ | localFlowStep(nd, succ) | flowsToGuardExpr(succ, guard)) |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * A comparison that guards a sensitive action, e.g. the comparison in: |
| 32 | + * ```rb |
| 33 | + * ok = x == y |
| 34 | + * if ok |
| 35 | + * login |
| 36 | + * end |
| 37 | + * ``` |
| 38 | + */ |
| 39 | +class SensitiveActionGuardComparison extends ComparisonOperation { |
| 40 | + SensitiveActionGuardConditional guard; |
| 41 | + |
| 42 | + SensitiveActionGuardComparison() { |
| 43 | + exists(DataFlow::Node node | this = node.asExpr().getExpr() | flowsToGuardExpr(node, guard)) |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the guard that uses this comparison. |
| 48 | + */ |
| 49 | + SensitiveActionGuardConditional getGuard() { result = guard } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * An intermediary sink to enable reuse of the taint configuration. |
| 54 | + * This sink should not be presented to the client of this query. |
| 55 | + */ |
| 56 | +class SensitiveActionGuardComparisonOperand extends Sink { |
| 57 | + SensitiveActionGuardComparison comparison; |
| 58 | + |
| 59 | + SensitiveActionGuardComparisonOperand() { this.asExpr().getExpr() = comparison.getAnOperand() } |
| 60 | + |
| 61 | + override SensitiveAction getAction() { result = comparison.getGuard().getAction() } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Holds if `sink` guards `action`, and `source` taints `sink`. |
| 66 | + * |
| 67 | + * If flow from `source` taints `sink`, then an attacker can |
| 68 | + * control if `action` should be executed or not. |
| 69 | + */ |
| 70 | +predicate isTaintedGuardForSensitiveAction( |
| 71 | + DataFlow::PathNode sink, DataFlow::PathNode source, SensitiveAction action |
| 72 | +) { |
| 73 | + action = sink.getNode().(Sink).getAction() and |
| 74 | + // exclude the intermediary sink |
| 75 | + not sink.getNode() instanceof SensitiveActionGuardComparisonOperand and |
| 76 | + exists(Configuration cfg | cfg.hasFlowPath(source, sink)) |
| 77 | +} |
| 78 | + |
| 79 | +from DataFlow::PathNode source, DataFlow::PathNode sink, SensitiveAction action |
| 80 | +where isTaintedGuardForSensitiveAction(sink, source, action) |
| 81 | +select sink.getNode(), source, sink, "This condition guards a sensitive $@, but $@ controls it.", |
| 82 | + action, "action", source.getNode(), "a user-provided value" |
0 commit comments