|
| 1 | +/** |
| 2 | + * @name Unsafe HMAC Comparison |
| 3 | + * @description An HMAC is being compared using the equality operator. This may be vulnerable to a cryptographic timing attack |
| 4 | + * because the equality operation does not occur in constant time." |
| 5 | + * @kind path-problem |
| 6 | + * @problem.severity error |
| 7 | + * @security-severity 6.0 |
| 8 | + * @precision high |
| 9 | + * @id rb/unsafe-hmac-comparison |
| 10 | + * @tags security |
| 11 | + * external/cwe/cwe-208 |
| 12 | + */ |
| 13 | + |
| 14 | +private import codeql.ruby.AST |
| 15 | +private import codeql.ruby.DataFlow |
| 16 | +private import codeql.ruby.ApiGraphs |
| 17 | + |
| 18 | +private class OpenSslHmacSource extends DataFlow::Node { |
| 19 | + OpenSslHmacSource() { |
| 20 | + exists(API::Node hmacNode | hmacNode = API::getTopLevelMember("OpenSSL").getMember("HMAC") | |
| 21 | + this = hmacNode.getAMethodCall(["hexdigest", "to_s", "digest", "base64digest"]) |
| 22 | + or |
| 23 | + this = hmacNode.getAnInstantiation() |
| 24 | + ) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +private module UnsafeHmacComparison { |
| 29 | + private module Config implements DataFlow::ConfigSig { |
| 30 | + predicate isSource(DataFlow::Node source) { source instanceof OpenSslHmacSource } |
| 31 | + |
| 32 | + // Holds if a given sink is an Equality Operation (== or !=) |
| 33 | + predicate isSink(DataFlow::Node sink) { |
| 34 | + any(EqualityOperation eqOp).getAnOperand() = sink.asExpr().getExpr() |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + import DataFlow::Global<Config> |
| 39 | +} |
| 40 | + |
| 41 | +private import UnsafeHmacComparison::PathGraph |
| 42 | + |
| 43 | +from UnsafeHmacComparison::PathNode source, UnsafeHmacComparison::PathNode sink |
| 44 | +where UnsafeHmacComparison::flowPath(source, sink) |
| 45 | +select sink.getNode(), source, sink, "This comparison is potentially vulnerable to a timing attack." |
0 commit comments