131 lines
6.9 KiB
PowerShell
131 lines
6.9 KiB
PowerShell
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$PythonPath
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$installerPath = Join-Path $PSScriptRoot 'install_windows_release_client.ps1'
|
|
$fixtureScript = Join-Path $PSScriptRoot 'test_windows_release_launcher.py'
|
|
$temporaryBase = [IO.Path]::GetFullPath([IO.Path]::GetTempPath())
|
|
$temporaryBase = [IO.Path]::GetFullPath((Resolve-Path -LiteralPath $temporaryBase).Path).TrimEnd('\')
|
|
$temporaryName = 'akyldash-bundle-mode-' + [guid]::NewGuid().ToString('N')
|
|
$temporaryRoot = [IO.Path]::Combine($temporaryBase, $temporaryName)
|
|
if (Test-Path -LiteralPath $temporaryRoot) { throw 'Temporary fixture directory already exists.' }
|
|
[void][IO.Directory]::CreateDirectory($temporaryRoot)
|
|
$temporaryRootItem = Get-Item -LiteralPath $temporaryRoot -Force
|
|
if (($temporaryRootItem.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) {
|
|
throw 'Temporary fixture root must not be a reparse point.'
|
|
}
|
|
$temporaryRootCreated = $temporaryRootItem.CreationTimeUtc
|
|
$temporaryAclOwner = (Get-Acl -LiteralPath $temporaryRoot).Owner
|
|
$temporaryOwnerSid = ([Security.Principal.NTAccount]$temporaryAclOwner).Translate(
|
|
[Security.Principal.SecurityIdentifier]).Value
|
|
$currentSid = [Security.Principal.WindowsIdentity]::GetCurrent().User.Value
|
|
if ($temporaryOwnerSid -cne $currentSid) { throw 'Temporary fixture root is not owned by the current user.' }
|
|
$bundlePath = Join-Path $temporaryRoot 'fixture.zip'
|
|
|
|
try {
|
|
$fixtureOutput = & $PythonPath -B -E -s -S $fixtureScript --write-bundle-fixture $bundlePath
|
|
if ($LASTEXITCODE -ne 0) { throw 'The temporary packager fixture failed.' }
|
|
$fixture = ($fixtureOutput -join "`n") | ConvertFrom-Json
|
|
|
|
$tokens = $null
|
|
$parseErrors = $null
|
|
$installerAst = [System.Management.Automation.Language.Parser]::ParseFile(
|
|
$installerPath, [ref]$tokens, [ref]$parseErrors)
|
|
if ($parseErrors.Count -gt 0) { throw 'Installer PowerShell syntax is invalid.' }
|
|
$validatorAst = $installerAst.Find({
|
|
param($node)
|
|
$node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
|
|
$node.Name -ceq 'Assert-ReceiverBundleEntryMode'
|
|
}, $true)
|
|
if ($null -eq $validatorAst) { throw 'Installer bundle-mode validator was not found.' }
|
|
. ([scriptblock]::Create($validatorAst.Extent.Text))
|
|
|
|
Add-Type -AssemblyName System.IO.Compression
|
|
$stream = [IO.File]::OpenRead($bundlePath)
|
|
try {
|
|
$archive = [System.IO.Compression.ZipArchive]::new(
|
|
$stream, [System.IO.Compression.ZipArchiveMode]::Read, $true)
|
|
try {
|
|
foreach ($entry in $archive.Entries) {
|
|
Assert-ReceiverBundleEntryMode -Entry $entry
|
|
}
|
|
} finally { $archive.Dispose() }
|
|
} finally { $stream.Dispose() }
|
|
|
|
$alteredPath = Join-Path $temporaryRoot 'altered-mode.zip'
|
|
$sourceStream = [IO.File]::OpenRead($bundlePath)
|
|
$targetStream = [IO.File]::Create($alteredPath)
|
|
try {
|
|
$sourceArchive = [System.IO.Compression.ZipArchive]::new(
|
|
$sourceStream, [System.IO.Compression.ZipArchiveMode]::Read, $true)
|
|
$targetArchive = [System.IO.Compression.ZipArchive]::new(
|
|
$targetStream, [System.IO.Compression.ZipArchiveMode]::Create, $true)
|
|
try {
|
|
$changed = $false
|
|
foreach ($entry in $sourceArchive.Entries) {
|
|
$copy = $targetArchive.CreateEntry($entry.FullName, [System.IO.Compression.CompressionLevel]::NoCompression)
|
|
$copy.ExternalAttributes = $entry.ExternalAttributes
|
|
if (-not $changed) {
|
|
$copy.ExternalAttributes = [BitConverter]::ToInt32(
|
|
[BitConverter]::GetBytes([uint32]0x81A00000), 0)
|
|
$changed = $true
|
|
}
|
|
$entryInput = $entry.Open()
|
|
$entryOutput = $copy.Open()
|
|
try { $entryInput.CopyTo($entryOutput) }
|
|
finally { $entryOutput.Dispose(); $entryInput.Dispose() }
|
|
}
|
|
if (-not $changed) { throw 'Packager fixture had no entries to mutate.' }
|
|
} finally { $targetArchive.Dispose(); $sourceArchive.Dispose() }
|
|
} finally { $targetStream.Dispose(); $sourceStream.Dispose() }
|
|
|
|
$alteredStream = [IO.File]::OpenRead($alteredPath)
|
|
try {
|
|
$alteredArchive = [System.IO.Compression.ZipArchive]::new(
|
|
$alteredStream, [System.IO.Compression.ZipArchiveMode]::Read, $true)
|
|
try {
|
|
$alteredEntry = $alteredArchive.Entries[0]
|
|
$alteredMode = (([long]$alteredEntry.ExternalAttributes -shr 16) -band 0xFFFF)
|
|
if ($alteredMode -ne 0x81A0) { throw 'Altered ZIP fixture does not contain the intended unsafe mode.' }
|
|
$rejected = $false
|
|
try { Assert-ReceiverBundleEntryMode -Entry $alteredEntry }
|
|
catch {
|
|
if ($_.Exception.Message -notmatch 'unsafe file mode') { throw }
|
|
$rejected = $true
|
|
}
|
|
if (-not $rejected) { throw 'Installer validator accepted an altered ZIP entry mode.' }
|
|
} finally { $alteredArchive.Dispose() }
|
|
} finally { $alteredStream.Dispose() }
|
|
|
|
Write-Output ("PASS: installer validator accepted the actual packager bundle for commit {0} (SHA-256 {1}) and rejected altered mode." -f $fixture.commit, $fixture.sha256)
|
|
} finally {
|
|
$expectedRoot = [IO.Path]::Combine($temporaryBase, $temporaryName)
|
|
$rootItem = Get-Item -LiteralPath $expectedRoot -Force
|
|
$resolvedRoot = [IO.Path]::GetFullPath((Resolve-Path -LiteralPath $expectedRoot).Path).TrimEnd('\')
|
|
if ([string]::Compare($resolvedRoot, $expectedRoot, [StringComparison]::OrdinalIgnoreCase) -ne 0 -or
|
|
[string]::Compare([IO.Path]::GetDirectoryName($resolvedRoot), $temporaryBase,
|
|
[StringComparison]::OrdinalIgnoreCase) -ne 0 -or
|
|
[string]::Compare([IO.Path]::GetFileName($resolvedRoot), $temporaryName,
|
|
[StringComparison]::OrdinalIgnoreCase) -ne 0 -or
|
|
($rootItem.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0 -or
|
|
$rootItem.CreationTimeUtc -ne $temporaryRootCreated) {
|
|
throw 'Refusing to remove a replaced, moved, or reparse temporary fixture directory.'
|
|
}
|
|
$aclOwner = (Get-Acl -LiteralPath $expectedRoot).Owner
|
|
$ownerSid = ([Security.Principal.NTAccount]$aclOwner).Translate(
|
|
[Security.Principal.SecurityIdentifier]).Value
|
|
if ($ownerSid -cne $temporaryOwnerSid -or $ownerSid -cne $currentSid) {
|
|
throw 'Refusing to remove a temporary fixture directory with a changed owner.'
|
|
}
|
|
foreach ($item in Get-ChildItem -LiteralPath $expectedRoot -Force -Recurse) {
|
|
$itemPath = [IO.Path]::GetFullPath($item.FullName)
|
|
if (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0 -or
|
|
-not $itemPath.StartsWith($resolvedRoot + '\', [StringComparison]::OrdinalIgnoreCase)) {
|
|
throw 'Refusing to recursively remove a reparse or out-of-root fixture entry.'
|
|
}
|
|
}
|
|
[IO.Directory]::Delete($resolvedRoot, $true)
|
|
}
|