| 1 | # Records only the files verified in this fixed guest after installation. |
| 2 | param([guid]$VMId,[string]$GUIUserSID,[string]$ManifestJSON) |
| 3 | $ErrorActionPreference='Stop' |
| 4 | if ([guid](Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters').VirtualMachineId -ne $VMId -or |
| 5 | $GUIUserSID -notmatch '^S-1-5-21-\d+-\d+-\d+-\d+$' -or [Text.Encoding]::UTF8.GetByteCount($ManifestJSON) -gt 4MB) { |
| 6 | throw '组件版本记录与客机身份不一致。' |
| 7 | } |
| 8 | $manifest=$ManifestJSON|ConvertFrom-Json |
| 9 | if ($manifest.protocol -ne 1 -or $manifest.system -cne 'windows-11' -or $manifest.revision -notmatch '^[a-f0-9]{64}$' -or |
| 10 | @($manifest.files).Count -lt 1 -or @($manifest.files).Count -gt 10000) { throw '组件版本清单无效。' } |
| 11 | $roots=@{bridge=(Join-Path $env:ProgramFiles ('Cage Guest Bridge\'+$VMId));network=(Join-Path $env:ProgramFiles ('Cage Guest Network\'+$VMId))} |
| 12 | $seen=@{} |
| 13 | foreach ($entry in $manifest.files) { |
| 14 | if (!$entry.target) { continue } |
| 15 | if ($entry.target -notmatch '^(bridge|network)/[^:\\]+$' -or $entry.target -match '(^|/)\.\.?(/|$)' -or |
| 16 | $entry.installed_sha256 -notmatch '^[a-f0-9]{64}$' -or $seen.ContainsKey($entry.target)) { throw '组件文件记录无效。' } |
| 17 | $seen[$entry.target]=$true |
| 18 | $parts=$entry.target -split '/',2 |
| 19 | $file=Join-Path $roots[$parts[0]] $parts[1] |
| 20 | $item=Get-Item -LiteralPath $file -Force |
| 21 | if ($item -isnot [IO.FileInfo]) { throw '安装的组件文件缺失,请继续同步环境组件。' } |
| 22 | for ($parent=$item; $parent; $parent=if($parent -is [IO.FileInfo]){$parent.Directory}else{$parent.Parent}) { |
| 23 | if ($parent.Attributes -band [IO.FileAttributes]::ReparsePoint) { throw '组件安装目录包含文件链接。' } |
| 24 | } |
| 25 | if ((Get-FileHash -LiteralPath $file).Hash -ne $entry.installed_sha256) { throw '安装的组件与当前应用版本不匹配,请继续同步环境组件。' } |
| 26 | } |
| 27 | if (!$seen.Count) { throw '组件清单缺少安装文件。' } |
| 28 | $target=Join-Path $roots.bridge 'native-components.json' |
| 29 | if (Test-Path -LiteralPath $target) { |
| 30 | $old=Get-Item -LiteralPath $target -Force |
| 31 | if ($old -isnot [IO.FileInfo] -or $old.Attributes -band [IO.FileAttributes]::ReparsePoint) { throw '现有组件记录无法安全更新。' } |
| 32 | } |
| 33 | $temporary=Join-Path $roots.bridge ('native-components.'+[guid]::NewGuid().ToString('N')+'.json') |
| 34 | try { |
| 35 | $file=[IO.File]::Open($temporary,'CreateNew','Write','None') |
| 36 | try { |
| 37 | $bytes=[Text.UTF8Encoding]::new($false).GetBytes($ManifestJSON) |
| 38 | $file.Write($bytes,0,$bytes.Length) |
| 39 | $file.Flush($true) |
| 40 | } finally { $file.Dispose() } |
| 41 | $acl=[Security.AccessControl.FileSecurity]::new() |
| 42 | $acl.SetAccessRuleProtection($true,$false) |
| 43 | $acl.SetOwner([Security.Principal.SecurityIdentifier]::new('S-1-5-32-544')) |
| 44 | foreach ($sid in @('S-1-5-18','S-1-5-32-544')) { |
| 45 | $acl.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new([Security.Principal.SecurityIdentifier]::new($sid),'FullControl','Allow')) |
| 46 | } |
| 47 | $acl.AddAccessRule([Security.AccessControl.FileSystemAccessRule]::new([Security.Principal.SecurityIdentifier]::new($GUIUserSID),'ReadAndExecute','Allow')) |
| 48 | Set-Acl -LiteralPath $temporary -AclObject $acl |
| 49 | if (Test-Path -LiteralPath $target) { [IO.File]::Replace($temporary,$target,[Management.Automation.Language.NullString]::Value) } |
| 50 | else { [IO.File]::Move($temporary,$target) } |
| 51 | } finally { |
| 52 | if ([IO.File]::Exists($temporary)) { [IO.File]::Delete($temporary) } |
| 53 | } |