Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a422b23
unified: Copy unsafe unpacking query and tests unchanged
geoffw0 Dec 1, 2026
bca27b2
unified: Collect unsafe unpacking configuration and models
geoffw0 Dec 1, 2026
777d0a4
unified: Port unsafe unpacking query to unified Swift
geoffw0 Dec 1, 2026
5bb5894
unified: Cover safe archive inputs in unsafe unpacking tests
geoffw0 Dec 1, 2026
c572613
unified: Clarify unsafe unpacking help and safe example
geoffw0 Dec 1, 2026
1802558
Unified: Tiny layout adjustments.
geoffw0 Dec 1, 2026
7c0b36f
Unified: Delete supporting code from the examples that does not make …
geoffw0 Dec 1, 2026
30c942c
Unified: Fix the examples and include them accurately in the query test.
geoffw0 Dec 1, 2026
f86a3fb
Unified: Update .qhelp and example comments based on some research ab…
geoffw0 Dec 1, 2026
c528fca
Unified: Remove the ZipFoundation example as the situation there is c…
geoffw0 Dec 1, 2026
03d1de9
Unified: Rename the samples to be consistent with the .qhelp and quer…
geoffw0 Dec 1, 2026
c2870cb
Unified: Use remote taint source for now (active taint sources will b…
geoffw0 Dec 1, 2026
df8a8ff
unified: Cover stored downloads with explicit writing options
geoffw0 Dec 1, 2026
326334c
unified: Model Data.write flow with a taint summary
geoffw0 Dec 1, 2026
7612242
Unified: Simplify and explain the new test cases a little.
geoffw0 Dec 1, 2026
a9e0289
Unified: There's no reason to consider this 'experimental' any more (…
geoffw0 Dec 1, 2026
3b43f81
Merge branch 'main' into unifiedpacking
geoffw0 Dec 3, 2026
386bf9d
Unified: Fix test / example compile errors.
geoffw0 Dec 3, 2026
bbda882
Unified: Update Data/NSData init models to account for an optional ar…
geoffw0 Dec 3, 2026
e0787cf
Merge branch 'main' into unifiedpacking
geoffw0 Dec 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions unified/ql/lib/ext/legacy-swift.model.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
["", "UITextInput", true, "text(in:)", "", "", "ReturnValue", "local", "manual"],
["", "UITextInput", true, "shouldChangeText(in:replacementText:)", "", "", "Parameter[replacementText:]", "local", "manual"],
["", "NSData", true, "init(contentsOf:)", "", "", "ReturnValue", "remote", "manual"],
["", "NSData", true, "init(contentsOf:options:)", "", "", "ReturnValue", "remote", "manual"],
["", "NSData", true, "init(contentsOf:options?:)", "", "", "ReturnValue", "remote", "manual"],
["", "NSString", true, "length", "", "", "", "nsstring-length", "manual"],
["", "NSString", true, "init(contentsOf:)", "", "", "ReturnValue", "remote", "manual"],
["", "NSString", true, "init(contentsOfFile:)", "", "", "ReturnValue", "local", "manual"],
Expand All @@ -43,7 +43,7 @@
["", "NSString", true, "init(contentsOf:usedEncoding:)", "", "", "ReturnValue", "remote", "manual"],
["", "NSString", true, "init(contentsOfFile:usedEncoding:)", "", "", "ReturnValue", "local", "manual"],
["", "FileManager", true, "contents(atPath:)", "", "", "ReturnValue", "local", "manual"],
["", "Data", true, "init(contentsOf:options:)", "", "", "ReturnValue", "remote", "manual"],
["", "Data", true, "init(contentsOf:options?:)", "", "", "ReturnValue", "remote", "manual"],
["", "Data", true, "init(contentsOf:)", "", "", "ReturnValue", "remote", "manual"],
["", "UISceneDelegate", true, "scene(_:continue:)", "", "", "Parameter[continue:]", "remote", "manual"],
["", "UISceneDelegate", true, "scene(_:didUpdate:)", "", "", "Parameter[didUpdate:]", "remote", "manual"],
Expand Down Expand Up @@ -109,7 +109,7 @@
["", "", false, "vfprintf(_:_:_:)", "", "", "Argument[1,2]", "log-injection", "manual"],
["", "NSData", true, "init(contentsOf:)", "", "", "Argument[contentsOf:]", "path-injection", "manual"],
["", "NSData", true, "init(contentsOfFile:)", "", "", "Argument[contentsOfFile:]", "path-injection", "manual"],
["", "NSData", true, "init(contentsOf:options:)", "", "", "Argument[contentsOf:]", "path-injection", "manual"],
["", "NSData", true, "init(contentsOf:options?:)", "", "", "Argument[contentsOf:]", "path-injection", "manual"],
["", "NSData.Type", true, "dataWithContentsOfMappedFile(_:)", "", "", "Argument[0]", "path-injection", "manual"],
["", "NSData", true, "init(contentsOfFile:options:)", "", "", "Argument[contentsOfFile:]", "path-injection", "manual"],
["", "NSData", true, "init(contentsOfMappedFile:)", "", "", "Argument[contentsOfMappedFile:]", "path-injection", "manual"],
Expand Down Expand Up @@ -163,7 +163,7 @@
["", "FileManager", true, "subpathsOfDirectory(atPath:)", "", "", "Argument[atPath:]", "path-injection", "manual"],
["", "FileManager", true, "trashItem(at:resultingItemURL:)", "", "", "Argument[at:]", "path-injection", "manual"],
["", "FileManager", true, "unmountVolume(at:options?:completionHandler:)", "", "", "Argument[at:]", "path-injection", "manual"],
["", "Data", true, "init(contentsOf:options:)", "", "", "Argument[contentsOf:]", "path-injection", "manual"],
["", "Data", true, "init(contentsOf:options?:)", "", "", "Argument[contentsOf:]", "path-injection", "manual"],
["", "Data", true, "md5()", "", "", "Argument[self]", "weak-hash-input-MD5", "manual"],
["", "Data", true, "sha1()", "", "", "Argument[self]", "weak-hash-input-SHA1", "manual"],
["", "Data", true, "sha2(_:)", "", "", "Argument[self]", "weak-password-hash-input-SHA2", "manual"],
Expand Down Expand Up @@ -870,7 +870,7 @@
["", "URL", true, "withUnsafeFileSystemRepresentation(_:)", "", "", "Argument[0].ReturnValue", "ReturnValue", "taint", "manual"],
["", "NSData", true, "init(contentsOf:)", "", "", "Argument[contentsOf:]", "ReturnValue", "taint", "manual"],
["", "NSData", true, "init(contentsOfFile:)", "", "", "Argument[contentsOfFile:]", "ReturnValue", "taint", "manual"],
["", "NSData", true, "init(contentsOf:options:)", "", "", "Argument[contentsOf:]", "ReturnValue", "taint", "manual"],
["", "NSData", true, "init(contentsOf:options?:)", "", "", "Argument[contentsOf:]", "ReturnValue", "taint", "manual"],
["", "NSData", true, "init(data:)", "", "", "Argument[data:]", "ReturnValue", "taint", "manual"],
["", "NSData", true, "init(bytes:length:)", "", "", "Argument[bytes:]", "ReturnValue", "taint", "manual"],
["", "NSData", true, "base64EncodedData(options?:)", "", "", "Argument[self]", "ReturnValue", "taint", "manual"],
Expand Down Expand Up @@ -983,6 +983,7 @@
["", "Data", true, "append(_:count:)", "", "", "Argument[0]", "Argument[self]", "taint", "manual"],
["", "Data", true, "insert(contentsOf:at:)", "", "", "Argument[contentsOf:]", "Argument[self]", "taint", "manual"],
["", "Data", true, "replaceSubrange(_:with:)", "", "", "Argument[with:]", "Argument[self]", "taint", "manual"],
["", "Data", true, "write(to:options?:)", "", "", "Argument[self]", "Argument[to:]", "taint", "manual"],
["", "Data", true, "withUnsafeMutableBytes(_:)", "", "", "Argument[self]", "Argument[0].Parameter[0].CollectionElement", "taint", "manual"],
["", "Data", true, "withUnsafeMutableBytes(_:)", "", "", "Argument[self].CollectionElement", "Argument[0].Parameter[0].CollectionElement", "taint", "manual"],
["", "Data", true, "withUnsafeMutableBytes(_:)", "", "", "Argument[0].Parameter[0].CollectionElement", "Argument[self].CollectionElement", "taint", "manual"],
Expand All @@ -1000,7 +1001,7 @@
["", "Data", true, "replacing(_:with:subrange:maxReplacements?:)", "", "", "Argument[with:]", "Argument[self]", "taint", "manual"],
["", "Data", true, "trimmingPrefix(while:)", "", "", "Argument[self]", "ReturnValue", "taint", "manual"],
["", "Data", true, "trimmingPrefix(_:)", "", "", "Argument[self]", "ReturnValue", "taint", "manual"],
["", "Data", true, "init(contentsOf:options:)", "", "", "Argument[contentsOf:]", "ReturnValue", "taint", "manual"],
["", "Data", true, "init(contentsOf:options?:)", "", "", "Argument[contentsOf:]", "ReturnValue", "taint", "manual"],
["", "Data", true, "base64EncodedData(options?:)", "", "", "Argument[self]", "ReturnValue", "taint", "manual"],
["", "Data", true, "base64EncodedString(options?:)", "", "", "Argument[self]", "ReturnValue", "taint", "manual"],
["", "Data", true, "init(base64Encoded:options?:)", "", "", "Argument[base64Encoded:]", "ReturnValue", "taint", "manual"],
Expand Down
40 changes: 40 additions & 0 deletions unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>

<overview>
<p>

Unpacking files from a malicious zip without properly validating that the destination file path
is within the destination directory, or allowing symlinks to point to files outside the extraction directory,
allows an attacker to extract files to arbitrary locations outside the extraction directory. This can
overwrite sensitive user data and, in some cases, can lead to code execution if an
attacker overwrites an application's shared object file.
</p>
</overview>

<recommendation>
<p>Use an up-to-date archive library that validates entry paths and symlink targets,
such as <code>ZipArchive</code>. Keep these validation checks enabled.</p>
</recommendation>

<example>
<p>
The following example unpacks a remote zip using <code>Zip.unzipFile()</code>.
Zip 2.1.2 does not validate archive entry paths and is vulnerable to path traversal.
</p>
<sample src="UnsafeUnpackBad.swift" />

<p>The following example uses <code>ZipArchive</code>. Up-to-date versions of
<code>ZipArchive</code> validate entry paths and symlink targets.</p>
<sample src="UnsafeUnpackGood.swift" />
</example>

<references>
<li>
Ostorlab:
<a href="https://blog.ostorlab.co/zip-packages-exploitation.html">Zip Packages Exploitation</a>.
</li>
</references>
</qhelp>
40 changes: 40 additions & 0 deletions unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @name Arbitrary file write during a zip extraction from a user controlled source
* @description Unpacking user controlled zips without validating whether the
* destination file path is within the destination directory can cause files
* outside the destination directory to be overwritten.
* @kind path-problem
* @problem.severity error
* @security-severity 7.5
* @precision high
* @id unified/swift/unsafe-unpacking
* @tags security
* external/cwe/cwe-022
*/

import unified

/**
* A taint configuration for tainted data that reaches an unsafe unpack sink.
*/
module UnsafeUnpackConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node node) { Models::isSource(node, "remote") }
Comment thread
geoffw0 marked this conversation as resolved.
Resolved

