Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code scanning alert - Arbitrary file access during archive extraction ("Zip Slip") #117

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Winget.CommunityRepository.Ef/WingetRepositoryWithEf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
response.EnsureSuccessStatusCode();
using var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
using var zip = new ZipArchive(stream, ZipArchiveMode.Read);
zip.ExtractToDirectory(folder);
ExtractToDirectory(zip, folder);
var file = Path.Combine(folder, "Public", "index.db");
var connectionString = $"Data Source='{file}';Pooling=false;";
var results = new List<Models.WingetEntry>();
Expand Down Expand Up @@ -54,4 +54,25 @@

return results;
}

private void ExtractToDirectory(ZipArchive archive, string destinationDirectoryName)
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
string filePath = Path.Combine(destinationDirectoryName, entry.FullName);
Dismissed Show dismissed Hide dismissed

if (!filePath.StartsWith(destinationDirectoryName, StringComparison.OrdinalIgnoreCase))
{
throw new IOException("Extracting Zip entry would have resulted in a file outside the specified destination directory.");
}

if (string.IsNullOrEmpty(entry.Name))
{
Directory.CreateDirectory(Path.GetDirectoryName(filePath));

Check warning on line 71 in src/Winget.CommunityRepository.Ef/WingetRepositoryWithEf.cs

View workflow job for this annotation

GitHub Actions / 🛠️ Build and Test C#

Possible null reference argument for parameter 'path' in 'DirectoryInfo Directory.CreateDirectory(string path)'. [/home/runner/work/WingetIntune/WingetIntune/src/Winget.CommunityRepository.Ef/Winget.CommunityRepository.Ef.csproj::TargetFramework=net8.0]

Check warning on line 71 in src/Winget.CommunityRepository.Ef/WingetRepositoryWithEf.cs

View workflow job for this annotation

GitHub Actions / 🛠️ Build and Test C#

Possible null reference argument for parameter 'path' in 'DirectoryInfo Directory.CreateDirectory(string path)'. [/home/runner/work/WingetIntune/WingetIntune/src/Winget.CommunityRepository.Ef/Winget.CommunityRepository.Ef.csproj::TargetFramework=net6.0]

Check warning on line 71 in src/Winget.CommunityRepository.Ef/WingetRepositoryWithEf.cs

View workflow job for this annotation

GitHub Actions / 🛠️ Build and Test C#

Possible null reference argument for parameter 'path' in 'DirectoryInfo Directory.CreateDirectory(string path)'. [/home/runner/work/WingetIntune/WingetIntune/src/Winget.CommunityRepository.Ef/Winget.CommunityRepository.Ef.csproj::TargetFramework=net8.0]
continue;
}

entry.ExtractToFile(filePath, true);
}
}
}
33 changes: 31 additions & 2 deletions src/WingetIntune/Implementations/DefaultFileManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using System.IO.Compression;
using System.Security.Cryptography;

Expand Down Expand Up @@ -134,7 +134,29 @@

public void ExtractFileToFolder(string zipPath, string destinationFolder)
{
ZipFile.ExtractToDirectory(zipPath, destinationFolder);
using (FileStream inputStream = new FileStream(zipPath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize: 4096, useAsync: false))
{
using (ZipArchive archive = new ZipArchive(inputStream, ZipArchiveMode.Read, false))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
string filePath = Path.Combine(destinationFolder, entry.FullName);
Dismissed Show dismissed Hide dismissed

if (!filePath.StartsWith(destinationFolder, StringComparison.OrdinalIgnoreCase))
{
throw new IOException("Extracting Zip entry would have resulted in a file outside the specified destination directory.");
}

if (string.IsNullOrEmpty(entry.Name))
{
Directory.CreateDirectory(Path.GetDirectoryName(filePath));

Check warning on line 152 in src/WingetIntune/Implementations/DefaultFileManager.cs

View workflow job for this annotation

GitHub Actions / 🛠️ Build and Test C#

Possible null reference argument for parameter 'path' in 'DirectoryInfo Directory.CreateDirectory(string path)'. [/home/runner/work/WingetIntune/WingetIntune/src/WingetIntune/WingetIntune.csproj::TargetFramework=net8.0]

Check warning on line 152 in src/WingetIntune/Implementations/DefaultFileManager.cs

View workflow job for this annotation

GitHub Actions / 🛠️ Build and Test C#

Possible null reference argument for parameter 'path' in 'DirectoryInfo Directory.CreateDirectory(string path)'. [/home/runner/work/WingetIntune/WingetIntune/src/WingetIntune/WingetIntune.csproj::TargetFramework=net6.0]

Check warning on line 152 in src/WingetIntune/Implementations/DefaultFileManager.cs

View workflow job for this annotation

GitHub Actions / 🛠️ Build and Test C#

Possible null reference argument for parameter 'path' in 'DirectoryInfo Directory.CreateDirectory(string path)'. [/home/runner/work/WingetIntune/WingetIntune/src/WingetIntune/WingetIntune.csproj::TargetFramework=net8.0]

Check warning on line 152 in src/WingetIntune/Implementations/DefaultFileManager.cs

View workflow job for this annotation

GitHub Actions / 🛠️ Build and Test PowerShell

Possible null reference argument for parameter 'path' in 'DirectoryInfo Directory.CreateDirectory(string path)'. [/home/runner/work/WingetIntune/WingetIntune/src/WingetIntune/WingetIntune.csproj::TargetFramework=net6.0]

Check warning on line 152 in src/WingetIntune/Implementations/DefaultFileManager.cs

View workflow job for this annotation

GitHub Actions / 🛠️ Build and Test PowerShell

Possible null reference argument for parameter 'path' in 'DirectoryInfo Directory.CreateDirectory(string path)'. [/home/runner/work/WingetIntune/WingetIntune/src/WingetIntune/WingetIntune.csproj::TargetFramework=net6.0]
continue;
}

entry.ExtractToFile(filePath, true);
}
}
}
}

public async Task ExtractFileToFolderAsync(string zipPath, string destinationFolder, CancellationToken cancellationToken = default)
Expand All @@ -147,6 +169,13 @@
{
if (cancellationToken.IsCancellationRequested)
break;
string filePath = Path.Combine(destinationFolder, entry.FullName);

if (!filePath.StartsWith(destinationFolder, StringComparison.OrdinalIgnoreCase))
{
throw new IOException("Extracting Zip entry would have resulted in a file outside the specified destination directory.");
}

string directoryName = Path.Combine(destinationFolder, Path.GetDirectoryName(entry.FullName)!);
if (!string.IsNullOrEmpty(directoryName))
Directory.CreateDirectory(directoryName);
Expand Down
Loading