Protect PowerShell from runtime package replacement

This commit is contained in:
Dandaedre 2026-11-21 11:05:32 +01:00
parent 94e290fb31
commit 07a71e0c98
5 changed files with 35 additions and 3 deletions

View File

@ -164,3 +164,7 @@ open until it is run on disposable machines with the required account setups.
- [x] Register PDF Presenter as a per-user `.pdf` handler and document the one-time supported Windows Default Apps selection (167 Linux Pester tests pass; Windows acceptance remains open above).
- [x] Remove superseded portable versions after successful activation while retaining the working version on failed updates and retrying interrupted cleanup (168 Linux Pester tests pass; 5 Windows-only skips).
- [x] Give PDF Presenter associations a dedicated branded PDF document icon that remains stable across portable updates (169 Linux Pester tests pass; 5 Windows-only skips).
## PowerShell prerequisite reliability
- [x] Remove PowerShell from routine software updates and block runtime replacement in plans/workers, including missing inventory and stale plans (Linux mocked package tests pass).

View File

@ -4,8 +4,7 @@
"name": "base",
"description": "Common tools for every Windows machine.",
"packages": [
{ "id": "Git.Git", "source": "winget", "scope": "machine", "update": "latest" },
{ "id": "Microsoft.PowerShell", "source": "winget", "scope": "machine", "update": "latest" }
{ "id": "Git.Git", "source": "winget", "scope": "machine", "update": "latest" }
],
"configs": [
{

View File

@ -64,6 +64,10 @@ function Get-PackagePlan {
continue
}
$key = "$($package.source)/$($package.id)".ToLowerInvariant()
if ($package.id -in @('Microsoft.PowerShell', 'Microsoft.PowerShell.Preview')) {
$plan.Add([pscustomobject]@{ kind = 'package'; name = $package.id; action = 'Warning'; reason = 'PowerShell is the Config Central runtime; update it separately with Config Central closed, using the original installation method.'; package = $package })
continue
}
$installed = if ($Inventory.Contains($key)) { $Inventory[$key] } else { $null }
$versionProperty = $package.PSObject.Properties['version']
$requestedVersion = if ($null -ne $versionProperty) { [string]$versionProperty.Value } else { $null }
@ -111,6 +115,10 @@ function Invoke-PackageOperation {
return [pscustomobject]@{ name = $Operation.name; status = $Operation.action; exitCode = 0; rebootRequired = $false; message = $Operation.reason }
}
$package = $Operation.package
# Enforce this in the worker too, including stale plans and missing inventory.
if ($package.id -in @('Microsoft.PowerShell', 'Microsoft.PowerShell.Preview')) {
return [pscustomobject]@{ name = $Operation.name; status = 'Warning'; exitCode = 0; rebootRequired = $false; message = 'PowerShell is the Config Central runtime; update it separately with Config Central closed, using the original installation method.' }
}
$verb = if ($Operation.action -eq 'Upgrade') { 'upgrade' } else { 'install' }
$arguments = @($verb, '--id', $package.id, '--exact', '--source', $package.source, '--silent', '--disable-interactivity', '--accept-package-agreements', '--accept-source-agreements')
if ($package.PSObject.Properties['scope']) { $arguments += @('--scope', $package.scope) }

View File

@ -5,7 +5,8 @@ BeforeAll {
Describe 'Manifest resolution' {
It 'resolves the checked-in example in profile order' {
$result = Resolve-MachineManifest -RepositoryRoot "$PSScriptRoot/.." -Machine 'beffroi-laptop'
$result.packages.Count | Should -Be 15
$result.packages.Count | Should -Be 14
$result.packages.id | Should -Not -Contain 'Microsoft.PowerShell'
$result.configs.Count | Should -Be 9
$result.packages[0].id | Should -Be 'Git.Git'
$result.packages.id | Should -Contain 'XnSoft.XnViewMP'

View File

@ -130,3 +130,23 @@ Describe 'Missing installation during upgrade' {
}
}
}
Describe 'PowerShell runtime protection' {
It 'warns even when PowerShell is missing from WinGet inventory' {
$package = [pscustomobject]@{ id = 'Microsoft.PowerShell'; source = 'winget'; update = 'latest' }
(Get-PackagePlan -Packages @($package) -Inventory @{})[0].action | Should -Be 'Warning'
}
It 'blocks stale install and upgrade plans in the worker' {
InModuleScope Packages {
Mock Invoke-WingetCapture { throw 'The running runtime must not be replaced' }
foreach ($id in @('Microsoft.PowerShell', 'Microsoft.PowerShell.Preview')) {
foreach ($action in @('Install', 'Upgrade')) {
$operation = [pscustomobject]@{ name = $id; action = $action; package = [pscustomobject]@{ id = $id; source = 'winget' } }
(Invoke-PackageOperation $operation).status | Should -Be 'Warning'
}
}
Should -Invoke Invoke-WingetCapture -Times 0
}
}
}