mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-11-26 07:26:18 +01:00
feat(launcher): verify the release archive against a published SHA-256 (#831)
This commit is contained in:
+47
-1
@@ -124,6 +124,28 @@ $tempRootPath = $env:TEMP
|
|||||||
$tempWorkPath = Join-Path $tempRootPath 'Win11Debloat'
|
$tempWorkPath = Join-Path $tempRootPath 'Win11Debloat'
|
||||||
$tempArchivePath = Join-Path $tempRootPath 'win11debloat.zip'
|
$tempArchivePath = Join-Path $tempRootPath 'win11debloat.zip'
|
||||||
|
|
||||||
|
# Read the expected SHA-256 of a file from the text of a SHA256SUMS file (lines of "<64 hex digits> <file name>", as
|
||||||
|
# written by sha256sum). Returns $null when the file is not listed.
|
||||||
|
function Get-ExpectedFileHash([string]$SumsText, [string]$FileName) {
|
||||||
|
foreach ($line in ($SumsText -split "`r?`n")) {
|
||||||
|
if ($line -match '^([0-9a-fA-F]{64})\s+\*?(.+?)\s*$' -and $Matches[2] -ceq $FileName) {
|
||||||
|
return $Matches[1].ToLowerInvariant()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $null
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check a file against an expected SHA-256 value (hex digits, any case).
|
||||||
|
function Test-FileHashMatch([string]$Path, [string]$ExpectedHash) {
|
||||||
|
return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash -ieq $ExpectedHash
|
||||||
|
}
|
||||||
|
|
||||||
|
# Names of the release assets the archive is verified with. When a release does not publish both, the source archive
|
||||||
|
# GitHub generates for the release is downloaded as before, without a checksum to compare it to.
|
||||||
|
$releaseArchiveName = 'Win11Debloat.zip'
|
||||||
|
$releaseChecksumsName = 'SHA256SUMS'
|
||||||
|
$expectedArchiveHash = $null
|
||||||
|
|
||||||
# Download Win11Debloat from GitHub as a zip archive.
|
# Download Win11Debloat from GitHub as a zip archive.
|
||||||
try {
|
try {
|
||||||
if ($Dev) {
|
if ($Dev) {
|
||||||
@@ -131,7 +153,21 @@ try {
|
|||||||
$sourceUri = "https://github.com/Raphire/Win11Debloat/archive/refs/heads/master.zip"
|
$sourceUri = "https://github.com/Raphire/Win11Debloat/archive/refs/heads/master.zip"
|
||||||
} else {
|
} else {
|
||||||
Write-Output "> Downloading Win11Debloat..."
|
Write-Output "> Downloading Win11Debloat..."
|
||||||
$sourceUri = (Invoke-RestMethod https://api.github.com/repos/Raphire/Win11Debloat/releases/latest).zipball_url
|
$release = Invoke-RestMethod https://api.github.com/repos/Raphire/Win11Debloat/releases/latest
|
||||||
|
$sourceUri = $release.zipball_url
|
||||||
|
|
||||||
|
$archiveAsset = @($release.assets | Where-Object { $_.name -ceq $releaseArchiveName }) | Select-Object -First 1
|
||||||
|
$checksumsAsset = @($release.assets | Where-Object { $_.name -ceq $releaseChecksumsName }) | Select-Object -First 1
|
||||||
|
if ($archiveAsset -and $checksumsAsset) {
|
||||||
|
$checksumsPath = Join-Path $tempRootPath 'win11debloat.sha256sums'
|
||||||
|
Invoke-RestMethod $checksumsAsset.browser_download_url -OutFile $checksumsPath
|
||||||
|
$expectedArchiveHash = Get-ExpectedFileHash ([System.IO.File]::ReadAllText($checksumsPath)) $releaseArchiveName
|
||||||
|
Remove-Item $checksumsPath -Force
|
||||||
|
if (-not $expectedArchiveHash) {
|
||||||
|
throw "$releaseChecksumsName does not list $releaseArchiveName."
|
||||||
|
}
|
||||||
|
$sourceUri = $archiveAsset.browser_download_url
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Invoke-RestMethod $sourceUri -OutFile $tempArchivePath
|
Invoke-RestMethod $sourceUri -OutFile $tempArchivePath
|
||||||
}
|
}
|
||||||
@@ -144,6 +180,16 @@ catch {
|
|||||||
Exit 1
|
Exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Do not use an archive that differs from the checksum published with the release
|
||||||
|
if ($expectedArchiveHash -and -not (Test-FileHashMatch $tempArchivePath $expectedArchiveHash)) {
|
||||||
|
Remove-Item $tempArchivePath -Force
|
||||||
|
Write-Host "The downloaded Win11Debloat archive does not match the SHA-256 checksum published with the release." -ForegroundColor Red
|
||||||
|
Write-Output ""
|
||||||
|
Write-Output "Press enter to exit..."
|
||||||
|
Read-Host | Out-Null
|
||||||
|
Exit 1
|
||||||
|
}
|
||||||
|
|
||||||
# Remove old script folder if it exists, but keep configs, logs and backups
|
# Remove old script folder if it exists, but keep configs, logs and backups
|
||||||
if (Test-Path $tempWorkPath) {
|
if (Test-Path $tempWorkPath) {
|
||||||
Write-Output ""
|
Write-Output ""
|
||||||
|
|||||||
@@ -0,0 +1,177 @@
|
|||||||
|
BeforeDiscovery {
|
||||||
|
# Test data for -ForEach is built while Pester discovers the tests, before BeforeAll runs.
|
||||||
|
$abcHash = 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
|
||||||
|
$otherHash = 'f' * 64
|
||||||
|
}
|
||||||
|
|
||||||
|
BeforeAll {
|
||||||
|
# Load only the checksum functions from the launcher; dot-sourcing Get.ps1 would download and run the script.
|
||||||
|
$tokens = $null
|
||||||
|
$parseErrors = $null
|
||||||
|
$ast = [System.Management.Automation.Language.Parser]::ParseFile((Join-Path $PSScriptRoot '..\Scripts\Get.ps1'), [ref]$tokens, [ref]$parseErrors)
|
||||||
|
$parseErrors | Should -BeNullOrEmpty
|
||||||
|
foreach ($functionName in 'Get-ExpectedFileHash', 'Test-FileHashMatch') {
|
||||||
|
$function = $ast.Find({
|
||||||
|
param($node)
|
||||||
|
$node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq $functionName
|
||||||
|
}, $true)
|
||||||
|
$function | Should -Not -BeNullOrEmpty
|
||||||
|
. ([scriptblock]::Create($function.Extent.Text))
|
||||||
|
}
|
||||||
|
|
||||||
|
# SHA-256 of the three ASCII bytes "abc" (FIPS 180-2 test vector).
|
||||||
|
$script:AbcHash = 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
|
||||||
|
$script:OtherHash = 'f' * 64
|
||||||
|
}
|
||||||
|
|
||||||
|
Describe 'Get-ExpectedFileHash' {
|
||||||
|
It 'reads a hash written by sha256sum in <Case>' -ForEach @(
|
||||||
|
@{ Case = 'text mode'; Line = "$($abcHash) Win11Debloat.zip" }
|
||||||
|
@{ Case = 'binary mode'; Line = "$($abcHash) *Win11Debloat.zip" }
|
||||||
|
@{ Case = 'single space'; Line = "$($abcHash) Win11Debloat.zip" }
|
||||||
|
@{ Case = 'upper case digits'; Line = "$($abcHash.ToUpperInvariant()) Win11Debloat.zip" }
|
||||||
|
) {
|
||||||
|
Get-ExpectedFileHash $Line 'Win11Debloat.zip' | Should -BeExactly $script:AbcHash
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'picks the requested file out of several lines with CRLF or LF endings' -ForEach @(
|
||||||
|
@{ Newline = "`r`n" }
|
||||||
|
@{ Newline = "`n" }
|
||||||
|
) {
|
||||||
|
$text = "$($script:OtherHash) Get.ps1$Newline$($script:AbcHash) Win11Debloat.zip$Newline"
|
||||||
|
|
||||||
|
Get-ExpectedFileHash $text 'Win11Debloat.zip' | Should -BeExactly $script:AbcHash
|
||||||
|
Get-ExpectedFileHash $text 'Get.ps1' | Should -BeExactly $script:OtherHash
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'returns nothing for <Case>' -ForEach @(
|
||||||
|
@{ Case = 'a file that is not listed'; Text = "$($abcHash) Get.ps1" }
|
||||||
|
@{ Case = 'a name that only ends with the requested one'; Text = "$($abcHash) old-Win11Debloat.zip" }
|
||||||
|
@{ Case = 'a name that differs in case'; Text = "$($abcHash) win11debloat.zip" }
|
||||||
|
@{ Case = 'a hash that is too short'; Text = "$('a' * 63) Win11Debloat.zip" }
|
||||||
|
@{ Case = 'a hash with non-hex digits'; Text = "$('g' * 64) Win11Debloat.zip" }
|
||||||
|
@{ Case = 'empty text'; Text = '' }
|
||||||
|
) {
|
||||||
|
Get-ExpectedFileHash $Text 'Win11Debloat.zip' | Should -BeNullOrEmpty
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Describe 'Test-FileHashMatch' {
|
||||||
|
BeforeAll {
|
||||||
|
$script:AbcFile = Join-Path $TestDrive 'abc [1].bin'
|
||||||
|
[System.IO.File]::WriteAllBytes($script:AbcFile, [System.Text.Encoding]::ASCII.GetBytes('abc'))
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'accepts the matching hash in lower and upper case' {
|
||||||
|
Test-FileHashMatch $script:AbcFile $script:AbcHash | Should -BeTrue
|
||||||
|
Test-FileHashMatch $script:AbcFile $script:AbcHash.ToUpperInvariant() | Should -BeTrue
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'rejects a different hash' {
|
||||||
|
Test-FileHashMatch $script:AbcFile $script:OtherHash | Should -BeFalse
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'rejects a file whose content changed' {
|
||||||
|
$changedFile = Join-Path $TestDrive 'changed.bin'
|
||||||
|
[System.IO.File]::WriteAllBytes($changedFile, [System.Text.Encoding]::ASCII.GetBytes('abd'))
|
||||||
|
|
||||||
|
Test-FileHashMatch $changedFile $script:AbcHash | Should -BeFalse
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Describe 'Launcher archive verification flow' {
|
||||||
|
# Runs the real Get.ps1 with every download, prompt and process start mocked, so the verification gate itself is
|
||||||
|
# exercised (the helper tests above would still pass if the launcher stopped calling them). Nothing reaches the network:
|
||||||
|
# the release data uses .invalid URLs and TEMP points to TestDrive.
|
||||||
|
BeforeAll {
|
||||||
|
$getScriptPath = Join-Path $PSScriptRoot '..\Scripts\Get.ps1'
|
||||||
|
$archiveUrl = 'https://example.invalid/download/Win11Debloat.zip'
|
||||||
|
$checksumsUrl = 'https://example.invalid/download/SHA256SUMS'
|
||||||
|
$zipballUrl = 'https://example.invalid/zipball'
|
||||||
|
}
|
||||||
|
|
||||||
|
BeforeEach {
|
||||||
|
$originalTemp = $env:TEMP
|
||||||
|
$env:TEMP = $TestDrive
|
||||||
|
$archivePath = Join-Path $TestDrive 'win11debloat.zip'
|
||||||
|
|
||||||
|
# Mock bodies read this table, so a test changes it instead of re-creating variables.
|
||||||
|
$state = @{
|
||||||
|
Release = [pscustomobject]@{
|
||||||
|
zipball_url = $zipballUrl
|
||||||
|
assets = @(
|
||||||
|
[pscustomobject]@{ name = 'Win11Debloat.zip'; browser_download_url = $archiveUrl }
|
||||||
|
[pscustomobject]@{ name = 'SHA256SUMS'; browser_download_url = $checksumsUrl }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
SumsText = ''
|
||||||
|
ArchiveBytes = [System.Text.Encoding]::ASCII.GetBytes('abc')
|
||||||
|
}
|
||||||
|
|
||||||
|
Mock Clear-Host {}
|
||||||
|
Mock Read-Host {}
|
||||||
|
Mock Start-Process { throw 'The launcher must not start the script in these tests.' }
|
||||||
|
Mock Expand-Archive { throw 'reached extraction' }
|
||||||
|
Mock Invoke-RestMethod {
|
||||||
|
switch -Regex ([string]$Uri) {
|
||||||
|
'releases/latest$' { return $state.Release }
|
||||||
|
'SHA256SUMS$' { [System.IO.File]::WriteAllText($OutFile, $state.SumsText) }
|
||||||
|
default { [System.IO.File]::WriteAllBytes($OutFile, $state.ArchiveBytes) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AfterEach {
|
||||||
|
$env:TEMP = $originalTemp
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'exits without unpacking and deletes the archive when its hash does not match' {
|
||||||
|
$state.SumsText = "$('f' * 64) Win11Debloat.zip"
|
||||||
|
$global:LASTEXITCODE = 0
|
||||||
|
$ErrorActionPreference = 'Continue'
|
||||||
|
|
||||||
|
& $getScriptPath *> $null
|
||||||
|
|
||||||
|
$global:LASTEXITCODE | Should -Be 1
|
||||||
|
Test-Path -LiteralPath $archivePath | Should -BeFalse
|
||||||
|
Should -Invoke Invoke-RestMethod -Times 1 -Exactly -ParameterFilter { $Uri -eq $archiveUrl }
|
||||||
|
Should -Invoke Expand-Archive -Times 0 -Exactly
|
||||||
|
Should -Invoke Start-Process -Times 0 -Exactly
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'exits without falling back when SHA256SUMS does not list the archive' {
|
||||||
|
$state.SumsText = "$('a' * 64) Get.ps1"
|
||||||
|
$global:LASTEXITCODE = 0
|
||||||
|
$ErrorActionPreference = 'Continue'
|
||||||
|
|
||||||
|
& $getScriptPath *> $null
|
||||||
|
|
||||||
|
$global:LASTEXITCODE | Should -Be 1
|
||||||
|
Test-Path -LiteralPath $archivePath | Should -BeFalse
|
||||||
|
Should -Invoke Invoke-RestMethod -Times 1 -Exactly -ParameterFilter { $Uri -eq $checksumsUrl }
|
||||||
|
Should -Invoke Invoke-RestMethod -Times 0 -Exactly -ParameterFilter { $Uri -eq $archiveUrl -or $Uri -eq $zipballUrl }
|
||||||
|
Should -Invoke Expand-Archive -Times 0 -Exactly
|
||||||
|
Should -Invoke Start-Process -Times 0 -Exactly
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'goes on to unpack the archive when its hash matches' {
|
||||||
|
$state.SumsText = 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad Win11Debloat.zip'
|
||||||
|
$ErrorActionPreference = 'Continue'
|
||||||
|
|
||||||
|
{ & $getScriptPath *> $null } | Should -Throw '*reached extraction*'
|
||||||
|
|
||||||
|
Should -Invoke Invoke-RestMethod -Times 1 -Exactly -ParameterFilter { $Uri -eq $archiveUrl }
|
||||||
|
Should -Invoke Expand-Archive -Times 1 -Exactly
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'downloads the generated source archive when the release has no checksum asset' {
|
||||||
|
$state.Release.assets = @($state.Release.assets | Where-Object { $_.name -ne 'SHA256SUMS' })
|
||||||
|
$ErrorActionPreference = 'Continue'
|
||||||
|
|
||||||
|
{ & $getScriptPath *> $null } | Should -Throw '*reached extraction*'
|
||||||
|
|
||||||
|
Should -Invoke Invoke-RestMethod -Times 1 -Exactly -ParameterFilter { $Uri -eq $zipballUrl }
|
||||||
|
Should -Invoke Invoke-RestMethod -Times 0 -Exactly -ParameterFilter { $Uri -eq $checksumsUrl -or $Uri -eq $archiveUrl }
|
||||||
|
Should -Invoke Expand-Archive -Times 1 -Exactly
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user