+
+
+
+
+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.
+
+
+
+
+Use an up-to-date archive library that validates entry paths and symlink targets,
+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.
+
+
+
+
+
+Ostorlab:
+Zip Packages Exploitation.
+
+
+
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..2686e18b6de4
--- /dev/null
+++ b/unified/ql/src/queries/security/CWE-022/UnsafeUnpack.ql
@@ -0,0 +1,40 @@
+/**
+ * @name Arbitrary file write during a zip extraction from a user controlled source
+ * @description Unpacking user controlled zips without validating whether the
+ * destination file path is within the destination directory can cause files
+ * outside the destination directory to be overwritten.
+ * @kind path-problem
+ * @problem.severity error
+ * @security-severity 7.5
+ * @precision high
+ * @id unified/swift/unsafe-unpacking
+ * @tags security
+ * external/cwe/cwe-022
+ */
+
+import unified
+
+/**
+ * A taint configuration for tainted data that reaches an unsafe unpack sink.
+ */
+module UnsafeUnpackConfig implements DataFlow::ConfigSig {
+ predicate isSource(DataFlow::Node node) { Models::isSource(node, "remote") }
+
+ predicate isSink(DataFlow::Node node) { Models::isSink(node, "unsafe-unpack") }
+
+ predicate isBarrier(DataFlow::Node node) {
+ // TODO: Port numeric barriers from original Swift's UnsafeUnpackExtensions.qll.
+ none()
+ }
+
+ predicate observeDiffInformedIncrementalMode() { any() }
+}
+
+module UnsafeUnpackFlow = TaintTracking::Global