From a422b23ed85806912e14b97b7f721253bea1e398 Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 09:45:53 +0000
Subject: [PATCH 01/18] unified: Copy unsafe unpacking query and tests
unchanged
Copy the original Swift query, help, referenced samples and query-specific tests byte-for-byte as the baseline for the unified Swift port. Adaptations follow separately.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
.../security/CWE-022/UnsafeUnpack.qhelp | 43 +++++++++++
.../queries/security/CWE-022/UnsafeUnpack.ql | 23 ++++++
.../security/CWE-022/ZipArchiveGood.swift | 25 +++++++
.../src/queries/security/CWE-022/ZipBad.swift | 28 ++++++++
.../security/CWE-022/ZipFoundationBad.swift | 28 ++++++++
.../UnsafeUnpack/UnsafeUnpack.expected | 13 ++++
.../CWE-022/UnsafeUnpack/UnsafeUnpack.qlref | 2 +
.../CWE-022/UnsafeUnpack/UnsafeUnpack.swift | 71 +++++++++++++++++++
8 files changed, 233 insertions(+)
create mode 100644 unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
create mode 100644 unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
create mode 100644 unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
create mode 100644 unified/ql/src/queries/security/CWE-022/ZipBad.swift
create mode 100644 unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
create mode 100644 unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
create mode 100644 unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.qlref
create mode 100644 unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
new file mode 100644
index 000000000000..6c53b3a789a5
--- /dev/null
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
@@ -0,0 +1,43 @@
+
+
+
+
+
+
+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 helps
+overwrite sensitive user data and, in some cases, can lead to code execution if an
+attacker overwrites an application's shared object file.
+
+
+
+
+Consider using a safer module, such as: ZIPArchive
+
+
+
+
+The following examples unpacks a remote zip using `Zip.unzipFile()` which is vulnerable to path traversal.
+
+
+
+
+The following examples unpacks a remote zip using `fileManager.unzipItem()` which is vulnerable to symlink path traversal.
+
+
+
+
+Consider using a safer module, such as: ZIPArchive
+
+
+
+
+
+Ostorlab:
+Zip Packages Exploitation.
+
+
+
\ No newline at end of file
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
new file mode 100644
index 000000000000..be7f4cfd0844
--- /dev/null
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
@@ -0,0 +1,23 @@
+/**
+ * @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 swift/unsafe-unpacking
+ * @tags security
+ * experimental
+ * external/cwe/cwe-022
+ */
+
+import swift
+import codeql.swift.security.UnsafeUnpackQuery
+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."
diff --git a/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift b/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
new file mode 100644
index 000000000000..e30f17c16923
--- /dev/null
+++ b/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
@@ -0,0 +1,25 @@
+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 is safe
+ try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: self)
+ } catch {
+ }
+}
+
+func main() {
+ let sourcePath = "/sourcePath"
+ let destinationPath = "/destinationPath"
+ unzipFile(at: sourcePath, to: destinationPath)
+}
+
+main()
\ No newline at end of file
diff --git a/unified/ql/src/queries/security/CWE-022/ZipBad.swift b/unified/ql/src/queries/security/CWE-022/ZipBad.swift
new file mode 100644
index 000000000000..4a90e6b3ea86
--- /dev/null
+++ b/unified/ql/src/queries/security/CWE-022/ZipBad.swift
@@ -0,0 +1,28 @@
+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
+ try Zip.unzipFile(source, destination: destination, overwrite: true, password: nil)
+ } catch {
+ }
+}
+
+func main() {
+ let sourcePath = "/sourcePath"
+ let destinationPath = "/destinationPath"
+ unzipFile(at: sourcePath, to: destinationPath)
+}
+
+main()
\ No newline at end of file
diff --git a/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift b/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
new file mode 100644
index 000000000000..7dfd86feffdb
--- /dev/null
+++ b/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
@@ -0,0 +1,28 @@
+import Foundation
+import ZIPFoundation
+
+
+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
+ try fileManager.unzipItem(at:source, to: destination)
+ } catch {
+ }
+}
+
+func main() {
+ let sourcePath = "/sourcePath"
+ let destinationPath = "/destinationPath"
+ unzipFile(at: sourcePath, to: destinationPath)
+}
+
+main()
\ No newline at end of file
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
new file mode 100644
index 000000000000..013959ee4505
--- /dev/null
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
@@ -0,0 +1,13 @@
+edges
+| UnsafeUnpack.swift:62:9:62:48 | call to Data.init(contentsOf:options:) | UnsafeUnpack.swift:62:60:62:60 | source | provenance | Config |
+| UnsafeUnpack.swift:62:60:62:60 | source | UnsafeUnpack.swift:64:27:64:27 | source | provenance | |
+| UnsafeUnpack.swift:62:60:62:60 | source | UnsafeUnpack.swift:67:39:67:39 | source | provenance | |
+nodes
+| UnsafeUnpack.swift:62:9:62:48 | call to Data.init(contentsOf:options:) | semmle.label | call to Data.init(contentsOf:options:) |
+| UnsafeUnpack.swift:62:60:62:60 | source | semmle.label | source |
+| UnsafeUnpack.swift:64:27:64:27 | source | semmle.label | source |
+| UnsafeUnpack.swift:67:39:67:39 | source | semmle.label | source |
+subpaths
+#select
+| UnsafeUnpack.swift:64:27:64:27 | source | UnsafeUnpack.swift:62:9:62:48 | call to Data.init(contentsOf:options:) | UnsafeUnpack.swift:64:27:64:27 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
+| UnsafeUnpack.swift:67:39:67:39 | source | UnsafeUnpack.swift:62:9:62:48 | call to Data.init(contentsOf:options:) | UnsafeUnpack.swift:67:39:67:39 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.qlref b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.qlref
new file mode 100644
index 000000000000..f637622e3a15
--- /dev/null
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.qlref
@@ -0,0 +1,2 @@
+query: experimental/Security/CWE-022/UnsafeUnpack.ql
+postprocess: utils/test/InlineExpectationsTestQuery.ql
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
new file mode 100644
index 000000000000..0f6a7cc8b280
--- /dev/null
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
@@ -0,0 +1,71 @@
+
+// --- 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(_ 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 testCommandInjectionQhelpExamples() {
+ guard let remoteURL = URL(string: "https://example.com/") else {
+ return
+ }
+
+ let source = URL(fileURLWithPath: "/sourcePath")
+ let destination = URL(fileURLWithPath: "/destination")
+
+ try Data(contentsOf: remoteURL, options: []).write(to: source) // $ Source
+ do {
+ try Zip.unzipFile(source, destination: destination, overwrite: true, password: nil) // $ Alert
+
+ let fileManager = FileManager()
+ try fileManager.unzipItem(at: source, to: destination) // $ Alert
+ } catch {
+ print("Error: \(error)")
+ }
+}
\ No newline at end of file
From bca27b2579e579453f0535db9bfb145d90a6015b Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 09:48:04 +0000
Subject: [PATCH 02/18] unified: Collect unsafe unpacking configuration and
models
Inline the data flow configuration and query-specific models from the original Swift libraries without API or behavior changes, preserving their declarations and provenance for review.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
.../queries/security/CWE-022/UnsafeUnpack.ql | 114 +++++++++++++++++-
1 file changed, 113 insertions(+), 1 deletion(-)
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
index be7f4cfd0844..838993b3d1f9 100644
--- a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
@@ -14,7 +14,119 @@
*/
import swift
-import codeql.swift.security.UnsafeUnpackQuery
+import codeql.swift.dataflow.TaintTracking
+import codeql.swift.dataflow.FlowSources
+import codeql.swift.dataflow.DataFlow
+import codeql.swift.dataflow.ExternalFlow
+
+// Copied from swift/ql/lib/codeql/swift/security/UnsafeUnpackQuery.qll.
+/**
+ * Provides default sources, sinks and sanitizers for reasoning about
+ * unsafe unpack vulnerabilities, as well as extension points for
+ * adding your own.
+ */
+
+/**
+ * A taint configuration for tainted data that reaches a unsafe unpack sink.
+ */
+module UnsafeUnpackConfig implements DataFlow::ConfigSig {
+ predicate isSource(DataFlow::Node node) {
+ node instanceof FlowSource or node instanceof RemoteFlowSource
+ }
+
+ predicate isSink(DataFlow::Node node) { node instanceof UnsafeUnpackSink }
+
+ predicate isBarrier(DataFlow::Node barrier) { barrier instanceof UnsafeUnpackBarrier }
+
+ predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
+ any(UnsafeUnpackAdditionalFlowStep s).step(nodeFrom, nodeTo)
+ }
+
+ predicate observeDiffInformedIncrementalMode() { any() }
+}
+
+/**
+ * Detect taint flow of tainted data that reaches a unsafe unpack sink.
+ */
+module UnsafeUnpackFlow = TaintTracking::Global;
+
+// Copied from swift/ql/lib/codeql/swift/security/UnsafeUnpackExtensions.qll.
+/**
+ * Provides default sources, sinks and sanitizers for reasoning about
+ * unsafe unpack vulnerabilities, as well as extension points for
+ * adding your own.
+ */
+
+/**
+ * A dataflow source for unsafe unpack vulnerabilities.
+ */
+abstract class UnsafeUnpackSource extends DataFlow::Node { }
+
+/**
+ * A dataflow sink for unsafe unpack vulnerabilities.
+ */
+abstract class UnsafeUnpackSink extends DataFlow::Node { }
+
+/**
+ * A barrier for unsafe unpack vulnerabilities.
+ */
+abstract class UnsafeUnpackBarrier extends DataFlow::Node { }
+
+/**
+ * A unit class for adding additional flow steps.
+ */
+class UnsafeUnpackAdditionalFlowStep extends Unit {
+ /**
+ * Holds if the step from `node1` to `node2` should be considered a flow
+ * step for paths related to unsafe unpack vulnerabilities.
+ */
+ abstract predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo);
+}
+
+/**
+ * A sink defined in a CSV model.
+ */
+private class DefaultUnsafeUnpackSink extends UnsafeUnpackSink {
+ DefaultUnsafeUnpackSink() { sinkNode(this, "unsafe-unpack") }
+}
+
+private class UnsafeUnpackSinks extends SinkModelCsv {
+ override predicate row(string row) {
+ row =
+ [
+ ";Zip;true;unzipFile(_:destination:overwrite:password:progress:fileOutputHandler:);;;Argument[0];unsafe-unpack",
+ ";FileManager;true;unzipItem(at:to:skipCRC32:progress:pathEncoding:);;;Argument[0];unsafe-unpack",
+ ]
+ }
+}
+
+/**
+ * An additional taint step for unsafe unpack vulnerabilities.
+ */
+private class UnsafeUnpackAdditionalDataFlowStep extends UnsafeUnpackAdditionalFlowStep {
+ override predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
+ exists(CallExpr initCall, CallExpr call |
+ // If a zip file is remotely downloaded the destination path is tainted
+ call.getStaticTarget().(Method).hasQualifiedName("Data", "write(to:options:)") and
+ call.getQualifier() = initCall and
+ initCall.getStaticTarget().(Initializer).getEnclosingDecl().(TypeDecl).getName() = "Data" and
+ nodeFrom.asExpr() = initCall and
+ nodeTo.asExpr() = call.getAnArgument().getExpr()
+ )
+ }
+}
+
+/**
+ * A barrier for unsafe unpack vulnerabilities.
+ */
+private class UnsafeUnpackDefaultBarrier extends UnsafeUnpackBarrier {
+ UnsafeUnpackDefaultBarrier() {
+ // any numeric type
+ this.asExpr().getType().getUnderlyingType().getABaseType*().getName() =
+ ["Numeric", "SignedInteger", "UnsignedInteger"]
+ }
+}
+
import UnsafeUnpackFlow::PathGraph
from UnsafeUnpackFlow::PathNode sourceNode, UnsafeUnpackFlow::PathNode sinkNode
From 777d0a43de866d3bdacfe0b45b9b853ccb4b6091 Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 09:50:43 +0000
Subject: [PATCH 03/18] unified: Port unsafe unpacking query to unified Swift
Reuse the existing source and unsafe-unpack sink models and propagate downloaded Data into the written path's post-update value. The original two inline alert expectations and source expectation pass. Numeric barriers remain a TODO until the necessary type information is available.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
.../queries/security/CWE-022/UnsafeUnpack.ql | 119 ++++--------------
.../UnsafeUnpack/UnsafeUnpack.expected | 21 ++--
.../CWE-022/UnsafeUnpack/UnsafeUnpack.qlref | 5 +-
3 files changed, 35 insertions(+), 110 deletions(-)
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
index 838993b3d1f9..74f78692a9e9 100644
--- a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
@@ -7,126 +7,49 @@
* @problem.severity error
* @security-severity 7.5
* @precision high
- * @id swift/unsafe-unpacking
+ * @id unified/swift/unsafe-unpacking
* @tags security
* experimental
* external/cwe/cwe-022
*/
-import swift
-import codeql.swift.dataflow.TaintTracking
-import codeql.swift.dataflow.FlowSources
-import codeql.swift.dataflow.DataFlow
-import codeql.swift.dataflow.ExternalFlow
+import unified
-// Copied from swift/ql/lib/codeql/swift/security/UnsafeUnpackQuery.qll.
/**
- * Provides default sources, sinks and sanitizers for reasoning about
- * unsafe unpack vulnerabilities, as well as extension points for
- * adding your own.
- */
-
-/**
- * A taint configuration for tainted data that reaches a unsafe unpack sink.
+ * A taint configuration for tainted data that reaches an unsafe unpack sink.
*/
module UnsafeUnpackConfig implements DataFlow::ConfigSig {
- predicate isSource(DataFlow::Node node) {
- node instanceof FlowSource or node instanceof RemoteFlowSource
- }
+ predicate isSource(DataFlow::Node node) { Models::isSource(node, _) }
- predicate isSink(DataFlow::Node node) { node instanceof UnsafeUnpackSink }
+ predicate isSink(DataFlow::Node node) { Models::isSink(node, "unsafe-unpack") }
- predicate isBarrier(DataFlow::Node barrier) { barrier instanceof UnsafeUnpackBarrier }
+ predicate isBarrier(DataFlow::Node node) {
+ // TODO: Port numeric barriers from original Swift's UnsafeUnpackExtensions.qll.
+ none()
+ }
predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
- any(UnsafeUnpackAdditionalFlowStep s).step(nodeFrom, nodeTo)
+ exists(CallExpr initCall, CallExpr call, MemberAccessExpr member, Argument arg |
+ // Downloading data to a file taints the path used for subsequent extraction.
+ call.getCallee() = member and
+ member.getMemberName() = "write" and
+ member.getBase() = initCall and
+ initCall.getCallee().(Identifier).getValue() = "Data" and
+ arg = call.getAnArgument() and
+ arg.getName() = "to" and
+ nodeFrom.isResultValue(initCall) and
+ nodeTo.isPostUpdate(arg.getValue())
+ )
}
predicate observeDiffInformedIncrementalMode() { any() }
}
/**
- * Detect taint flow of tainted data that reaches a unsafe unpack sink.
+ * Detect taint flow of tainted data that reaches an unsafe unpack sink.
*/
module UnsafeUnpackFlow = TaintTracking::Global;
-// Copied from swift/ql/lib/codeql/swift/security/UnsafeUnpackExtensions.qll.
-/**
- * Provides default sources, sinks and sanitizers for reasoning about
- * unsafe unpack vulnerabilities, as well as extension points for
- * adding your own.
- */
-
-/**
- * A dataflow source for unsafe unpack vulnerabilities.
- */
-abstract class UnsafeUnpackSource extends DataFlow::Node { }
-
-/**
- * A dataflow sink for unsafe unpack vulnerabilities.
- */
-abstract class UnsafeUnpackSink extends DataFlow::Node { }
-
-/**
- * A barrier for unsafe unpack vulnerabilities.
- */
-abstract class UnsafeUnpackBarrier extends DataFlow::Node { }
-
-/**
- * A unit class for adding additional flow steps.
- */
-class UnsafeUnpackAdditionalFlowStep extends Unit {
- /**
- * Holds if the step from `node1` to `node2` should be considered a flow
- * step for paths related to unsafe unpack vulnerabilities.
- */
- abstract predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo);
-}
-
-/**
- * A sink defined in a CSV model.
- */
-private class DefaultUnsafeUnpackSink extends UnsafeUnpackSink {
- DefaultUnsafeUnpackSink() { sinkNode(this, "unsafe-unpack") }
-}
-
-private class UnsafeUnpackSinks extends SinkModelCsv {
- override predicate row(string row) {
- row =
- [
- ";Zip;true;unzipFile(_:destination:overwrite:password:progress:fileOutputHandler:);;;Argument[0];unsafe-unpack",
- ";FileManager;true;unzipItem(at:to:skipCRC32:progress:pathEncoding:);;;Argument[0];unsafe-unpack",
- ]
- }
-}
-
-/**
- * An additional taint step for unsafe unpack vulnerabilities.
- */
-private class UnsafeUnpackAdditionalDataFlowStep extends UnsafeUnpackAdditionalFlowStep {
- override predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
- exists(CallExpr initCall, CallExpr call |
- // If a zip file is remotely downloaded the destination path is tainted
- call.getStaticTarget().(Method).hasQualifiedName("Data", "write(to:options:)") and
- call.getQualifier() = initCall and
- initCall.getStaticTarget().(Initializer).getEnclosingDecl().(TypeDecl).getName() = "Data" and
- nodeFrom.asExpr() = initCall and
- nodeTo.asExpr() = call.getAnArgument().getExpr()
- )
- }
-}
-
-/**
- * A barrier for unsafe unpack vulnerabilities.
- */
-private class UnsafeUnpackDefaultBarrier extends UnsafeUnpackBarrier {
- UnsafeUnpackDefaultBarrier() {
- // any numeric type
- this.asExpr().getType().getUnderlyingType().getABaseType*().getName() =
- ["Numeric", "SignedInteger", "UnsignedInteger"]
- }
-}
-
import UnsafeUnpackFlow::PathGraph
from UnsafeUnpackFlow::PathNode sourceNode, UnsafeUnpackFlow::PathNode sinkNode
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
index 013959ee4505..604949794dd4 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
@@ -1,13 +1,14 @@
+#select
+| UnsafeUnpack.swift:64:27:64:32 | source | UnsafeUnpack.swift:62:9:62:48 | Data(...) | UnsafeUnpack.swift:64:27:64:32 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
+| UnsafeUnpack.swift:67:39:67:44 | source | UnsafeUnpack.swift:62:9:62:48 | Data(...) | UnsafeUnpack.swift:67:39:67:44 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
edges
-| UnsafeUnpack.swift:62:9:62:48 | call to Data.init(contentsOf:options:) | UnsafeUnpack.swift:62:60:62:60 | source | provenance | Config |
-| UnsafeUnpack.swift:62:60:62:60 | source | UnsafeUnpack.swift:64:27:64:27 | source | provenance | |
-| UnsafeUnpack.swift:62:60:62:60 | source | UnsafeUnpack.swift:67:39:67:39 | source | provenance | |
+| UnsafeUnpack.swift:62:9:62:48 | Data(...) | UnsafeUnpack.swift:62:60:62:65 | [post] source | provenance | |
+| UnsafeUnpack.swift:62:9:62:48 | Data(...) | UnsafeUnpack.swift:62:60:62:65 | [post] source | provenance | Config |
+| UnsafeUnpack.swift:62:60:62:65 | [post] source | UnsafeUnpack.swift:64:27:64:32 | source | provenance | |
+| UnsafeUnpack.swift:62:60:62:65 | [post] source | UnsafeUnpack.swift:67:39:67:44 | source | provenance | |
nodes
-| UnsafeUnpack.swift:62:9:62:48 | call to Data.init(contentsOf:options:) | semmle.label | call to Data.init(contentsOf:options:) |
-| UnsafeUnpack.swift:62:60:62:60 | source | semmle.label | source |
-| UnsafeUnpack.swift:64:27:64:27 | source | semmle.label | source |
-| UnsafeUnpack.swift:67:39:67:39 | source | semmle.label | source |
+| UnsafeUnpack.swift:62:9:62:48 | Data(...) | semmle.label | Data(...) |
+| UnsafeUnpack.swift:62:60:62:65 | [post] source | semmle.label | [post] source |
+| UnsafeUnpack.swift:64:27:64:32 | source | semmle.label | source |
+| UnsafeUnpack.swift:67:39:67:44 | source | semmle.label | source |
subpaths
-#select
-| UnsafeUnpack.swift:64:27:64:27 | source | UnsafeUnpack.swift:62:9:62:48 | call to Data.init(contentsOf:options:) | UnsafeUnpack.swift:64:27:64:27 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
-| UnsafeUnpack.swift:67:39:67:39 | source | UnsafeUnpack.swift:62:9:62:48 | call to Data.init(contentsOf:options:) | UnsafeUnpack.swift:67:39:67:39 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.qlref b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.qlref
index f637622e3a15..955e45651c88 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.qlref
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.qlref
@@ -1,2 +1,3 @@
-query: experimental/Security/CWE-022/UnsafeUnpack.ql
-postprocess: utils/test/InlineExpectationsTestQuery.ql
+query: queries/security/CWE-022/UnsafeUnpack.ql
+postprocess:
+ - utils/test/InlineExpectationsTestQuery.ql
From 5bb5894e75c5ca9934b7555edfe992668a271b97 Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 09:54:43 +0000
Subject: [PATCH 04/18] unified: Cover safe archive inputs in unsafe unpacking
tests
Check that trusted data written to an archive and a separate trusted archive extracted into a downloaded path do not trigger alerts. Both original detections remain unchanged; the learned baseline also passes without --learn.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
.../CWE-022/UnsafeUnpack/UnsafeUnpack.swift | 27 ++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
index 0f6a7cc8b280..7a7d236954b3 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
@@ -51,7 +51,7 @@ extension String {
// --- tests ---
-func testCommandInjectionQhelpExamples() {
+func testUnsafeUnpackQhelpExamples() {
guard let remoteURL = URL(string: "https://example.com/") else {
return
}
@@ -68,4 +68,29 @@ func testCommandInjectionQhelpExamples() {
} catch {
print("Error: \(error)")
}
+}
+
+func testTrustedArchive() throws {
+ let source = URL(fileURLWithPath: "/trusted.zip")
+ let destination = URL(fileURLWithPath: "/destination")
+
+ Data([0x50, 0x4b]).write(to: source)
+
+ try Zip.unzipFile(source, destination: destination, overwrite: true, password: nil)
+ try FileManager().unzipItem(at: source, to: destination)
+}
+
+func testUnrelatedArchive() throws {
+ guard let remoteURL = URL(string: "https://example.com/") else {
+ return
+ }
+
+ let downloadedArchive = URL(fileURLWithPath: "/downloaded.zip")
+ let trustedArchive = URL(fileURLWithPath: "/trusted.zip")
+
+ Data(contentsOf: remoteURL, options: []).write(to: downloadedArchive)
+
+ // Downloading to a path does not taint an unrelated archive extracted into that path.
+ try Zip.unzipFile(trustedArchive, destination: downloadedArchive, overwrite: true, password: nil)
+ try FileManager().unzipItem(at: trustedArchive, to: downloadedArchive)
}
\ No newline at end of file
From c572613285ad5c0e5ef6c40036e37a5252e9b5ef Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 09:56:12 +0000
Subject: [PATCH 05/18] unified: Clarify unsafe unpacking help and safe example
Use the correct ZipArchive module name, describe the version-dependent risks, and fix the free-function example's invalid self reference. Validate help generation, XML and all sample references; the external Ostorlab reference was also verified.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
.../security/CWE-022/UnsafeUnpack.qhelp | 20 +++++++++++--------
.../security/CWE-022/ZipArchiveGood.swift | 2 +-
2 files changed, 13 insertions(+), 9 deletions(-)
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
index 6c53b3a789a5..d0236f562396 100644
--- a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
@@ -6,31 +6,35 @@
-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 helps
-overwrite sensitive user data and, in some cases, can lead to code execution if an
+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.
-Consider using a safer module, such as: ZIPArchive
+Use an up-to-date archive library that validates entry paths and symlink targets,
+such as ZipArchive. Ensure that all extracted files remain within the
+destination directory.
-The following examples unpacks a remote zip using `Zip.unzipFile()` which is vulnerable to path traversal.
+The following example unpacks a remote zip using Zip.unzipFile().
+Versions of this library that do not validate archive entry paths are vulnerable to path traversal.
-The following examples unpacks a remote zip using `fileManager.unzipItem()` which is vulnerable to symlink path traversal.
+The following example unpacks a remote zip using fileManager.unzipItem().
+Versions of this library that do not validate symlink targets are vulnerable to symlink path traversal.
-Consider using a safer module, such as: ZIPArchive
+The following example uses ZipArchive instead.
diff --git a/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift b/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
index e30f17c16923..d43ce0c09ee1 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
@@ -11,7 +11,7 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
try Data(contentsOf: remoteURL).write(to: source)
// ZipArchive is safe
- try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: self)
+ try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
} catch {
}
}
From 1802558c675bfa607e7db5ea418997dbfc8389a6 Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 10:22:00 +0000
Subject: [PATCH 06/18] Unified: Tiny layout adjustments.
---
.../src/queries/security/CWE-022/ZipArchiveGood.swift | 9 ++++-----
unified/ql/src/queries/security/CWE-022/ZipBad.swift | 10 ++++------
.../queries/security/CWE-022/ZipFoundationBad.swift | 10 ++++------
.../security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift | 4 ++--
4 files changed, 14 insertions(+), 19 deletions(-)
diff --git a/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift b/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
index d43ce0c09ee1..7212dbfd488e 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
@@ -4,10 +4,9 @@ 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
+ // Malicious zip is downloaded
try Data(contentsOf: remoteURL).write(to: source)
// ZipArchive is safe
@@ -17,9 +16,9 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
}
func main() {
- let sourcePath = "/sourcePath"
- let destinationPath = "/destinationPath"
+ let sourcePath = "/sourcePath"
+ let destinationPath = "/destinationPath"
unzipFile(at: sourcePath, to: destinationPath)
}
-main()
\ No newline at end of file
+main()
diff --git a/unified/ql/src/queries/security/CWE-022/ZipBad.swift b/unified/ql/src/queries/security/CWE-022/ZipBad.swift
index 4a90e6b3ea86..7e2fb3d2b7b2 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipBad.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipBad.swift
@@ -1,15 +1,13 @@
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
+ // Malicious zip is downloaded
try Data(contentsOf: remoteURL).write(to: source)
let fileManager = FileManager()
@@ -20,9 +18,9 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
}
func main() {
- let sourcePath = "/sourcePath"
- let destinationPath = "/destinationPath"
+ let sourcePath = "/sourcePath"
+ let destinationPath = "/destinationPath"
unzipFile(at: sourcePath, to: destinationPath)
}
-main()
\ No newline at end of file
+main()
diff --git a/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift b/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
index 7dfd86feffdb..700fdaea2e97 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
@@ -1,15 +1,13 @@
import Foundation
import ZIPFoundation
-
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
+ // Malicious zip is downloaded
try Data(contentsOf: remoteURL).write(to: source)
let fileManager = FileManager()
@@ -20,9 +18,9 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
}
func main() {
- let sourcePath = "/sourcePath"
- let destinationPath = "/destinationPath"
+ let sourcePath = "/sourcePath"
+ let destinationPath = "/destinationPath"
unzipFile(at: sourcePath, to: destinationPath)
}
-main()
\ No newline at end of file
+main()
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
index 7a7d236954b3..c84d42a97f59 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
@@ -1,5 +1,6 @@
// --- stubs ---
+
struct URL
{
init?(string: String) {}
@@ -10,7 +11,6 @@ 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 {
}
@@ -93,4 +93,4 @@ func testUnrelatedArchive() throws {
// Downloading to a path does not taint an unrelated archive extracted into that path.
try Zip.unzipFile(trustedArchive, destination: downloadedArchive, overwrite: true, password: nil)
try FileManager().unzipItem(at: trustedArchive, to: downloadedArchive)
-}
\ No newline at end of file
+}
From 7c0b36f73ba30129afe5dfc4fc2319b25d1d9867 Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 10:25:13 +0000
Subject: [PATCH 07/18] Unified: Delete supporting code from the examples that
does not make the issue clearer.
---
.../ql/src/queries/security/CWE-022/ZipArchiveGood.swift | 8 --------
unified/ql/src/queries/security/CWE-022/ZipBad.swift | 8 --------
.../src/queries/security/CWE-022/ZipFoundationBad.swift | 8 --------
3 files changed, 24 deletions(-)
diff --git a/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift b/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
index 7212dbfd488e..d6b14e910320 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
@@ -14,11 +14,3 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
} catch {
}
}
-
-func main() {
- let sourcePath = "/sourcePath"
- let destinationPath = "/destinationPath"
- unzipFile(at: sourcePath, to: destinationPath)
-}
-
-main()
diff --git a/unified/ql/src/queries/security/CWE-022/ZipBad.swift b/unified/ql/src/queries/security/CWE-022/ZipBad.swift
index 7e2fb3d2b7b2..cf5d1e6357ef 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipBad.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipBad.swift
@@ -16,11 +16,3 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
} catch {
}
}
-
-func main() {
- let sourcePath = "/sourcePath"
- let destinationPath = "/destinationPath"
- unzipFile(at: sourcePath, to: destinationPath)
-}
-
-main()
diff --git a/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift b/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
index 700fdaea2e97..cafb8925e705 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
@@ -16,11 +16,3 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
} catch {
}
}
-
-func main() {
- let sourcePath = "/sourcePath"
- let destinationPath = "/destinationPath"
- unzipFile(at: sourcePath, to: destinationPath)
-}
-
-main()
From 30c942cce4bce6a5058ee82dc93a339fdc8d3b9c Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 10:28:23 +0000
Subject: [PATCH 08/18] Unified: Fix the examples and include them accurately
in the query test.
---
.../security/CWE-022/ZipArchiveGood.swift | 1 +
.../src/queries/security/CWE-022/ZipBad.swift | 1 +
.../security/CWE-022/ZipFoundationBad.swift | 1 +
.../UnsafeUnpack/UnsafeUnpack.expected | 24 +++++----
.../CWE-022/UnsafeUnpack/UnsafeUnpack.swift | 52 +++++++++++++++----
5 files changed, 58 insertions(+), 21 deletions(-)
diff --git a/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift b/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
index d6b14e910320..87e45a063b84 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
@@ -12,5 +12,6 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
// ZipArchive is safe
try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
} catch {
+ }
}
}
diff --git a/unified/ql/src/queries/security/CWE-022/ZipBad.swift b/unified/ql/src/queries/security/CWE-022/ZipBad.swift
index cf5d1e6357ef..538b74b4fa25 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipBad.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipBad.swift
@@ -14,5 +14,6 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
// Malicious zip is unpacked
try Zip.unzipFile(source, destination: destination, overwrite: true, password: nil)
} catch {
+ }
}
}
diff --git a/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift b/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
index cafb8925e705..92c0fa5573bf 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
@@ -14,5 +14,6 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
// Malicious zip is unpacked
try fileManager.unzipItem(at:source, to: destination)
} catch {
+ }
}
}
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
index 604949794dd4..4e8d53eef2e6 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
@@ -1,14 +1,18 @@
#select
-| UnsafeUnpack.swift:64:27:64:32 | source | UnsafeUnpack.swift:62:9:62:48 | Data(...) | UnsafeUnpack.swift:64:27:64:32 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
-| UnsafeUnpack.swift:67:39:67:44 | source | UnsafeUnpack.swift:62:9:62:48 | Data(...) | UnsafeUnpack.swift:67:39:67:44 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
+| 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:82:38:82:43 | source | UnsafeUnpack.swift:78:13:78:39 | Data(...) | UnsafeUnpack.swift:82:38:82:43 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
edges
-| UnsafeUnpack.swift:62:9:62:48 | Data(...) | UnsafeUnpack.swift:62:60:62:65 | [post] source | provenance | |
-| UnsafeUnpack.swift:62:9:62:48 | Data(...) | UnsafeUnpack.swift:62:60:62:65 | [post] source | provenance | Config |
-| UnsafeUnpack.swift:62:60:62:65 | [post] source | UnsafeUnpack.swift:64:27:64:32 | source | provenance | |
-| UnsafeUnpack.swift:62:60:62:65 | [post] source | UnsafeUnpack.swift:67:39:67:44 | source | provenance | |
+| UnsafeUnpack.swift:61:13:61:39 | Data(...) | UnsafeUnpack.swift:61:51:61:56 | [post] source | provenance | |
+| UnsafeUnpack.swift:61:13:61:39 | Data(...) | UnsafeUnpack.swift:61:51:61:56 | [post] source | provenance | Config |
+| UnsafeUnpack.swift:61:51:61:56 | [post] source | UnsafeUnpack.swift:65:27:65:32 | source | provenance | |
+| UnsafeUnpack.swift:78:13:78:39 | Data(...) | UnsafeUnpack.swift:78:51:78:56 | [post] source | provenance | |
+| UnsafeUnpack.swift:78:13:78:39 | Data(...) | UnsafeUnpack.swift:78:51:78:56 | [post] source | provenance | Config |
+| UnsafeUnpack.swift:78:51:78:56 | [post] source | UnsafeUnpack.swift:82:38:82:43 | source | provenance | |
nodes
-| UnsafeUnpack.swift:62:9:62:48 | Data(...) | semmle.label | Data(...) |
-| UnsafeUnpack.swift:62:60:62:65 | [post] source | semmle.label | [post] source |
-| UnsafeUnpack.swift:64:27:64:32 | source | semmle.label | source |
-| UnsafeUnpack.swift:67:39:67:44 | source | semmle.label | source |
+| 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:78:13:78:39 | Data(...) | semmle.label | Data(...) |
+| UnsafeUnpack.swift:78:51:78:56 | [post] source | semmle.label | [post] source |
+| UnsafeUnpack.swift:82:38:82:43 | source | semmle.label | source |
subpaths
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
index c84d42a97f59..1258f7a9da01 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
@@ -51,22 +51,52 @@ extension String {
// --- tests ---
-func testUnsafeUnpackQhelpExamples() {
- guard let remoteURL = URL(string: "https://example.com/") else {
- return
- }
+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)
- let source = URL(fileURLWithPath: "/sourcePath")
- let destination = URL(fileURLWithPath: "/destination")
+ // Malicious zip is downloaded
+ try Data(contentsOf: remoteURL).write(to: source) // $ Source
- try Data(contentsOf: remoteURL, options: []).write(to: source) // $ Source
- do {
+ let fileManager = FileManager()
+ // Malicious zip is unpacked
try Zip.unzipFile(source, destination: destination, overwrite: true, password: nil) // $ Alert
+ } catch {
+ }
+ }
+}
+
+func testUnsafeUnpackQhelpExample2ZipFoundationBad(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()
- try fileManager.unzipItem(at: source, to: destination) // $ Alert
- } catch {
- print("Error: \(error)")
+ // Malicious zip is unpacked
+ try fileManager.unzipItem(at:source, to: destination) // $ Alert
+ } catch {
+ }
+ }
+}
+
+func testUnsafeUnpackQhelpExample3ZipArchiveGood(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 is safe
+ try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
+ } catch {
+ }
}
}
From f86a3fb6fde56673a445da0a337a3f3362384f22 Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 11:15:41 +0000
Subject: [PATCH 09/18] Unified: Update .qhelp and example comments based on
some research about the current state of these libraries.
---
.../queries/security/CWE-022/UnsafeUnpack.qhelp | 15 +++++++--------
.../queries/security/CWE-022/ZipArchiveGood.swift | 2 +-
.../ql/src/queries/security/CWE-022/ZipBad.swift | 2 +-
.../security/CWE-022/ZipFoundationBad.swift | 2 +-
.../CWE-022/UnsafeUnpack/UnsafeUnpack.swift | 8 ++++----
5 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
index d0236f562396..d873b110dbb9 100644
--- a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
@@ -16,25 +16,24 @@ attacker overwrites an application's shared object file.
Use an up-to-date archive library that validates entry paths and symlink targets,
-such as ZipArchive. Ensure that all extracted files remain within the
-destination directory.
+such as ZipArchive. Keep these validation checks enabled.
The following example unpacks a remote zip using Zip.unzipFile().
-Versions of this library that do not validate archive entry paths are vulnerable to path traversal.
+Zip 2.1.2 does not validate archive entry paths and is vulnerable to path traversal.
-The following example unpacks a remote zip using fileManager.unzipItem().
-Versions of this library that do not validate symlink targets are vulnerable to symlink path traversal.
+The following example uses fileManager.unzipItem().
+Older ZIPFoundation versions allowed symlink path traversal.
-
-The following example uses ZipArchive instead.
+The following example uses ZipArchive. Up-to-date versions of
+ZipArchive validate entry paths and symlink targets.
@@ -44,4 +43,4 @@ Ostorlab:
Zip Packages Exploitation.
-
\ No newline at end of file
+
diff --git a/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift b/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
index 87e45a063b84..ab3add8f40e0 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
@@ -9,7 +9,7 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
// Malicious zip is downloaded
try Data(contentsOf: remoteURL).write(to: source)
- // ZipArchive is safe
+ // ZipArchive validates paths and symlinks
try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
} catch {
}
diff --git a/unified/ql/src/queries/security/CWE-022/ZipBad.swift b/unified/ql/src/queries/security/CWE-022/ZipBad.swift
index 538b74b4fa25..b41706178ede 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipBad.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipBad.swift
@@ -11,7 +11,7 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
try Data(contentsOf: remoteURL).write(to: source)
let fileManager = FileManager()
- // Malicious zip is unpacked
+ // Malicious zip is unpacked; Zip 2.1.2 does not validate archive entry paths
try Zip.unzipFile(source, destination: destination, overwrite: true, password: nil)
} catch {
}
diff --git a/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift b/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
index 92c0fa5573bf..c40f1e3738c6 100644
--- a/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
+++ b/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
@@ -11,7 +11,7 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
try Data(contentsOf: remoteURL).write(to: source)
let fileManager = FileManager()
- // Malicious zip is unpacked
+ // Malicious zip is unpacked; older versions of ZIPFoundation allow symlink path traversal
try fileManager.unzipItem(at:source, to: destination)
} catch {
}
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
index 1258f7a9da01..085dd754f8d4 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
@@ -61,7 +61,7 @@ func testUnsafeUnpackQhelpExample1ZipBad(at sourcePath: String, to destinationPa
try Data(contentsOf: remoteURL).write(to: source) // $ Source
let fileManager = FileManager()
- // Malicious zip is unpacked
+ // 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
} catch {
}
@@ -78,7 +78,7 @@ func testUnsafeUnpackQhelpExample2ZipFoundationBad(at sourcePath: String, to des
try Data(contentsOf: remoteURL).write(to: source) // $ Source
let fileManager = FileManager()
- // Malicious zip is unpacked
+ // Malicious zip is unpacked; older versions of ZIPFoundation allow symlink path traversal
try fileManager.unzipItem(at:source, to: destination) // $ Alert
} catch {
}
@@ -92,8 +92,8 @@ func testUnsafeUnpackQhelpExample3ZipArchiveGood(at sourcePath: String, to desti
// Malicious zip is downloaded
try Data(contentsOf: remoteURL).write(to: source)
-
- // ZipArchive is safe
+v
+ // ZipArchive validates paths and symlinks
try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
} catch {
}
From c528fcaca60d605f22cd8919d768710bcbaf9b06 Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 11:29:50 +0000
Subject: [PATCH 10/18] Unified: Remove the ZipFoundation example as the
situation there is complicating the message.
---
.../security/CWE-022/UnsafeUnpack.qhelp | 6 -----
.../security/CWE-022/ZipFoundationBad.swift | 19 ----------------
.../UnsafeUnpack/UnsafeUnpack.expected | 14 ++++++------
.../CWE-022/UnsafeUnpack/UnsafeUnpack.swift | 22 +++++++++----------
4 files changed, 18 insertions(+), 43 deletions(-)
delete mode 100644 unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
index d873b110dbb9..c120525ce2e4 100644
--- a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
@@ -26,12 +26,6 @@ Zip 2.1.2 does not validate archive entry paths and is vulnerable to path traver
-
-The following example uses fileManager.unzipItem().
-Older ZIPFoundation versions allowed symlink path traversal.
-
-
-
The following example uses ZipArchive. Up-to-date versions of
ZipArchive validate entry paths and symlink targets.
diff --git a/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift b/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
deleted file mode 100644
index c40f1e3738c6..000000000000
--- a/unified/ql/src/queries/security/CWE-022/ZipFoundationBad.swift
+++ /dev/null
@@ -1,19 +0,0 @@
-import Foundation
-import ZIPFoundation
-
-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; older versions of ZIPFoundation allow symlink path traversal
- try fileManager.unzipItem(at:source, to: destination)
- } catch {
- }
- }
-}
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
index 4e8d53eef2e6..2e3776226c1c 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
@@ -1,18 +1,18 @@
#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:82:38:82:43 | source | UnsafeUnpack.swift:78:13:78:39 | Data(...) | UnsafeUnpack.swift:82:38:82:43 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
+| UnsafeUnpack.swift:97:38:97:43 | source | UnsafeUnpack.swift:93:13:93:39 | Data(...) | UnsafeUnpack.swift:97:38:97:43 | source | 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:13:61:39 | Data(...) | UnsafeUnpack.swift:61:51:61:56 | [post] source | provenance | Config |
| UnsafeUnpack.swift:61:51:61:56 | [post] source | UnsafeUnpack.swift:65:27:65:32 | source | provenance | |
-| UnsafeUnpack.swift:78:13:78:39 | Data(...) | UnsafeUnpack.swift:78:51:78:56 | [post] source | provenance | |
-| UnsafeUnpack.swift:78:13:78:39 | Data(...) | UnsafeUnpack.swift:78:51:78:56 | [post] source | provenance | Config |
-| UnsafeUnpack.swift:78:51:78:56 | [post] source | UnsafeUnpack.swift:82:38:82:43 | source | provenance | |
+| UnsafeUnpack.swift:93:13:93:39 | Data(...) | UnsafeUnpack.swift:93:51:93:56 | [post] source | provenance | |
+| UnsafeUnpack.swift:93:13:93:39 | Data(...) | UnsafeUnpack.swift:93:51:93:56 | [post] source | provenance | Config |
+| UnsafeUnpack.swift:93:51:93:56 | [post] source | UnsafeUnpack.swift:97:38:97:43 | source | 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:78:13:78:39 | Data(...) | semmle.label | Data(...) |
-| UnsafeUnpack.swift:78:51:78:56 | [post] source | semmle.label | [post] source |
-| UnsafeUnpack.swift:82:38:82:43 | source | semmle.label | source |
+| UnsafeUnpack.swift:93:13:93:39 | Data(...) | semmle.label | Data(...) |
+| UnsafeUnpack.swift:93:51:93:56 | [post] source | semmle.label | [post] source |
+| UnsafeUnpack.swift:97:38:97:43 | source | semmle.label | source |
subpaths
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
index 085dd754f8d4..f5ecbc2fbab3 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
@@ -68,33 +68,33 @@ func testUnsafeUnpackQhelpExample1ZipBad(at sourcePath: String, to destinationPa
}
}
-func testUnsafeUnpackQhelpExample2ZipFoundationBad(at sourcePath: String, to destinationPath: String) {
+func testUnsafeUnpackQhelpExample2ZipArchiveGood(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
+ try Data(contentsOf: remoteURL).write(to: source)
- let fileManager = FileManager()
- // Malicious zip is unpacked; older versions of ZIPFoundation allow symlink path traversal
- try fileManager.unzipItem(at:source, to: destination) // $ Alert
+ // ZipArchive validates paths and symlinks
+ try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
} catch {
}
}
}
-func testUnsafeUnpackQhelpExample3ZipArchiveGood(at sourcePath: String, to destinationPath: String) {
+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)
-v
- // ZipArchive validates paths and symlinks
- try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
+ 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
} catch {
}
}
From 03d1de99b142ab914815c447adc5789149d3cce0 Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 13:00:35 +0000
Subject: [PATCH 11/18] Unified: Rename the samples to be consistent with the
.qhelp and query files names.
---
unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp | 4 ++--
.../security/CWE-022/{ZipBad.swift => UnsafeUnpackBad.swift} | 0
.../CWE-022/{ZipArchiveGood.swift => UnsafeUnpackGood.swift} | 0
3 files changed, 2 insertions(+), 2 deletions(-)
rename unified/ql/src/queries/security/CWE-022/{ZipBad.swift => UnsafeUnpackBad.swift} (100%)
rename unified/ql/src/queries/security/CWE-022/{ZipArchiveGood.swift => UnsafeUnpackGood.swift} (100%)
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
index c120525ce2e4..8c62389f9576 100644
--- a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp
@@ -24,11 +24,11 @@ such as ZipArchive. Keep these validation checks enabled.
The following example unpacks a remote zip using Zip.unzipFile().
Zip 2.1.2 does not validate archive entry paths and is vulnerable to path traversal.
-
+
The following example uses ZipArchive. Up-to-date versions of
ZipArchive validate entry paths and symlink targets.
-
+
diff --git a/unified/ql/src/queries/security/CWE-022/ZipBad.swift b/unified/ql/src/queries/security/CWE-022/UnsafeUnpackBad.swift
similarity index 100%
rename from unified/ql/src/queries/security/CWE-022/ZipBad.swift
rename to unified/ql/src/queries/security/CWE-022/UnsafeUnpackBad.swift
diff --git a/unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift b/unified/ql/src/queries/security/CWE-022/UnsafeUnpackGood.swift
similarity index 100%
rename from unified/ql/src/queries/security/CWE-022/ZipArchiveGood.swift
rename to unified/ql/src/queries/security/CWE-022/UnsafeUnpackGood.swift
From c2870cbb0cd95f2c548b20f501b2300d21d651cc Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 13:15:32 +0000
Subject: [PATCH 12/18] Unified: Use remote taint source for now (active taint
sources will be a better fit, when supported).
---
unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
index 74f78692a9e9..fd58946d971f 100644
--- a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
@@ -19,7 +19,7 @@ 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, _) }
+ predicate isSource(DataFlow::Node node) { Models::isSource(node, "remote") }
predicate isSink(DataFlow::Node node) { Models::isSink(node, "unsafe-unpack") }
From df8a8ff526164fad0c59306891ae84a83b3a2b05 Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 13:20:10 +0000
Subject: [PATCH 13/18] unified: Cover stored downloads with explicit writing
options
Add an unsafe-unpacking regression where remote Data is stored before write(to:options:). Record the currently missing source and two alerts, and check that extraction before the write remains unflagged.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
.../src/queries/security/CWE-022/UnsafeUnpack.ql | 12 ++++++++++--
.../CWE-022/UnsafeUnpack/UnsafeUnpack.swift | 14 ++++++++++++++
2 files changed, 24 insertions(+), 2 deletions(-)
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
index fd58946d971f..fbb759417609 100644
--- a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
@@ -30,9 +30,17 @@ module UnsafeUnpackConfig implements DataFlow::ConfigSig {
predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
exists(CallExpr initCall, CallExpr call, MemberAccessExpr member, Argument arg |
- // Downloading data to a file taints the path used for subsequent extraction.
+ // Downloading data to a file taints the path used for subsequent extraction. That is, we
+ // want flow from:
+ // ```
+ // try Data(contentsOf: remoteURL).write(to: source)
+ // ```
+ // to:
+ // ```
+ // try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
+ // ```
call.getCallee() = member and
- member.getMemberName() = "write" and
+ member.getMemberName() = "write" and // `Data::write(to:options?:)` I think.
member.getBase() = initCall and
initCall.getCallee().(Identifier).getValue() = "Data" and
arg = call.getAnArgument() and
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
index f5ecbc2fbab3..cb63a5aa0a41 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
@@ -124,3 +124,17 @@ func testUnrelatedArchive() throws {
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: []) // $ MISSING: Source
+
+ try Zip.unzipFile(archive, destination: destination, overwrite: true, password: nil)
+
+ data.write(to: archive, options: [])
+
+ try Zip.unzipFile(archive, destination: destination, overwrite: true, password: nil) // $ MISSING: Alert
+ try FileManager().unzipItem(at: archive, to: destination) // $ MISSING: Alert
+}
From 326334c69ad38e8b16573a6deff9c54cd6d9a83b Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 13:26:10 +0000
Subject: [PATCH 14/18] unified: Model Data.write flow with a taint summary
Replace unsafe-unpacking's constructor-specific flow step with a Data.write(to:options?:) receiver-to-destination summary. The output represents the path after the write, allowing stored Data and explicit writing options while preserving the original alerts and safe cases.
UnsafeUnpack, PathInjection and CleartextLogging tests pass without learning; only the two intended stored-download alerts are added.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
unified/ql/lib/ext/legacy-swift.model.json | 1 +
.../queries/security/CWE-022/UnsafeUnpack.ql | 22 -------------------
.../UnsafeUnpack/UnsafeUnpack.expected | 15 +++++++++++--
.../CWE-022/UnsafeUnpack/UnsafeUnpack.swift | 6 ++---
4 files changed, 17 insertions(+), 27 deletions(-)
diff --git a/unified/ql/lib/ext/legacy-swift.model.json b/unified/ql/lib/ext/legacy-swift.model.json
index 284c3af43d30..4d23de3c7840 100644
--- a/unified/ql/lib/ext/legacy-swift.model.json
+++ b/unified/ql/lib/ext/legacy-swift.model.json
@@ -988,6 +988,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"],
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
index fbb759417609..e284c68a3d67 100644
--- a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
@@ -28,28 +28,6 @@ module UnsafeUnpackConfig implements DataFlow::ConfigSig {
none()
}
- predicate isAdditionalFlowStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
- exists(CallExpr initCall, CallExpr call, MemberAccessExpr member, Argument arg |
- // Downloading data to a file taints the path used for subsequent extraction. That is, we
- // want flow from:
- // ```
- // try Data(contentsOf: remoteURL).write(to: source)
- // ```
- // to:
- // ```
- // try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
- // ```
- call.getCallee() = member and
- member.getMemberName() = "write" and // `Data::write(to:options?:)` I think.
- member.getBase() = initCall and
- initCall.getCallee().(Identifier).getValue() = "Data" and
- arg = call.getAnArgument() and
- arg.getName() = "to" and
- nodeFrom.isResultValue(initCall) and
- nodeTo.isPostUpdate(arg.getValue())
- )
- }
-
predicate observeDiffInformedIncrementalMode() { any() }
}
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
index 2e3776226c1c..f976b816c018 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
@@ -1,13 +1,18 @@
#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:97:38:97:43 | source | UnsafeUnpack.swift:93:13:93:39 | Data(...) | UnsafeUnpack.swift:97:38:97:43 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
+| UnsafeUnpack.swift:138:23:138:29 | archive | UnsafeUnpack.swift:132:16:132:55 | Data(...) | UnsafeUnpack.swift:138:23:138:29 | archive | Unsafe unpacking from a malicious zip retrieved from a remote location. |
+| UnsafeUnpack.swift:139:37:139:43 | archive | UnsafeUnpack.swift:132:16:132:55 | Data(...) | UnsafeUnpack.swift:139:37:139:43 | 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:13:61:39 | Data(...) | UnsafeUnpack.swift:61:51:61:56 | [post] source | provenance | Config |
| UnsafeUnpack.swift:61:51:61:56 | [post] source | UnsafeUnpack.swift:65:27:65:32 | source | provenance | |
| UnsafeUnpack.swift:93:13:93:39 | Data(...) | UnsafeUnpack.swift:93:51:93:56 | [post] source | provenance | |
-| UnsafeUnpack.swift:93:13:93:39 | Data(...) | UnsafeUnpack.swift:93:51:93:56 | [post] source | provenance | Config |
| UnsafeUnpack.swift:93:51:93:56 | [post] source | UnsafeUnpack.swift:97:38:97:43 | source | provenance | |
+| UnsafeUnpack.swift:132:9:132:12 | data | UnsafeUnpack.swift:136:5:136:8 | data | provenance | |
+| UnsafeUnpack.swift:132:16:132:55 | Data(...) | UnsafeUnpack.swift:132:9:132:12 | data | provenance | |
+| UnsafeUnpack.swift:136:5:136:8 | data | UnsafeUnpack.swift:136:20:136:26 | [post] archive | provenance | |
+| UnsafeUnpack.swift:136:20:136:26 | [post] archive | UnsafeUnpack.swift:138:23:138:29 | archive | provenance | |
+| UnsafeUnpack.swift:136:20:136:26 | [post] archive | UnsafeUnpack.swift:139:37:139:43 | 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 |
@@ -15,4 +20,10 @@ nodes
| UnsafeUnpack.swift:93:13:93:39 | Data(...) | semmle.label | Data(...) |
| UnsafeUnpack.swift:93:51:93:56 | [post] source | semmle.label | [post] source |
| UnsafeUnpack.swift:97:38:97:43 | source | semmle.label | source |
+| UnsafeUnpack.swift:132:9:132:12 | data | semmle.label | data |
+| UnsafeUnpack.swift:132:16:132:55 | Data(...) | semmle.label | Data(...) |
+| UnsafeUnpack.swift:136:5:136:8 | data | semmle.label | data |
+| UnsafeUnpack.swift:136:20:136:26 | [post] archive | semmle.label | [post] archive |
+| UnsafeUnpack.swift:138:23:138:29 | archive | semmle.label | archive |
+| UnsafeUnpack.swift:139:37:139:43 | archive | semmle.label | archive |
subpaths
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
index cb63a5aa0a41..ef57cab3388f 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
@@ -129,12 +129,12 @@ 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: []) // $ MISSING: Source
+ let data = Data(contentsOf: remoteURL, options: []) // $ Source
try Zip.unzipFile(archive, destination: destination, overwrite: true, password: nil)
data.write(to: archive, options: [])
- try Zip.unzipFile(archive, destination: destination, overwrite: true, password: nil) // $ MISSING: Alert
- try FileManager().unzipItem(at: archive, to: destination) // $ MISSING: Alert
+ try Zip.unzipFile(archive, destination: destination, overwrite: true, password: nil) // $ Alert
+ try FileManager().unzipItem(at: archive, to: destination) // $ Alert
}
From 76122425e34b74bf83e9d7ba987185b70e928f3f Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 13:46:26 +0000
Subject: [PATCH 15/18] Unified: Simplify and explain the new test cases a
little.
---
.../CWE-022/UnsafeUnpack/UnsafeUnpack.expected | 17 +++++++----------
.../CWE-022/UnsafeUnpack/UnsafeUnpack.swift | 13 +++++++------
2 files changed, 14 insertions(+), 16 deletions(-)
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
index f976b816c018..220904aecc1d 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
@@ -1,18 +1,16 @@
#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:97:38:97:43 | source | UnsafeUnpack.swift:93:13:93:39 | Data(...) | UnsafeUnpack.swift:97:38:97:43 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
-| UnsafeUnpack.swift:138:23:138:29 | archive | UnsafeUnpack.swift:132:16:132:55 | Data(...) | UnsafeUnpack.swift:138:23:138:29 | archive | Unsafe unpacking from a malicious zip retrieved from a remote location. |
-| UnsafeUnpack.swift:139:37:139:43 | archive | UnsafeUnpack.swift:132:16:132:55 | Data(...) | UnsafeUnpack.swift:139:37:139:43 | archive | Unsafe unpacking from a malicious zip retrieved from a remote location. |
+| UnsafeUnpack.swift:140:23:140:29 | archive | UnsafeUnpack.swift:132:16:132:55 | Data(...) | UnsafeUnpack.swift:140:23:140: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:93:13:93:39 | Data(...) | UnsafeUnpack.swift:93:51:93:56 | [post] source | provenance | |
| UnsafeUnpack.swift:93:51:93:56 | [post] source | UnsafeUnpack.swift:97:38:97:43 | source | provenance | |
-| UnsafeUnpack.swift:132:9:132:12 | data | UnsafeUnpack.swift:136:5:136:8 | data | provenance | |
+| UnsafeUnpack.swift:132:9:132:12 | data | UnsafeUnpack.swift:137:5:137:8 | data | provenance | |
| UnsafeUnpack.swift:132:16:132:55 | Data(...) | UnsafeUnpack.swift:132:9:132:12 | data | provenance | |
-| UnsafeUnpack.swift:136:5:136:8 | data | UnsafeUnpack.swift:136:20:136:26 | [post] archive | provenance | |
-| UnsafeUnpack.swift:136:20:136:26 | [post] archive | UnsafeUnpack.swift:138:23:138:29 | archive | provenance | |
-| UnsafeUnpack.swift:136:20:136:26 | [post] archive | UnsafeUnpack.swift:139:37:139:43 | archive | provenance | |
+| UnsafeUnpack.swift:137:5:137:8 | data | UnsafeUnpack.swift:137:20:137:26 | [post] archive | provenance | |
+| UnsafeUnpack.swift:137:20:137:26 | [post] archive | UnsafeUnpack.swift:140:23:140: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 |
@@ -22,8 +20,7 @@ nodes
| UnsafeUnpack.swift:97:38:97:43 | source | semmle.label | source |
| UnsafeUnpack.swift:132:9:132:12 | data | semmle.label | data |
| UnsafeUnpack.swift:132:16:132:55 | Data(...) | semmle.label | Data(...) |
-| UnsafeUnpack.swift:136:5:136:8 | data | semmle.label | data |
-| UnsafeUnpack.swift:136:20:136:26 | [post] archive | semmle.label | [post] archive |
-| UnsafeUnpack.swift:138:23:138:29 | archive | semmle.label | archive |
-| UnsafeUnpack.swift:139:37:139:43 | archive | semmle.label | archive |
+| UnsafeUnpack.swift:137:5:137:8 | data | semmle.label | data |
+| UnsafeUnpack.swift:137:20:137:26 | [post] archive | semmle.label | [post] archive |
+| UnsafeUnpack.swift:140:23:140:29 | archive | semmle.label | archive |
subpaths
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
index ef57cab3388f..a658f0668148 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
@@ -104,23 +104,23 @@ 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 {
- guard let remoteURL = URL(string: "https://example.com/") else {
- return
- }
-
+ 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)
- // Downloading to a path does not taint an unrelated archive extracted into that path.
+ // unzip `trustedArchive` over `downloadedArchive` (safe)
try Zip.unzipFile(trustedArchive, destination: downloadedArchive, overwrite: true, password: nil)
try FileManager().unzipItem(at: trustedArchive, to: downloadedArchive)
}
@@ -133,8 +133,9 @@ func testStoredDownloadWithExplicitWritingOptions() throws {
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
- try FileManager().unzipItem(at: archive, to: destination) // $ Alert
}
From a9e0289e1949671a60c4c1c6776f700e9f3859f9 Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Tue, 1 Dec 2026 14:48:26 +0000
Subject: [PATCH 16/18] Unified: There's no reason to consider this
'experimental' any more (relative to the other unified queries).
---
unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql | 4 ----
1 file changed, 4 deletions(-)
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
index e284c68a3d67..2686e18b6de4 100644
--- a/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
@@ -9,7 +9,6 @@
* @precision high
* @id unified/swift/unsafe-unpacking
* @tags security
- * experimental
* external/cwe/cwe-022
*/
@@ -31,9 +30,6 @@ module UnsafeUnpackConfig implements DataFlow::ConfigSig {
predicate observeDiffInformedIncrementalMode() { any() }
}
-/**
- * Detect taint flow of tainted data that reaches an unsafe unpack sink.
- */
module UnsafeUnpackFlow = TaintTracking::Global;
import UnsafeUnpackFlow::PathGraph
From 386bf9d9d61e0007b083b109e0fca9e3ebde9c7f Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Thu, 3 Dec 2026 10:26:18 +0000
Subject: [PATCH 17/18] Unified: Fix test / example compile errors.
---
.../security/CWE-022/UnsafeUnpackBad.swift | 2 --
.../security/CWE-022/UnsafeUnpackGood.swift | 2 --
.../UnsafeUnpack/UnsafeUnpack.expected | 32 +++++++++----------
.../CWE-022/UnsafeUnpack/UnsafeUnpack.swift | 8 +----
4 files changed, 17 insertions(+), 27 deletions(-)
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpackBad.swift b/unified/ql/src/queries/security/CWE-022/UnsafeUnpackBad.swift
index b41706178ede..3dac40943dde 100644
--- a/unified/ql/src/queries/security/CWE-022/UnsafeUnpackBad.swift
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpackBad.swift
@@ -13,7 +13,5 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
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)
- } catch {
- }
}
}
diff --git a/unified/ql/src/queries/security/CWE-022/UnsafeUnpackGood.swift b/unified/ql/src/queries/security/CWE-022/UnsafeUnpackGood.swift
index ab3add8f40e0..deb53f7ee469 100644
--- a/unified/ql/src/queries/security/CWE-022/UnsafeUnpackGood.swift
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpackGood.swift
@@ -11,7 +11,5 @@ func unzipFile(at sourcePath: String, to destinationPath: String) {
// ZipArchive validates paths and symlinks
try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
- } catch {
- }
}
}
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
index 220904aecc1d..c112b82171f7 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected
@@ -1,26 +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:97:38:97:43 | source | UnsafeUnpack.swift:93:13:93:39 | Data(...) | UnsafeUnpack.swift:97:38:97:43 | source | Unsafe unpacking from a malicious zip retrieved from a remote location. |
-| UnsafeUnpack.swift:140:23:140:29 | archive | UnsafeUnpack.swift:132:16:132:55 | Data(...) | UnsafeUnpack.swift:140:23:140:29 | archive | 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:93:13:93:39 | Data(...) | UnsafeUnpack.swift:93:51:93:56 | [post] source | provenance | |
-| UnsafeUnpack.swift:93:51:93:56 | [post] source | UnsafeUnpack.swift:97:38:97:43 | source | provenance | |
-| UnsafeUnpack.swift:132:9:132:12 | data | UnsafeUnpack.swift:137:5:137:8 | data | provenance | |
-| UnsafeUnpack.swift:132:16:132:55 | Data(...) | UnsafeUnpack.swift:132:9:132:12 | data | provenance | |
-| UnsafeUnpack.swift:137:5:137:8 | data | UnsafeUnpack.swift:137:20:137:26 | [post] archive | provenance | |
-| UnsafeUnpack.swift:137:20:137:26 | [post] archive | UnsafeUnpack.swift:140:23:140:29 | archive | 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:93:13:93:39 | Data(...) | semmle.label | Data(...) |
-| UnsafeUnpack.swift:93:51:93:56 | [post] source | semmle.label | [post] source |
-| UnsafeUnpack.swift:97:38:97:43 | source | semmle.label | source |
-| UnsafeUnpack.swift:132:9:132:12 | data | semmle.label | data |
-| UnsafeUnpack.swift:132:16:132:55 | Data(...) | semmle.label | Data(...) |
-| UnsafeUnpack.swift:137:5:137:8 | data | semmle.label | data |
-| UnsafeUnpack.swift:137:20:137:26 | [post] archive | semmle.label | [post] archive |
-| UnsafeUnpack.swift:140:23:140:29 | archive | semmle.label | archive |
+| 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
diff --git a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
index a658f0668148..a832906dd410 100644
--- a/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
+++ b/unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift
@@ -29,7 +29,7 @@ class Data : DataProtocol {
struct WritingOptions : OptionSet { let rawValue: Int }
init(_ elements: S) { count = 0 }
- init(contentsOf: URL, options: ReadingOptions) { count = 0 }
+ init(contentsOf: URL, options: ReadingOptions = []) { count = 0 }
func write(to: URL, options: Data.WritingOptions = []) {}
var count: Int
@@ -63,8 +63,6 @@ func testUnsafeUnpackQhelpExample1ZipBad(at sourcePath: String, to destinationPa
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
- } catch {
- }
}
}
@@ -78,8 +76,6 @@ func testUnsafeUnpackQhelpExample2ZipArchiveGood(at sourcePath: String, to desti
// ZipArchive validates paths and symlinks
try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
- } catch {
- }
}
}
@@ -95,8 +91,6 @@ func testUnsafeUnpackZipFoundationBad(at sourcePath: String, to destinationPath:
let fileManager = FileManager()
// Malicious zip is unpacked; older versions of ZIPFoundation allow symlink path traversal
try fileManager.unzipItem(at:source, to: destination) // $ Alert
- } catch {
- }
}
}
From bbda88240d78355b3268f08950c5bdb7d3456d2a Mon Sep 17 00:00:00 2001
From: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
Date: Thu, 3 Dec 2026 10:34:17 +0000
Subject: [PATCH 18/18] Unified: Update Data/NSData init models to account for
an optional argument.
---
unified/ql/lib/ext/legacy-swift.model.json | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/unified/ql/lib/ext/legacy-swift.model.json b/unified/ql/lib/ext/legacy-swift.model.json
index b967cc4d1aff..0cf1e2fab424 100644
--- a/unified/ql/lib/ext/legacy-swift.model.json
+++ b/unified/ql/lib/ext/legacy-swift.model.json
@@ -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"],
@@ -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"],
@@ -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", 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"],
@@ -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"],
@@ -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"],
@@ -1001,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"],