Files
Dj GilcreaseandClaude Fable 5.1 c953fe3995 feat: add msi support (#1097)
* chore(deps): add go-msi and go-pkcs12

go.digitalxero.dev/go-msi is the pure-Go Windows Installer database
writer, validator, reader and Authenticode signer behind the new msi
packager; go-pkcs12 becomes a direct dependency for the signing tests.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Hnro326ZTAu2EqUTGGkCB

* feat: add msi support

Adds an `msi` packager that writes a real Windows Installer database with
the pure-Go go-msi library: contents are mapped onto the standard Windows
folders (everything else under the product's Program Files folder), root
metadata (description, homepage, license, vendor/maintainer, scripts) is
reused where MSI has a place for it, and advertised shortcuts, Windows
services, registry values, per-user installs and Authenticode signing are
exposed under `msi:`.

Every release replaces older installs of the same upgrade code in place:
the upgrade code and component GUIDs are derived from identities that
survive releases, while the product code follows the full version. A
version Windows Installer cannot represent is an error rather than a
silently clamped (and colliding) one.

Maintainer scripts run as elevated deferred custom actions embedded in the
package so they work before any file is installed and after every file is
removed; they are extracted to a randomly named, freshly created file
since the SYSTEM temp directory is world-writable.

Unit tests build real packages, run go-msi's ICE validator and assert on
the decoded tables; golden GUIDs pin the derivation contract. The
acceptance suite installs, exercises, upgrades and removes the package
with msiexec on a Windows runner and proves a failing script rolls back.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Hnro326ZTAu2EqUTGGkCB

* ci: validate nfpm-signed MSI on real Windows

Build and Authenticode-sign the MSI on Linux with nfpm's pure-Go signer
and a throwaway openssl certificate, then validate the signature with the
real Windows trust stack (Get-AuthenticodeSignature, signtool verify) and
install, exercise and remove it with msiexec on a Windows runner. The
unsigned msi-windows-install job remains as the control.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Hnro326ZTAu2EqUTGGkCB

* chore(deps): update go-msi to latest main

* Revert "chore(deps): update go-msi to latest main"

This reverts commit 05aaa91684.

* ci: integrate MSI validation with merge queue checks

* fix(msi): decode quoted maintainer names

* fix: preserve address-only maintainer fallback

* fix(msi): update go-msi and support explicit file versions

Pin the latest upstream fixes for string references, column limits, and PE version detection. Expose per-destination file version overrides so intentionally unversioned Go binaries can still be packaged, and update fixtures, docs, and schema.

* chore(deps): pin go-msi v0.5.1 release

* fix(msi): Correct downgrade removal and shortcut resources

Match Windows destinations without case sensitivity, reuse PE icon streams by source and target extension, and cover newer products in downgrade removal ranges.

Add table regressions, native icon extraction and ICE50 checks, and Windows downgrade acceptance with distinct payloads. Run MSI installation checks on pull requests.

See #1097

* test(msi): Correct native icon extraction assertions

Distinguish one RT_GROUP_ICON resource from the two extracted icon handles requested from the Windows shell. Preserve checks for both handles and cover malformed resource directories and extensionless icon targets.

See #1097

* fix(msi): Adopt go-msi v0.5.2 and reject install-path aliases

Use the released dependency fixes for downgrade sequencing, non-elevating per-user packages, and lossless non-ASCII summary metadata. Preserve the explicit removal rule for allowed downgrades.

Reject duplicate resolved installation targets while preserving GUID identities. Add downstream table regressions and native silent-downgrade and standard-user Unicode/script verification.

See #1097

* fix(msi): Adopt go-msi v0.5.3 and close authoring gaps

* test(msi): Normalize redirected native install paths

* fix(msi): Preserve upgrade targets and release artifacts

* fix(msi): Adopt go-msi v0.5.5 signing and AppSearch fixes

* fix(msi): expand remaining strings and prove hook rollback

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-11 02:25:17 +00:00

254 lines
11 KiB
PowerShell

param(
# The package built from msi.install.yaml (or msi.signed.yaml): installed,
# exercised, and removed.
[string]$Msi = './dist/foo.msi',
# The same product one version up (msi.upgrade.yaml). When given, it is
# installed over $Msi and must replace it in place: exactly one entry in
# Apps & Features, the new version, the old files gone with the product.
[string]$Upgrade,
# Install 2.0.0, then replace it with 1.0.0 allowing downgrades. The
# payloads print different messages so product registration alone is
# insufficient to pass this check.
[string]$Newer,
[string]$Downgrade,
# A package whose postinstall script fails (msi.rollback.yaml). When given,
# installing it must fail and leave nothing behind.
[string]$Rollback,
# Pass this same target during installation and maintenance/uninstall.
[string]$InstallDirectory
)
$ErrorActionPreference = 'Stop'
$msi = Resolve-Path $Msi
$defaultPolicy = $msi
$installDir = 'C:\Program Files\NfpmMsiTest'
if ($InstallDirectory) {
$InstallDirectory = [IO.Path]::GetFullPath($InstallDirectory.Replace('/', '\'))
$installDir = $InstallDirectory
}
$exe = Join-Path $installDir 'testapp.exe'
$svcExe = Join-Path $installDir 'testsvc.exe'
$conf = Join-Path $installDir 'etc\testapp\testapp.conf'
$shortcut = Join-Path $env:ProgramData 'Microsoft\Windows\Start Menu\Programs\Nfpm MSI Test.lnk'
$desktopShortcut = Join-Path $env:PUBLIC 'Desktop\Nfpm MSI Test.lnk'
$regKey = 'HKLM:\Software\NfpmMsiTest'
$serviceName = 'NfpmMsiTestSvc'
# Marker files written by the nfpm maintainer scripts (run as SYSTEM, whose
# TEMP resolves under the Windows directory).
$markers = @{
preinstall = Join-Path $env:SystemRoot 'Temp\nfpm-acc-preinstall.txt'
postinstall = Join-Path $env:SystemRoot 'Temp\nfpm-acc-postinstall.txt'
preremove = Join-Path $env:SystemRoot 'Temp\nfpm-acc-preremove.txt'
postremove = Join-Path $env:SystemRoot 'Temp\nfpm-acc-postremove.txt'
postinstallFail = Join-Path $env:SystemRoot 'Temp\nfpm-acc-postinstall-fail.txt'
}
function Reset-Markers {
$markers.Values | ForEach-Object { Remove-Item -Force $_ -ErrorAction SilentlyContinue }
}
function Assert-Markers([string[]]$hooks) {
foreach ($hook in $hooks) {
if (-not (Test-Path $markers[$hook])) {
throw "$hook script did not run (marker $($markers[$hook]) missing)"
}
if ($hook -eq 'postinstall') {
$text = Get-Content $markers[$hook] -Raw -Encoding UTF8
$expected = 'postinstall-ran ' + [char]0x00e9 + ' ' + [char]0x65e5 + ' ' + [char]0x0416
if ($text.Trim() -ne $expected) { throw 'PowerShell 5.1 changed the UTF-8 hook literals' }
}
}
}
function Assert-NoMarkers([string[]]$hooks) {
foreach ($hook in $hooks) {
if (Test-Path $markers[$hook]) {
throw "$hook script ran but must not have (marker $($markers[$hook]) present)"
}
}
}
function Invoke-Msiexec([string[]]$arguments, [string]$log) {
if ($InstallDirectory) {
$arguments += "INSTALLFOLDER=`"$InstallDirectory`""
}
$log = Get-LogPath $log
$proc = Start-Process msiexec.exe -ArgumentList ($arguments + @('/qn', '/norestart', '/l*v', "`"$log`"")) -Wait -PassThru
return $proc.ExitCode
}
function Get-LogPath([string]$log) {
if ($InstallDirectory) { return Join-Path (Split-Path $log) ('redirect-' + (Split-Path $log -Leaf)) }
return $log
}
function Show-Log([string]$log) {
$log = Get-LogPath $log
if (Test-Path $log) { Get-Content $log | Write-Host }
}
# Installed products with the given display name, from the Apps & Features
# registry (what msiexec /x and Windows Installer's Upgrade table consult).
function Get-InstalledProducts([string]$displayName) {
return @(Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*' -ErrorAction SilentlyContinue |
Where-Object { $_.DisplayName -eq $displayName })
}
function Assert-Installed([string]$version, [string]$expectedOutput = 'nfpm-msix-test-ok') {
foreach ($path in @($exe, $svcExe, $conf, $shortcut, $desktopShortcut)) {
if (-not (Test-Path $path)) { throw "expected $path after install" }
}
Write-Host "Files, shortcut and service executable are in place"
if ($InstallDirectory -and (Test-Path 'C:\Program Files\NfpmMsiTest\testapp.exe')) {
throw 'Redirected installation left the executable in the default folder'
}
$output = & $exe 2>&1
if ($output -ne $expectedOutput) {
throw "expected the installed app to print '$expectedOutput' but got '$output'"
}
Write-Host "Installed application ran correctly"
$svc = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if (-not $svc) { throw "service $serviceName is not registered" }
if ($svc.Status -ne 'Stopped') { throw "service $serviceName should be registered but not started, status is $($svc.Status)" }
Write-Host "Service $serviceName is registered"
$installPath = (Get-ItemProperty $regKey -ErrorAction SilentlyContinue).InstallPath
if ($installPath -ne "$installDir\") {
throw "expected registry value InstallPath '$installDir\' but got '$installPath'"
}
Write-Host "Registry value is in place"
$products = Get-InstalledProducts 'NfpmMsiTest'
if ($products.Count -ne 1) { throw "expected exactly one NfpmMsiTest product, found $($products.Count)" }
if ($products[0].DisplayVersion -ne $version) {
throw "expected product version $version but found $($products[0].DisplayVersion)"
}
Write-Host "Exactly one NfpmMsiTest product is registered, version $version"
}
function Assert-Removed {
if (Test-Path $installDir) { throw "install directory $installDir still exists after uninstall" }
if (Test-Path $shortcut) { throw "shortcut $shortcut still exists after uninstall" }
if (Test-Path $desktopShortcut) { throw "shortcut $desktopShortcut still exists after uninstall" }
if (Test-Path $regKey) { throw "registry key $regKey still exists after uninstall" }
if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue) { throw "service $serviceName still exists after uninstall" }
if ((Get-InstalledProducts 'NfpmMsiTest').Count -ne 0) { throw "NfpmMsiTest is still registered after uninstall" }
Write-Host "Files, shortcut, registry key, service and product registration are gone"
}
# --- install -----------------------------------------------------------------
Reset-Markers
Write-Host "Installing MSI: $msi ($((Get-Item $msi).Length) bytes)"
$code = Invoke-Msiexec @('/i', "`"$msi`"") './dist/install.log'
if ($code -ne 0) {
Write-Host "msiexec install failed with exit code $code"
Show-Log './dist/install.log'
exit 1
}
Write-Host "Package installed successfully"
Assert-Markers @('preinstall', 'postinstall')
Assert-NoMarkers @('preremove', 'postremove')
Write-Host "Install scripts ran correctly"
Assert-Installed '1.0.0'
# --- upgrade in place --------------------------------------------------------
if ($Upgrade) {
$upgrade = Resolve-Path $Upgrade
Reset-Markers
Write-Host "Upgrading with MSI: $upgrade"
$code = Invoke-Msiexec @('/i', "`"$upgrade`"") './dist/upgrade.log'
if ($code -ne 0) {
Write-Host "msiexec upgrade failed with exit code $code"
Show-Log './dist/upgrade.log'
exit 1
}
Write-Host "Package upgraded successfully"
# The new product's install hooks run; the old product is replaced, not
# uninstalled, so its remove hooks stay quiet.
Assert-Markers @('preinstall', 'postinstall')
Assert-NoMarkers @('preremove', 'postremove')
Write-Host "Upgrade scripts ran correctly"
Assert-Installed '1.0.1'
$msi = $upgrade
}
# --- downgrade in place, including replacement of the newer payload ----------
if ($Newer -or $Downgrade) {
if (-not ($Newer -and $Downgrade)) { throw '-Newer and -Downgrade must be supplied together' }
$newer = Resolve-Path $Newer
$downgrade = Resolve-Path $Downgrade
foreach ($replacement in @(
@{ Msi = $newer; Version = '2.0.0'; Output = 'nfpm-msi-v2-test-ok'; Log = './dist/newer.log' },
@{ Msi = $downgrade; Version = '1.0.0'; Output = 'nfpm-msix-test-ok'; Log = './dist/downgrade.log' }
)) {
Reset-Markers
$package = $replacement.Msi
$code = Invoke-Msiexec @('/i', "`"$package`"") $replacement.Log
if ($code -ne 0) {
Show-Log $replacement.Log
throw "installing version $($replacement.Version) failed with exit code $code"
}
Assert-Markers @('preinstall', 'postinstall')
Assert-NoMarkers @('preremove', 'postremove')
Assert-Installed $replacement.Version $replacement.Output
if ($replacement.Version -eq '2.0.0') {
$before = (Get-FileHash $exe).Hash
Reset-Markers
$code = Invoke-Msiexec @('/i', "`"$defaultPolicy`"") './dist/blocked-downgrade.log'
if ($code -ne 1603) {
Show-Log './dist/blocked-downgrade.log'
throw "expected default-policy silent downgrade to fail with 1603, got $code"
}
Assert-Installed '2.0.0' 'nfpm-msi-v2-test-ok'
if ((Get-FileHash $exe).Hash -ne $before) { throw 'Rejected downgrade changed the newer executable' }
Assert-NoMarkers @('preinstall', 'postinstall', 'preremove', 'postremove')
Write-Host 'SILENT_DOWNGRADE_REJECTED_ONE_PRODUCT_UNCHANGED_NEWER_OK'
}
}
$msi = $downgrade
}
# --- uninstall ---------------------------------------------------------------
Reset-Markers
$code = Invoke-Msiexec @('/x', "`"$msi`"") './dist/uninstall.log'
if ($code -ne 0) {
Write-Host "msiexec uninstall failed with exit code $code"
Show-Log './dist/uninstall.log'
exit 1
}
Write-Host "Package uninstalled successfully"
Assert-Markers @('preremove', 'postremove')
Write-Host "Remove scripts ran correctly"
Assert-Removed
# --- failed postinstall rolls back -------------------------------------------
if ($Rollback) {
$rollback = Resolve-Path $Rollback
Reset-Markers
Assert-NoMarkers @('postinstallFail')
Write-Host "Installing MSI with a failing postinstall: $rollback"
$code = Invoke-Msiexec @('/i', "`"$rollback`"") './dist/rollback.log'
if ($code -ne 1603) {
Write-Host "expected the failing postinstall to return MSI error 1603, got $code"
Show-Log './dist/rollback.log'
exit 1
}
Write-Host "msiexec failed as expected with exit code $code"
Assert-Markers @('preinstall', 'postinstallFail')
$failureMarker = Get-Content -LiteralPath $markers.postinstallFail -Raw -Encoding UTF8
if ($failureMarker.Trim() -ne 'postinstall-fail-ran') { throw 'Unexpected failing postinstall marker contents' }
if (Test-Path 'C:\Program Files\NfpmMsiRollback') { throw "install directory of the rolled-back product still exists" }
if ((Get-InstalledProducts 'NfpmMsiRollback').Count -ne 0) { throw "the rolled-back product is registered" }
Write-Host "Failed install rolled back cleanly"
Write-Host 'POSTINSTALL_FAILURE_EXECUTED_ROLLBACK_OK'
Reset-Markers
}