Skip to content

Commit bc4724b

Browse files
committed
Swift: Test the customurlschemes fields that inherit taint.
1 parent 1c8297b commit bc4724b

File tree

2 files changed

+110
-13
lines changed

2 files changed

+110
-13
lines changed

swift/ql/test/library-tests/dataflow/flowsources/FlowSourcesInline.ql

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import swift
22
import TestUtilities.InlineExpectationsTest
33
import FlowConfig
4+
import codeql.swift.dataflow.TaintTracking
5+
import codeql.swift.dataflow.DataFlow
6+
7+
module TaintReachConfiguration implements DataFlow::ConfigSig {
8+
predicate isSource(DataFlow::Node src) { src instanceof FlowSource }
9+
10+
predicate isSink(DataFlow::Node sink) { any() }
11+
}
12+
13+
module TaintReachFlow = TaintTracking::Global<TaintReachConfiguration>;
414

515
string describe(FlowSource source) {
616
source instanceof RemoteFlowSource and result = "remote"
@@ -9,7 +19,7 @@ string describe(FlowSource source) {
919
}
1020

1121
module FlowSourcesTest implements TestSig {
12-
string getARelevantTag() { result = "source" }
22+
string getARelevantTag() { result = ["source", "tainted"] }
1323

1424
predicate hasActualResult(Location location, string element, string tag, string value) {
1525
exists(FlowSource source |
@@ -20,6 +30,19 @@ module FlowSourcesTest implements TestSig {
2030
value = describe(source)
2131
)
2232
}
33+
34+
predicate hasOptionalResult(Location location, string element, string tag, string value) {
35+
// this is not really what the "flowsources" test is about, but sometimes it's helpful to
36+
// confirm that taint reaches certain obvious points in the flow source test code.
37+
exists(DataFlow::Node n |
38+
TaintReachFlow::flowTo(n) and
39+
location = n.getLocation() and
40+
location.getFile().getBaseName() != "" and
41+
element = n.toString() and
42+
tag = "tainted" and
43+
value = ""
44+
)
45+
}
2346
}
2447

2548
import MakeTest<FlowSourcesTest>

swift/ql/test/library-tests/dataflow/flowsources/customurlschemes.swift

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,33 @@ protocol UIApplicationDelegate {
2626
}
2727

2828
class UIScene {
29-
class ConnectionOptions {}
29+
class ConnectionOptions {
30+
var userActivities: Set<NSUserActivity> { get { return Set() } }
31+
var urlContexts: Set<UIOpenURLContext> { get { return Set() } }
32+
}
3033
}
3134

3235
class UISceneSession {}
3336

34-
class NSUserActivity {}
37+
class NSUserActivity: Hashable {
38+
static func == (lhs: NSUserActivity, rhs: NSUserActivity) -> Bool {
39+
return true;
40+
}
41+
42+
func hash(into hasher: inout Hasher) {}
43+
44+
var webpageURL: URL? { get { return nil } set { } }
45+
var referrerURL: URL? { get { return nil } set { } }
46+
}
3547

3648
class UIOpenURLContext: Hashable {
3749
static func == (lhs: UIOpenURLContext, rhs: UIOpenURLContext) -> Bool {
3850
return true;
3951
}
4052

4153
func hash(into hasher: inout Hasher) {}
54+
55+
var url: URL { get { return URL() } }
4256
}
4357

4458
protocol UISceneDelegate {
@@ -64,28 +78,88 @@ class AppDelegate: UIApplicationDelegate {
6478
}
6579

6680
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
67-
launchOptions?[.url] // $ source=remote
81+
_ = launchOptions?[.url] // $ source=remote
6882
return true
6983
}
7084

7185
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
72-
launchOptions?[.url] // $ source=remote
86+
_ = launchOptions?[.url] // $ source=remote
7387
return true
7488
}
7589
}
7690

7791
class SceneDelegate : UISceneDelegate {
78-
func scene(_: UIScene, willConnectTo: UISceneSession, options: UIScene.ConnectionOptions) {} // $ source=remote
79-
func scene(_: UIScene, continue: NSUserActivity) {} // $ source=remote
80-
func scene(_: UIScene, didUpdate: NSUserActivity) {} // $ source=remote
81-
func scene(_: UIScene, openURLContexts: Set<UIOpenURLContext>) {} // $ source=remote
92+
func scene(_: UIScene, willConnectTo: UISceneSession, options: UIScene.ConnectionOptions) { // $ source=remote
93+
for userActivity in options.userActivities {
94+
let x = userActivity.webpageURL
95+
x // $ MISSING: tainted
96+
let y = userActivity.referrerURL
97+
y // $ MISSING: tainted
98+
}
99+
100+
for urlContext in options.urlContexts {
101+
let z = urlContext.url
102+
z // $ MISSING: tainted
103+
}
104+
}
105+
106+
func scene(_: UIScene, continue: NSUserActivity) { // $ source=remote
107+
let x = `continue`.webpageURL
108+
x // $ tainted
109+
let y = `continue`.referrerURL
110+
y // $ MISSING: tainted
111+
}
112+
113+
func scene(_: UIScene, didUpdate: NSUserActivity) { // $ source=remote
114+
let x = didUpdate.webpageURL
115+
x // $ tainted
116+
let y = didUpdate.referrerURL
117+
y // $ MISSING: tainted
118+
}
119+
120+
func scene(_: UIScene, openURLContexts: Set<UIOpenURLContext>) { // $ source=remote
121+
for openURLContext in openURLContexts {
122+
let x = openURLContext.url
123+
x // $ MISSING: tainted
124+
}
125+
}
82126
}
83127

84128
class Extended {}
85129

86130
extension Extended : UISceneDelegate {
87-
func scene(_: UIScene, willConnectTo: UISceneSession, options: UIScene.ConnectionOptions) {} // $ source=remote
88-
func scene(_: UIScene, continue: NSUserActivity) {} // $ source=remote
89-
func scene(_: UIScene, didUpdate: NSUserActivity) {} // $ source=remote
90-
func scene(_: UIScene, openURLContexts: Set<UIOpenURLContext>) {} // $ source=remote
131+
func scene(_: UIScene, willConnectTo: UISceneSession, options: UIScene.ConnectionOptions) { // $ source=remote
132+
for userActivity in options.userActivities {
133+
let x = userActivity.webpageURL
134+
x // $ MISSING: tainted
135+
let y = userActivity.referrerURL
136+
y // $ MISSING: tainted
137+
}
138+
139+
for urlContext in options.urlContexts {
140+
let z = urlContext.url
141+
z // $ MISSING: tainted
142+
}
143+
}
144+
145+
func scene(_: UIScene, continue: NSUserActivity) { // $ source=remote
146+
let x = `continue`.webpageURL
147+
x // $ tainted
148+
let y = `continue`.referrerURL
149+
y // $ MISSING: tainted
150+
}
151+
152+
func scene(_: UIScene, didUpdate: NSUserActivity) { // $ source=remote
153+
let x = didUpdate.webpageURL
154+
x // $ tainted
155+
let y = didUpdate.referrerURL
156+
y // $ MISSING: tainted
157+
}
158+
159+
func scene(_: UIScene, openURLContexts: Set<UIOpenURLContext>) { // $ source=remote
160+
for openURLContext in openURLContexts {
161+
let x = openURLContext.url
162+
x // $ MISSING: tainted
163+
}
164+
}
91165
}

0 commit comments

Comments
 (0)