Skip to content

Commit 7bf61d1

Browse files
committed
Swift: Add test cases.
1 parent 683ca2d commit 7bf61d1

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

swift/ql/test/query-tests/Security/CWE-078/CommandInjection.expected

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ edges
136136
| CommandInjection.swift:166:45:166:77 | call to URL.init(string:) [some:0] | CommandInjection.swift:166:45:166:78 | ...! | provenance | |
137137
| CommandInjection.swift:166:45:166:78 | ...! | file://:0:0:0:0 | url | provenance | |
138138
| CommandInjection.swift:166:57:166:57 | userControlledString | CommandInjection.swift:166:45:166:77 | call to URL.init(string:) [some:0] | provenance | |
139+
| CommandInjection.swift:170:19:170:83 | call to contentsOfDirectory(atPath:) | CommandInjection.swift:176:22:176:22 | files | provenance | |
140+
| CommandInjection.swift:170:19:170:83 | call to contentsOfDirectory(atPath:) | CommandInjection.swift:178:23:178:30 | ...[...] | provenance | |
141+
| CommandInjection.swift:176:3:176:3 | [post] task12 [arguments] | CommandInjection.swift:176:3:176:3 | [post] task12 | provenance | |
142+
| CommandInjection.swift:176:22:176:22 | files | CommandInjection.swift:176:3:176:3 | [post] task12 [arguments] | provenance | |
143+
| CommandInjection.swift:176:22:176:22 | files | CommandInjection.swift:176:22:176:22 | files | provenance | |
144+
| CommandInjection.swift:176:22:176:22 | files | CommandInjection.swift:178:23:178:30 | ...[...] | provenance | |
145+
| CommandInjection.swift:178:3:178:3 | [post] task12 [arguments, Collection element] | CommandInjection.swift:178:3:178:3 | [post] task12 | provenance | |
146+
| CommandInjection.swift:178:22:178:31 | [...] [Collection element] | CommandInjection.swift:178:3:178:3 | [post] task12 [arguments, Collection element] | provenance | |
147+
| CommandInjection.swift:178:23:178:30 | ...[...] | CommandInjection.swift:178:22:178:31 | [...] [Collection element] | provenance | |
139148
| CommandInjection.swift:193:3:193:3 | newValue [Collection element] | CommandInjection.swift:194:19:194:19 | newValue [Collection element] | provenance | |
140149
| CommandInjection.swift:193:3:193:3 | newValue [Collection element] | CommandInjection.swift:195:20:195:20 | newValue [Collection element] | provenance | |
141150
| CommandInjection.swift:193:3:193:3 | newValue [Collection element] | CommandInjection.swift:196:19:196:19 | newValue [Collection element] | provenance | |
@@ -284,6 +293,14 @@ nodes
284293
| CommandInjection.swift:166:45:166:78 | ...! | semmle.label | ...! |
285294
| CommandInjection.swift:166:45:166:78 | ...! | semmle.label | ...! |
286295
| CommandInjection.swift:166:57:166:57 | userControlledString | semmle.label | userControlledString |
296+
| CommandInjection.swift:170:19:170:83 | call to contentsOfDirectory(atPath:) | semmle.label | call to contentsOfDirectory(atPath:) |
297+
| CommandInjection.swift:176:3:176:3 | [post] task12 | semmle.label | [post] task12 |
298+
| CommandInjection.swift:176:3:176:3 | [post] task12 [arguments] | semmle.label | [post] task12 [arguments] |
299+
| CommandInjection.swift:176:22:176:22 | files | semmle.label | files |
300+
| CommandInjection.swift:178:3:178:3 | [post] task12 | semmle.label | [post] task12 |
301+
| CommandInjection.swift:178:3:178:3 | [post] task12 [arguments, Collection element] | semmle.label | [post] task12 [arguments, Collection element] |
302+
| CommandInjection.swift:178:22:178:31 | [...] [Collection element] | semmle.label | [...] [Collection element] |
303+
| CommandInjection.swift:178:23:178:30 | ...[...] | semmle.label | ...[...] |
287304
| CommandInjection.swift:193:3:193:3 | newValue [Collection element] | semmle.label | newValue [Collection element] |
288305
| CommandInjection.swift:194:4:194:4 | [post] getter for .p1 | semmle.label | [post] getter for .p1 |
289306
| CommandInjection.swift:194:4:194:4 | [post] getter for .p1 [arguments, Collection element] | semmle.label | [post] getter for .p1 [arguments, Collection element] |
@@ -351,6 +368,8 @@ subpaths
351368
| CommandInjection.swift:163:40:163:73 | ...! | CommandInjection.swift:105:40:105:94 | call to String.init(contentsOf:) | CommandInjection.swift:163:40:163:73 | ...! | This command depends on a $@. | CommandInjection.swift:105:40:105:94 | call to String.init(contentsOf:) | user-provided value |
352369
| CommandInjection.swift:164:32:164:53 | [...] | CommandInjection.swift:105:40:105:94 | call to String.init(contentsOf:) | CommandInjection.swift:164:32:164:53 | [...] | This command depends on a $@. | CommandInjection.swift:105:40:105:94 | call to String.init(contentsOf:) | user-provided value |
353370
| CommandInjection.swift:166:45:166:78 | ...! | CommandInjection.swift:105:40:105:94 | call to String.init(contentsOf:) | CommandInjection.swift:166:45:166:78 | ...! | This command depends on a $@. | CommandInjection.swift:105:40:105:94 | call to String.init(contentsOf:) | user-provided value |
371+
| CommandInjection.swift:176:3:176:3 | [post] task12 | CommandInjection.swift:170:19:170:83 | call to contentsOfDirectory(atPath:) | CommandInjection.swift:176:3:176:3 | [post] task12 | This command depends on a $@. | CommandInjection.swift:170:19:170:83 | call to contentsOfDirectory(atPath:) | user-provided value |
372+
| CommandInjection.swift:178:3:178:3 | [post] task12 | CommandInjection.swift:170:19:170:83 | call to contentsOfDirectory(atPath:) | CommandInjection.swift:178:3:178:3 | [post] task12 | This command depends on a $@. | CommandInjection.swift:170:19:170:83 | call to contentsOfDirectory(atPath:) | user-provided value |
354373
| CommandInjection.swift:194:4:194:4 | [post] getter for .p1 | CommandInjection.swift:201:41:201:95 | call to String.init(contentsOf:) | CommandInjection.swift:194:4:194:4 | [post] getter for .p1 | This command depends on a $@. | CommandInjection.swift:201:41:201:95 | call to String.init(contentsOf:) | user-provided value |
355374
| CommandInjection.swift:195:4:195:6 | [post] ...! | CommandInjection.swift:201:41:201:95 | call to String.init(contentsOf:) | CommandInjection.swift:195:4:195:6 | [post] ...! | This command depends on a $@. | CommandInjection.swift:201:41:201:95 | call to String.init(contentsOf:) | user-provided value |
356375
| CommandInjection.swift:196:4:196:4 | [post] ...! | CommandInjection.swift:201:41:201:95 | call to String.init(contentsOf:) | CommandInjection.swift:196:4:196:4 | [post] ...! | This command depends on a $@. | CommandInjection.swift:201:41:201:95 | call to String.init(contentsOf:) | user-provided value |

