Skip to content

Commit 383aa36

Browse files
Validate manifest ModuleVersion before using it as a path segment
Parse ModuleVersion as [version] and fail fast on a malformed value so a crafted manifest cannot inject path separators/traversal into the install path. Empty/unstamped manifests still fall back to the 999.0.0 placeholder.
1 parent 88c954d commit 383aa36

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

src/Helpers/Helpers.psm1

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,18 @@ function Install-PSModule {
315315
Write-Host '::endgroup::'
316316
# Install into a version folder that matches the manifest's ModuleVersion so PowerShell
317317
# accepts the module. Fall back to the 999.0.0 placeholder only when the manifest is
318-
# unstamped (e.g. a build that opted out of version resolution).
319-
$moduleVersion = (Import-PowerShellDataFile -Path $manifestFilePath).ModuleVersion
320-
if ([string]::IsNullOrWhiteSpace($moduleVersion)) {
318+
# unstamped (e.g. a build that opted out of version resolution). Validate the version as a
319+
# real [version] so a malformed manifest cannot inject path separators/traversal into the
320+
# install path.
321+
$manifestVersion = (Import-PowerShellDataFile -Path $manifestFilePath).ModuleVersion
322+
if ([string]::IsNullOrWhiteSpace($manifestVersion)) {
321323
$moduleVersion = '999.0.0'
324+
} else {
325+
$parsedVersion = $null
326+
if (-not [version]::TryParse($manifestVersion, [ref] $parsedVersion)) {
327+
throw "ModuleVersion '$manifestVersion' in [$manifestFilePath] is not a valid version."
328+
}
329+
$moduleVersion = $parsedVersion.ToString()
322330
}
323331
$PSModulePath = $env:PSModulePath -split [System.IO.Path]::PathSeparator | Select-Object -First 1
324332
$codePath = New-Item -Path (Join-Path -Path (Join-Path -Path $PSModulePath -ChildPath $moduleName) -ChildPath $moduleVersion) -ItemType Directory -Force | Select-Object -ExpandProperty FullName

0 commit comments

Comments
 (0)