|
| 1 | +/** |
| 2 | + * @name Uncontrolled data used in path expression |
| 3 | + * @description Accessing paths influenced by users can allow an attacker to access unexpected resources. |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity error |
| 6 | + * @security-severity 7.5 |
| 7 | + * @precision high |
| 8 | + * @id unified/swift/path-injection |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-022 |
| 11 | + * external/cwe/cwe-023 |
| 12 | + * external/cwe/cwe-036 |
| 13 | + * external/cwe/cwe-073 |
| 14 | + * external/cwe/cwe-099 |
| 15 | + */ |
| 16 | + |
| 17 | +import unified |
| 18 | + |
| 19 | +/** |
| 20 | + * A string that might be a label for a path argument. |
| 21 | + */ |
| 22 | +pragma[inline] |
| 23 | +private predicate pathLikeHeuristic(string label) { |
| 24 | + label = |
| 25 | + [ |
| 26 | + "atFile", "atPath", "atDirectory", "toFile", "toPath", "toDirectory", "inFile", "inPath", |
| 27 | + "inDirectory", "contentsOfFile", "contentsOfPath", "contentsOfDirectory", "filePath", |
| 28 | + "directory", "directoryPath" |
| 29 | + ] |
| 30 | +} |
| 31 | + |
| 32 | +predicate heuristicSink(DataFlow::Node node) { |
| 33 | + node.isIncomingValue(any(Identifier id | id.getValue() = "sqlite3_temp_directory")) |
| 34 | + or |
| 35 | + exists(Argument arg | |
| 36 | + pathLikeHeuristic(arg.getName()) and |
| 37 | + node.asExpr() = arg.getValue() |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +module PathInjectionConfig implements DataFlow::ConfigSig { |
| 42 | + predicate isSource(DataFlow::Node node) { Models::isSource(node, _) } |
| 43 | + |
| 44 | + predicate isSink(DataFlow::Node node) { |
| 45 | + Models::isSink(node, "path-injection") or |
| 46 | + heuristicSink(node) |
| 47 | + } |
| 48 | + |
| 49 | + predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) { none() } |
| 50 | + |
| 51 | + predicate isBarrier(DataFlow::Node node) { |
| 52 | + // TODO: add barriers |
| 53 | + none() |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +module PathInjectionFlow = DataFlow::Global<PathInjectionConfig>; |
| 58 | + |
| 59 | +import PathInjectionFlow::PathGraph |
| 60 | + |
| 61 | +from PathInjectionFlow::PathNode source, PathInjectionFlow::PathNode sink |
| 62 | +where PathInjectionFlow::flowPath(source, sink) |
| 63 | +select sink.getNode(), source, sink, "This path depends on a $@.", source.getNode(), |
| 64 | + "user-provided value" |
0 commit comments