diff --git a/Directory.Packages.props b/Directory.Packages.props index be1a360..c935a1f 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -5,6 +5,7 @@ + diff --git a/ModuleFast.tests.ps1 b/ModuleFast.tests.ps1 index 045c30f..7ed29f5 100644 --- a/ModuleFast.tests.ps1 +++ b/ModuleFast.tests.ps1 @@ -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 diff --git a/Source/Core/Core.csproj b/Source/Core/Core.csproj index 804d698..6a59246 100644 --- a/Source/Core/Core.csproj +++ b/Source/Core/Core.csproj @@ -9,6 +9,7 @@ + diff --git a/Source/Core/ModuleFastInstaller.cs b/Source/Core/ModuleFastInstaller.cs index 2cfea89..c7ad5ea 100644 --- a/Source/Core/ModuleFastInstaller.cs +++ b/Source/Core/ModuleFastInstaller.cs @@ -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); + } + } + public ModuleFastInstaller(string source) { _sourceRepository = new SourceRepository( @@ -55,7 +95,8 @@ public async Task> InstallModules( FindPackageByIdResource findPackageByIdResource = await _sourceRepository .GetResourceAsync(ct) - .ConfigureAwait(false); + .ConfigureAwait(false) + ?? throw new InvalidOperationException("The package source does not provide package lookup."); ConcurrentBag results = []; ParallelOptions opts = new() @@ -183,9 +224,7 @@ public async Task> 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( diff --git a/Source/Core/ModuleFastPlanner.cs b/Source/Core/ModuleFastPlanner.cs index 1ddfb0b..07c154b 100644 --- a/Source/Core/ModuleFastPlanner.cs +++ b/Source/Core/ModuleFastPlanner.cs @@ -31,7 +31,8 @@ public async Task> GetPlan( { PackageMetadataResource metadataResource = await _sourceRepository .GetResourceAsync(ct) - .ConfigureAwait(false); + .ConfigureAwait(false) + ?? throw new InvalidOperationException("The package source does not provide package metadata."); ConcurrentDictionary modulesToInstall = []; ConcurrentDictionary bestLocalCandidates = []; diff --git a/Source/Core/SpecFileReader.cs b/Source/Core/SpecFileReader.cs index 6dc0b93..5ab9e0e 100644 --- a/Source/Core/SpecFileReader.cs +++ b/Source/Core/SpecFileReader.cs @@ -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 FindRequiredSpecFiles(string path) @@ -286,7 +285,8 @@ internal static async Task ReadRequiredSpecFile(string requiredSpecPath, { return PSDataFileReader.Parse(content, requiredSpecPath, default, cmdlet); } - return JsonSerializer.Deserialize(content, _jsonOpts)!; + using JsonDocument document = JsonDocument.Parse(content); + return document.RootElement.Clone(); } string resolvedPath = Path.GetFullPath(requiredSpecPath); @@ -326,7 +326,8 @@ internal static async Task ReadRequiredSpecFile(string requiredSpecPath, if (extension is ".json" or ".jsonc") { string content = File.ReadAllText(resolvedPath); - JsonElement json = JsonSerializer.Deserialize(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(); diff --git a/Source/PowerShell/Commands/Install.cs b/Source/PowerShell/Commands/Install.cs index 3337a97..bf8ff85 100644 --- a/Source/PowerShell/Commands/Install.cs +++ b/Source/PowerShell/Commands/Install.cs @@ -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; @@ -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); } } @@ -388,4 +389,8 @@ protected override async Task End() _timeoutSource?.Dispose(); } } -} \ No newline at end of file +} + +[JsonSerializable(typeof(Dictionary))] +[JsonSourceGenerationOptions(WriteIndented = true)] +internal partial class InstallJsonContext : JsonSerializerContext { } \ No newline at end of file diff --git a/Source/PowerShell/PowerShell.csproj b/Source/PowerShell/PowerShell.csproj index f0d8bb4..b15797d 100644 --- a/Source/PowerShell/PowerShell.csproj +++ b/Source/PowerShell/PowerShell.csproj @@ -15,5 +15,6 @@ +