predicate isSink(DataFlow::Node node) { Models::isSink(node, "unsafe-unpack") }

predicate isBarrier(DataFlow::Node node) {
// TODO: Port numeric barriers from original Swift's UnsafeUnpackExtensions.qll.
none()
}

predicate observeDiffInformedIncrementalMode() { any() }
}

module UnsafeUnpackFlow = TaintTracking::Global<UnsafeUnpackConfig>;

import UnsafeUnpackFlow::PathGraph

from UnsafeUnpackFlow::PathNode sourceNode, UnsafeUnpackFlow::PathNode sinkNode
where UnsafeUnpackFlow::flowPath(sourceNode, sinkNode)
select sinkNode.getNode(), sourceNode, sinkNode,
"Unsafe unpacking from a malicious zip retrieved from a remote location."
17 changes: 17 additions & 0 deletions unified/ql/src/queries/security/CWE-022/UnsafeUnpackBad.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation
import Zip

func unzipFile(at sourcePath: String, to destinationPath: String) {
do {
let remoteURL = URL(string: "https://example.com/")!
let source = URL(fileURLWithPath: sourcePath)
let destination = URL(fileURLWithPath: destinationPath)

// Malicious zip is downloaded
try Data(contentsOf: remoteURL).write(to: source)

let fileManager = FileManager()
// Malicious zip is unpacked; Zip 2.1.2 does not validate archive entry paths
try Zip.unzipFile(source, destination: destination, overwrite: true, password: nil)
}
}
15 changes: 15 additions & 0 deletions unified/ql/src/queries/security/CWE-022/UnsafeUnpackGood.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation
import ZipArchive

