Skip to content
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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="System.Management.Automation" Version="7.6.0" />
<PackageVersion Include="System.Security.Cryptography.Pkcs" Version="10.0.13" />
<PackageVersion Include="NuGet.Protocol" Version="7.9.0" />
<PackageVersion Include="NuGet.Versioning" Version="7.9.0" />
<PackageVersion Include="Polly.Core" Version="8.8.0" />
Expand Down
88 changes: 88 additions & 0 deletions ModuleFast.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,94 @@ Describe 'ModuleFastSpec' {
}
}

Describe 'C# package extraction URL-encoded paths' {
It 'Decodes URL-encoded file and directory names' {
$archiveStream = [IO.MemoryStream]::new()
$archive = [IO.Compression.ZipArchive]::new($archiveStream, [IO.Compression.ZipArchiveMode]::Create, $true)
$entry = $archive.CreateEntry('sub%20folder/file%20with%20spaces.txt')
$writer = [IO.StreamWriter]::new($entry.Open())
$writer.Write('decoded')
$writer.Dispose()
$archive.Dispose()
$archiveStream.Position = 0

$destination = Join-Path $TestDrive 'decoded-package'
$installerType = [ModuleFastInfo].Assembly.GetType('ModuleFast.ModuleFastInstaller')
$extractor = $installerType.GetMethod(
'ExtractPackageAsync',
[Reflection.BindingFlags]::Static -bor [Reflection.BindingFlags]::NonPublic
)
$arguments = [object[]]::new(3)
$arguments[0] = $archiveStream
$arguments[1] = [string]$destination
$arguments[2] = [Threading.CancellationToken]::None
$extractor.Invoke($null, $arguments).GetAwaiter().GetResult()

$installedFile = Join-Path $destination 'sub folder/file with spaces.txt'
Get-Content $installedFile -Raw | Should -Be 'decoded'
Test-Path (Join-Path $destination 'sub%20folder/file%20with%20spaces.txt') | Should -BeFalse
$archiveStream.Dispose()
}

It 'Replaces invalid filename characters after decoding' {
$invalidCharacter = [IO.Path]::GetInvalidFileNameChars() |
Where-Object { $_ -ne [IO.Path]::DirectorySeparatorChar -and $_ -ne [IO.Path]::AltDirectorySeparatorChar } |
Select-Object -First 1
$escapedInvalidCharacter = [Uri]::EscapeDataString([string]$invalidCharacter)
$archiveStream = [IO.MemoryStream]::new()
$archive = [IO.Compression.ZipArchive]::new($archiveStream, [IO.Compression.ZipArchiveMode]::Create, $true)
$entry = $archive.CreateEntry("folder$escapedInvalidCharacter/file$escapedInvalidCharacter.txt")
$writer = [IO.StreamWriter]::new($entry.Open())
$writer.Write('sanitized')
$writer.Dispose()
$archive.Dispose()
$archiveStream.Position = 0

$destination = Join-Path $TestDrive 'sanitized-package'
$installerType = [ModuleFastInfo].Assembly.GetType('ModuleFast.ModuleFastInstaller')
$extractor = $installerType.GetMethod(
'ExtractPackageAsync',
[Reflection.BindingFlags]::Static -bor [Reflection.BindingFlags]::NonPublic
)
$arguments = [object[]]::new(3)
$arguments[0] = $archiveStream
$arguments[1] = [string]$destination
$arguments[2] = [Threading.CancellationToken]::None
$extractor.Invoke($null, $arguments).GetAwaiter().GetResult()

Get-Content (Join-Path $destination 'folder_/file_.txt') -Raw | Should -Be 'sanitized'
$archiveStream.Dispose()
}

It 'Stops before extracting directory entries when canceled' {
$archiveStream = [IO.MemoryStream]::new()
$archive = [IO.Compression.ZipArchive]::new($archiveStream, [IO.Compression.ZipArchiveMode]::Create, $true)
$archive.CreateEntry('subfolder/') | Out-Null
$archive.Dispose()
$archiveStream.Position = 0

$destination = Join-Path $TestDrive 'canceled-package'
$installerType = [ModuleFastInfo].Assembly.GetType('ModuleFast.ModuleFastInstaller')
$extractor = $installerType.GetMethod(
'ExtractPackageAsync',
[Reflection.BindingFlags]::Static -bor [Reflection.BindingFlags]::NonPublic
)
$cancellationSource = [Threading.CancellationTokenSource]::new()
$cancellationSource.Cancel()
$arguments = [object[]]::new(3)
$arguments[0] = $archiveStream
$arguments[1] = [string]$destination
$arguments[2] = $cancellationSource.Token

{
$extractor.Invoke($null, $arguments).GetAwaiter().GetResult()
} | Should -Throw
Test-Path (Join-Path $destination 'subfolder') | Should -BeFalse
$cancellationSource.Dispose()
$archiveStream.Dispose()
}
}

