Skip to content

Commit acd0073

Browse files
[windows] implement checksum validation for Powershell Core (#8247)
1 parent 08f6a05 commit acd0073

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

images/win/scripts/Installers/Install-PowershellCore.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
################################################################################
22
## File: Install-PowershellCore.ps1
33
## Desc: Install PowerShell Core
4+
## Supply chain security: checksum validation
45
################################################################################
56

67
$ErrorActionPreference = "Stop"
78

9+
#region functions
10+
Function Get-PowerShellCoreHash
11+
{
12+
Param (
13+
[Parameter(Mandatory = $True)]
14+
[string] $Release
15+
)
16+
17+
$hashURL = "https://github.com/PowerShell/PowerShell/releases/download/v${Release}/hashes.sha256"
18+
(Invoke-RestMethod -Uri $hashURL).ToString().Split("`n").Where({ $_ -ilike "*PowerShell-${Release}-win-x64.msi*" }).Split(' ')[0]
19+
20+
}
21+
#endregion
22+
823
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())
924
$null = New-Item -ItemType Directory -Path $tempDir -Force -ErrorAction SilentlyContinue
1025
try {
@@ -21,6 +36,19 @@ try {
2136
$packagePath = Join-Path -Path $tempDir -ChildPath $packageName
2237
Invoke-WebRequest -Uri $downloadURL -OutFile $packagePath
2338

39+
#region Supply chain security
40+
Write-Verbose "Performing checksum verification"
41+
42+
$distributor_file_hash = Get-PowerShellCoreHash -Release $release
43+
$local_file_hash = (Get-FileHash -Path $packagePath -Algorithm SHA256).Hash
44+
45+
if ($local_file_hash -ne $distributor_file_hash) {
46+
Write-Host "hash must be equal to: ${distributor_file_hash}"
47+
Write-Host "actual hash is: ${local_file_hash}"
48+
throw 'Checksum verification failed, please rerun install'
49+
}
50+
#endregion
51+
2452
Write-Verbose "Performing quiet install"
2553
$ArgumentList=@("/i", $packagePath, "/quiet")
2654
$process = Start-Process msiexec -ArgumentList $ArgumentList -Wait -PassThru

0 commit comments

Comments
 (0)