@@ -7,6 +7,25 @@ import swift
7
7
private import codeql.swift.dataflow.DataFlow
8
8
private import codeql.swift.dataflow.FlowSources
9
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
+
10
29
/**
11
30
* An imprecise flow source for an initializer call with a "contentsOf"
12
31
* argument that appears to be remote. For example:
@@ -15,14 +34,7 @@ private import codeql.swift.dataflow.FlowSources
15
34
* ```
16
35
*/
17
36
private class InitializerContentsOfRemoteSource extends RemoteFlowSource {
18
- InitializerContentsOfRemoteSource ( ) {
19
- exists ( InitializerCallExpr ce , Argument arg |
20
- ce .getAnArgument ( ) = arg and
21
- arg .getLabel ( ) = [ "contentsOf" , "contentsOfFile" , "contentsOfPath" , "contentsOfDirectory" ] and
22
- arg .getExpr ( ) .getType ( ) .getUnderlyingType ( ) .getName ( ) = [ "URL" , "NSURL" ] and
23
- this .asExpr ( ) = ce
24
- )
25
- }
37
+ InitializerContentsOfRemoteSource ( ) { contentsOfInitializer ( this .asExpr ( ) , true ) }
26
38
27
39
override string getSourceType ( ) { result = "contentsOf initializer" }
28
40
}
@@ -35,14 +47,7 @@ private class InitializerContentsOfRemoteSource extends RemoteFlowSource {
35
47
* ```
36
48
*/
37
49
private class InitializerContentsOfLocalSource extends LocalFlowSource {
38
- InitializerContentsOfLocalSource ( ) {
39
- exists ( InitializerCallExpr ce , Argument arg |
40
- ce .getAnArgument ( ) = arg and
41
- arg .getLabel ( ) = [ "contentsOf" , "contentsOfFile" , "contentsOfPath" , "contentsOfDirectory" ] and
42
- not arg .getExpr ( ) .getType ( ) .getUnderlyingType ( ) .getName ( ) = [ "URL" , "NSURL" ] and
43
- this .asExpr ( ) = ce
44
- )
45
- }
50
+ InitializerContentsOfLocalSource ( ) { contentsOfInitializer ( this .asExpr ( ) , false ) }
46
51
47
52
override string getSourceType ( ) { result = "contentsOf initializer" }
48
53
}
0 commit comments