|
| 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 swift |
| 7 | + * import TestUtilities.InlineFlowTest |
| 8 | + * import DefaultFlowTest |
| 9 | + * import PathGraph |
| 10 | + * |
| 11 | + * from PathNode source, PathNode sink |
| 12 | + * where flowPath(source, sink) |
| 13 | + * select sink, source, sink, "$@", source, source.toString() |
| 14 | + * ``` |
| 15 | + * |
| 16 | + * To declare expectations, you can use the $hasTaintFlow or $hasValueFlow comments within the test source files. |
| 17 | + * Example of the corresponding test file, e.g. Test.java |
| 18 | + * ```swift |
| 19 | + * func source() -> Any { return nil } |
| 20 | + * func taint() -> Any { return nil } |
| 21 | + * func sink(_ o: Any) { } |
| 22 | + * |
| 23 | + * func test() { |
| 24 | + * let s = source() |
| 25 | + * sink(s) // $ hasValueFlow |
| 26 | + * let t = "foo" + taint() |
| 27 | + * sink(t); // $ hasTaintFlow |
| 28 | + * } |
| 29 | + * ``` |
| 30 | + * |
| 31 | + * If you are only interested in value flow, then instead of importing `DefaultFlowTest`, you can import |
| 32 | + * `ValueFlowTest<DefaultFlowConfig>`. Similarly, if you are only interested in taint flow, then instead of |
| 33 | + * importing `DefaultFlowTest`, you can import `TaintFlowTest<DefaultFlowConfig>`. In both cases |
| 34 | + * `DefaultFlowConfig` can be replaced by another implementation of `DataFlow::ConfigSig`. |
| 35 | + * |
| 36 | + * If you need more fine-grained tuning, consider implementing a test using `InlineExpectationsTest`. |
| 37 | + */ |
| 38 | + |
| 39 | +import codeql.swift.dataflow.DataFlow |
| 40 | +import codeql.swift.dataflow.ExternalFlow |
| 41 | +import codeql.swift.dataflow.TaintTracking |
| 42 | +import TestUtilities.InlineExpectationsTest |
| 43 | + |
| 44 | +private predicate defaultSource(DataFlow::Node source) { |
| 45 | + source.asExpr().(MethodCallExpr).getStaticTarget().getName() = ["source", "taint"] |
| 46 | +} |
| 47 | + |
| 48 | +private predicate defaultSink(DataFlow::Node sink) { |
| 49 | + exists(MethodCallExpr ma | ma.getStaticTarget().hasName("sink") | |
| 50 | + sink.asExpr() = ma.getAnArgument().getExpr() |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +module DefaultFlowConfig implements DataFlow::ConfigSig { |
| 55 | + predicate isSource(DataFlow::Node source) { defaultSource(source) } |
| 56 | + |
| 57 | + predicate isSink(DataFlow::Node sink) { defaultSink(sink) } |
| 58 | + |
| 59 | + int fieldFlowBranchLimit() { result = 1000 } |
| 60 | +} |
| 61 | + |
| 62 | +private module NoFlowConfig implements DataFlow::ConfigSig { |
| 63 | + predicate isSource(DataFlow::Node source) { none() } |
| 64 | + |
| 65 | + predicate isSink(DataFlow::Node sink) { none() } |
| 66 | +} |
| 67 | + |
| 68 | +private string getSourceArgString(DataFlow::Node src) { |
| 69 | + defaultSource(src) and |
| 70 | + src.asExpr().(MethodCallExpr).getAnArgument().getExpr().(StringLiteralExpr).getValue() = result |
| 71 | +} |
| 72 | + |
| 73 | +module FlowTest<DataFlow::ConfigSig ValueFlowConfig, DataFlow::ConfigSig TaintFlowConfig> { |
| 74 | + module ValueFlow = DataFlow::Global<ValueFlowConfig>; |
| 75 | + |
| 76 | + module TaintFlow = TaintTracking::Global<TaintFlowConfig>; |
| 77 | + |
| 78 | + private module InlineTest implements TestSig { |
| 79 | + string getARelevantTag() { result = ["hasValueFlow", "hasTaintFlow"] } |
| 80 | + |
| 81 | + predicate hasActualResult(Location location, string element, string tag, string value) { |
| 82 | + tag = "hasValueFlow" and |
| 83 | + exists(DataFlow::Node src, DataFlow::Node sink | ValueFlow::flow(src, sink) | |
| 84 | + sink.getLocation() = location and |
| 85 | + element = sink.toString() and |
| 86 | + if exists(getSourceArgString(src)) then value = getSourceArgString(src) else value = "" |
| 87 | + ) |
| 88 | + or |
| 89 | + tag = "hasTaintFlow" and |
| 90 | + exists(DataFlow::Node src, DataFlow::Node sink | |
| 91 | + TaintFlow::flow(src, sink) and not ValueFlow::flow(src, sink) |
| 92 | + | |
| 93 | + sink.getLocation() = location and |
| 94 | + element = sink.toString() and |
| 95 | + if exists(getSourceArgString(src)) then value = getSourceArgString(src) else value = "" |
| 96 | + ) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + import MakeTest<InlineTest> |
| 101 | + import DataFlow::MergePathGraph<ValueFlow::PathNode, TaintFlow::PathNode, ValueFlow::PathGraph, TaintFlow::PathGraph> |
| 102 | + |
| 103 | + predicate flowPath(PathNode source, PathNode sink) { |
| 104 | + ValueFlow::flowPath(source.asPathNode1(), sink.asPathNode1()) or |
| 105 | + TaintFlow::flowPath(source.asPathNode2(), sink.asPathNode2()) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +module DefaultFlowTest = FlowTest<DefaultFlowConfig, DefaultFlowConfig>; |
| 110 | + |
| 111 | +module ValueFlowTest<DataFlow::ConfigSig ValueFlowConfig> { |
| 112 | + import FlowTest<ValueFlowConfig, NoFlowConfig> |
| 113 | +} |
| 114 | + |
| 115 | +module TaintFlowTest<DataFlow::ConfigSig TaintFlowConfig> { |
| 116 | + import FlowTest<NoFlowConfig, TaintFlowConfig> |
| 117 | +} |
0 commit comments