提交记录修改internal/guestos/windows_host_enrollment_native_windows_test.go+19 −4看文件
修改scripts/ci-windows-build.ps1+1 −1看文件
修改scripts/guest-os/guest-clipboard-windows/install-ccs.ps1+17 −3看文件
修改scripts/guest-os/guest-clipboard-windows/install-cli.ps1+17 −3看文件
新增scripts/guest-os/windows-user-tools-acl.tests.ps1+81 −0看文件
| @@ -34,9 +34,24 @@ func TestWindowsHostEnrollmentOrdinaryUser(t *testing.T) { | ||
| 34 | 34 | t.Fatalf("build enrollment helper: %v\n%s", err, output) |
| 35 | 35 | } |
| 36 | 36 | } |
| 37 | 新增: runWindowsOrdinaryUserPowerShell(t, filepath.Join(root, "scripts", "guest-os", "windows-host-enrollment-smoke.ps1"), "-ManagerExecutable", manager) | |
| 38 | 新增:} | |
| 39 | 新增: | |
| 40 | 新增:func TestWindowsUserToolACLOrdinaryUser(t *testing.T) { | |
| 41 | 新增: script, err := filepath.Abs(filepath.Join("..", "..", "scripts", "guest-os", "windows-user-tools-acl.tests.ps1")) | |
| 42 | 新增: if err != nil { | |
| 43 | 新增: t.Fatal(err) | |
| 44 | 新增: } | |
| 45 | 新增: runWindowsOrdinaryUserPowerShell(t, script) | |
| 46 | 新增:} | |
| 47 | 新增: | |
| 48 | 新增:func runWindowsOrdinaryUserPowerShell(t *testing.T, script string, arguments ...string) { | |
| 49 | 新增: t.Helper() | |
| 50 | 新增: ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) | |
| 51 | 新增: defer cancel() | |
| 37 | 52 | shell := filepath.Join(os.Getenv("SystemRoot"), "System32", "WindowsPowerShell", "v1.0", "powershell.exe") |
| 38 | 删除: cmd := exec.CommandContext(ctx, shell, "-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", | |
| 39 | 删除: "-File", filepath.Join(root, "scripts", "guest-os", "windows-host-enrollment-smoke.ps1"), "-ManagerExecutable", manager) | |
| 53 | 新增: flags := append([]string{"-NoLogo", "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", script}, arguments...) | |
| 54 | 新增: cmd := exec.CommandContext(ctx, shell, flags...) | |
| 40 | 55 | if windows.GetCurrentProcessToken().IsElevated() { |
| 41 | 56 | var token windows.Token |
| 42 | 57 | if err := windows.OpenProcessToken(windows.CurrentProcess(), windows.TOKEN_DUPLICATE|windows.TOKEN_QUERY|windows.TOKEN_ASSIGN_PRIMARY|windows.TOKEN_ADJUST_DEFAULT, &token); err != nil { |
| @@ -85,11 +100,11 @@ func TestWindowsHostEnrollmentOrdinaryUser(t *testing.T) { | ||
| 85 | 100 | defer ordinary.Close() |
| 86 | 101 | } |
| 87 | 102 | if ordinary.IsElevated() { |
| 88 | 删除: t.Fatal("the enrollment regression requires an unelevated token") | |
| 103 | 新增: t.Fatal("the native regression requires an unelevated token") | |
| 89 | 104 | } |
| 90 | 105 | cmd.SysProcAttr = &syscall.SysProcAttr{Token: syscall.Token(ordinary)} |
| 91 | 106 | } |
| 92 | 107 | if output, err := cmd.CombinedOutput(); err != nil { |
| 93 | 删除: t.Fatalf("ordinary-user enrollment: %v\n%s", err, output) | |
| 108 | 新增: t.Fatalf("ordinary-user PowerShell %s: %v\n%s", filepath.Base(script), err, output) | |
| 94 | 109 | } |
| 95 | 110 | } |
| @@ -65,7 +65,7 @@ try { | ||
| 65 | 65 | foreach ($test in @('windows-iso-setup.tests.ps1', 'windows-host-image.tests.ps1', 'new-windows-test-vm.tests.ps1')) { |
| 66 | 66 | Invoke-BuildTool $windowsPowerShell @('-NoProfile', '-NonInteractive', '-File', "scripts/guest-os/$test") |
| 67 | 67 | } |
| 68 | 删除: Invoke-BuildTool 'go' @('test', './internal/guestos', '-run', '^TestWindowsHostEnrollment', '-count=1') | |
| 68 | 新增: Invoke-BuildTool 'go' @('test', './internal/guestos', '-run', '^TestWindows(HostEnrollment|UserToolACL)', '-count=1') | |
| 69 | 69 | if (-not $Release) { |
| 70 | 70 | $windowsPowerShell = Join-Path ([Environment]::SystemDirectory) 'WindowsPowerShell/v1.0/powershell.exe' |
| 71 | 71 | foreach ($test in @('install-guest-powershell.tests.ps1', 'windows-evaluation.tests.ps1')) { |
| @@ -20,14 +20,28 @@ function Assert-NoReparse([string]$Path) { | ||
| 20 | 20 | while ($null -ne $item) { if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) { throw 'A native CCS path is a reparse point.' }; if ($item -is [IO.FileInfo]) { $item=$item.Directory } else { $item=$item.Parent } } |
| 21 | 21 | } |
| 22 | 22 | function Protect-Path([string]$Path,[bool]$Directory) { |
| 23 | 删除: if ($Directory) { $acl=New-Object Security.AccessControl.DirectorySecurity } else { $acl=New-Object Security.AccessControl.FileSecurity } | |
| 24 | 删除: $acl.SetOwner($user.User);$acl.SetAccessRuleProtection($true,$false) | |
| 23 | 新增: $ownerSection = [Security.AccessControl.AccessControlSections]::Owner | |
| 24 | 新增: $accessSection = [Security.AccessControl.AccessControlSections]::Access | |
| 25 | 新增: if ($Directory) { | |
| 26 | 新增: $existing = [Security.AccessControl.DirectorySecurity]::new($Path, $ownerSection) | |
| 27 | 新增: $acl = New-Object Security.AccessControl.DirectorySecurity | |
| 28 | 新增: } else { | |
| 29 | 新增: $existing = [Security.AccessControl.FileSecurity]::new($Path, $ownerSection) | |
| 30 | 新增: $acl = New-Object Security.AccessControl.FileSecurity | |
| 31 | 新增: } | |
| 32 | 新增: if ($existing.GetOwner([Security.Principal.SecurityIdentifier]) -ne $user.User) { throw 'The Cage tool path must already belong to the current GUI user.' } | |
| 33 | 新增: $acl.SetAccessRuleProtection($true,$false) | |
| 25 | 34 | foreach ($sid in @($user.User,[Security.Principal.SecurityIdentifier]'S-1-5-18',[Security.Principal.SecurityIdentifier]'S-1-5-32-544')) { |
| 26 | 35 | if ($Directory) { $rule=New-Object Security.AccessControl.FileSystemAccessRule($sid,'FullControl','ContainerInherit,ObjectInherit','None','Allow') } |
| 27 | 36 | else { $rule=New-Object Security.AccessControl.FileSystemAccessRule($sid,'FullControl','Allow') } |
| 28 | 37 | $acl.AddAccessRule($rule) |
| 29 | 38 | } |
| 30 | 删除: Set-Acl -LiteralPath $Path -AclObject $acl | |
| 39 | 新增: # Change only Access on a descriptor loaded without Audit. Set-Acl can | |
| 40 | 新增: # request SeSecurityPrivilege when persisting a newly built descriptor. | |
| 41 | 新增: $existing.SetSecurityDescriptorSddlForm($acl.GetSecurityDescriptorSddlForm($accessSection), $accessSection) | |
| 42 | 新增: $item = Get-Item -LiteralPath $Path -Force | |
| 43 | 新增: if ($PSVersionTable.PSEdition -eq 'Desktop') { $item.SetAccessControl($existing) } | |
| 44 | 新增: else { [IO.FileSystemAclExtensions]::SetAccessControl($item, $existing) } | |
| 31 | 45 | } |
| 32 | 46 | function PrivateDirectory([string]$Path) { |
| 33 | 47 | $parent=[IO.DirectoryInfo]$Path |
| @@ -24,14 +24,28 @@ function Assert-NoReparse([string]$Path) { | ||
| 24 | 24 | } |
| 25 | 25 | } |
| 26 | 26 | function Protect-Path([string]$Path, [bool]$Directory) { |
| 27 | 删除: if ($Directory) { $acl = New-Object Security.AccessControl.DirectorySecurity } else { $acl = New-Object Security.AccessControl.FileSecurity } | |
| 28 | 删除: $acl.SetOwner($user.User); $acl.SetAccessRuleProtection($true, $false) | |
| 27 | 新增: $ownerSection = [Security.AccessControl.AccessControlSections]::Owner | |
| 28 | 新增: $accessSection = [Security.AccessControl.AccessControlSections]::Access | |
| 29 | 新增: if ($Directory) { | |
| 30 | 新增: $existing = [Security.AccessControl.DirectorySecurity]::new($Path, $ownerSection) | |
| 31 | 新增: $acl = New-Object Security.AccessControl.DirectorySecurity | |
| 32 | 新增: } else { | |
| 33 | 新增: $existing = [Security.AccessControl.FileSecurity]::new($Path, $ownerSection) | |
| 34 | 新增: $acl = New-Object Security.AccessControl.FileSecurity | |
| 35 | 新增: } | |
| 36 | 新增: if ($existing.GetOwner([Security.Principal.SecurityIdentifier]) -ne $user.User) { throw 'The Cage tool path must already belong to the current GUI user.' } | |
| 37 | 新增: $acl.SetAccessRuleProtection($true, $false) | |
| 29 | 38 | foreach ($sid in @($user.User, [Security.Principal.SecurityIdentifier]'S-1-5-18', [Security.Principal.SecurityIdentifier]'S-1-5-32-544')) { |
| 30 | 39 | if ($Directory) { $rule = New-Object Security.AccessControl.FileSystemAccessRule($sid,'FullControl','ContainerInherit,ObjectInherit','None','Allow') } |
| 31 | 40 | else { $rule = New-Object Security.AccessControl.FileSystemAccessRule($sid,'FullControl','Allow') } |
| 32 | 41 | $acl.AddAccessRule($rule) |
| 33 | 42 | } |
| 34 | 删除: Set-Acl -LiteralPath $Path -AclObject $acl | |
| 43 | 新增: # Change only Access on a descriptor loaded without Audit. Set-Acl can | |
| 44 | 新增: # request SeSecurityPrivilege when persisting a newly built descriptor. | |
| 45 | 新增: $existing.SetSecurityDescriptorSddlForm($acl.GetSecurityDescriptorSddlForm($accessSection), $accessSection) | |
| 46 | 新增: $item = Get-Item -LiteralPath $Path -Force | |
| 47 | 新增: if ($PSVersionTable.PSEdition -eq 'Desktop') { $item.SetAccessControl($existing) } | |
| 48 | 新增: else { [IO.FileSystemAclExtensions]::SetAccessControl($item, $existing) } | |
| 35 | 49 | } |
| 36 | 50 | function New-PrivateDirectory([string]$Path) { |
| 37 | 51 | $parent = [IO.DirectoryInfo]$Path |
| @@ -0,0 +1,81 @@ | ||
| 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) } | |

