Skip to content

Commit 2d313ef

Browse files
committed
Swift: Add some path injection sink models.
1 parent cd14703 commit 2d313ef

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

swift/ql/lib/codeql/swift/security/PathInjectionExtensions.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,14 @@ private class PathInjectionSinks extends SinkModelCsv {
8787
override predicate row(string row) {
8888
row =
8989
[
90+
";Data;true;init(contentsOf:options:);;;Argument[0];path-injection",
9091
";Data;true;write(to:options:);;;Argument[0];path-injection",
92+
";NSData;true;init(contentsOfFile:);;;Argument[0];path-injection",
93+
";NSData;true;init(contentsOfFile:options:);;;Argument[0];path-injection",
94+
";NSData;true;init(contentsOf:);;;Argument[0];path-injection",
95+
";NSData;true;init(contentsOf:options:);;;Argument[0];path-injection",
96+
";NSData;true;init(contentsOfMappedFile:);;;Argument[0];path-injection",
97+
";NSData;true;dataWithContentsOfMappedFile(_:);;;Argument[0];path-injection",
9198
";NSData;true;write(to:atomically:);;;Argument[0];path-injection",
9299
";NSData;true;write(to:options:);;;Argument[0];path-injection",
93100
";NSData;true;write(toFile:atomically:);;;Argument[0];path-injection",
@@ -118,12 +125,14 @@ private class PathInjectionSinks extends SinkModelCsv {
118125
";FileManager;true;fileExists(atPath:);;;Argument[0];path-injection",
119126
";FileManager;true;fileExists(atPath:isDirectory:);;;Argument[0];path-injection",
120127
";FileManager;true;setAttributes(_:ofItemAtPath:);;;Argument[1];path-injection",
128+
";FileManager;true;attributesOfItem(atPath:);;;Argument[0];path-injection",
121129
";FileManager;true;contents(atPath:);;;Argument[0];path-injection",
122130
";FileManager;true;contentsEqual(atPath:andPath:);;;Argument[0..1];path-injection",
123131
";FileManager;true;changeCurrentDirectoryPath(_:);;;Argument[0];path-injection",
124132
";FileManager;true;unmountVolume(at:options:completionHandler:);;;Argument[0];path-injection",
125133
// Deprecated FileManager methods:
126134
";FileManager;true;changeFileAttributes(_:atPath:);;;Argument[1];path-injection",
135+
";FileManager;true;fileAttributes(atPath:traverseLink:);;;Argument[0];path-injection",
127136
";FileManager;true;directoryContents(atPath:);;;Argument[0];path-injection",
128137
";FileManager;true;createDirectory(atPath:attributes:);;;Argument[0];path-injection",
129138
";FileManager;true;createSymbolicLink(atPath:pathContent:);;;Argument[0..1];path-injection",
@@ -146,6 +155,7 @@ private class PathInjectionSinks extends SinkModelCsv {
146155
";ArchiveByteStream;true;withFileStream(path:mode:options:permissions:_:);;;Argument[0];path-injection",
147156
";Bundle;true;init(url:);;;Argument[0];path-injection",
148157
";Bundle;true;init(path:);;;Argument[0];path-injection",
158+
";NSURL;writeBookmarkData(_:to:options:);;;Argument[1];path-injection",
149159
// GRDB
150160
";Database;true;init(path:description:configuration:);;;Argument[0];path-injection",
151161
";DatabasePool;true;init(path:configuration:);;;Argument[0];path-injection",

swift/ql/test/query-tests/Security/CWE-022/testPathInjection.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,13 @@ func testPathInjection2(s1: UnsafeMutablePointer<String>, s2: UnsafeMutablePoint
447447
var u1 = URL(filePath: "")
448448
_ = NSData(contentsOf: u1)
449449
_ = NSData(contentsOf: u1.appendingPathComponent(""))
450-
_ = NSData(contentsOf: u1.appendingPathComponent(remoteString)) // $ MISSING: hasPathInjection=445
451-
_ = NSData(contentsOf: u1.appendingPathComponent(remoteString).appendingPathComponent("")) // $ MISSING: hasPathInjection=445
450+
_ = NSData(contentsOf: u1.appendingPathComponent(remoteString)) // $ hasPathInjection=445
451+
_ = NSData(contentsOf: u1.appendingPathComponent(remoteString).appendingPathComponent("")) // $ hasPathInjection=445
452452
u1.appendPathComponent(remoteString)
453-
_ = NSData(contentsOf: u1) // $ MISSING: hasPathInjection=445
453+
_ = NSData(contentsOf: u1) // $ hasPathInjection=445
454454

455455
let u2 = URL(filePath: remoteString)
456-
_ = NSData(contentsOf: u2) // $ MISSING: hasPathInjection=445
456+
_ = NSData(contentsOf: u2) // $ hasPathInjection=445
457457

458458
let u3 = NSURL(string: "")!
459459
Data("").write(to: u3.filePathURL!, options: [])
@@ -464,9 +464,9 @@ func testPathInjection2(s1: UnsafeMutablePointer<String>, s2: UnsafeMutablePoint
464464
Data("").write(to: u4.filePathURL!, options: []) // $ hasPathInjection=445
465465
Data("").write(to: u4.appendingPathComponent("")!, options: []) // $ hasPathInjection=445
466466

467-
_ = NSData(contentsOfFile: remoteString)! // $ MISSING: hasPathInjection=445
468-
_ = NSData(contentsOfMappedFile: remoteString)! // $ MISSING: hasPathInjection=445
469-
_ = NSData.dataWithContentsOfMappedFile(remoteString)! // $ MISSING: hasPathInjection=445
467+
_ = NSData(contentsOfFile: remoteString)! // $ hasPathInjection=445
468+
_ = NSData(contentsOfMappedFile: remoteString)! // $ hasPathInjection=445
469+
_ = NSData.dataWithContentsOfMappedFile(remoteString)! // $ hasPathInjection=445
470470

471471
_ = NSData().write(toFile: s1.pointee, atomically: true)
472472
s1.pointee = remoteString
@@ -478,8 +478,8 @@ func testPathInjection2(s1: UnsafeMutablePointer<String>, s2: UnsafeMutablePoint
478478
_ = remoteString.completePath(into: s3, caseSensitive: false, matchesInto: nil, filterTypes: nil)
479479
_ = NSData().write(toFile: s3.pointee, atomically: true) // $ MISSING: hasPathInjection=445
480480

481-
_ = fm.fileAttributes(atPath: remoteString, traverseLink: true) // $ MISSING: hasPathInjection=445
482-
_ = try fm.attributesOfItem(atPath: remoteString) // $ MISSING: hasPathInjection=445
481+
_ = fm.fileAttributes(atPath: remoteString, traverseLink: true) // $ hasPathInjection=445
482+
_ = try fm.attributesOfItem(atPath: remoteString) // $ hasPathInjection=445
483483
}
484484

485485
// ---

0 commit comments

Comments
 (0)