|
| 1 | +/** |
| 2 | + * Provides a simple base test for flow-related tests using inline expectations. |
| 3 | + * |
| 4 | + * Example for a test.ql: |
| 5 | + * ```ql |
| 6 | + * import java |
| 7 | + * import TestUtilities.InlineFlowTest |
| 8 | + * ``` |
| 9 | + * |
| 10 | + * To declare expecations, you can use the $hasTaintFlow or $hasValueFlow comments within the test source files. |
| 11 | + * Example of the corresponding test file, e.g. Test.java |
| 12 | + * ```java |
| 13 | + * public class Test { |
| 14 | + * |
| 15 | + * Object source() { return null; } |
| 16 | + * String taint() { return null; } |
| 17 | + * void sink(Object o) { } |
| 18 | + * |
| 19 | + * public void test() { |
| 20 | + * Object s = source(); |
| 21 | + * sink(s); //$hasValueFlow |
| 22 | + * String t = "foo" + taint(); |
| 23 | + * sink(t); //$hasTaintFlow |
| 24 | + * } |
| 25 | + * |
| 26 | + * } |
| 27 | + * ``` |
| 28 | + * |
| 29 | + * If you're not interested in a specific flow type, you can disable either value or taint flow expectations as follows: |
| 30 | + * ```ql |
| 31 | + * class HasFlowTest extends InlineFlowTest { |
| 32 | + * override DataFlow::Configuration getTaintFlowConfig() { none() } |
| 33 | + * |
| 34 | + * override DataFlow::Configuration getValueFlowConfig() { none() } |
| 35 | + * } |
| 36 | + * ``` |
| 37 | + * |
| 38 | + * If you need more fine-grained tuning, consider implementing a test using `InlineExpectationsTest`. |
| 39 | + */ |
| 40 | + |
| 41 | +import semmle.code.java.dataflow.DataFlow |
| 42 | +import semmle.code.java.dataflow.ExternalFlow |
| 43 | +import semmle.code.java.dataflow.TaintTracking |
| 44 | +import TestUtilities.InlineExpectationsTest |
| 45 | + |
| 46 | +class DefaultValueFlowConf extends DataFlow::Configuration { |
| 47 | + DefaultValueFlowConf() { this = "qltest:defaultValueFlowConf" } |
| 48 | + |
| 49 | + override predicate isSource(DataFlow::Node n) { |
| 50 | + n.asExpr().(MethodAccess).getMethod().getName() = ["source", "taint"] |
| 51 | + } |
| 52 | + |
| 53 | + override predicate isSink(DataFlow::Node n) { |
| 54 | + exists(MethodAccess ma | ma.getMethod().hasName("sink") | n.asExpr() = ma.getAnArgument()) |
| 55 | + } |
| 56 | + |
| 57 | + override int fieldFlowBranchLimit() { result = 1000 } |
| 58 | +} |
| 59 | + |
| 60 | +class DefaultTaintFlowConf extends TaintTracking::Configuration { |
| 61 | + DefaultTaintFlowConf() { this = "qltest:defaultTaintFlowConf" } |
| 62 | + |
| 63 | + override predicate isSource(DataFlow::Node n) { |
| 64 | + n.asExpr().(MethodAccess).getMethod().getName() = ["source", "taint"] |
| 65 | + } |
| 66 | + |
| 67 | + override predicate isSink(DataFlow::Node n) { |
| 68 | + exists(MethodAccess ma | ma.getMethod().hasName("sink") | n.asExpr() = ma.getAnArgument()) |
| 69 | + } |
| 70 | + |
| 71 | + override int fieldFlowBranchLimit() { result = 1000 } |
| 72 | +} |
| 73 | + |
| 74 | +class InlineFlowTest extends InlineExpectationsTest { |
| 75 | + InlineFlowTest() { this = "HasFlowTest" } |
| 76 | + |
| 77 | + override string getARelevantTag() { result = ["hasValueFlow", "hasTaintFlow"] } |
| 78 | + |
| 79 | + override predicate hasActualResult(Location location, string element, string tag, string value) { |
| 80 | + tag = "hasValueFlow" and |
| 81 | + exists(DataFlow::Node src, DataFlow::Node sink | getValueFlowConfig().hasFlow(src, sink) | |
| 82 | + sink.getLocation() = location and |
| 83 | + element = sink.toString() and |
| 84 | + value = "" |
| 85 | + ) |
| 86 | + or |
| 87 | + tag = "hasTaintFlow" and |
| 88 | + exists(DataFlow::Node src, DataFlow::Node sink | |
| 89 | + getTaintFlowConfig().hasFlow(src, sink) and not getValueFlowConfig().hasFlow(src, sink) |
| 90 | + | |
| 91 | + sink.getLocation() = location and |
| 92 | + element = sink.toString() and |
| 93 | + value = "" |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + DataFlow::Configuration getValueFlowConfig() { result = any(DefaultValueFlowConf config) } |
| 98 | + |
| 99 | + DataFlow::Configuration getTaintFlowConfig() { result = any(DefaultTaintFlowConf config) } |
| 100 | +} |
0 commit comments