diff --git a/src/Configs.psm1 b/src/Configs.psm1 index cf15fa3..f86193b 100644 --- a/src/Configs.psm1 +++ b/src/Configs.psm1 @@ -143,10 +143,13 @@ function Invoke-ConfigOperation { function Restore-ConfigBackup { param([Parameter(Mandatory)][string]$Name, [Parameter(Mandatory)]$Paths, [Parameter(Mandatory)]$State) + if (-not $State.Contains('configs') -or -not $State.configs.Contains($Name)) { throw "No managed state exists for config '$Name'." } + $mode = if ($State.configs[$Name].Contains('mode')) { [string]$State.configs[$Name].mode } else { 'copy' } + if ($mode -eq 'registry') { throw "Automatic restore is not supported for registry config '$Name'; inspect its JSON backup and restore selected values deliberately." } + if ($mode -eq 'hosts-block') { throw "Automatic restore is not supported for hosts config '$Name'; restore its backup from an administrator shell." } $folder = Join-Path $Paths.Backups $Name $backup = Get-ChildItem -LiteralPath $folder -Filter '*.bak' -File -ErrorAction SilentlyContinue | Sort-Object LastWriteTimeUtc -Descending | Select-Object -First 1 if ($null -eq $backup) { throw "No backup exists for config '$Name'." } - if (-not $State.Contains('configs') -or -not $State.configs.Contains($Name)) { throw "No managed state exists for config '$Name'." } $target = [string]$State.configs[$Name].target $current = [pscustomobject]@{ name = $Name; target = $target } [void](Backup-ConfigTarget -Operation $current -Paths $Paths) diff --git a/tests/Configs.Tests.ps1 b/tests/Configs.Tests.ps1 index 344ae37..ebd143e 100644 --- a/tests/Configs.Tests.ps1 +++ b/tests/Configs.Tests.ps1 @@ -58,4 +58,11 @@ Describe 'Configuration deployment' { (Invoke-ConfigOperation -Operation $plan[0] -Paths $paths -State $state).status | Should -Be 'Succeeded' Test-Path -LiteralPath $configs[0].target | Should -BeTrue } + + It 'refuses unsafe automatic restoration of registry and hosts backups' { + $registryState = @{ configs = @{ 'mpc-hc-settings' = @{ mode = 'registry'; target = 'HKCU:\Software\MPC-HC\MPC-HC' } } } + { Restore-ConfigBackup -Name mpc-hc-settings -Paths $paths -State $registryState } | Should -Throw '*not supported for registry*' + $hostsState = @{ configs = @{ 'windows-hosts' = @{ mode = 'hosts-block'; target = 'C:\Windows\System32\drivers\etc\hosts' } } } + { Restore-ConfigBackup -Name windows-hosts -Paths $paths -State $hostsState } | Should -Throw '*not supported for hosts*' + } }