| 1 | #requires -Version 5.1 |
| 2 | $ErrorActionPreference = 'Stop' |
| 3 | Set-StrictMode -Version Latest |
| 4 | $installers = @('install-cli.ps1', 'install-ccs.ps1') |
| 5 | $functions = @{} |
| 6 | foreach ($name in $installers) { |
| 7 | $errors = $null |
| 8 | $ast = [Management.Automation.Language.Parser]::ParseFile((Join-Path $PSScriptRoot ('guest-clipboard-windows/'+$name)), [ref]$null, [ref]$errors) |
| 9 | if ($errors.Count) { throw $errors[0] } |
| 10 | $protect = $ast.Find({ param($node) $node -is [Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Protect-Path' }, $true) |
| 11 | $wideWrite = $protect.Find({ |
| 12 | param($node) |
| 13 | ($node -is [Management.Automation.Language.CommandAst] -and $node.GetCommandName() -eq 'Set-Acl') -or |
| 14 | ($node -is [Management.Automation.Language.InvokeMemberExpressionAst] -and $node.Member.Value -in @('SetOwner','SetGroup','SetAuditRule','SetAuditRuleProtection')) |
| 15 | }, $true) |
| 16 | if ($wideWrite) { throw "$name must persist only DACL changes for the ordinary GUI user." } |
| 17 | $functions[$name] = $ast.FindAll({ param($node) $node -is [Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -in @('Assert-NoReparse','Protect-Path','New-PrivateDirectory','PrivateDirectory') }, $true) |
| 18 | } |
| 19 | if ([Environment]::OSVersion.Platform -ne 'Win32NT') { |
| 20 | Write-Output 'Parsed both installers and checked the DACL-only write contract. Native ACL fixtures require Windows.' |
| 21 | return |
| 22 | } |
| 23 | $user = [Security.Principal.WindowsIdentity]::GetCurrent() |
| 24 | if ([Security.Principal.WindowsPrincipal]::new($user).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Run the ACL fixtures as an ordinary user.' } |
| 25 | $privileges = & (Join-Path ([Environment]::SystemDirectory) 'whoami.exe') /priv /fo csv /nh | ConvertFrom-Csv -Header Name,Description,State |
| 26 | if ($LASTEXITCODE -ne 0 -or @($privileges | Where-Object Name -eq 'SeSecurityPrivilege').Count) { throw 'The ACL fixtures require a token without SeSecurityPrivilege.' } |
| 27 | $readSections = [Security.AccessControl.AccessControlSections]::Owner -bor [Security.AccessControl.AccessControlSections]::Group -bor [Security.AccessControl.AccessControlSections]::Access |
| 28 | function Read-TestSecurity([string]$Path, [bool]$Directory) { |
| 29 | if ($Directory) { return [Security.AccessControl.DirectorySecurity]::new($Path, $readSections) } |
| 30 | return [Security.AccessControl.FileSecurity]::new($Path, $readSections) |
| 31 | } |
| 32 | function Check-PrivateAccess($Before, $After, [bool]$Directory) { |
| 33 | $sidType = [Security.Principal.SecurityIdentifier] |
| 34 | if ($Before.GetOwner($sidType) -ne $After.GetOwner($sidType) -or $Before.GetGroup($sidType) -ne $After.GetGroup($sidType)) { throw 'Owner or group changed.' } |
| 35 | $expected = @($user.User.Value, 'S-1-5-18', 'S-1-5-32-544') |
| 36 | $rules = @($After.GetAccessRules($true, $true, $sidType)) |
| 37 | if (!$After.AreAccessRulesProtected -or $rules.Count -ne 3) { throw 'The private DACL has the wrong protection or rule count.' } |
| 38 | if ((@($rules.IdentityReference.Value | Sort-Object) -join ',') -cne (@($expected | Sort-Object) -join ',')) { throw 'The private DACL has the wrong principals.' } |
| 39 | $inheritance = if ($Directory) { [Security.AccessControl.InheritanceFlags]'ContainerInherit,ObjectInherit' } else { [Security.AccessControl.InheritanceFlags]::None } |
| 40 | foreach ($rule in $rules) { |
| 41 | if ($rule.IsInherited -or $rule.FileSystemRights -ne [Security.AccessControl.FileSystemRights]::FullControl -or |
| 42 | $rule.AccessControlType -ne 'Allow' -or $rule.InheritanceFlags -ne $inheritance -or $rule.PropagationFlags -ne 'None') { throw 'The private DACL changed its access or inheritance contract.' } |
| 43 | } |
| 44 | } |
| 45 | $root = Join-Path ([IO.Path]::GetTempPath()) ('cage-user-tools-acl-'+[guid]::NewGuid().ToString('N')) |
| 46 | $null = [IO.Directory]::CreateDirectory($root) |
| 47 | try { |
| 48 | foreach ($name in $installers) { |
| 49 | foreach ($definition in $functions[$name]) { . ([scriptblock]::Create($definition.Extent.Text)) } |
| 50 | $directory = Join-Path $root ($name+' tools') |
| 51 | $null = [IO.Directory]::CreateDirectory($directory) |
| 52 | $before = Read-TestSecurity $directory $true |
| 53 | if ($name -eq 'install-cli.ps1') { New-PrivateDirectory $directory } else { PrivateDirectory $directory } |
| 54 | Check-PrivateAccess $before (Read-TestSecurity $directory $true) $true |
| 55 | # A fresh child inherits all three directory rules before its own |
| 56 | # explicit protection. Its content represents a retained install receipt. |
| 57 | $file = Join-Path $directory 'retained receipt.json' |
| 58 | [IO.File]::WriteAllText($file, 'existing fixture contents') |
| 59 | $before = Read-TestSecurity $file $false |
| 60 | $inherited = @($before.GetAccessRules($true, $true, [Security.Principal.SecurityIdentifier])) |
| 61 | if ($inherited.Count -ne 3 -or @($inherited | Where-Object { !$_.IsInherited }).Count) { throw 'New files did not inherit the protected directory rules.' } |
| 62 | Protect-Path $file $false |
| 63 | Check-PrivateAccess $before (Read-TestSecurity $file $false) $false |
| 64 | foreach ($entry in @(@($directory,$true), @($file,$false))) { |
| 65 | $before = Read-TestSecurity $entry[0] $entry[1] |
| 66 | Protect-Path $entry[0] $entry[1] |
| 67 | $after = Read-TestSecurity $entry[0] $entry[1] |
| 68 | if ($before.GetSecurityDescriptorSddlForm($readSections) -cne $after.GetSecurityDescriptorSddlForm($readSections)) { throw 'Retry changed existing security metadata.' } |
| 69 | $actualUser = $user |
| 70 | try { |
| 71 | $user = @{ User = [Security.Principal.SecurityIdentifier]::new('S-1-5-21-111-222-333-1001') } |
| 72 | try { Protect-Path $entry[0] $entry[1]; throw 'Wrong owner accepted' } |
| 73 | catch { if ($_.Exception.Message -notlike '*must already belong to the current GUI user*') { throw } } |
| 74 | } finally { $user = $actualUser } |
| 75 | $after = Read-TestSecurity $entry[0] $entry[1] |
| 76 | if ($before.GetSecurityDescriptorSddlForm($readSections) -cne $after.GetSecurityDescriptorSddlForm($readSections)) { throw 'Owner rejection changed security metadata.' } |
| 77 | } |
| 78 | if ([IO.File]::ReadAllText($file) -cne 'existing fixture contents') { throw 'Protection changed existing file contents.' } |
| 79 | } |
| 80 | Write-Output 'Passed ordinary-user CLI/CCS directory and file DACLs, inherited children, owner/group preservation, retries and owner rejection.' |
| 81 | } finally { [IO.Directory]::Delete($root, $true) } |