Skip to content

Commit

Permalink
Merge pull request #117 from svrooij/fix-zip-slip
Browse files Browse the repository at this point in the history
Fix code scanning alert - Arbitrary file access during archive extraction ("Zip Slip")
  • Loading branch information
svrooij authored Sep 4, 2024
2 parents 401c934 + 5235db3 commit 8f2041a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
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 @@ public sealed class WingetRepositoryWithEf : WingetRepository
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 @@ public sealed class WingetRepositoryWithEf : WingetRepository

return results;
}

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

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]

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]
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 async Task DownloadFileAsync(string url, string path, string? expectedHas

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);

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 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]

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]
continue;
}

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

public async Task ExtractFileToFolderAsync(string zipPath, string destinationFolder, CancellationToken cancellationToken = default)
Expand All @@ -147,6 +169,13 @@ public async Task ExtractFileToFolderAsync(string zipPath, string destinationFol
{
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

0 comments on commit 8f2041a

Please sign in to comment.