Pode
Pode copied to clipboard
Vulnerable Add-PodeStaticRoute
Describe the Bug
StaticRoute gives access to all files outside the Source
(Edited)
Expected Behavior
StaticRoute shoud only give access to -Source
Platform
- OS: Windows Server 2022
- Browser: Any
- Versions:
- Pode: 2.12.1
- PowerShell: 7.5.4 / 7.5.5
Hi @Artyum,
Thanks for reporting this, I'll look at getting it fixed.
Hello Please consider attached fix.
<#
.SYNOPSIS
Checks if TestPath is a child path relative to BasePath (protection against path traversal).
#>
function Test-PodePathIsWithinBase {
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory = $true)]
[string]$BasePath,
[Parameter(Mandatory = $true)]
[string]$TestPath
)
try {
# Normalize both paths to full paths, removing trailing separators
$baseFull = [System.IO.Path]::GetFullPath($BasePath).TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar)
$testFull = [System.IO.Path]::GetFullPath($TestPath).TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar)
# Compare case-insensitively (standard for Windows) and normalize slashes
$baseFull = $baseFull.Replace('\', '/').ToLowerInvariant()
$testFull = $testFull.Replace('\', '/').ToLowerInvariant()
# Must be exactly the same directory OR a subdirectory/file within it
return $testFull.StartsWith($baseFull + '/', [StringComparison]::Ordinal) -or
$testFull -eq $baseFull
}
catch {
# If normalization fails, treat the path as unsafe
return $false
}
}
<#
.SYNOPSIS
Finds a static route for a given path in a Pode web server application, with optional checks for public routes.
.DESCRIPTION
This function searches for a static route matching the specified path within a Pode web server application. It attempts to resolve the route to a physical file or directory and supports additional checks for public routes as a fallback option. The function returns a hashtable with route details, including whether the route is for a downloadable file, if it's cacheable, and whether it redirects to a default document.
.PARAMETER Path
The URL path for which to find a static route. This parameter is mandatory.
.PARAMETER EndpointName
Optional. Specifies the name of the endpoint to which the route may belong. If not provided, the function searches across all endpoints.
.PARAMETER CheckPublic
A switch parameter. If specified, the function also checks for the route in public routes as a fallback option.
.PARAMETER NoEscape
If supplied, the path will not be escaped. This is useful for paths that contain expected wildcards, or are already escaped.
.EXAMPLE
$staticRoute = Find-PodeStaticRoute -Path '/images/logo.png' -CheckPublic
Searches for a static route for '/images/logo.png'. If not found, checks if a public route exists for the same path.
.EXAMPLE
$staticRoute = Find-PodeStaticRoute -Path '/css/style.css' -EndpointName 'WebUI'
Searches for a static route for '/css/style.css' specifically within the 'WebUI' endpoint, without checking public routes.
.OUTPUTS
Hashtable. Returns a hashtable containing the route details, such as the source path, download flag, cacheability, and redirect status.
.NOTES
This is an internal function and may change in future releases of Pode.
#>
function Find-PodeStaticRoute {
[CmdletBinding()]
[OutputType([hashtable])]
param(
[Parameter(Mandatory = $true)]
[string]
$Path,
[Parameter()]
[string]
$EndpointName,
[switch]
$CheckPublic,
[switch]
$NoEscape
)
# escape characters in the path
$Path = Protect-PodePath -Path $Path -NoEscape:$NoEscape
# attempt to get a static route for the path
$found = Find-PodeRoute -Method 'static' -Path $Path -EndpointName $EndpointName
$download = [bool]$found.Download
$source = $null
$isDefault = $false
$redirectToDefault = [bool]$found.RedirectToDefault
$fileInfo = $null
# if we have a defined static route, use that
if ($null -ne $found) {
# see if we have a file
$file = [string]::Empty
$matchingPath = "$($found.Path.Replace('*', '.+?'))$"
if ($Path -imatch $matchingPath) {
$file = Protect-PodeValue -Value $Matches['file'] -Default ([string]::Empty)
}
# --- PATH TRAVERSAL PROTECTION ---
$combinedPath = [System.IO.Path]::Combine($found.Source, $file)
if (-not (Test-PodePathIsWithinBase -BasePath $found.Source -TestPath $combinedPath)) {
# Attempt to exit source directory -> block (404)
# Write-PodeHost "Path traversal attempt blocked for static route: $combinedPath" -Level Warning -NoNewline
return $null
}
# ------------------------------------------------
# if $file doesn't exist return $null
$fileInfo = Get-Item -Path $combinedPath -Force -ErrorAction Ignore
if ($null -eq $fileInfo) {
return $null
}
# if this is a folder, we need to check defaults
if (!$found.Download -and $fileInfo.PSIsContainer -and (($null -ne $found.Defaults) -and ($found.Defaults.Count -gt 0))) {
foreach ($def in $found.Defaults) {
if ([string]::IsNullOrEmpty($def)) {
continue
}
$defCombined = [System.IO.Path]::Combine($fileInfo.FullName, $def)
# Additional validation for default files
if (-not (Test-PodePathIsWithinBase -BasePath $found.Source -TestPath $defCombined)) {
continue
}
$defFileInfo = Get-Item -Path $defCombined -Force -ErrorAction Ignore
if ($null -ne $defFileInfo) {
$file = [System.IO.Path]::Combine($file, $def)
$fileInfo = $defFileInfo
$isDefault = $true
break
}
}
}
$source = [System.IO.Path]::Combine($found.Source, $file)
# Final verification of source (just in case)
if (-not (Test-PodePathIsWithinBase -BasePath $found.Source -TestPath $source)) {
return $null
}
}
# check public, if flagged
if ($CheckPublic -and [string]::IsNullOrEmpty($source)) {
# check if we have a public route
$pubRoute = Find-PodePublicRoute -Path $Path
$source = $pubRoute.Source
$fileInfo = $pubRoute.FileInfo
# set download/default flags
$download = $false
$found = $null
$isDefault = $false
$redirectToDefault = $false
}
# return nothing if no source
if ([string]::IsNullOrEmpty($source)) {
return $null
}
# return the route details
return @{
Content = @{
Source = $source
FileInfo = $fileInfo
IsDownload = $download
IsCachable = (Test-PodeRouteValidForCaching -Path $Path)
RedirectToDefault = ($redirectToDefault -and $isDefault)
}
Route = $found
}
}
This issue was discovered by @Tosieeek. Thanks!