Repository navigation
Conversation
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>
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>
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>
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>
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>
…the issue clearer.
…out the current state of these libraries.
…omplicating the message.
…e a better fit, when supported).
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>
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>
…relative to the other unified queries).
|
QHelp previews: unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelpArbitrary file write during a zip extraction from a user controlled sourceUnpacking 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. RecommendationUse an up-to-date archive library that validates entry paths and symlink targets, such as ExampleThe following example unpacks a remote zip using import Foundation
import Zip
func unzipFile(at sourcePath: String, to destinationPath: String) {
do {
let remoteURL = URL(string: "https://example.com/")!
let source = URL(fileURLWithPath: sourcePath)
let destination = URL(fileURLWithPath: destinationPath)
// Malicious zip is downloaded
try Data(contentsOf: remoteURL).write(to: source)
let fileManager = FileManager()
// Malicious zip is unpacked; Zip 2.1.2 does not validate archive entry paths
try Zip.unzipFile(source, destination: destination, overwrite: true, password: nil)
}
}The following example uses import Foundation
import ZipArchive
func unzipFile(at sourcePath: String, to destinationPath: String) {
do {
let remoteURL = URL(string: "https://example.com/")!
let source = URL(fileURLWithPath: sourcePath)
// Malicious zip is downloaded
try Data(contentsOf: remoteURL).write(to: source)
// ZipArchive validates paths and symlinks
try SSZipArchive.unzipFile(atPath: sourcePath, toDestination: destinationPath, delegate: nil)
}
}References
|
There was a problem hiding this comment.
🟡 Changes recommended
The taint changes introduce false-positive path alerts and miss unsafe archive extractions, while unmatched braces undermine the regression fixtures.
5 open issues
What changed in this PR
Ports Swift’s unsafe archive-unpacking query to unified Swift, detecting flows from remote data to potentially unsafe archive-extraction APIs.
Changes:
- Adds
unified/swift/unsafe-unpackingwith query help and vulnerable/safe examples. - Adds taint propagation from written data to archive URLs.
- Adds regression cases for downloaded, trusted, unrelated, and stored archives.
| File | Description |
|---|---|
unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.swift |
Adds archive-extraction regression cases. |
unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.qlref |
Connects the query to inline-expectation testing. |
unified/ql/test/query-tests/security/CWE-022/UnsafeUnpack/UnsafeUnpack.expected |
Records expected alerts and flow paths. |
unified/ql/src/queries/security/CWE-022/UnsafeUnpackGood.swift |
Demonstrates extraction using ZipArchive. |
unified/ql/src/queries/security/CWE-022/UnsafeUnpackBad.swift |
Demonstrates vulnerable extraction using Zip. |
unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql |
Implements the unified Swift path query. |
unified/ql/src/queries/security/CWE-022/UnsafeUnpack.qhelp |
Explains risks, recommendations, and examples. |
unified/ql/lib/ext/legacy-swift.model.json |
Adds the Data.write(to:options:) taint summary. |
Review effort: Max (adaptive)
| ["", "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, "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"], |
| * A taint configuration for tainted data that reaches an unsafe unpack sink. | ||
| */ | ||
| module UnsafeUnpackConfig implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node node) { Models::isSource(node, "remote") } |
|
TODO:
|

Port original Swift unsafe unpacking query (
swift/unsafe-unpacking) to unified Swift asunified/swift/unsafe-unpacking. Initial port by Copilot + GPT-6.1 Sol High effort. I had to do some cleanup afterwards, much of which was because the original query was in experimental and was in need of a bit of cleanup anyway.We could do with improving this query to understand what version of the zip library is being used. This is particularly true for the
ZipFoundationlibrary, the latest version of which I think may actually have been made safe. I’ve removed explicit mentions of that particular library from the.qhelpand examples, so as to not cause confusion, though it's still modelled for now. The core advice to "Use an up-to-date archive library that validates entry paths and symlink targets" stands.We need a docs team review of the
.qhelpfor this one.I’ll start a DCA run as well.