swift/ql/test/query-tests/Security/CWE-078/CommandInjection.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ class NSUserAutomatorTask : NSUserScriptTask {
5353
var variables: [String: Any]? { get { return nil } set { } }
5454
}
5555

56+
class FileManager : NSObject {
57+
class var `default`: FileManager { get { return 0 as! FileManager } }
5658

57-
58-
59-
60-
59+
func contentsOfDirectory(atPath path: String) throws -> [String] { [] }
60+
}
6161

6262
// --- tests ---
6363

@@ -167,17 +167,17 @@ func testCommandInjectionMore(mySafeString: String) {
167167
task11.variables = ["abc": userControlledString] // BAD [NOT DETECTED]
168168
task11.execute(withInput: nil)
169169

170-
171-
172-
173-
174-
175-
176-
177-
178-
179-
180-
170+
let files = try! FileManager.default.contentsOfDirectory(atPath: "some/directory")
171+
for file in files {
172+
let task12 = Process()
173+
task12.launchPath = "/bin/rm" // GOOD
174+
task12.arguments = [file] // GOOD (cases like this vary, but our analysis doesn't work well on them)
175+
task12.launch()
176+
task12.arguments = files // GOOD (similar to previous) [FALSE POSITIVE]
177+
task12.launch()
178+
task12.arguments = [files[0]] // GOOD (similar to previous) [FALSE POSITIVE]
179+
task12.launch()
180+
}
181181

182182
}
183183

0 commit comments

Comments
 (0)