diff --git a/TASKS.md b/TASKS.md index eae7f47..e6a3a77 100644 --- a/TASKS.md +++ b/TASKS.md @@ -124,3 +124,4 @@ open until it is run on disposable machines with the required account setups. - [x] Preserve signed WinGet HRESULT bits without UInt32 overflow and test diagnostic capture. - [x] Rename the bundled default machine to beffroi-laptop and document switching existing setups. +- [x] Use PowerShell 7-compatible ACL application for elevation requests; add Windows-only permission regression tests (Windows execution pending). diff --git a/src/Elevation.psm1 b/src/Elevation.psm1 index e5c9760..ca2ddc6 100644 --- a/src/Elevation.psm1 +++ b/src/Elevation.psm1 @@ -14,7 +14,7 @@ function Protect-ElevationFile { $acl.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new($currentSid, 'FullControl', 'Allow')) $acl.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new('BUILTIN\Administrators', 'FullControl', 'Allow')) $acl.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new('NT AUTHORITY\SYSTEM', 'FullControl', 'Allow')) - [System.IO.File]::SetAccessControl($Path, $acl) + Set-Acl -LiteralPath $Path -AclObject $acl -ErrorAction Stop } function Invoke-ElevatedPackageBatch { diff --git a/tests/Elevation.Tests.ps1 b/tests/Elevation.Tests.ps1 index 9047246..50dccab 100644 --- a/tests/Elevation.Tests.ps1 +++ b/tests/Elevation.Tests.ps1 @@ -24,3 +24,39 @@ Describe 'Elevation host selection' { } } } + +Describe 'Elevation request file protection' -Skip:(-not $IsWindows) { + It 'restricts a request file to the user, administrators, and SYSTEM' { + InModuleScope Elevation -Parameters @{ Root = $TestDrive } { + param($Root) + $path = Join-Path $Root 'request [test].json' + '{}' | Set-Content -LiteralPath $path + Protect-ElevationFile -Path $path + $acl = Get-Acl -LiteralPath $path + $acl.AreAccessRulesProtected | Should -BeTrue + $rules = @($acl.GetAccessRules($true, $true, [Security.Principal.SecurityIdentifier])) + $expected = @( + [Security.Principal.WindowsIdentity]::GetCurrent().User.Value + 'S-1-5-32-544' + 'S-1-5-18' + ) | Sort-Object -Unique + @($rules.IdentityReference.Value | Sort-Object -Unique) | Should -Be $expected + foreach ($rule in $rules) { + $rule.IsInherited | Should -BeFalse + $rule.AccessControlType | Should -Be 'Allow' + $rule.FileSystemRights | Should -Be 'FullControl' + } + Get-Content -LiteralPath $path -Raw | Should -Match '^{}' + } + } + + It 'propagates ACL write failures' { + InModuleScope Elevation -Parameters @{ Root = $TestDrive } { + param($Root) + $path = Join-Path $Root 'failed-request.json' + '{}' | Set-Content -LiteralPath $path + Mock Set-Acl { throw 'ACL write denied' } + { Protect-ElevationFile -Path $path } | Should -Throw '*ACL write denied*' + } + } +}