From 418b87c2cc87337ef66392e1f2dcb47f6d279a13 Mon Sep 17 00:00:00 2001 From: Justin Grote Date: Tue, 20 Oct 2026 23:58:52 +0000 Subject: [PATCH 1/8] Fix unescaped data path extraction issue Address an issue where data paths were not properly escaped during extraction, leading to potential security vulnerabilities. This update ensures that all data paths are correctly handled to prevent any unintended exposure. Fixes #115 --- ModuleFast.tests.ps1 | 31 +++++++++++++++++++++ Source/Core/ModuleFastInstaller.cs | 43 +++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/ModuleFast.tests.ps1 b/ModuleFast.tests.ps1 index 045c30f..1954e15 100644 --- a/ModuleFast.tests.ps1 +++ b/ModuleFast.tests.ps1 @@ -74,6 +74,37 @@ 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) + $archive.CreateEntry('sub%20folder/') | Out-Null + $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() + } +} + Describe 'Install-ModuleFast -Plan' -Tag 'E2E' { BeforeAll { $SCRIPT:__existingPSModulePath = $env:PSModulePath diff --git a/Source/Core/ModuleFastInstaller.cs b/Source/Core/ModuleFastInstaller.cs index 2cfea89..34aab44 100644 --- a/Source/Core/ModuleFastInstaller.cs +++ b/Source/Core/ModuleFastInstaller.cs @@ -30,6 +30,45 @@ 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) + { + string entryName = Uri.UnescapeDataString(entry.FullName); + 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 (entryName.EndsWith('/')) + { + Directory.CreateDirectory(entryPath); + continue; + } + + Directory.CreateDirectory(Path.GetDirectoryName(entryPath)!); + await using Stream entryStream = entry.Open(); + await using FileStream fileStream = new(entryPath, new FileStreamOptions + { + Mode = FileMode.CreateNew, + Access = FileAccess.Write, + Share = FileShare.None, + Options = FileOptions.Asynchronous, + }); + await entryStream.CopyToAsync(fileStream, ct).ConfigureAwait(false); + } + } + public ModuleFastInstaller(string source) { _sourceRepository = new SourceRepository( @@ -183,9 +222,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( From 3374e82b93910f1fd7d6e6cf057f139606ff3457 Mon Sep 17 00:00:00 2001 From: Justin Grote Date: Fri, 23 Oct 2026 09:54:59 -0700 Subject: [PATCH 2/8] Add cancellation token check in Zip archive processing Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- Source/Core/ModuleFastInstaller.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/Core/ModuleFastInstaller.cs b/Source/Core/ModuleFastInstaller.cs index 34aab44..79cd087 100644 --- a/Source/Core/ModuleFastInstaller.cs +++ b/Source/Core/ModuleFastInstaller.cs @@ -43,6 +43,7 @@ private static async Task ExtractPackageAsync(Stream packageStream, string desti using ZipArchive archive = new(packageStream, ZipArchiveMode.Read, leaveOpen: true); foreach (ZipArchiveEntry entry in archive.Entries) { + ct.ThrowIfCancellationRequested(); string entryName = Uri.UnescapeDataString(entry.FullName); string entryPath = Path.GetFullPath(Path.Combine( extractionRoot, From 4235e47b94e656cac47c4fcd6f05b8a4a2464824 Mon Sep 17 00:00:00 2001 From: Justin Grote Date: Fri, 23 Oct 2026 09:55:11 -0700 Subject: [PATCH 3/8] Simplify file extraction using ExtractToFileAsync Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- Source/Core/ModuleFastInstaller.cs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/Source/Core/ModuleFastInstaller.cs b/Source/Core/ModuleFastInstaller.cs index 79cd087..d126d79 100644 --- a/Source/Core/ModuleFastInstaller.cs +++ b/Source/Core/ModuleFastInstaller.cs @@ -43,7 +43,8 @@ private static async Task ExtractPackageAsync(Stream packageStream, string desti using ZipArchive archive = new(packageStream, ZipArchiveMode.Read, leaveOpen: true); foreach (ZipArchiveEntry entry in archive.Entries) { - ct.ThrowIfCancellationRequested(); + ct.ThrowIfCancellationRequested(); + string entryName = Uri.UnescapeDataString(entry.FullName); string entryPath = Path.GetFullPath(Path.Combine( extractionRoot, @@ -57,15 +58,7 @@ private static async Task ExtractPackageAsync(Stream packageStream, string desti continue; } - Directory.CreateDirectory(Path.GetDirectoryName(entryPath)!); - await using Stream entryStream = entry.Open(); - await using FileStream fileStream = new(entryPath, new FileStreamOptions - { - Mode = FileMode.CreateNew, - Access = FileAccess.Write, - Share = FileShare.None, - Options = FileOptions.Asynchronous, - }); + await entry.ExtractToFileAsync(entryPath, overwrite: false, ct).ConfigureAwait(false); await entryStream.CopyToAsync(fileStream, ct).ConfigureAwait(false); } } From ebb25dfcbc37fb757954de0f822e506ad7e4c976 Mon Sep 17 00:00:00 2001 From: Justin Grote Date: Fri, 23 Oct 2026 09:55:22 -0700 Subject: [PATCH 4/8] Refactor directory check to use EndsInDirectorySeparator Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- Source/Core/ModuleFastInstaller.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/Core/ModuleFastInstaller.cs b/Source/Core/ModuleFastInstaller.cs index d126d79..6dcd45e 100644 --- a/Source/Core/ModuleFastInstaller.cs +++ b/Source/Core/ModuleFastInstaller.cs @@ -52,13 +52,14 @@ private static async Task ExtractPackageAsync(Stream packageStream, string desti if (!entryPath.StartsWith(extractionRootWithSeparator, pathComparison)) throw new InvalidDataException($"Package entry '{entry.FullName}' resolves outside the extraction directory."); - if (entryName.EndsWith('/')) + if (Path.EndsInDirectorySeparator(entryName)) { Directory.CreateDirectory(entryPath); continue; } - await entry.ExtractToFileAsync(entryPath, overwrite: false, ct).ConfigureAwait(false); + await entry.ExtractToFileAsync(entryPath, overwrite: false, ct).ConfigureAwait(false); + await entryStream.CopyToAsync(fileStream, ct).ConfigureAwait(false); } } From d79e2069d1d6e736d80e41c4925e853bf0925920 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:57:18 +0000 Subject: [PATCH 5/8] Respect cancellation for archive directories Co-authored-by: JustinGrote <15258962+JustinGrote@users.noreply.github.com> --- ModuleFast.tests.ps1 | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/ModuleFast.tests.ps1 b/ModuleFast.tests.ps1 index 1954e15..069eff8 100644 --- a/ModuleFast.tests.ps1 +++ b/ModuleFast.tests.ps1 @@ -103,6 +103,34 @@ Describe 'C# package extraction URL-encoded paths' { Test-Path (Join-Path $destination 'sub%20folder/file%20with%20spaces.txt') | Should -BeFalse $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' { From 371cdba25c3d7facd703f3696f88384d1969140d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 23 Oct 2026 17:01:24 +0000 Subject: [PATCH 6/8] Fix package extraction regressions Co-authored-by: JustinGrote <15258962+JustinGrote@users.noreply.github.com> --- ModuleFast.tests.ps1 | 1 - Source/Core/ModuleFastInstaller.cs | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/ModuleFast.tests.ps1 b/ModuleFast.tests.ps1 index 069eff8..8527b79 100644 --- a/ModuleFast.tests.ps1 +++ b/ModuleFast.tests.ps1 @@ -78,7 +78,6 @@ 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) - $archive.CreateEntry('sub%20folder/') | Out-Null $entry = $archive.CreateEntry('sub%20folder/file%20with%20spaces.txt') $writer = [IO.StreamWriter]::new($entry.Open()) $writer.Write('decoded') diff --git a/Source/Core/ModuleFastInstaller.cs b/Source/Core/ModuleFastInstaller.cs index 6dcd45e..b9ec2a1 100644 --- a/Source/Core/ModuleFastInstaller.cs +++ b/Source/Core/ModuleFastInstaller.cs @@ -52,15 +52,14 @@ private static async Task ExtractPackageAsync(Stream packageStream, string desti if (!entryPath.StartsWith(extractionRootWithSeparator, pathComparison)) throw new InvalidDataException($"Package entry '{entry.FullName}' resolves outside the extraction directory."); - if (Path.EndsInDirectorySeparator(entryName)) + if (Path.EndsInDirectorySeparator(entryName)) { Directory.CreateDirectory(entryPath); continue; } + Directory.CreateDirectory(Path.GetDirectoryName(entryPath)!); await entry.ExtractToFileAsync(entryPath, overwrite: false, ct).ConfigureAwait(false); - - await entryStream.CopyToAsync(fileStream, ct).ConfigureAwait(false); } } From 0383b945a35dffeaa7c9d83632a5d487f3edeb76 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 23 Oct 2026 17:12:56 +0000 Subject: [PATCH 7/8] Eliminate build warnings Co-authored-by: JustinGrote <15258962+JustinGrote@users.noreply.github.com> --- Directory.Packages.props | 1 + Source/Core/Core.csproj | 1 + Source/Core/ModuleFastInstaller.cs | 3 ++- Source/Core/ModuleFastPlanner.cs | 3 ++- Source/Core/SpecFileReader.cs | 7 ++++--- Source/PowerShell/Commands/Install.cs | 9 +++++++-- Source/PowerShell/PowerShell.csproj | 1 + 7 files changed, 18 insertions(+), 7 deletions(-) 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/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 b9ec2a1..ef9d081 100644 --- a/Source/Core/ModuleFastInstaller.cs +++ b/Source/Core/ModuleFastInstaller.cs @@ -88,7 +88,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() 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 @@ + From 5907ea67fa9e81e542c48a3e3c54ac6f7ed3c03a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 23 Oct 2026 18:05:37 +0000 Subject: [PATCH 8/8] Sanitize invalid decoded package filename characters Co-authored-by: JustinGrote <15258962+JustinGrote@users.noreply.github.com> --- ModuleFast.tests.ps1 | 30 ++++++++++++++++++++++++++++++ Source/Core/ModuleFastInstaller.cs | 7 +++++++ 2 files changed, 37 insertions(+) diff --git a/ModuleFast.tests.ps1 b/ModuleFast.tests.ps1 index 8527b79..7ed29f5 100644 --- a/ModuleFast.tests.ps1 +++ b/ModuleFast.tests.ps1 @@ -103,6 +103,36 @@ Describe 'C# package extraction URL-encoded paths' { $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) diff --git a/Source/Core/ModuleFastInstaller.cs b/Source/Core/ModuleFastInstaller.cs index ef9d081..c7ad5ea 100644 --- a/Source/Core/ModuleFastInstaller.cs +++ b/Source/Core/ModuleFastInstaller.cs @@ -46,6 +46,13 @@ private static async Task ExtractPackageAsync(Stream packageStream, string desti 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)));