Skip to content

Commit

Permalink
[msbuild] Validate that we don't write outside the target directory w…
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfbjarne committed Jan 13, 2025
1 parent e7f4ff5 commit 49b446e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions msbuild/Xamarin.MacDev.Tasks/Decompress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ static bool TryDecompressUsingSystemIOCompression (TaskLoggingHelper log, string
resource = resource.TrimEnd ('/', '\\');
resource = resource.Replace ('\\', zipDirectorySeparator);
var resourceAsDir = resource + zipDirectorySeparator;
decompressionDir = Path.GetFullPath (decompressionDir);

using var archive = ZipFile.OpenRead (zip);
foreach (var entry in archive.Entries) {
Expand Down Expand Up @@ -204,6 +205,16 @@ static bool TryDecompressUsingSystemIOCompression (TaskLoggingHelper log, string

var isDir = entryPath [entryPath.Length - 1] == zipDirectorySeparator;
var targetPath = Path.Combine (decompressionDir, entryPath.Replace (zipDirectorySeparator, Path.DirectorySeparatorChar));

// canonicalize the path
targetPath = Path.GetFullPath (targetPath);

// validate that the unzipped file is inside the target directory
if (!targetPath.StartsWith (decompressionDir + Path.DirectorySeparatorChar)) {
log.LogMessage (MessageImportance.Low, "Did not extract {0} because it would write outside the target directory.", entryPath);
continue;
}

if (isDir) {
Directory.CreateDirectory (targetPath);
} else {
Expand Down

0 comments on commit 49b446e

Please sign in to comment.