|
| 1 | +import java |
| 2 | +import semmle.code.java.dataflow.FlowSources |
| 3 | +import semmle.code.java.dataflow.TaintTracking |
| 4 | +import DataFlow::PathGraph |
| 5 | + |
| 6 | +private class NonConstantTimeEqualsCall extends MethodAccess { |
| 7 | + NonConstantTimeEqualsCall() { |
| 8 | + this.getMethod().hasQualifiedName("java.lang", "String", ["equals", "contentEquals", "equalsIgnoreCase"]) or |
| 9 | + this.getMethod().hasQualifiedName("java.nio", "ByteBuffer", ["equals", "compareTo"]) |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +private predicate isNonConstantTimeEqualsCall(Expr firstObject, Expr secondObject) { |
| 14 | + exists(NonConstantTimeEqualsCall call | |
| 15 | + firstObject = call.getQualifier() and |
| 16 | + secondObject = call.getAnArgument() |
| 17 | + or |
| 18 | + firstObject = call.getAnArgument() and |
| 19 | + secondObject = call.getQualifier() |
| 20 | + ) |
| 21 | +} |
| 22 | +class NonConstantTimeComparisonSink extends DataFlow::Node { |
| 23 | + Expr anotherParameter; |
| 24 | + NonConstantTimeComparisonSink() { |
| 25 | + isNonConstantTimeEqualsCall(this.asExpr(), anotherParameter) |
| 26 | + } |
| 27 | +} |
| 28 | +class ClientSuppliedIpTokenCheck extends DataFlow::Node { |
| 29 | + ClientSuppliedIpTokenCheck() { |
| 30 | + exists(MethodAccess ma | |
| 31 | + ma.getMethod().hasName("getHeader") and |
| 32 | + ma.getArgument(0).(CompileTimeConstantExpr).getStringValue().toLowerCase() in [ |
| 33 | + "x-auth-token", "x-csrf-token", "http_x_csrf_token", "x-csrf-param", "x-csrf-header", |
| 34 | + "http_x_csrf_token" |
| 35 | + ] and |
| 36 | + ma = this.asExpr() |
| 37 | + ) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +class NonConstantTimeComparisonConfig extends TaintTracking::Configuration { |
| 42 | + NonConstantTimeComparisonConfig() { this = "NonConstantTimeCryptoComparisonConfig" } |
| 43 | + |
| 44 | + override predicate isSource(DataFlow::Node source) { source instanceof ClientSuppliedIpTokenCheck } |
| 45 | + |
| 46 | + override predicate isSink(DataFlow::Node sink) { sink instanceof NonConstantTimeComparisonSink } |
| 47 | +} |
| 48 | + |
| 49 | +from DataFlow::PathNode source, DataFlow::PathNode sink, NonConstantTimeComparisonConfig conf |
| 50 | +where conf.hasFlowPath(source, sink) |
| 51 | +select sink.getNode(), source, sink, "Possible timing attack against $@ validation.", source, |
| 52 | + source.getNode() |
0 commit comments