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
+16 -11
View File
@@ -211,6 +211,10 @@ function Remove-WinGetApp {
else { "user $($runOnceTarget.UserName)" }
Write-Host "Adding RunOnce uninstall for $app for $targetDescription..."
$scheduleSucceeded = Set-RunOnceWingetTask -appId $app -Target $runOnceTarget
if ($scheduleSucceeded) {
if (-not $script:WingetDeferredRemovals) { $script:WingetDeferredRemovals = @{} }
$script:WingetDeferredRemovals[$app] = $runOnceTarget.UserName
}
}
if ($null -ne $SchedulingFailed) { $SchedulingFailed.Value = -not $scheduleSucceeded }
@@ -549,10 +553,9 @@ function Get-RunOnceWingetTargetUserNames {
which handles hive loading and HKEY_USERS\Default → SID remapping.
Used instead of static .reg files to avoid file dependency for each WinGet app.
The winget command is Base64-encoded and invoked via powershell.exe -EncodedCommand
rather than interpolated directly into cmd.exe /c. This prevents shell metacharacters
(such as &, |, <, >, ^, ") in the app ID from being interpreted as command syntax,
even if future catalog updates introduce IDs containing those characters.
Invokes winget.exe directly with a quoted app ID, without a shell wrapper.
Escapes embedded quotes and trailing backslashes for native argument parsing,
and rejects commands exceeding RunOnce's 260-character limit.
.PARAMETER appId
The winget package ID to schedule for uninstall (e.g. 'XP9CXNGPPJ97XX').
@@ -584,18 +587,20 @@ function Set-RunOnceWingetTask {
$taskName = "Uninstall_$safeAppId"
# Escape single quotes in appId, then wrap in single quotes so cmd/pwsh metacharacters
# like & | < > ^ " are treated as literals. Base64-encode the whole command so the
# RunOnce value contains only [A-Za-z0-9+/=] — safe in any shell parser.
$escapedAppId = $appId.Replace("'", "''")
$wingetCommand = "winget uninstall --accept-source-agreements --disable-interactivity --id '$escapedAppId'"
$encodedWingetCommand = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($wingetCommand))
# Preserve the ID as one native argument. No cmd.exe or PowerShell interprets its contents.
$escapedAppId = $appId -replace '(\\*)"', '$1$1\"'
$escapedAppId = $escapedAppId -replace '(\\+)$', '$1$1'
$wingetCommand = "winget.exe uninstall --accept-source-agreements --disable-interactivity --id `"$escapedAppId`""
if ($wingetCommand.Length -gt 260) {
Write-Error "Failed to schedule uninstall task for $($appId): the RunOnce command exceeds 260 characters."
return $false
}
$operation = [PSCustomObject]@{
KeyPath = 'HKEY_USERS\Default\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce'
ValueName = $taskName
ValueType = 'String'
ValueData = "powershell.exe -NoProfile -EncodedCommand $encodedWingetCommand"
ValueData = $wingetCommand
OperationType = 'SetValue'
}
+1 -2
View File
@@ -185,9 +185,8 @@ function Show-ApplyModal {
}
}
if ($script:WingetDeferredRemovals -and $script:WingetDeferredRemovals.Count -gt 0) {
foreach ($appId in @($script:WingetDeferredRemovals.Keys | Sort-Object)) {
foreach ($appName in @(Get-DeferredAppRemovalLabels)) {
$tb = [System.Windows.Controls.TextBlock]::new()
$appName = Get-Translation -Key $appId -Field 'FriendlyName' -Section 'Apps'
$label = Get-Translation -Key 'ApplyRebootRequiredWingetDeferred' -FormatArgs @($appName)
$tb.Text = "$([char]0x2022) $label"
$tb.FontSize = 12
+23 -1
View File
@@ -24,4 +24,26 @@ function Get-RebootFeatureLabels {
}
return $rebootFeatureLabels
}
}
<#
.SYNOPSIS
Resolves one deferred-removal display label per catalog app, grouping its package IDs.
#>
function Get-DeferredAppRemovalLabels {
if (-not $script:WingetDeferredRemovals -or $script:WingetDeferredRemovals.Count -eq 0) { return }
$catalog = Import-JsonFile -filePath $script:AppsListFilePath
$primaryIds = @{}
foreach ($app in $catalog.Apps) {
$ids = @($app.AppId)
foreach ($id in $ids) { $primaryIds[$id] = $ids[0] }
}
$deferredPrimaryIds = foreach ($appId in $script:WingetDeferredRemovals.Keys) {
if ($primaryIds.ContainsKey($appId)) { $primaryIds[$appId] } else { $appId }
}
foreach ($appId in @($deferredPrimaryIds | Sort-Object -Unique)) {
Get-Translation -Key $appId -Field 'FriendlyName' -Section 'Apps'
}
}