|
| 1 | +/** |
| 2 | + * Provides heuristic models that match taint sources and flow through unknown |
| 3 | + * classes and libraries. |
| 4 | + */ |
| 5 | + |
| 6 | +import swift |
| 7 | +private import codeql.swift.dataflow.DataFlow |
| 8 | +private import codeql.swift.dataflow.FlowSources |
| 9 | + |
| 10 | +/** |
| 11 | + * An initializer call `ce` that has a "contentsOf" argument, along with a |
| 12 | + * guess `isRemote` as to whether it is the contents of a remote source. For |
| 13 | + * example: |
| 14 | + * ``` |
| 15 | + * let myObject = MyClass(contentsOf: url) // isRemote = true |
| 16 | + * let myObject = MyClass(contentsOfFile: "foo.txt") // isRemote = false |
| 17 | + * ``` |
| 18 | + */ |
| 19 | +private predicate contentsOfInitializer(InitializerCallExpr ce, boolean isRemote) { |
| 20 | + exists(Argument arg | |
| 21 | + ce.getAnArgument() = arg and |
| 22 | + arg.getLabel() = ["contentsOf", "contentsOfFile", "contentsOfPath", "contentsOfDirectory"] and |
| 23 | + if arg.getExpr().getType().getUnderlyingType().getName() = ["URL", "NSURL"] |
| 24 | + then isRemote = true |
| 25 | + else isRemote = false |
| 26 | + ) |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * An imprecise flow source for an initializer call with a "contentsOf" |
| 31 | + * argument that appears to be remote. For example: |
| 32 | + * ``` |
| 33 | + * let myObject = MyClass(contentsOf: url) |
| 34 | + * ``` |
| 35 | + */ |
| 36 | +private class InitializerContentsOfRemoteSource extends RemoteFlowSource { |
| 37 | + InitializerContentsOfRemoteSource() { contentsOfInitializer(this.asExpr(), true) } |
| 38 | + |
| 39 | + override string getSourceType() { result = "contentsOf initializer" } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * An imprecise flow source for an initializer call with a "contentsOf" |
| 44 | + * argument that appears to be local. For example: |
| 45 | + * ``` |
| 46 | + * let myObject = MyClass(contentsOfFile: "foo.txt") |
| 47 | + * ``` |
| 48 | + */ |
| 49 | +private class InitializerContentsOfLocalSource extends LocalFlowSource { |
| 50 | + InitializerContentsOfLocalSource() { contentsOfInitializer(this.asExpr(), false) } |
| 51 | + |
| 52 | + override string getSourceType() { result = "contentsOf initializer" } |
| 53 | +} |
0 commit comments