Improve backup validation (#849)

This commit is contained in:
Jeffrey
2026-10-21 22:47:01 +02:00
committed by GitHub
parent 97f3f805ef
commit 039ef80a8b
18 changed files with 1504 additions and 46 deletions
+47 -4
View File
@@ -40,8 +40,9 @@ function Import-RegistryBackup {
.DESCRIPTION
Validates the structure and content of the supplied backup and converts
it into a normalized representation that can be safely consumed by the
restore workflow. Throws if validation fails.
it into a normalized representation for the restore workflow. Executable
content mismatches are returned in ExecutableValueWarnings for confirmation;
structural and authorization failures throw.
.PARAMETER Backup
The raw backup object (typically parsed from JSON) to normalize.
@@ -59,6 +60,7 @@ function ConvertTo-NormalizedRegistryBackup {
)
$errors = New-Object System.Collections.Generic.List[string]
$executableValueWarnings = New-Object 'System.Collections.Generic.List[object]'
if (-not $Backup.PSObject.Properties['Version']) {
$errors.Add((Get-Translation -Key 'BackupMissingProperty' -FormatArgs @('Version')))
@@ -137,7 +139,22 @@ function ConvertTo-NormalizedRegistryBackup {
}
else {
try {
$allowListValidationErrors = @(Test-RegistryBackupMatchesSelectedFeatures -SelectedFeatureIds @($selectedFeatures) -SelectedUndoFeatureIds @($selectedUndoFeatures) -Target $normalizedTarget -RegistryKeys @($normalizedKeys))
$planErrors = New-Object System.Collections.Generic.List[string]
$planMap = Get-RegistryBackupValidationPlanMap -SelectedFeatureIds @($selectedFeatures) -SelectedUndoFeatureIds @($selectedUndoFeatures) -Target $normalizedTarget -Errors $planErrors
foreach ($planError in $planErrors) {
$errors.Add($planError)
}
if ($planErrors.Count -eq 0) {
$normalizedKeys = @(ConvertTo-CompatibleRegistryKeySnapshots -RegistryKeys @($normalizedKeys) -PlanMap $planMap)
$allowListValidationErrors = @(Test-RegistryBackupAgainstAllowList -RegistryKeys @($normalizedKeys) -PlanMap $planMap -ExecutableValueWarnings $executableValueWarnings)
if ($allowListValidationErrors.Count -eq 0) {
Test-RegistryBackupSnapshotsCanBeRestored -RegistryKeys @($normalizedKeys)
}
}
else {
$allowListValidationErrors = @()
}
foreach ($allowListValidationError in $allowListValidationErrors) {
$errors.Add([string]$allowListValidationError)
}
@@ -167,6 +184,7 @@ function ConvertTo-NormalizedRegistryBackup {
SelectedFeatures = @($selectedFeatures)
SelectedUndoFeatures = @($selectedUndoFeatures)
RegistryKeys = @($normalizedKeys)
ExecutableValueWarnings = @($executableValueWarnings.ToArray())
}
}
@@ -182,6 +200,10 @@ function ConvertTo-NormalizedRegistryBackup {
A normalized backup object (as produced by ConvertTo-NormalizedRegistryBackup) whose
RegistryKeys snapshots should be restored.
.PARAMETER ConfirmUntrustedValues
Receives the freshly validated executable mismatches before registry writes or hive
loading. Must return Boolean true after user confirmation; otherwise restore is canceled.
.OUTPUTS
PSCustomObject
Returns an object with a Result property set to $true when the restore
@@ -190,9 +212,13 @@ function ConvertTo-NormalizedRegistryBackup {
function Restore-RegistryBackupState {
param(
[Parameter(Mandatory)]
$Backup
$Backup,
# Return Boolean true only after the user explicitly accepts the displayed mismatches.
[scriptblock]$ConfirmUntrustedValues
)
# Recompute warnings and blocking validation; backup-provided flags cannot authorize restoration.
$Backup = ConvertTo-NormalizedRegistryBackup -Backup $Backup
$friendlyTarget = Get-FriendlyRegistryBackupTarget -Target ([string]$Backup.Target)
if ($script:Params.ContainsKey("WhatIf")) {
@@ -200,6 +226,23 @@ function Restore-RegistryBackupState {
return [PSCustomObject]@{ Result = $true }
}
if (@($Backup.ExecutableValueWarnings).Count -gt 0) {
Write-Host "Registry restore for $friendlyTarget requires confirmation of $(@($Backup.ExecutableValueWarnings).Count) unrecognized executable setting(s)." -ForegroundColor Yellow
foreach ($mismatch in $Backup.ExecutableValueWarnings) {
Write-Host "Unrecognized executable registry setting: $($mismatch | ConvertTo-Json -Compress -Depth 10)"
}
if (-not $ConfirmUntrustedValues) {
Write-Host 'Registry restore blocked: executable settings require explicit user confirmation.'
throw (Get-Translation -Key 'BackupExecutableConfirmationRequired')
}
$approved = & $ConfirmUntrustedValues $Backup.ExecutableValueWarnings
if ($approved -isnot [bool] -or -not $approved) {
Write-Host 'Registry restore canceled: user did not approve the unrecognized executable settings.'
return [PSCustomObject]@{ Result = $false }
}
Write-Host 'User approved restoring the unrecognized executable settings.'
}
$restoreAction = {
param($normalizedBackup)