Describe 'Install-ModuleFast -Plan' -Tag 'E2E' {
BeforeAll {
$SCRIPT:__existingPSModulePath = $env:PSModulePath
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Management.Automation" PrivateAssets="all" />
<PackageReference Include="System.Security.Cryptography.Pkcs" />
<PackageReference Include="NuGet.Protocol" />
<PackageReference Include="NuGet.Versioning" />
<PackageReference Include="Polly.Core" />
Expand Down
47 changes: 43 additions & 4 deletions Source/Core/ModuleFastInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,46 @@ public class ModuleFastInstaller
: null;
}

private static async Task ExtractPackageAsync(Stream packageStream, string destination, CancellationToken ct)
{
string extractionRoot = Path.GetFullPath(destination);
string extractionRootWithSeparator = Path.EndsInDirectorySeparator(extractionRoot)
? extractionRoot
: extractionRoot + Path.DirectorySeparatorChar;
StringComparison pathComparison = OperatingSystem.IsWindows()
? StringComparison.OrdinalIgnoreCase
: StringComparison.Ordinal;

using ZipArchive archive = new(packageStream, ZipArchiveMode.Read, leaveOpen: true);
foreach (ZipArchiveEntry entry in archive.Entries)
{
ct.ThrowIfCancellationRequested();

string entryName = Uri.UnescapeDataString(entry.FullName);
char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
entryName = new string(entryName.Select(character =>
invalidFileNameChars.Contains(character) &&
character != Path.DirectorySeparatorChar &&
character != Path.AltDirectorySeparatorChar
? '_'
: character).ToArray());
string entryPath = Path.GetFullPath(Path.Combine(
extractionRoot,
entryName.Replace('/', Path.DirectorySeparatorChar)));
if (!entryPath.StartsWith(extractionRootWithSeparator, pathComparison))
throw new InvalidDataException($"Package entry '{entry.FullName}' resolves outside the extraction directory.");

if (Path.EndsInDirectorySeparator(entryName))
{
Directory.CreateDirectory(entryPath);
continue;
}

Directory.CreateDirectory(Path.GetDirectoryName(entryPath)!);
await entry.ExtractToFileAsync(entryPath, overwrite: false, ct).ConfigureAwait(false);
Comment thread
Copilot marked this conversation as resolved.
Resolved
}
}

public ModuleFastInstaller(string source)
{
_sourceRepository = new SourceRepository(
Expand All @@ -55,7 +95,8 @@ public async Task<List<ModuleFastInfo>> InstallModules(

FindPackageByIdResource findPackageByIdResource = await _sourceRepository
.GetResourceAsync<FindPackageByIdResource>(ct)
.ConfigureAwait(false);
.ConfigureAwait(false)
?? throw new InvalidOperationException("The package source does not provide package lookup.");

ConcurrentBag<ModuleFastInfo> results = [];
ParallelOptions opts = new()
Expand Down Expand Up @@ -183,9 +224,7 @@ public async Task<List<ModuleFastInfo>> InstallModules(
Options = FileOptions.WriteThrough | FileOptions.Asynchronous,
})) { /* zero-byte sentinel; just creating the file is enough */ }

// ZipFile.ExtractToDirectoryAsync (.NET 10) uses async file I/O internally and
// includes built-in zip-slip protection, replacing the custom ExtractZipAsync method.
await ZipFile.ExtractToDirectoryAsync(packageStream, installPath, overwriteFiles: false, ct).ConfigureAwait(false);
await ExtractPackageAsync(packageStream, installPath, ct).ConfigureAwait(false);

string manifestPath = FindManifestPath(installPath, module.Name)
?? throw new FileNotFoundException(
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/ModuleFastPlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public async Task<HashSet<ModuleFastInfo>> GetPlan(
{
PackageMetadataResource metadataResource = await _sourceRepository
.GetResourceAsync<PackageMetadataResource>(ct)
.ConfigureAwait(false);
.ConfigureAwait(false)
?? throw new InvalidOperationException("The package source does not provide package metadata.");

ConcurrentDictionary<ModuleFastInfo, byte> modulesToInstall = [];
ConcurrentDictionary<ModuleFastSpec, ModuleFastInfo> bestLocalCandidates = [];
Expand Down
7 changes: 4 additions & 3 deletions Source/Core/SpecFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public enum SpecFileType { AutoDetect, ModuleFast, PSResourceGet, PSDepend }

public static class SpecFileReader
{
private static readonly JsonSerializerOptions _jsonOpts = new() { PropertyNameCaseInsensitive = true };
private static readonly Regex _psDependExtendedKeyRegex = new(@"^(.+)::(.+)$", RegexOptions.Compiled);

public static IEnumerable<string> FindRequiredSpecFiles(string path)
Expand Down Expand Up @@ -286,7 +285,8 @@ internal static async Task<object> ReadRequiredSpecFile(string requiredSpecPath,
{
return PSDataFileReader.Parse(content, requiredSpecPath, default, cmdlet);
}
return JsonSerializer.Deserialize<object>(content, _jsonOpts)!;
using JsonDocument document = JsonDocument.Parse(content);
return document.RootElement.Clone();
}

string resolvedPath = Path.GetFullPath(requiredSpecPath);
Expand Down Expand Up @@ -326,7 +326,8 @@ internal static async Task<object> ReadRequiredSpecFile(string requiredSpecPath,
if (extension is ".json" or ".jsonc")
{
string content = File.ReadAllText(resolvedPath);
JsonElement json = JsonSerializer.Deserialize<JsonElement>(content, _jsonOpts);
using JsonDocument document = JsonDocument.Parse(content);
JsonElement json = document.RootElement;
if (json.ValueKind == JsonValueKind.Array)
{
string[] strings = json.EnumerateArray().Select(e => e.GetString() ?? "").ToArray();
Expand Down
9 changes: 7 additions & 2 deletions Source/PowerShell/Commands/Install.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Linq;
using System.Management.Automation;
using System.Text.Json;
using System.Text.Json.Serialization;

using static System.IO.Path;

Expand Down Expand Up @@ -371,7 +372,7 @@ protected override async Task End()
foreach (ModuleFastInfo m in finalInstallPlan)
lockFile[m.Name] = m.ModuleVersion.ToString();

string json = JsonSerializer.Serialize(lockFile, new JsonSerializerOptions { WriteIndented = true });
string json = JsonSerializer.Serialize(lockFile, InstallJsonContext.Default.DictionaryStringString);
File.WriteAllText(CILockFilePath, json);
}
}
Expand All @@ -388,4 +389,8 @@ protected override async Task End()
_timeoutSource?.Dispose();
}
}
}
}

[JsonSerializable(typeof(Dictionary<string, string>))]
[JsonSourceGenerationOptions(WriteIndented = true)]
internal partial class InstallJsonContext : JsonSerializerContext { }
1 change: 1 addition & 0 deletions Source/PowerShell/PowerShell.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Management.Automation" PrivateAssets="all" />
<PackageReference Include="System.Security.Cryptography.Pkcs" />
</ItemGroup>
</Project>