|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for detecting |
| 3 | + * "use of a broken or weak cryptographic hashing algorithm on sensitive data" |
| 4 | + * vulnerabilities, as well as extension points for adding your own. |
| 5 | + */ |
| 6 | + |
| 7 | +private import ruby |
| 8 | +private import codeql.ruby.Concepts |
| 9 | +private import codeql.ruby.security.SensitiveActions |
| 10 | +private import codeql.ruby.dataflow.BarrierGuards |
| 11 | + |
| 12 | +/** |
| 13 | + * Provides default sources, sinks and sanitizers for detecting |
| 14 | + * "use of a broken or weak cryptographic hashing algorithm on sensitive data" |
| 15 | + * vulnerabilities on sensitive data that does NOT require computationally expensive |
| 16 | + * hashing, as well as extension points for adding your own. |
| 17 | + * |
| 18 | + * Also see the `ComputationallyExpensiveHashFunction` module. |
| 19 | + */ |
| 20 | +module NormalHashFunction { |
| 21 | + /** |
| 22 | + * A data flow source for "use of a broken or weak cryptographic hashing algorithm on |
| 23 | + * sensitive data" vulnerabilities. |
| 24 | + */ |
| 25 | + abstract class Source extends DataFlow::Node { |
| 26 | + Source() { not this instanceof ComputationallyExpensiveHashFunction::Source } |
| 27 | + |
| 28 | + /** Gets the classification of the sensitive data. */ |
| 29 | + abstract string getClassification(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * A data flow sink for "use of a broken or weak cryptographic hashing algorithm on |
| 34 | + * sensitive data" vulnerabilities. |
| 35 | + */ |
| 36 | + abstract class Sink extends DataFlow::Node { |
| 37 | + /** |
| 38 | + * Gets the name of the weak hashing algorithm. |
| 39 | + */ |
| 40 | + abstract string getAlgorithmName(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * A sanitizer for "use of a broken or weak cryptographic hashing algorithm on |
| 45 | + * sensitive data" vulnerabilities. |
| 46 | + */ |
| 47 | + abstract class Sanitizer extends DataFlow::Node { } |
| 48 | + |
| 49 | + /** |
| 50 | + * A source of sensitive data, considered as a flow source. |
| 51 | + */ |
| 52 | + class SensitiveNodeSourceAsSource extends Source instanceof SensitiveNode { |
| 53 | + override SensitiveDataClassification getClassification() { |
| 54 | + result = SensitiveNode.super.getClassification() |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** The input to a hashing operation using a weak algorithm, considered as a flow sink. */ |
| 59 | + class WeakHashingOperationInputSink extends Sink { |
| 60 | + Cryptography::HashingAlgorithm algorithm; |
| 61 | + |
| 62 | + WeakHashingOperationInputSink() { |
| 63 | + exists(Cryptography::CryptographicOperation operation | |
| 64 | + algorithm = operation.getAlgorithm() and |
| 65 | + algorithm.isWeak() and |
| 66 | + this = operation.getAnInput() |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + override string getAlgorithmName() { result = algorithm.getName() } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Provides default sources, sinks and sanitizers for detecting |
| 76 | + * "use of a broken or weak cryptographic hashing algorithm on sensitive data" |
| 77 | + * vulnerabilities on sensitive data that DOES require computationally expensive |
| 78 | + * hashing, as well as extension points for adding your own. |
| 79 | + * |
| 80 | + * Also see the `NormalHashFunction` module. |
| 81 | + */ |
| 82 | +module ComputationallyExpensiveHashFunction { |
| 83 | + /** |
| 84 | + * A data flow source of sensitive data that requires computationally expensive |
| 85 | + * hashing for "use of a broken or weak cryptographic hashing algorithm on sensitive |
| 86 | + * data" vulnerabilities. |
| 87 | + */ |
| 88 | + abstract class Source extends DataFlow::Node { |
| 89 | + /** Gets the classification of the sensitive data. */ |
| 90 | + abstract string getClassification(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * A data flow sink for sensitive data that requires computationally expensive |
| 95 | + * hashing for "use of a broken or weak cryptographic hashing algorithm on sensitive |
| 96 | + * data" vulnerabilities. |
| 97 | + */ |
| 98 | + abstract class Sink extends DataFlow::Node { |
| 99 | + /** |
| 100 | + * Gets the name of the weak hashing algorithm. |
| 101 | + */ |
| 102 | + abstract string getAlgorithmName(); |
| 103 | + |
| 104 | + /** |
| 105 | + * Holds if this sink is for a computationally expensive hash function (meaning that |
| 106 | + * hash function is just weak in some other regard. |
| 107 | + */ |
| 108 | + abstract predicate isComputationallyExpensive(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * A sanitizer of sensitive data that requires computationally expensive |
| 113 | + * hashing for "use of a broken or weak cryptographic hashing |
| 114 | + * algorithm on sensitive data" vulnerabilities. |
| 115 | + */ |
| 116 | + abstract class Sanitizer extends DataFlow::Node { } |
| 117 | + |
| 118 | + /** |
| 119 | + * A source of passwords, considered as a flow source. |
| 120 | + */ |
| 121 | + class PasswordSourceAsSource extends Source instanceof SensitiveNode { |
| 122 | + PasswordSourceAsSource() { |
| 123 | + this.(SensitiveNode).getClassification() = SensitiveDataClassification::password() |
| 124 | + } |
| 125 | + |
| 126 | + override SensitiveDataClassification getClassification() { |
| 127 | + result = SensitiveNode.super.getClassification() |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * The input to a password hashing operation using a weak algorithm, considered as a |
| 133 | + * flow sink. |
| 134 | + */ |
| 135 | + class WeakPasswordHashingOperationInputSink extends Sink { |
| 136 | + Cryptography::CryptographicAlgorithm algorithm; |
| 137 | + |
| 138 | + WeakPasswordHashingOperationInputSink() { |
| 139 | + ( |
| 140 | + algorithm instanceof Cryptography::PasswordHashingAlgorithm and |
| 141 | + algorithm.isWeak() |
| 142 | + or |
| 143 | + algorithm instanceof Cryptography::HashingAlgorithm // Note that HashingAlgorithm and PasswordHashingAlgorithm are disjoint |
| 144 | + ) and |
| 145 | + exists(Cryptography::CryptographicOperation operation | |
| 146 | + algorithm = operation.getAlgorithm() and |
| 147 | + this = operation.getAnInput() |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + override string getAlgorithmName() { result = algorithm.getName() } |
| 152 | + |
| 153 | + override predicate isComputationallyExpensive() { |
| 154 | + algorithm instanceof Cryptography::PasswordHashingAlgorithm |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments