Files
akyldash/infra/deploy/test_windows_release_bundle_mode.ps1
codex 54e30d4228
CI / tests-and-images (pull_request) Successful in 2m16s
Runner smoke test / runner-smoke (pull_request) Successful in 2s
Make Windows harness cleanup safe for Git objects
2026-11-03 10:35:49 +03:00

211 lines
11 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'
function Get-VerifiedFixtureRoot([string]$Candidate) {
$expectedRoot = [IO.Path]::Combine($temporaryBase, $temporaryName)
$candidatePath = [IO.Path]::GetFullPath($Candidate)
if ([string]::Compare($candidatePath, $expectedRoot, [StringComparison]::OrdinalIgnoreCase) -ne 0) {
throw 'Refusing a fixture root outside the exact temporary base/name.'
}
$item = Get-Item -LiteralPath $candidatePath -Force
if ($item -isnot [IO.DirectoryInfo] -or
($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) {
throw 'Fixture root must remain a real directory, not a reparse point.'
}
$resolved = [IO.Path]::GetFullPath((Resolve-Path -LiteralPath $candidatePath).Path).TrimEnd('\')
if ([string]::Compare($resolved, $expectedRoot, [StringComparison]::OrdinalIgnoreCase) -ne 0 -or
[string]::Compare([IO.Path]::GetDirectoryName($resolved), $temporaryBase,
[StringComparison]::OrdinalIgnoreCase) -ne 0 -or
[string]::Compare([IO.Path]::GetFileName($resolved), $temporaryName,
[StringComparison]::OrdinalIgnoreCase) -ne 0 -or
$item.CreationTimeUtc -ne $temporaryRootCreated) {
throw 'Fixture root was replaced, moved, or resolved outside its original identity.'
}
$ownerName = (Get-Acl -LiteralPath $candidatePath).Owner
$ownerSid = ([Security.Principal.NTAccount]$ownerName).Translate(
[Security.Principal.SecurityIdentifier]).Value
if ($ownerSid -cne $temporaryOwnerSid -or $ownerSid -cne $currentSid) {
throw 'Fixture root owner changed.'
}
return $resolved
}
function Get-VerifiedFixtureEntry([string]$Path, [string]$ResolvedRoot) {
$item = Get-Item -LiteralPath $Path -Force
if (($item -isnot [IO.FileInfo] -and $item -isnot [IO.DirectoryInfo]) -or
($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) {
throw 'Fixture entry must be a regular file or directory without reparse points.'
}
$resolved = [IO.Path]::GetFullPath((Resolve-Path -LiteralPath $item.FullName).Path)
if (-not $resolved.StartsWith($ResolvedRoot + '\', [StringComparison]::OrdinalIgnoreCase)) {
throw 'Fixture entry resolved outside its verified temporary root.'
}
$ownerName = (Get-Acl -LiteralPath $resolved).Owner
$ownerSid = ([Security.Principal.NTAccount]$ownerName).Translate(
[Security.Principal.SecurityIdentifier]).Value
if ($ownerSid -cne $temporaryOwnerSid -or $ownerSid -cne $currentSid) {
throw 'Fixture entry is not owned by the current user.'
}
return Get-Item -LiteralPath $resolved -Force
}
$wrongRootRejected = $false
try { [void](Get-VerifiedFixtureRoot -Candidate $temporaryBase) }
catch { $wrongRootRejected = $true }
if (-not $wrongRootRejected) { throw 'Fixture cleanup accepted an incorrect root path.' }
$passMessage = $null
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
$gitObjectDirectory = Join-Path $temporaryRoot 'fixture-repository\.git\objects'
$readonlyObject = Get-ChildItem -LiteralPath $gitObjectDirectory -File -Force -Recurse | Select-Object -First 1
if ($null -eq $readonlyObject) { throw 'Packager fixture did not create a loose Git object.' }
$verifiedRoot = Get-VerifiedFixtureRoot -Candidate $temporaryRoot
$verifiedObject = Get-VerifiedFixtureEntry -Path $readonlyObject.FullName -ResolvedRoot $verifiedRoot
[IO.File]::SetAttributes($verifiedObject.FullName,
[IO.FileAttributes]([int]$verifiedObject.Attributes -bor [int][IO.FileAttributes]::ReadOnly))
$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 = [int]-2120220672
$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() }
$passMessage = ("PASS: installer validator accepted the actual packager bundle for commit {0} (SHA-256 {1}) and rejected altered mode; read-only Git object cleanup succeeded." -f $fixture.commit, $fixture.sha256)
} finally {
$resolvedRoot = Get-VerifiedFixtureRoot -Candidate $temporaryRoot
$entries = @(Get-ChildItem -LiteralPath $resolvedRoot -Force -Recurse)
$verifiedEntries = @(
foreach ($entry in $entries) {
Get-VerifiedFixtureEntry -Path $entry.FullName -ResolvedRoot $resolvedRoot
}
)
foreach ($entry in ($verifiedEntries | Sort-Object { $_.FullName.Length } -Descending)) {
$resolvedRoot = Get-VerifiedFixtureRoot -Candidate $temporaryRoot
$verified = Get-VerifiedFixtureEntry -Path $entry.FullName -ResolvedRoot $resolvedRoot
if (($verified.Attributes -band [IO.FileAttributes]::ReadOnly) -ne 0) {
$writableAttributes = [IO.FileAttributes]([int]$verified.Attributes -band
(-bnot [int][IO.FileAttributes]::ReadOnly))
[IO.File]::SetAttributes($verified.FullName, $writableAttributes)
$verified = Get-VerifiedFixtureEntry -Path $verified.FullName -ResolvedRoot $resolvedRoot
if (($verified.Attributes -band [IO.FileAttributes]::ReadOnly) -ne 0) {
throw 'Could not clear the read-only attribute from the verified fixture entry.'
}
}
}
$remainingRoot = Get-VerifiedFixtureRoot -Candidate $temporaryRoot
$remainingEntries = @(Get-ChildItem -LiteralPath $remainingRoot -Force -Recurse)
$initialPaths = @($entries | ForEach-Object { [IO.Path]::GetFullPath($_.FullName) } | Sort-Object)
$remainingPaths = @($remainingEntries | ForEach-Object { [IO.Path]::GetFullPath($_.FullName) } | Sort-Object)
if (($initialPaths -join "`n") -cne ($remainingPaths -join "`n")) {
throw 'Temporary fixture contents changed during cleanup; refusing deletion.'
}
foreach ($entry in ($remainingEntries | Sort-Object { $_.FullName.Length } -Descending)) {
$resolvedRoot = Get-VerifiedFixtureRoot -Candidate $temporaryRoot
$verified = Get-VerifiedFixtureEntry -Path $entry.FullName -ResolvedRoot $resolvedRoot
if (($verified.Attributes -band [IO.FileAttributes]::ReadOnly) -ne 0) {
throw 'Refusing fixture deletion while a verified entry remains read-only.'
}
if ($verified -is [IO.DirectoryInfo]) {
[IO.Directory]::Delete($verified.FullName, $false)
} elseif ($verified -is [IO.FileInfo]) {
[IO.File]::Delete($verified.FullName)
} else {
throw 'Refusing to delete an unknown temporary fixture entry type.'
}
}
$resolvedRoot = Get-VerifiedFixtureRoot -Candidate $temporaryRoot
[IO.Directory]::Delete($resolvedRoot, $false)
}
Write-Output $passMessage