|
11 | 11 |
|
12 | 12 | private import codeql.ruby.AST |
13 | 13 | private import codeql.ruby.DataFlow |
| 14 | +import codeql.ruby.security.internal.SensitiveDataHeuristics |
| 15 | +private import HeuristicNames |
| 16 | +private import codeql.ruby.CFG |
| 17 | + |
| 18 | +/** An expression that might contain sensitive data. */ |
| 19 | +cached |
| 20 | +abstract class SensitiveNode extends DataFlow::Node { |
| 21 | + /** Gets a human-readable description of this expression for use in alert messages. */ |
| 22 | + cached |
| 23 | + abstract string describe(); |
| 24 | + |
| 25 | + /** Gets a classification of the kind of sensitive data this expression might contain. */ |
| 26 | + cached |
| 27 | + abstract SensitiveDataClassification getClassification(); |
| 28 | +} |
| 29 | + |
| 30 | +/** A method call that might produce sensitive data. */ |
| 31 | +class SensitiveCall extends SensitiveNode instanceof DataFlow::CallNode { |
| 32 | + SensitiveDataClassification classification; |
| 33 | + |
| 34 | + SensitiveCall() { |
| 35 | + classification = this.getMethodName().(SensitiveDataMethodName).getClassification() |
| 36 | + or |
| 37 | + // This is particularly to pick up methods with an argument like "password", which |
| 38 | + // may indicate a lookup. |
| 39 | + exists(string s | super.getArgument(_).asExpr().getConstantValue().isStringlikeValue(s) | |
| 40 | + nameIndicatesSensitiveData(s, classification) |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + override string describe() { result = "a call to " + super.getMethodName() } |
| 45 | + |
| 46 | + override SensitiveDataClassification getClassification() { result = classification } |
| 47 | +} |
| 48 | + |
| 49 | +/** An access to a variable or hash value that might contain sensitive data. */ |
| 50 | +abstract class SensitiveVariableAccess extends SensitiveNode { |
| 51 | + string name; |
| 52 | + |
| 53 | + SensitiveVariableAccess() { |
| 54 | + this.asExpr().(CfgNodes::ExprNodes::VariableAccessCfgNode).getExpr().getVariable().hasName(name) |
| 55 | + or |
| 56 | + this.asExpr() |
| 57 | + .(CfgNodes::ExprNodes::ElementReferenceCfgNode) |
| 58 | + .getAnArgument() |
| 59 | + .getConstantValue() |
| 60 | + .isStringlikeValue(name) |
| 61 | + } |
| 62 | + |
| 63 | + override string describe() { result = "an access to " + name } |
| 64 | +} |
| 65 | + |
| 66 | +/** A write to a location that might contain sensitive data. */ |
| 67 | +abstract class SensitiveWrite extends DataFlow::Node { } |
| 68 | + |
| 69 | +/** |
| 70 | + * Holds if `node` is a write to a variable or hash value named `name`. |
| 71 | + * |
| 72 | + * Helper predicate factored out for performance, |
| 73 | + * to filter `name` as much as possible before using it in |
| 74 | + * regex matching. |
| 75 | + */ |
| 76 | +pragma[nomagic] |
| 77 | +private predicate writesProperty(DataFlow::Node node, string name) { |
| 78 | + exists(VariableWriteAccess vwa | vwa.getVariable().getName() = name | |
| 79 | + node.asExpr().getExpr() = vwa |
| 80 | + ) |
| 81 | + or |
| 82 | + // hash value assignment |
| 83 | + node.(DataFlow::CallNode).getMethodName() = "[]=" and |
| 84 | + node.(DataFlow::CallNode).getArgument(0).asExpr().getConstantValue().isStringlikeValue(name) |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Instance and class variable names are reported with their respective `@` |
| 89 | + * and `@@` prefixes. This predicate strips these prefixes. |
| 90 | + */ |
| 91 | +bindingset[name] |
| 92 | +private string unprefixedVariableName(string name) { result = name.regexpReplaceAll("^@*", "") } |
| 93 | + |
| 94 | +/** A write to a variable or property that might contain sensitive data. */ |
| 95 | +private class BasicSensitiveWrite extends SensitiveWrite { |
| 96 | + SensitiveDataClassification classification; |
| 97 | + |
| 98 | + BasicSensitiveWrite() { |
| 99 | + exists(string name | |
| 100 | + /* |
| 101 | + * PERFORMANCE OPTIMISATION: |
| 102 | + * `nameIndicatesSensitiveData` performs a `regexpMatch` on `name`. |
| 103 | + * To carry out a regex match, we must first compute the Cartesian product |
| 104 | + * of all possible `name`s and regexes, then match. |
| 105 | + * To keep this product as small as possible, |
| 106 | + * we want to filter `name` as much as possible before the product. |
| 107 | + * |
| 108 | + * Do this by factoring out a helper predicate containing the filtering |
| 109 | + * logic that restricts `name`. This helper predicate will get picked first |
| 110 | + * in the join order, since it is the only call here that binds `name`. |
| 111 | + */ |
| 112 | + |
| 113 | + writesProperty(this, name) and |
| 114 | + nameIndicatesSensitiveData(unprefixedVariableName(name), classification) |
| 115 | + ) |
| 116 | + } |
| 117 | + |
| 118 | + /** Gets a classification of the kind of sensitive data the write might handle. */ |
| 119 | + SensitiveDataClassification getClassification() { result = classification } |
| 120 | +} |
| 121 | + |
| 122 | +/** An access to a variable or hash value that might contain sensitive data. */ |
| 123 | +private class BasicSensitiveVariableAccess extends SensitiveVariableAccess { |
| 124 | + SensitiveDataClassification classification; |
| 125 | + |
| 126 | + BasicSensitiveVariableAccess() { |
| 127 | + nameIndicatesSensitiveData(unprefixedVariableName(name), classification) |
| 128 | + } |
| 129 | + |
| 130 | + override SensitiveDataClassification getClassification() { result = classification } |
| 131 | +} |
| 132 | + |
| 133 | +/** A method name that suggests it may be sensitive. */ |
| 134 | +abstract class SensitiveMethodName extends string { |
| 135 | + SensitiveMethodName() { this = any(MethodBase m).getName() } |
| 136 | +} |
| 137 | + |
| 138 | +/** A method name that suggests it may produce sensitive data. */ |
| 139 | +abstract class SensitiveDataMethodName extends SensitiveMethodName { |
| 140 | + /** Gets a classification of the kind of sensitive data this method may produce. */ |
| 141 | + abstract SensitiveDataClassification getClassification(); |
| 142 | +} |
| 143 | + |
| 144 | +/** A method name that might return sensitive credential data. */ |
| 145 | +class CredentialsMethodName extends SensitiveDataMethodName { |
| 146 | + SensitiveDataClassification classification; |
| 147 | + |
| 148 | + CredentialsMethodName() { nameIndicatesSensitiveData(this, classification) } |
| 149 | + |
| 150 | + override SensitiveDataClassification getClassification() { result = classification } |
| 151 | +} |
14 | 152 |
|
15 | 153 | /** |
16 | 154 | * A sensitive action, such as transfer of sensitive data. |
|
0 commit comments