From dae0123a1795985a7b9c6b51654347583a98a9b0 Mon Sep 17 00:00:00 2001 From: Alexander Loren Date: Sat, 29 Aug 2026 06:21:03 -0700 Subject: [PATCH] Refactored directory check function --- export-windows.ps1 | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/export-windows.ps1 b/export-windows.ps1 index 7a5e9ae..951083a 100644 --- a/export-windows.ps1 +++ b/export-windows.ps1 @@ -17,6 +17,26 @@ function Require-Environment([string]$Name) { return $value } +function Test-PathIsWithinDirectory([string]$ParentDirectory, [string]$CandidateDirectory) { + # [IO.Path]::GetRelativePath is not available in Windows PowerShell 5.1. + # Compare complete paths with a separator boundary so, for example, + # C:\workspace-other is not considered a child of C:\workspace. + $parentPath = [IO.Path]::GetFullPath($ParentDirectory) + $candidatePath = [IO.Path]::GetFullPath($CandidateDirectory) + $directorySeparator = [IO.Path]::DirectorySeparatorChar.ToString() + $alternateSeparator = [IO.Path]::AltDirectorySeparatorChar.ToString() + + if ($candidatePath.Equals($parentPath, [StringComparison]::OrdinalIgnoreCase)) { + return $true + } + + if (-not $parentPath.EndsWith($directorySeparator) -and -not $parentPath.EndsWith($alternateSeparator)) { + $parentPath += $directorySeparator + } + + return $candidatePath.StartsWith($parentPath, [StringComparison]::OrdinalIgnoreCase) +} + function Get-ActualArchitecture { $architecture = $env:PROCESSOR_ARCHITEW6432 if ([string]::IsNullOrWhiteSpace($architecture)) { @@ -98,8 +118,7 @@ if ($HostArch -ne (Get-ActualArchitecture)) { Fail "host-arch '$HostArch' does n $workspaceDirectory = [IO.Path]::GetFullPath($Workspace) if ([IO.Path]::IsPathRooted($ProjectPath)) { Fail 'project-path must be relative to the checked-out workspace.' } $ProjectDirectory = [IO.Path]::GetFullPath((Join-Path $workspaceDirectory $ProjectPath)) -$projectRelativePath = [IO.Path]::GetRelativePath($workspaceDirectory, $ProjectDirectory) -if ($projectRelativePath -eq '..' -or $projectRelativePath.StartsWith("..$([IO.Path]::DirectorySeparatorChar)") -or [IO.Path]::IsPathRooted($projectRelativePath)) { Fail 'project-path must remain inside the checked-out workspace.' } +if (-not (Test-PathIsWithinDirectory $workspaceDirectory $ProjectDirectory)) { Fail 'project-path must remain inside the checked-out workspace.' } if (-not (Test-Path -LiteralPath (Join-Path $ProjectDirectory 'project.godot') -PathType Leaf)) { Fail "No project.godot was found in project-path '$ProjectPath'." } $PresetFile = Join-Path $ProjectDirectory 'export_presets.cfg' if (-not (Test-Path -LiteralPath $PresetFile -PathType Leaf)) { Fail "No export_presets.cfg was found in project-path '$ProjectPath'." }