Make Windows harness cleanup safe for Git objects
This commit is contained in:
1 parent
00b0af69c2
commit
54e30d4228
2 files changed
+114
-28
No files matched your search
@@ -24,11 +24,73 @@ $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(
|
||||
@@ -98,32 +160,51 @@ try {
|
||||
} 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)
|
||||
$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 {
|
||||
$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.'
|
||||
$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.'
|
||||
}
|
||||
}
|
||||
}
|
||||
[IO.Directory]::Delete($resolvedRoot, $true)
|
||||
$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
|
||||
@@ -258,11 +258,16 @@ class WindowsReleaseBootstrapTests(unittest.TestCase):
|
||||
def test_windows_bundle_mode_harness_uses_safe_python_and_validates_cleanup_target(self):
|
||||
harness = (ROOT / "infra/deploy/test_windows_release_bundle_mode.ps1").read_text(encoding="utf-8")
|
||||
self.assertIn("-B -E -s -S", harness)
|
||||
self.assertIn("$rootItem.CreationTimeUtc -ne $temporaryRootCreated", harness)
|
||||
self.assertIn("$item.CreationTimeUtc -ne $temporaryRootCreated", harness)
|
||||
self.assertIn("$ownerSid -cne $temporaryOwnerSid", harness)
|
||||
self.assertIn("[IO.FileAttributes]::ReparsePoint", harness)
|
||||
self.assertIn("[IO.Directory]::Delete($resolvedRoot, $true)", harness)
|
||||
self.assertIn("-not $itemPath.StartsWith($resolvedRoot + '\\'", harness)
|
||||
self.assertIn("[IO.Directory]::Delete($resolvedRoot, $false)", harness)
|
||||
self.assertNotIn("[IO.Directory]::Delete($resolvedRoot, $true)", harness)
|
||||
self.assertIn("$wrongRootRejected", harness)
|
||||
self.assertIn("$readonlyObject", harness)
|
||||
self.assertIn("-bnot [int][IO.FileAttributes]::ReadOnly", harness)
|
||||
self.assertIn("[IO.File]::Delete($verified.FullName)", harness)
|
||||
self.assertIn("-not $resolved.StartsWith($ResolvedRoot + '\\'", harness)
|
||||
self.assertLess(harness.index("if ($alteredMode -ne 0x81A0)"),
|
||||
harness.index("Assert-ReceiverBundleEntryMode -Entry $alteredEntry"))
|
||||
|
||||
|
||||
Reference in new issue
Block a user