Skip to content

Commit 78009a9

Browse files
cleemullinsgithub-advanced-security[bot]
authored andcommitted
Potential fix for code scanning alert no. 4: Arbitrary file access during archive extraction ("Zip Slip") (microsoft#6909)
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 1935ec0 commit 78009a9

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

tests/Microsoft.Bot.Builder.Tests/TranscriptUtilities.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,22 @@ private static void ExtractZipFolder(string zipFilePath, string zipFolder, strin
161161
{
162162
var entryName = entry.FullName.Remove(0, zipFolderEntry.FullName.Length);
163163

164+
// Compute the full extraction path and resolve to prevent Zip Slip
165+
var destinationPath = Path.GetFullPath(Path.Combine(path, entryName));
166+
var fullExtractionRoot = Path.GetFullPath(path + Path.DirectorySeparatorChar);
167+
if (!destinationPath.StartsWith(fullExtractionRoot, StringComparison.Ordinal))
168+
{
169+
throw new InvalidOperationException($"Entry is outside the target dir: {destinationPath}");
170+
}
171+
164172
if (string.IsNullOrEmpty(entry.Name))
165173
{
166174
// No Name, it is a folder
167-
CreateDirectoryIfNotExists(Path.Combine(path, entryName));
175+
CreateDirectoryIfNotExists(destinationPath);
168176
}
169177
else
170178
{
171-
entry.ExtractToFile(Path.Combine(path, entryName), overwrite: true);
179+
entry.ExtractToFile(destinationPath, overwrite: true);
172180
}
173181
}
174182
}

0 commit comments

Comments
 (0)