From d34700318c1e209c153c5fbd04f7e9558acf35d3 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 26 Aug 2026 14:08:33 -0500 Subject: [PATCH 1/9] [android-tools] Fix ZIP extraction containment Use the canonical extraction-root pattern directly in ExtractZipSafe so case-sensitive filesystems reject case-confused sibling paths and CodeQL can model the containment check. Expand coverage for traversal, rooted paths, prefix and case confusion, separators, and symbolic-link metadata. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../DownloadUtils.cs | 4 +- .../FileUtil.cs | 11 --- .../DownloadUtilsTests.cs | 84 ++++++++++++++++++- .../FileUtilTests.cs | 57 ------------- 4 files changed, 83 insertions(+), 73 deletions(-) diff --git a/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs b/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs index 2fc56cb4a41..d0a076d6270 100644 --- a/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs +++ b/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs @@ -113,6 +113,8 @@ public static void ExtractZipSafe (string archivePath, string destinationPath, C { using var archive = ZipFile.OpenRead (archivePath); var fullExtractRoot = Path.GetFullPath (destinationPath); + if (!fullExtractRoot.EndsWith (Path.DirectorySeparatorChar.ToString (), StringComparison.Ordinal)) + fullExtractRoot += Path.DirectorySeparatorChar; foreach (var entry in archive.Entries) { cancellationToken.ThrowIfCancellationRequested (); @@ -123,7 +125,7 @@ public static void ExtractZipSafe (string archivePath, string destinationPath, C var destinationFile = Path.GetFullPath (Path.Combine (fullExtractRoot, entry.FullName)); // Zip Slip protection - if (!FileUtil.IsUnderDirectory (destinationFile, fullExtractRoot)) { + if (!destinationFile.StartsWith (fullExtractRoot, StringComparison.Ordinal)) { throw new InvalidOperationException ($"Archive entry '{entry.FullName}' would extract outside target directory."); } diff --git a/src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs b/src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs index f324fbd6be4..458be0eac0c 100644 --- a/src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs +++ b/src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs @@ -185,16 +185,6 @@ internal static bool IsTargetPathWritable (string targetPath, ActionChecks if a path is under a given directory. - internal static bool IsUnderDirectory (string path, string directory) - { - if (string.IsNullOrEmpty (directory) || string.IsNullOrEmpty (path)) - return false; - if (path.Equals (directory, StringComparison.OrdinalIgnoreCase)) - return true; - return path.StartsWith (directory + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase); - } - // Returns .msi (Windows), .pkg (macOS), or null (Linux) internal static string? GetInstallerExtension () { @@ -271,4 +261,3 @@ internal static async Task SetExecutablePermissionsAsync (string directory, Acti #endif } } - diff --git a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs index 67b05125436..be471f5449c 100644 --- a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs +++ b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs @@ -163,16 +163,17 @@ public void ExtractZipSafe_ValidZip_ExtractsFiles () Assert.AreEqual ("hello world", File.ReadAllText (extractedFile)); } - [Test] - public void ExtractZipSafe_ZipSlip_Throws () + [TestCase ("../relative.txt")] + [TestCase ("../extracted-other/prefix.txt")] + [TestCase ("../EXTRACTED/case.txt")] + public void ExtractZipSafe_UnsafeRelativePath_Throws (string entryName) { var zipPath = Path.Combine (tempDir, "evil.zip"); var extractPath = Path.Combine (tempDir, "extracted"); Directory.CreateDirectory (extractPath); using (var archive = ZipFile.Open (zipPath, ZipArchiveMode.Create)) { - // Create an entry with a path traversal - var entry = archive.CreateEntry ("../relative.txt"); + var entry = archive.CreateEntry (entryName); using var writer = new StreamWriter (entry.Open ()); writer.Write ("malicious"); } @@ -182,6 +183,81 @@ public void ExtractZipSafe_ZipSlip_Throws () Assert.That (ex!.Message, Does.Contain ("outside target directory")); } + [Test] + public void ExtractZipSafe_PlatformSeparatorTraversal_Throws () + { + var zipPath = Path.Combine (tempDir, "evil.zip"); + var extractPath = Path.Combine (tempDir, "extracted"); + Directory.CreateDirectory (extractPath); + + using (var archive = ZipFile.Open (zipPath, ZipArchiveMode.Create)) { + var entry = archive.CreateEntry ($"..{Path.DirectorySeparatorChar}relative.txt"); + using var writer = new StreamWriter (entry.Open ()); + writer.Write ("malicious"); + } + + Assert.Throws (() => + DownloadUtils.ExtractZipSafe (zipPath, extractPath, CancellationToken.None)); + } + + [Test] + public void ExtractZipSafe_RootedPath_Throws () + { + var zipPath = Path.Combine (tempDir, "evil.zip"); + var extractPath = Path.Combine (tempDir, "extracted"); + Directory.CreateDirectory (extractPath); + var pathRoot = Path.GetPathRoot (extractPath); + Assert.IsNotNull (pathRoot); + + using (var archive = ZipFile.Open (zipPath, ZipArchiveMode.Create)) { + var entry = archive.CreateEntry (Path.Combine (pathRoot, "rooted.txt")); + using var writer = new StreamWriter (entry.Open ()); + writer.Write ("malicious"); + } + + Assert.Throws (() => + DownloadUtils.ExtractZipSafe (zipPath, extractPath, CancellationToken.None)); + } + + [Test] + public void ExtractZipSafe_TrailingSeparator_ExtractsFiles () + { + var zipPath = Path.Combine (tempDir, "test.zip"); + var extractPath = Path.Combine (tempDir, "extracted") + Path.DirectorySeparatorChar; + Directory.CreateDirectory (extractPath); + + using (var archive = ZipFile.Open (zipPath, ZipArchiveMode.Create)) { + var entry = archive.CreateEntry ("hello.txt"); + using var writer = new StreamWriter (entry.Open ()); + writer.Write ("hello world"); + } + + DownloadUtils.ExtractZipSafe (zipPath, extractPath, CancellationToken.None); + + Assert.AreEqual ("hello world", File.ReadAllText (Path.Combine (extractPath, "hello.txt"))); + } + + [Test] + public void ExtractZipSafe_SymbolicLinkMetadata_ExtractsRegularFile () + { + var zipPath = Path.Combine (tempDir, "test.zip"); + var extractPath = Path.Combine (tempDir, "extracted"); + Directory.CreateDirectory (extractPath); + + using (var archive = ZipFile.Open (zipPath, ZipArchiveMode.Create)) { + var entry = archive.CreateEntry ("link"); + entry.ExternalAttributes = unchecked ((0xA000 | 0x1FF) << 16); + using var writer = new StreamWriter (entry.Open ()); + writer.Write ("../outside.txt"); + } + + DownloadUtils.ExtractZipSafe (zipPath, extractPath, CancellationToken.None); + + var extractedFile = Path.Combine (extractPath, "link"); + Assert.IsTrue (File.Exists (extractedFile)); + Assert.IsFalse ((File.GetAttributes (extractedFile) & FileAttributes.ReparsePoint) != 0); + } + [Test] public void ExtractZipSafe_EmptyZip_NoFilesExtracted () { diff --git a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs index c94d2cc29a7..8df22a889d8 100644 --- a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs +++ b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs @@ -90,62 +90,5 @@ public void MoveWithRollback_SourceDoesNotExist_NoExistingTarget_Throws () Assert.IsFalse (Directory.Exists (target)); } - [Test] - public void IsUnderDirectory_ChildPath_ReturnsTrue () - { - var parent = Path.Combine ($"{Path.DirectorySeparatorChar}opt", "programs"); - var child = Path.Combine (parent, "java", "jdk-21"); - Assert.IsTrue (FileUtil.IsUnderDirectory (child, parent)); - } - - [Test] - public void IsUnderDirectory_ExactMatch_ReturnsTrue () - { - var dir = Path.Combine ($"{Path.DirectorySeparatorChar}opt", "programs"); - Assert.IsTrue (FileUtil.IsUnderDirectory (dir, dir)); - } - - [Test] - public void IsUnderDirectory_SiblingPath_ReturnsFalse () - { - Assert.IsFalse (FileUtil.IsUnderDirectory ( - Path.Combine ($"{Path.DirectorySeparatorChar}opt", "data", "java"), - Path.Combine ($"{Path.DirectorySeparatorChar}opt", "programs"))); - } - - [Test] - public void IsUnderDirectory_DifferentRoot_ReturnsFalse () - { - Assert.IsFalse (FileUtil.IsUnderDirectory ( - Path.Combine ($"{Path.DirectorySeparatorChar}other", "java"), - Path.Combine ($"{Path.DirectorySeparatorChar}opt", "programs"))); - } - - [TestCase (null, "/dir")] - [TestCase ("/dir", null)] - [TestCase ("", "/dir")] - [TestCase ("/dir", "")] - [TestCase (null, null)] - public void IsUnderDirectory_NullOrEmpty_ReturnsFalse (string path, string directory) - { - Assert.IsFalse (FileUtil.IsUnderDirectory (path!, directory!)); - } - - [Test] - public void IsUnderDirectory_CaseInsensitive () - { - var parent = Path.Combine ($"{Path.DirectorySeparatorChar}opt", "Programs"); - var child = Path.Combine ($"{Path.DirectorySeparatorChar}opt", "PROGRAMS", "java"); - Assert.IsTrue (FileUtil.IsUnderDirectory (child, parent)); - } - - [Test] - public void IsUnderDirectory_PartialDirNameMatch_ReturnsFalse () - { - var parent = Path.Combine ($"{Path.DirectorySeparatorChar}opt", "programs"); - Assert.IsFalse (FileUtil.IsUnderDirectory ( - Path.Combine ($"{Path.DirectorySeparatorChar}opt", "programs-extra", "java"), - parent)); - } } } From 7d9c7766b02c4e3b02dcd20534a7c55269fc0092 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 26 Aug 2026 14:15:02 -0500 Subject: [PATCH 2/9] [android-tools] Retain ZIP containment helper Keep ExtractZipSafe using FileUtil.IsUnderDirectory while making the helper canonicalize both inputs and enforce ordinal directory-boundary containment. Add a sink-specific CodeQL rationale and focused helper coverage. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../DownloadUtils.cs | 6 ++-- .../FileUtil.cs | 15 +++++++++ .../FileUtilTests.cs | 31 +++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs b/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs index d0a076d6270..e00908edc7d 100644 --- a/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs +++ b/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs @@ -113,8 +113,6 @@ public static void ExtractZipSafe (string archivePath, string destinationPath, C { using var archive = ZipFile.OpenRead (archivePath); var fullExtractRoot = Path.GetFullPath (destinationPath); - if (!fullExtractRoot.EndsWith (Path.DirectorySeparatorChar.ToString (), StringComparison.Ordinal)) - fullExtractRoot += Path.DirectorySeparatorChar; foreach (var entry in archive.Entries) { cancellationToken.ThrowIfCancellationRequested (); @@ -125,7 +123,7 @@ public static void ExtractZipSafe (string archivePath, string destinationPath, C var destinationFile = Path.GetFullPath (Path.Combine (fullExtractRoot, entry.FullName)); // Zip Slip protection - if (!destinationFile.StartsWith (fullExtractRoot, StringComparison.Ordinal)) { + if (!FileUtil.IsUnderDirectory (destinationFile, fullExtractRoot)) { throw new InvalidOperationException ($"Archive entry '{entry.FullName}' would extract outside target directory."); } @@ -133,7 +131,7 @@ public static void ExtractZipSafe (string archivePath, string destinationPath, C if (!string.IsNullOrEmpty (entryDir)) Directory.CreateDirectory (entryDir); - entry.ExtractToFile (destinationFile, overwrite: true); + entry.ExtractToFile (destinationFile, overwrite: true); // CodeQL [SM02729] IsUnderDirectory canonicalizes both paths and enforces ordinal directory-boundary containment. } } diff --git a/src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs b/src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs index 458be0eac0c..cc75e0b414b 100644 --- a/src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs +++ b/src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs @@ -185,6 +185,21 @@ internal static bool IsTargetPathWritable (string targetPath, ActionChecks if a path is under a given directory. + internal static bool IsUnderDirectory (string path, string directory) + { + if (string.IsNullOrEmpty (directory) || string.IsNullOrEmpty (path)) + return false; + + var fullPath = Path.GetFullPath (path); + var fullDirectory = Path.GetFullPath (directory); + if (fullPath.Equals (fullDirectory, StringComparison.Ordinal)) + return true; + if (!fullDirectory.EndsWith (Path.DirectorySeparatorChar.ToString (), StringComparison.Ordinal)) + fullDirectory += Path.DirectorySeparatorChar; + return fullPath.StartsWith (fullDirectory, StringComparison.Ordinal); + } + // Returns .msi (Windows), .pkg (macOS), or null (Linux) internal static string? GetInstallerExtension () { diff --git a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs index 8df22a889d8..c9e222619fd 100644 --- a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs +++ b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs @@ -90,5 +90,36 @@ public void MoveWithRollback_SourceDoesNotExist_NoExistingTarget_Throws () Assert.IsFalse (Directory.Exists (target)); } + [Test] + public void IsUnderDirectory_ChildPath_ReturnsTrue () + { + var parent = Path.Combine (tempDir, "programs"); + var child = Path.Combine (parent, "java", "jdk-21"); + Assert.IsTrue (FileUtil.IsUnderDirectory (child, parent)); + } + + [Test] + public void IsUnderDirectory_ExactMatch_ReturnsTrue () + { + var directory = Path.Combine (tempDir, "programs"); + Assert.IsTrue (FileUtil.IsUnderDirectory (directory, directory)); + } + + [Test] + public void IsUnderDirectory_PrefixMatch_ReturnsFalse () + { + var parent = Path.Combine (tempDir, "programs"); + var sibling = Path.Combine (tempDir, "programs-extra", "java"); + Assert.IsFalse (FileUtil.IsUnderDirectory (sibling, parent)); + } + + [Test] + public void IsUnderDirectory_CanonicalCaseMismatch_ReturnsFalse () + { + var parent = Path.Combine (tempDir, "programs"); + var sibling = Path.Combine (parent, "..", "PROGRAMS", "java"); + Assert.IsFalse (FileUtil.IsUnderDirectory (sibling, parent)); + } + } } From 50adafb84e88969ec9d6bb974c2cc3bf8241dcb9 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 26 Aug 2026 14:16:14 -0500 Subject: [PATCH 3/9] [tests] Use neutral ZIP fixture names Rename unsafe archive fixtures without changing test behavior. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../DownloadUtilsTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs index be471f5449c..c78fe5985c8 100644 --- a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs +++ b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs @@ -168,7 +168,7 @@ public void ExtractZipSafe_ValidZip_ExtractsFiles () [TestCase ("../EXTRACTED/case.txt")] public void ExtractZipSafe_UnsafeRelativePath_Throws (string entryName) { - var zipPath = Path.Combine (tempDir, "evil.zip"); + var zipPath = Path.Combine (tempDir, "unsafe.zip"); var extractPath = Path.Combine (tempDir, "extracted"); Directory.CreateDirectory (extractPath); @@ -186,7 +186,7 @@ public void ExtractZipSafe_UnsafeRelativePath_Throws (string entryName) [Test] public void ExtractZipSafe_PlatformSeparatorTraversal_Throws () { - var zipPath = Path.Combine (tempDir, "evil.zip"); + var zipPath = Path.Combine (tempDir, "unsafe.zip"); var extractPath = Path.Combine (tempDir, "extracted"); Directory.CreateDirectory (extractPath); @@ -203,7 +203,7 @@ public void ExtractZipSafe_PlatformSeparatorTraversal_Throws () [Test] public void ExtractZipSafe_RootedPath_Throws () { - var zipPath = Path.Combine (tempDir, "evil.zip"); + var zipPath = Path.Combine (tempDir, "unsafe.zip"); var extractPath = Path.Combine (tempDir, "extracted"); Directory.CreateDirectory (extractPath); var pathRoot = Path.GetPathRoot (extractPath); From 3b87916d300e7276c3be926094384505f8f2aba8 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 26 Aug 2026 14:16:54 -0500 Subject: [PATCH 4/9] [tests] Use standard ZIP fixture name Use test.zip consistently for the extraction safety cases. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../DownloadUtilsTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs index c78fe5985c8..e69db5075b6 100644 --- a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs +++ b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs @@ -168,7 +168,7 @@ public void ExtractZipSafe_ValidZip_ExtractsFiles () [TestCase ("../EXTRACTED/case.txt")] public void ExtractZipSafe_UnsafeRelativePath_Throws (string entryName) { - var zipPath = Path.Combine (tempDir, "unsafe.zip"); + var zipPath = Path.Combine (tempDir, "test.zip"); var extractPath = Path.Combine (tempDir, "extracted"); Directory.CreateDirectory (extractPath); @@ -186,7 +186,7 @@ public void ExtractZipSafe_UnsafeRelativePath_Throws (string entryName) [Test] public void ExtractZipSafe_PlatformSeparatorTraversal_Throws () { - var zipPath = Path.Combine (tempDir, "unsafe.zip"); + var zipPath = Path.Combine (tempDir, "test.zip"); var extractPath = Path.Combine (tempDir, "extracted"); Directory.CreateDirectory (extractPath); @@ -203,7 +203,7 @@ public void ExtractZipSafe_PlatformSeparatorTraversal_Throws () [Test] public void ExtractZipSafe_RootedPath_Throws () { - var zipPath = Path.Combine (tempDir, "unsafe.zip"); + var zipPath = Path.Combine (tempDir, "test.zip"); var extractPath = Path.Combine (tempDir, "extracted"); Directory.CreateDirectory (extractPath); var pathRoot = Path.GetPathRoot (extractPath); From 33f05949a351f897445672b8eb65a0b0218f55ea Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 26 Aug 2026 14:18:54 -0500 Subject: [PATCH 5/9] [tests] Restore containment helper coverage Restore null, empty, case, and partial-directory coverage while updating the case expectation for ordinal containment. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../FileUtil.cs | 2 +- .../FileUtilTests.cs | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs b/src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs index cc75e0b414b..7d6d61c7a2f 100644 --- a/src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs +++ b/src/Xamarin.Android.Tools.AndroidSdk/FileUtil.cs @@ -186,7 +186,7 @@ internal static bool IsTargetPathWritable (string targetPath, ActionChecks if a path is under a given directory. - internal static bool IsUnderDirectory (string path, string directory) + internal static bool IsUnderDirectory (string? path, string? directory) { if (string.IsNullOrEmpty (directory) || string.IsNullOrEmpty (path)) return false; diff --git a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs index c9e222619fd..7402947ad94 100644 --- a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs +++ b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs @@ -106,13 +106,31 @@ public void IsUnderDirectory_ExactMatch_ReturnsTrue () } [Test] - public void IsUnderDirectory_PrefixMatch_ReturnsFalse () + public void IsUnderDirectory_PartialDirNameMatch_ReturnsFalse () { var parent = Path.Combine (tempDir, "programs"); var sibling = Path.Combine (tempDir, "programs-extra", "java"); Assert.IsFalse (FileUtil.IsUnderDirectory (sibling, parent)); } + [TestCase (null, "/dir")] + [TestCase ("/dir", null)] + [TestCase ("", "/dir")] + [TestCase ("/dir", "")] + [TestCase (null, null)] + public void IsUnderDirectory_NullOrEmpty_ReturnsFalse (string path, string directory) + { + Assert.IsFalse (FileUtil.IsUnderDirectory (path, directory)); + } + + [Test] + public void IsUnderDirectory_CaseSensitive () + { + var parent = Path.Combine (tempDir, "Programs"); + var child = Path.Combine (tempDir, "PROGRAMS", "java"); + Assert.IsFalse (FileUtil.IsUnderDirectory (child, parent)); + } + [Test] public void IsUnderDirectory_CanonicalCaseMismatch_ReturnsFalse () { From 526dd1dde983a222b3cee54d9779dfbaca24c1ab Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 26 Aug 2026 14:31:48 -0500 Subject: [PATCH 6/9] [android-tools] Require an empty ZIP destination Reject reparse-point and non-empty extraction roots before opening an archive so pre-existing links cannot redirect writes outside the destination. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../DownloadUtils.cs | 13 +++++++++++-- .../DownloadUtilsTests.cs | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs b/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs index e00908edc7d..2b688e78f0e 100644 --- a/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs +++ b/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs @@ -108,12 +108,21 @@ static string ComputeHashString (HashAlgorithm hasher, Stream stream) return BitConverter.ToString (hash).Replace ("-", "").ToLowerInvariant (); } - /// Extracts a ZIP archive with Zip Slip protection. + /// Extracts a ZIP archive into an empty directory with Zip Slip protection. public static void ExtractZipSafe (string archivePath, string destinationPath, CancellationToken cancellationToken) { - using var archive = ZipFile.OpenRead (archivePath); var fullExtractRoot = Path.GetFullPath (destinationPath); + if (Directory.Exists (fullExtractRoot)) { + if ((File.GetAttributes (fullExtractRoot) & FileAttributes.ReparsePoint) != 0) + throw new InvalidOperationException ($"Extraction target '{destinationPath}' must not be a symbolic link or reparse point."); + using var entries = Directory.EnumerateFileSystemEntries (fullExtractRoot).GetEnumerator (); + if (entries.MoveNext ()) + throw new InvalidOperationException ($"Extraction target '{destinationPath}' must be empty."); + } else { + Directory.CreateDirectory (fullExtractRoot); + } + using var archive = ZipFile.OpenRead (archivePath); foreach (var entry in archive.Entries) { cancellationToken.ThrowIfCancellationRequested (); diff --git a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs index e69db5075b6..6eb619d0f3a 100644 --- a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs +++ b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs @@ -258,6 +258,24 @@ public void ExtractZipSafe_SymbolicLinkMetadata_ExtractsRegularFile () Assert.IsFalse ((File.GetAttributes (extractedFile) & FileAttributes.ReparsePoint) != 0); } + [Test] + public void ExtractZipSafe_NonEmptyDestination_Throws () + { + var zipPath = Path.Combine (tempDir, "test.zip"); + var extractPath = Path.Combine (tempDir, "extracted"); + Directory.CreateDirectory (Path.Combine (extractPath, "link")); + + using (var archive = ZipFile.Open (zipPath, ZipArchiveMode.Create)) { + var entry = archive.CreateEntry ("link/outside.txt"); + using var writer = new StreamWriter (entry.Open ()); + writer.Write ("content"); + } + + var ex = Assert.Throws (() => + DownloadUtils.ExtractZipSafe (zipPath, extractPath, CancellationToken.None)); + Assert.That (ex!.Message, Does.Contain ("must be empty")); + } + [Test] public void ExtractZipSafe_EmptyZip_NoFilesExtracted () { From 2ca25b57e033ae4e035c8348121387a9e6f09089 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 26 Aug 2026 14:37:01 -0500 Subject: [PATCH 7/9] [android-tools] Keep ZIP fix scoped to SM02729 Remove the unrelated empty-destination requirement and retain the canonical path-containment fix for the reported CodeQL finding. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../DownloadUtils.cs | 11 +---------- .../DownloadUtilsTests.cs | 18 ------------------ 2 files changed, 1 insertion(+), 28 deletions(-) diff --git a/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs b/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs index 2b688e78f0e..38feae4ee36 100644 --- a/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs +++ b/src/Xamarin.Android.Tools.AndroidSdk/DownloadUtils.cs @@ -108,19 +108,10 @@ static string ComputeHashString (HashAlgorithm hasher, Stream stream) return BitConverter.ToString (hash).Replace ("-", "").ToLowerInvariant (); } - /// Extracts a ZIP archive into an empty directory with Zip Slip protection. + /// Extracts a ZIP archive with Zip Slip protection. public static void ExtractZipSafe (string archivePath, string destinationPath, CancellationToken cancellationToken) { var fullExtractRoot = Path.GetFullPath (destinationPath); - if (Directory.Exists (fullExtractRoot)) { - if ((File.GetAttributes (fullExtractRoot) & FileAttributes.ReparsePoint) != 0) - throw new InvalidOperationException ($"Extraction target '{destinationPath}' must not be a symbolic link or reparse point."); - using var entries = Directory.EnumerateFileSystemEntries (fullExtractRoot).GetEnumerator (); - if (entries.MoveNext ()) - throw new InvalidOperationException ($"Extraction target '{destinationPath}' must be empty."); - } else { - Directory.CreateDirectory (fullExtractRoot); - } using var archive = ZipFile.OpenRead (archivePath); foreach (var entry in archive.Entries) { diff --git a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs index 6eb619d0f3a..e69db5075b6 100644 --- a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs +++ b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs @@ -258,24 +258,6 @@ public void ExtractZipSafe_SymbolicLinkMetadata_ExtractsRegularFile () Assert.IsFalse ((File.GetAttributes (extractedFile) & FileAttributes.ReparsePoint) != 0); } - [Test] - public void ExtractZipSafe_NonEmptyDestination_Throws () - { - var zipPath = Path.Combine (tempDir, "test.zip"); - var extractPath = Path.Combine (tempDir, "extracted"); - Directory.CreateDirectory (Path.Combine (extractPath, "link")); - - using (var archive = ZipFile.Open (zipPath, ZipArchiveMode.Create)) { - var entry = archive.CreateEntry ("link/outside.txt"); - using var writer = new StreamWriter (entry.Open ()); - writer.Write ("content"); - } - - var ex = Assert.Throws (() => - DownloadUtils.ExtractZipSafe (zipPath, extractPath, CancellationToken.None)); - Assert.That (ex!.Message, Does.Contain ("must be empty")); - } - [Test] public void ExtractZipSafe_EmptyZip_NoFilesExtracted () { From 32465cb5d3035d94a9c52ec0ecedc362b8bf7699 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 26 Aug 2026 14:41:00 -0500 Subject: [PATCH 8/9] [tests] Restore directory containment cases Restore sibling-path and separate-root coverage for IsUnderDirectory. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../FileUtilTests.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs index 7402947ad94..88ff197842f 100644 --- a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs +++ b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/FileUtilTests.cs @@ -105,6 +105,22 @@ public void IsUnderDirectory_ExactMatch_ReturnsTrue () Assert.IsTrue (FileUtil.IsUnderDirectory (directory, directory)); } + [Test] + public void IsUnderDirectory_SiblingPath_ReturnsFalse () + { + Assert.IsFalse (FileUtil.IsUnderDirectory ( + Path.Combine (tempDir, "data", "java"), + Path.Combine (tempDir, "programs"))); + } + + [Test] + public void IsUnderDirectory_DifferentRoot_ReturnsFalse () + { + Assert.IsFalse (FileUtil.IsUnderDirectory ( + Path.Combine (Path.GetPathRoot (tempDir), "other", "java"), + Path.Combine (tempDir, "programs"))); + } + [Test] public void IsUnderDirectory_PartialDirNameMatch_ReturnsFalse () { From df6f66b7dabf3b1c038a8572ba72101f14a5ee52 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 26 Aug 2026 15:34:53 -0500 Subject: [PATCH 9/9] [tests] Use neutral archive contents Replace malicious test payload labels with foo. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../DownloadUtilsTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs index e69db5075b6..bf6d5ac6752 100644 --- a/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs +++ b/tests/Xamarin.Android.Tools.AndroidSdk-Tests/DownloadUtilsTests.cs @@ -175,7 +175,7 @@ public void ExtractZipSafe_UnsafeRelativePath_Throws (string entryName) using (var archive = ZipFile.Open (zipPath, ZipArchiveMode.Create)) { var entry = archive.CreateEntry (entryName); using var writer = new StreamWriter (entry.Open ()); - writer.Write ("malicious"); + writer.Write ("foo"); } var ex = Assert.Throws (() => @@ -193,7 +193,7 @@ public void ExtractZipSafe_PlatformSeparatorTraversal_Throws () using (var archive = ZipFile.Open (zipPath, ZipArchiveMode.Create)) { var entry = archive.CreateEntry ($"..{Path.DirectorySeparatorChar}relative.txt"); using var writer = new StreamWriter (entry.Open ()); - writer.Write ("malicious"); + writer.Write ("foo"); } Assert.Throws (() => @@ -212,7 +212,7 @@ public void ExtractZipSafe_RootedPath_Throws () using (var archive = ZipFile.Open (zipPath, ZipArchiveMode.Create)) { var entry = archive.CreateEntry (Path.Combine (pathRoot, "rooted.txt")); using var writer = new StreamWriter (entry.Open ()); - writer.Write ("malicious"); + writer.Write ("foo"); } Assert.Throws (() =>