Skip to content

Commit b8f380d

Browse files
committed
fix(toggles): quote relaunch argument values
Get-AtlasToggleRelaunchArgumentList quoted -LauncherPath but left -Name and -State bare, so a value with a space or shell metacharacter would mis-split across the Start-Process and TrustedInstaller relaunch joins. Quote them the same way; -File binding strips the quotes on the receiving side.
1 parent 64b948d commit b8f380d

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

playbook/Executables/AtlasModules/Scripts/Modules/Atlas.Toggles/Domain/Engine.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ function Get-AtlasToggleRelaunchArgumentList {
222222
$argumentList = @(
223223
'-NoProfile', '-NoLogo', '-ExecutionPolicy', 'Bypass',
224224
'-File', ('"{0}"' -f $invokeTogglePath),
225-
'-Name', $Name
225+
'-Name', ('"{0}"' -f $Name)
226226
)
227227
if ($State) {
228-
$argumentList += @('-State', $State)
228+
$argumentList += @('-State', ('"{0}"' -f $State))
229229
}
230230
if ($LauncherPath) {
231231
$argumentList += @('-LauncherPath', ('"{0}"' -f $LauncherPath))

tests/Atlas.Toggles.Tests.ps1

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,48 @@ Describe 'New-ToggleLaunchers.ps1' {
552552
$LASTEXITCODE | Should -Be 0 -Because ($output -join "`n")
553553
}
554554
}
555+
556+
Describe 'Get-AtlasToggleRelaunchArgumentList' {
557+
# The relaunch argument list is space-joined by both consumers (Start-Process
558+
# and the TrustedInstaller cmd join), so every value that can contain a space
559+
# or shell metacharacter must be quoted. Get-AtlasToggleRelaunchArgumentList is
560+
# module-private, hence InModuleScope.
561+
562+
BeforeEach {
563+
Mock Get-AtlasContext -ModuleName Atlas.Toggles {
564+
[pscustomobject]@{ AtlasModulesPath = 'C:\Windows\AtlasModules' }
565+
}
566+
}
567+
568+
It 'quotes the -Name value so names with spaces survive the join' {
569+
InModuleScope Atlas.Toggles {
570+
$list = Get-AtlasToggleRelaunchArgumentList -Name 'My Toggle' -State 'Enable'
571+
572+
$nameIndex = [array]::IndexOf($list, '-Name')
573+
$list[$nameIndex + 1] | Should -Be '"My Toggle"'
574+
}
575+
}
576+
577+
It 'quotes the -State value when a state is supplied' {
578+
InModuleScope Atlas.Toggles {
579+
$list = Get-AtlasToggleRelaunchArgumentList -Name 'BackgroundApps' -State 'Disable'
580+
581+
$stateIndex = [array]::IndexOf($list, '-State')
582+
$list[$stateIndex + 1] | Should -Be '"Disable"'
583+
}
584+
}
585+
586+
It 'produces a joined command line with no unquoted space-bearing values' {
587+
InModuleScope Atlas.Toggles {
588+
$list = Get-AtlasToggleRelaunchArgumentList -Name 'My Toggle' -State 'Enable Now' -LauncherPath 'C:\Program Files\x.cmd'
589+
590+
$joined = $list -join ' '
591+
592+
# Every space in the joined string must fall inside a quoted region;
593+
# an odd number of quotes before any given space would reveal a bare gap.
594+
$joined | Should -Match '-Name "My Toggle"'
595+
$joined | Should -Match '-State "Enable Now"'
596+
$joined | Should -Match '-LauncherPath "C:\\Program Files\\x.cmd"'
597+
}
598+
}
599+
}

0 commit comments

Comments
 (0)