fix: update RunOnce command handling to avoid & enforce length limits (#866)

This commit is contained in:
Jeffrey
2026-10-27 23:08:46 +01:00
committed by GitHub
parent 9f192bd343
commit 08b6ddde9e
5 changed files with 172 additions and 19 deletions
+55
View File
@@ -1,6 +1,7 @@
BeforeAll {
$rebootFeatureLabelsScriptPath = Join-Path $PSScriptRoot '..\Scripts\Helpers\Get-RebootFeatureLabels.ps1'
. $rebootFeatureLabelsScriptPath
. (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-JsonFile.ps1')
. (Join-Path $PSScriptRoot '..\Scripts\FileIO\Import-LanguageFile.ps1')
# Builds a $script:Lang fixture whose Features section mirrors $script:Features exactly,
@@ -26,6 +27,60 @@ BeforeAll {
}
}
Describe 'Get-DeferredAppRemovalLabels' {
BeforeEach {
$script:AppsListFilePath = Join-Path $PSScriptRoot '..\Config\Apps.json'
$script:Lang = Import-LanguageFile -LanguageCode 'en-US' -LanguagesPath (Join-Path $PSScriptRoot '..\Config\Languages')
$script:WingetDeferredRemovals = @{}
}
It 'returns one Edge label for <Ids>' -ForEach @(
@{ Ids = @('Microsoft.Edge', 'XPFFTQ037JWMHS') }
@{ Ids = @('XPFFTQ037JWMHS') }
) {
foreach ($id in $Ids) { $script:WingetDeferredRemovals[$id] = 'Alice' }
$labels = @(Get-DeferredAppRemovalLabels)
$labels | Should -HaveCount 1
$labels[0] | Should -BeExactly 'Microsoft Edge'
}
It 'retains other apps and unknown IDs alongside a grouped app' {
$script:WingetDeferredRemovals = @{ 'Microsoft.Edge' = 'Alice'; 'XPFFTQ037JWMHS' = 'Alice'; 'Microsoft.OneDrive' = 'Alice'; 'Unknown.App' = 'Alice' }
$labels = @(Get-DeferredAppRemovalLabels)
$labels | Should -HaveCount 3
$labels | Should -Contain 'Microsoft Edge'
$labels | Should -Contain 'Unknown.App'
$labels | Should -Contain (Get-Translation -Key 'Microsoft.OneDrive' -Field 'FriendlyName' -Section 'Apps')
}
It 'keeps distinct catalog apps even when their translated names match' {
Mock Import-JsonFile {
[PSCustomObject]@{ Apps = @(
[PSCustomObject]@{ AppId = @('First.App', 'First.Alias') }
[PSCustomObject]@{ AppId = 'Second.App' }
) }
}
$script:Lang.Apps = [PSCustomObject]@{
'First.App' = [PSCustomObject]@{ FriendlyName = 'Shared name' }
'Second.App' = [PSCustomObject]@{ FriendlyName = 'Shared name' }
}
$script:WingetDeferredRemovals = @{ 'First.App' = 'Alice'; 'First.Alias' = 'Alice'; 'Second.App' = 'Alice' }
$labels = @(Get-DeferredAppRemovalLabels)
$labels | Should -HaveCount 2
@($labels | Where-Object { $_ -eq 'Shared name' }).Count | Should -Be 2
}
It 'returns no labels without deferred removals' {
@(Get-DeferredAppRemovalLabels) | Should -HaveCount 0
}
}
Describe 'Get-RebootFeatureLabels' {
BeforeEach {
$script:Params = @{
+77 -5
View File
@@ -231,6 +231,7 @@ Describe 'Remove-WinGetApp' {
Remove-WinGetApp -app 'One.App'
Should -Invoke Invoke-NonBlocking -Times 1 -Exactly -ParameterFilter { $ArgumentList -eq 'One.App' }
Should -Invoke Set-RunOnceWingetTask -Times $Scheduled -Exactly -ParameterFilter { $appId -eq 'One.App' }
$script:WingetDeferredRemovals.ContainsKey('One.App') | Should -Be ([bool]$Scheduled)
}
It 'limits a foreground winget uninstall to two minutes' {
@@ -293,6 +294,7 @@ Describe 'Remove-WinGetApp' {
Mock Set-RunOnceWingetTask { $false }
Remove-WinGetApp -app 'One.App' | Should -BeFalse
$script:WingetDeferredRemovals.ContainsKey('One.App') | Should -BeFalse
}
}
@@ -580,6 +582,11 @@ Describe 'Remove-AppxApp' {
}
Describe 'Set-RunOnceWingetTask' {
BeforeAll {
$captureScript = Join-Path $TestDrive 'Capture native arguments.ps1'
'$args | ConvertTo-Json -Compress' | Set-Content -LiteralPath $captureScript -Encoding UTF8
}
BeforeEach {
$script:Params = @{ User = 'Alice' }
Mock Invoke-WithTargetUserHive {
@@ -593,7 +600,7 @@ Describe 'Set-RunOnceWingetTask' {
}
}
It 'encodes shell metacharacters and writes a safe RunOnce operation' {
It 'writes a direct WinGet command with shell metacharacters kept as literals' {
Set-RunOnceWingetTask -appId "Vendor.App&'Test"
Should -Invoke Invoke-WithTargetUserHive -Times 1 -Exactly -ParameterFilter { $TargetUserName -eq 'Alice' }
Should -Invoke Invoke-RegistryOperation -Times 1 -Exactly -ParameterFilter {
@@ -601,10 +608,58 @@ Describe 'Set-RunOnceWingetTask' {
$RegFilePath -eq '<dynamic>'
}
$script:runOnceOperation.ValueData | Should -Match '^powershell\.exe -NoProfile -EncodedCommand [A-Za-z0-9+/=]+$'
$encodedCommand = $script:runOnceOperation.ValueData -replace '^powershell\.exe -NoProfile -EncodedCommand ', ''
$decodedCommand = [System.Text.Encoding]::Unicode.GetString([Convert]::FromBase64String($encodedCommand))
$decodedCommand | Should -Be "winget uninstall --accept-source-agreements --disable-interactivity --id 'Vendor.App&''Test'"
$script:runOnceOperation.ValueData | Should -BeExactly "winget.exe uninstall --accept-source-agreements --disable-interactivity --id `"Vendor.App&'Test`""
}
It 'quotes <AppId> as one native argument' -ForEach @(
@{ AppId = 'Vendor.App" --all'; QuotedId = '"Vendor.App\" --all"' }
@{ AppId = 'Vendor.App\"Test'; QuotedId = '"Vendor.App\\\"Test"' }
@{ AppId = 'Vendor.App\'; QuotedId = '"Vendor.App\\"' }
@{ AppId = 'Vendor.App & | < > ^ %PATH%'; QuotedId = '"Vendor.App & | < > ^ %PATH%"' }
) {
Set-RunOnceWingetTask -appId $AppId | Should -BeTrue
$script:runOnceOperation.ValueData | Should -BeExactly "winget.exe uninstall --accept-source-agreements --disable-interactivity --id $QuotedId"
# Exercise native parsing with a harmless capture script instead of running WinGet.
$process = [System.Diagnostics.Process]::new()
$process.StartInfo.FileName = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe'
$nativeArguments = $script:runOnceOperation.ValueData.Substring('winget.exe '.Length)
$process.StartInfo.Arguments = "-NoProfile -NonInteractive -ExecutionPolicy Bypass -File `"$captureScript`" $nativeArguments"
$process.StartInfo.UseShellExecute = $false
$process.StartInfo.CreateNoWindow = $true
$process.StartInfo.RedirectStandardOutput = $true
$process.StartInfo.RedirectStandardError = $true
try {
$null = $process.Start()
if (-not $process.WaitForExit(15000)) {
$process.Kill()
throw 'Native argument capture timed out.'
}
$output = $process.StandardOutput.ReadToEnd()
$stderr = $process.StandardError.ReadToEnd()
$process.ExitCode | Should -Be 0 -Because $stderr
$stderr | Should -BeNullOrEmpty
$captured = $output | ConvertFrom-Json
$captured | Should -HaveCount 5
$captured[3] | Should -BeExactly '--id'
$captured[4] | Should -BeExactly $AppId
}
finally { $process.Dispose() }
}
It 'keeps a typical command within the RunOnce limit' {
Set-RunOnceWingetTask -appId 'Microsoft.Edge' | Should -BeTrue
$script:runOnceOperation.ValueData.Length | Should -Be 93
}
It 'accepts the RunOnce length limit and rejects longer commands without writing to a hive' {
Set-RunOnceWingetTask -appId ('A' * 181) | Should -BeTrue
$script:runOnceOperation.ValueData.Length | Should -Be 260
Mock Write-Error {}
Set-RunOnceWingetTask -appId ('A' * 182) | Should -BeFalse
Should -Invoke Invoke-WithTargetUserHive -Times 1 -Exactly
Should -Invoke Write-Error -Times 1 -Exactly -ParameterFilter { $Message -like '*exceeds 260 characters*' }
}
It 'schedules every resolved target but reports incomplete profile enumeration' {
@@ -665,6 +720,23 @@ Describe 'AllUsers deferred WinGet profile enumeration' {
Mock Test-AppInWingetList { $false }
}
It 'defers both Edge IDs after successful Sysprep scheduling without immediate verification or a force-removal prompt' {
$script:Params = @{ Sysprep = $true; AppRemovalTarget = 'AllUsers' }
Mock Invoke-NonBlocking { [PSCustomObject]@{ ExitCode = 1; Output = @() } }
Mock Set-RunOnceWingetTask { $true }
Mock Request-EdgeForceRemove {}
Mock Test-AppInWingetList { $true }
Remove-SelectedApps -appsList @('Microsoft.Edge', 'XPFFTQ037JWMHS') | Should -BeTrue
$script:WingetDeferredRemovals.ContainsKey('Microsoft.Edge') | Should -BeTrue
$script:WingetDeferredRemovals.ContainsKey('XPFFTQ037JWMHS') | Should -BeTrue
$script:AppRemovalFailures | Should -Be 0
Should -Invoke Set-RunOnceWingetTask -Times 2 -Exactly -ParameterFilter { $Target.AllUsers }
Should -Invoke Get-WingetInstalledApps -Times 0 -Exactly
Should -Invoke Request-EdgeForceRemove -Times 0 -Exactly
}
It 'keeps resolved profiles and Default while returning a SID translation failure' {
$result = Get-RunOnceWingetTargetUserNames