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 $temporaryOwnerSid = (Get-Acl -LiteralPath $temporaryRoot).GetOwner( [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.' } $ownerSid = (Get-Acl -LiteralPath $candidatePath).GetOwner( [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.' } $ownerSid = (Get-Acl -LiteralPath $resolved).GetOwner( [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)) $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $mutationRights = [System.Security.AccessControl.FileSystemRights]( [int][System.Security.AccessControl.FileSystemRights]::WriteData -bor [int][System.Security.AccessControl.FileSystemRights]::AppendData -bor [int][System.Security.AccessControl.FileSystemRights]::WriteExtendedAttributes -bor [int][System.Security.AccessControl.FileSystemRights]::WriteAttributes -bor [int][System.Security.AccessControl.FileSystemRights]::DeleteSubdirectoriesAndFiles -bor [int][System.Security.AccessControl.FileSystemRights]::Delete -bor [int][System.Security.AccessControl.FileSystemRights]::ChangePermissions -bor [int][System.Security.AccessControl.FileSystemRights]::TakeOwnership ) $pathReplacementRights = [System.Security.AccessControl.FileSystemRights]( [int][System.Security.AccessControl.FileSystemRights]::DeleteSubdirectoriesAndFiles -bor [int][System.Security.AccessControl.FileSystemRights]::Delete -bor [int][System.Security.AccessControl.FileSystemRights]::ChangePermissions -bor [int][System.Security.AccessControl.FileSystemRights]::TakeOwnership ) foreach ($guardName in @('Assert-ProtectedPath', 'Assert-TrustedPath')) { $guardAst = $installerAst.Find({ param($node) $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -ceq $guardName }, $true) if ($null -eq $guardAst) { throw "Installer ACL guard was not found: $guardName" } . ([scriptblock]::Create($guardAst.Extent.Text)) } $unknownSid = [Security.Principal.SecurityIdentifier]::new( 'S-1-5-21-3141592653-2718281828-1618033988-987654') $unmapped = $false try { $null = $unknownSid.Translate([Security.Principal.NTAccount]) } catch [Security.Principal.IdentityNotMappedException] { $unmapped = $true } if (-not $unmapped) { throw 'The synthetic ACL SID unexpectedly resolves to an NTAccount.' } $savedMockAcl = $script:akyldashAclFixture function Get-Acl([string]$LiteralPath) { return $script:akyldashAclFixture } try { $sidSystem = [Security.Principal.SecurityIdentifier]::new('S-1-5-18') $sidAdmins = [Security.Principal.SecurityIdentifier]::new('S-1-5-32-544') $privateFixtureAcl = [System.Security.AccessControl.DirectorySecurity]::new() $privateFixtureAcl.SetAccessRuleProtection($true, $false) $privateFixtureAcl.SetOwner($identity.User) foreach ($sid in @($sidSystem, $sidAdmins, $identity.User)) { $null = $privateFixtureAcl.AddAccessRule([System.Security.AccessControl.FileSystemAccessRule]::new( $sid, [System.Security.AccessControl.FileSystemRights]::FullControl, [System.Security.AccessControl.AccessControlType]::Allow)) } $null = $privateFixtureAcl.AddAccessRule([System.Security.AccessControl.FileSystemAccessRule]::new( $unknownSid, [System.Security.AccessControl.FileSystemRights]::FullControl, [System.Security.AccessControl.AccessControlType]::Allow)) $script:akyldashAclFixture = $privateFixtureAcl $privateRejected = $false try { Assert-ProtectedPath -Path $temporaryRoot -AllowOwnerWrite $true } catch { if ($_.Exception.Message -notlike 'Private state path ACL must grant only full control*') { throw } $privateRejected = $true } if (-not $privateRejected) { throw 'Unmapped SID was not rejected by the protected-path guard.' } $trustedFixtureAcl = [System.Security.AccessControl.DirectorySecurity]::new() $trustedFixtureAcl.SetAccessRuleProtection($true, $false) $trustedFixtureAcl.SetOwner($sidSystem) $null = $trustedFixtureAcl.AddAccessRule([System.Security.AccessControl.FileSystemAccessRule]::new( $sidSystem, [System.Security.AccessControl.FileSystemRights]::FullControl, [System.Security.AccessControl.AccessControlType]::Allow)) $null = $trustedFixtureAcl.AddAccessRule([System.Security.AccessControl.FileSystemAccessRule]::new( $unknownSid, [System.Security.AccessControl.FileSystemRights]::WriteData, [System.Security.AccessControl.AccessControlType]::Allow)) $script:akyldashAclFixture = $trustedFixtureAcl $trustedRejected = $false try { Assert-TrustedPath -Path $temporaryRoot } catch { if ($_.Exception.Message -notlike 'Protected path grants replacement/write access*') { throw } $trustedRejected = $true } if (-not $trustedRejected) { throw 'Unmapped SID was not rejected by the trusted-path guard.' } } finally { Remove-Item Function:\Get-Acl -ErrorAction SilentlyContinue if ($null -eq $savedMockAcl) { Remove-Variable -Name akyldashAclFixture -Scope Script -ErrorAction SilentlyContinue } else { $script:akyldashAclFixture = $savedMockAcl } } 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