Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3075,6 +3075,24 @@ if ($compiled) {
Test-Assert ($cmdHits -eq 0 -and $cmdThrew -eq 0) ("a quote in a list element can't make cmd.exe /c run another command (" + $cmdHits + " of " + ($cmdNames.Count * $injPayloads.Count) + " did, " + $cmdThrew + " failed to run)")
$cmdBatch = Invoke-PkProcessBatch -MaxConcurrent 1 -Commands @(@{ FilePath = 'cmd.exe'; WorkingDirectory = $injDir; Arguments = @('/d', '/c', 'echo', $injPayloads[0]) })
Test-Assert (-not [System.IO.File]::Exists($injMarker) -and $cmdBatch.Results[0].ExitCode -eq 0) "the same for cmd.exe through the batch"
# A drive-relative path (C:cmd.exe, from System32 as that drive's current
# folder) still names cmd.exe, so it gets the same quoting.
$sys32 = Join-Path $env:SystemRoot 'System32'
$cmdDrive = $sys32.Substring(0, 2)
$relHits = 0
$relThrew = 0
$relRan = 0
Push-Location -LiteralPath $sys32
try {
foreach ($cmdName in @(($cmdDrive + 'cmd.exe'), ($cmdDrive + 'cmd'), ($cmdDrive + 'CMD.EXE.'))) {
try {
$relResult = Invoke-PkProcessWithTimeout -FilePath $cmdName -Arguments @('/d', '/c', 'echo', $injPayloads[0]) -WorkingDirectory $injDir -TimeoutMs 10000
if ($relResult.ExitCode -eq 0) { $relRan++ }
} catch { $relThrew++; Write-Host " injection probe threw: $_" }
if ([System.IO.File]::Exists($injMarker)) { $relHits++; [System.IO.File]::Delete($injMarker) }
}
} finally { Pop-Location }
Test-Assert ($relHits -eq 0 -and $relThrew -eq 0 -and $relRan -eq 3) ("nor under a drive-relative path to cmd.exe (" + $relHits + " of 3 did, " + $relThrew + " failed to run, " + $relRan + " ran)")

# A line break would end cmd.exe's command line and drop the rest, so it's
# refused for a batch file, and still fine for any other program.
Expand Down
4 changes: 2 additions & 2 deletions ParallelKit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1593,9 +1593,9 @@ function Resolve-PkArgumentList {
# Windows drops trailing dots and spaces from a file name, so
# 'run.cmd.' still runs through cmd.exe. cmd.exe itself, given a list
# (cmd /c tool "a b"), reads its command line the same way, so it gets
# the same quoting.
# the same quoting, under a drive-relative path (C:cmd.exe) too.
$exeName = $FilePath.Trim().Trim('"').TrimEnd(' ', '.')
$forBatchFile = ($exeName -match '\.(bat|cmd)$' -or $exeName -match '(^|[\\/])cmd(\.exe)?$')
$forBatchFile = ($exeName -match '\.(bat|cmd)$' -or $exeName -match '(^|[\\/:])cmd(\.exe)?$')
return (ConvertTo-PkArgumentStringCore -Arguments @($Arguments) -ForBatchFile:$forBatchFile)
}
return [string]$Arguments
Expand Down