func unzipFile(at sourcePath: String, to destinationPath: String) {
do {
let remoteURL = URL(string: "https://example.com/")!
let source = URL(fileURLWithPath: sourcePath)

// Malicious zip is downloaded
try Data(contentsOf: remoteURL).write(to: source)

// ZipArchive validates paths and symlinks
try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#select
| UnsafeUnpack.swift:65:27:65:32 | source | UnsafeUnpack.swift:61:13:61:39 | Data(...) | UnsafeUnpack.swift:65:27:65:32 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
| UnsafeUnpack.swift:93:38:93:43 | source | UnsafeUnpack.swift:89:13:89:39 | Data(...) | UnsafeUnpack.swift:93:38:93:43 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
| UnsafeUnpack.swift:134:23:134:29 | archive | UnsafeUnpack.swift:126:16:126:55 | Data(...) | UnsafeUnpack.swift:134:23:134:29 | archive | Unsafe unpacking from a malicious zip retrieved from a remote location. |
edges
| UnsafeUnpack.swift:61:13:61:39 | Data(...) | UnsafeUnpack.swift:61:51:61:56 | [post] source | provenance | |
| UnsafeUnpack.swift:61:51:61:56 | [post] source | UnsafeUnpack.swift:65:27:65:32 | source | provenance | |
| UnsafeUnpack.swift:89:13:89:39 | Data(...) | UnsafeUnpack.swift:89:51:89:56 | [post] source | provenance | |
| UnsafeUnpack.swift:89:51:89:56 | [post] source | UnsafeUnpack.swift:93:38:93:43 | source | provenance | |
| UnsafeUnpack.swift:126:9:126:12 | data | UnsafeUnpack.swift:131:5:131:8 | data | provenance | |
| UnsafeUnpack.swift:126:16:126:55 | Data(...) | UnsafeUnpack.swift:126:9:126:12 | data | provenance | |
| UnsafeUnpack.swift:131:5:131:8 | data | UnsafeUnpack.swift:131:20:131:26 | [post] archive | provenance | |
| UnsafeUnpack.swift:131:20:131:26 | [post] archive | UnsafeUnpack.swift:134:23:134:29 | archive | provenance | |
nodes
| UnsafeUnpack.swift:61:13:61:39 | Data(...) | semmle.label | Data(...) |
| UnsafeUnpack.swift:61:51:61:56 | [post] source | semmle.label | [post] source |
| UnsafeUnpack.swift:65:27:65:32 | source | semmle.label | source |
| UnsafeUnpack.swift:89:13:89:39 | Data(...) | semmle.label | Data(...) |
| UnsafeUnpack.swift:89:51:89:56 | [post] source | semmle.label | [post] source |
| UnsafeUnpack.swift:93:38:93:43 | source | semmle.label | source |
| UnsafeUnpack.swift:126:9:126:12 | data | semmle.label | data |
| UnsafeUnpack.swift:126:16:126:55 | Data(...) | semmle.label | Data(...) |
| UnsafeUnpack.swift:131:5:131:8 | data | semmle.label | data |
| UnsafeUnpack.swift:131:20:131:26 | [post] archive | semmle.label | [post] archive |
| UnsafeUnpack.swift:134:23:134:29 | archive | semmle.label | archive |
subpaths
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query: queries/security/CWE-022/UnsafeUnpack.ql
postprocess:
- utils/test/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@

// --- stubs ---

struct URL
{
init?(string: String) {}
init(fileURLWithPath: String) {}
}

class Zip {
class func unzipFile(_ zipFilePath: URL, destination: URL, overwrite: Bool, password: String?, progress: ((_ progress: Double) -> ())? = nil, fileOutputHandler: ((_ unzippedFile: URL) -> Void)? = nil) throws {}
}

class NSObject {
}

class Progress : NSObject {

}

class FileManager : NSObject {
func unzipItem(at sourceURL: URL, to destinationURL: URL, skipCRC32: Bool = false,
progress: Progress? = nil, pathEncoding: String.Encoding? = nil) throws {}
}

protocol DataProtocol { }
class Data : DataProtocol {
struct ReadingOptions : OptionSet { let rawValue: Int }
struct WritingOptions : OptionSet { let rawValue: Int }

init<S>(_ elements: S) { count = 0 }
init(contentsOf: URL, options: ReadingOptions = []) { count = 0 }
func write(to: URL, options: Data.WritingOptions = []) {}

var count: Int
}

extension String {

struct Encoding {
var rawValue: UInt

init(rawValue: UInt) { self.rawValue = rawValue }

static let ascii = Encoding(rawValue: 1)
}
init(contentsOf url: URL) throws {
self.init("")
}
}

// --- tests ---

func testUnsafeUnpackQhelpExample1ZipBad(at sourcePath: String, to destinationPath: String) {
do {
let remoteURL = URL(string: "https://example.com/")!
let source = URL(fileURLWithPath: sourcePath)
let destination = URL(fileURLWithPath: destinationPath)

// Malicious zip is downloaded
try Data(contentsOf: remoteURL).write(to: source) // $ Source

let fileManager = FileManager()
// Malicious zip is unpacked; Zip 2.1.2 does not validate archive entry paths
try Zip.unzipFile(source, destination: destination, overwrite: true, password: nil) // $ Alert
}
}
Comment thread
geoffw0 marked this conversation as resolved.
Resolved

func testUnsafeUnpackQhelpExample2ZipArchiveGood(at sourcePath: String, to destinationPath: String) {
do {
let remoteURL = URL(string: "https://example.com/")!
let source = URL(fileURLWithPath: sourcePath)

// Malicious zip is downloaded
try Data(contentsOf: remoteURL).write(to: source)

// ZipArchive validates paths and symlinks
try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
}
}

func testUnsafeUnpackZipFoundationBad(at sourcePath: String, to destinationPath: String) {
do {
let remoteURL = URL(string: "https://example.com/")!
let source = URL(fileURLWithPath: sourcePath)
let destination = URL(fileURLWithPath: destinationPath)

// Malicious zip is downloaded
try Data(contentsOf: remoteURL).write(to: source) // $ Source

let fileManager = FileManager()
// Malicious zip is unpacked; older versions of ZIPFoundation allow symlink path traversal
try fileManager.unzipItem(at:source, to: destination) // $ Alert
}
}

func testTrustedArchive() throws {
let source = URL(fileURLWithPath: "/trusted.zip")
let destination = URL(fileURLWithPath: "/destination")

// write known data to `source`
Data([0x50, 0x4b]).write(to: source)

// unzip known data (safe)
try Zip.unzipFile(source, destination: destination, overwrite: true, password: nil)
try FileManager().unzipItem(at: source, to: destination)
}

func testUnrelatedArchive() throws {
let remoteURL = URL(string: "https://example.com/")!
let downloadedArchive = URL(fileURLWithPath: "/downloaded.zip")
let trustedArchive = URL(fileURLWithPath: "/trusted.zip")

// write tainted data to `downloadedArchive`
Data(contentsOf: remoteURL, options: []).write(to: downloadedArchive)

// unzip `trustedArchive` over `downloadedArchive` (safe)
try Zip.unzipFile(trustedArchive, destination: downloadedArchive, overwrite: true, password: nil)
try FileManager().unzipItem(at: trustedArchive, to: downloadedArchive)
}

func testStoredDownloadWithExplicitWritingOptions() throws {
let remoteURL = URL(string: "https://example.com/archive.zip")!
let archive = URL(fileURLWithPath: "/downloaded.zip")
let destination = URL(fileURLWithPath: "/destination")
let data = Data(contentsOf: remoteURL, options: []) // $ Source

try Zip.unzipFile(archive, destination: destination, overwrite: true, password: nil)

// write previously read tainted data to `archive`
data.write(to: archive, options: [])

// unzip tainted data
try Zip.unzipFile(archive, destination: destination, overwrite: true, password: nil) // $ Alert
}
Loading