| 1 | # Execute as the paired ordinary GUI user. Management copies only the validated |
| 2 | # receipt into the readonly bridge; no ProgramFiles write permission is added. |
| 3 | [CmdletBinding()] |
| 4 | param( |
| 5 | [Parameter(Mandatory)][string]$RuntimeArchive, |
| 6 | [Parameter(Mandatory)][string]$NodeArchive, |
| 7 | [Parameter(Mandatory)][string]$BridgeDirectory, |
| 8 | [string]$ExistingNode = '', |
| 9 | [string]$ReceiptDirectory = (Join-Path $env:USERPROFILE 'Cage\CLI\install-receipts') |
| 10 | ) |
| 11 | $ErrorActionPreference='Stop' |
| 12 | Set-StrictMode -Version Latest |
| 13 | if (-not [Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([Runtime.InteropServices.OSPlatform]::Windows)) { throw 'Native Windows is required.' } |
| 14 | $user=[Security.Principal.WindowsIdentity]::GetCurrent() |
| 15 | if ((New-Object Security.Principal.WindowsPrincipal($user)).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Use the paired ordinary GUI user.' } |
| 16 | $bridge=[IO.Path]::GetFullPath($BridgeDirectory) |
| 17 | if (-not (Test-Path (Join-Path $bridge 'Cage.GuestClipboard.exe') -PathType Leaf)) { throw 'Install the paired bridge first.' } |
| 18 | function Assert-NoReparse([string]$Path) { |
| 19 | $item=Get-Item -LiteralPath $Path -Force |
| 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 | } |
| 22 | function Protect-Path([string]$Path,[bool]$Directory) { |
| 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) |
| 34 | foreach ($sid in @($user.User,[Security.Principal.SecurityIdentifier]'S-1-5-18',[Security.Principal.SecurityIdentifier]'S-1-5-32-544')) { |
| 35 | if ($Directory) { $rule=New-Object Security.AccessControl.FileSystemAccessRule($sid,'FullControl','ContainerInherit,ObjectInherit','None','Allow') } |
| 36 | else { $rule=New-Object Security.AccessControl.FileSystemAccessRule($sid,'FullControl','Allow') } |
| 37 | $acl.AddAccessRule($rule) |
| 38 | } |
| 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) } |
| 45 | } |
| 46 | function PrivateDirectory([string]$Path) { |
| 47 | $parent=[IO.DirectoryInfo]$Path |
| 48 | while (-not $parent.Exists) { $parent=$parent.Parent;if($null -eq $parent){throw 'Invalid native tool parent.'} } |
| 49 | Assert-NoReparse $parent.FullName |
| 50 | if(-not (Test-Path -LiteralPath $Path)){New-Item -ItemType Directory -Path $Path | Out-Null} |
| 51 | Assert-NoReparse $Path;Protect-Path $Path $true |
| 52 | } |
| 53 | Assert-NoReparse $RuntimeArchive;Assert-NoReparse $NodeArchive;Assert-NoReparse $bridge |
| 54 | if ((Get-FileHash $RuntimeArchive -Algorithm SHA256).Hash.ToLowerInvariant() -ne 'a0ab3bbac6b2c14ba53020474a4d6a6b307b9faf6f10998568b3200535c6b3e7') { throw 'CCS runtime archive checksum failed.' } |
| 55 | if ((Get-FileHash $NodeArchive -Algorithm SHA256).Hash.ToLowerInvariant() -ne 'ca1bff5d8ead13c9f2ea404a176db46bd430704d5b5113734049fa365e7799e2') { throw 'Official Node archive checksum failed.' } |
| 56 | $tools=Join-Path $env:USERPROFILE 'Cage\CLI\tools';PrivateDirectory $tools |
| 57 | $runtime=Join-Path $tools 'ccs\8.11.0' |
| 58 | if (-not (Test-Path -LiteralPath $runtime)) { |
| 59 | $parent=Split-Path $runtime -Parent;PrivateDirectory $parent |
| 60 | $stage=Join-Path $parent ('stage-'+[Guid]::NewGuid().ToString('N'));PrivateDirectory $stage |
| 61 | & (Join-Path $env:SystemRoot 'System32\tar.exe') -xzf $RuntimeArchive -C $stage |
| 62 | if($LASTEXITCODE -ne 0){throw 'CCS archive extraction failed; staged files were retained.'} |
| 63 | [IO.Directory]::Move($stage,$runtime) |
| 64 | } |
| 65 | Assert-NoReparse $runtime |
| 66 | $manifest=Join-Path $runtime 'runtime-files.json' |
| 67 | $files=Get-Content -LiteralPath $manifest -Raw | ConvertFrom-Json |
| 68 | foreach($entry in $files.PSObject.Properties) { |
| 69 | $file=[IO.Path]::GetFullPath((Join-Path $runtime $entry.Name)) |
| 70 | if(-not $file.StartsWith($runtime+'\',[StringComparison]::OrdinalIgnoreCase)){throw 'CCS runtime contains an invalid path.'} |
| 71 | Assert-NoReparse $file |
| 72 | if((Get-FileHash -LiteralPath $file -Algorithm SHA256).Hash.ToLowerInvariant() -ne $entry.Value){throw 'A CCS runtime file changed.'} |
| 73 | } |
| 74 | $nodeSha='1c331340fa6cac5e4294725959070a143fa0507195a78ad77eed7943f719aac9' |
| 75 | if ($ExistingNode -ne '') { |
| 76 | Assert-NoReparse $ExistingNode |
| 77 | if((Get-FileHash $ExistingNode -Algorithm SHA256).Hash.ToLowerInvariant() -ne $nodeSha){throw 'Existing Node does not match the fixed official version.'} |
| 78 | $node=[IO.Path]::GetFullPath($ExistingNode) |
| 79 | } else { |
| 80 | $nodeDir=Join-Path $tools 'node\24.22.2';PrivateDirectory $nodeDir |
| 81 | $node=Join-Path $nodeDir 'node-v24.22.2-win-x64\node.exe' |
| 82 | if(-not(Test-Path -LiteralPath $node)){Expand-Archive -LiteralPath $NodeArchive -DestinationPath $nodeDir} |
| 83 | Assert-NoReparse $node |
| 84 | if((Get-FileHash $node -Algorithm SHA256).Hash.ToLowerInvariant() -ne $nodeSha){throw 'Native Node changed.'} |
| 85 | } |
| 86 | $signature=Get-AuthenticodeSignature -FilePath $node |
| 87 | if($signature.Status -ne 'Valid'){throw 'Native Node signature is invalid.'} |
| 88 | $main=Join-Path $runtime 'node_modules\@kaitranntt\ccs\dist\ccs.js' |
| 89 | $receiptDir=[IO.Path]::GetFullPath($ReceiptDirectory) |
| 90 | $allowed=[IO.Path]::GetFullPath((Join-Path $env:USERPROFILE 'Cage\CLI')) |
| 91 | if(-not $receiptDir.StartsWith($allowed+'\',[StringComparison]::OrdinalIgnoreCase)){throw 'Receipt staging must stay in the current user Cage CLI directory.'} |
| 92 | PrivateDirectory $receiptDir |
| 93 | $receipt=[ordered]@{version=1;node=$node;node_version='24.22.2';node_sha256=$nodeSha;main=$main;main_sha256=(Get-FileHash $main -Algorithm SHA256).Hash.ToLowerInvariant();manifest=$manifest;manifest_sha256=(Get-FileHash $manifest -Algorithm SHA256).Hash.ToLowerInvariant();ccs_version='8.11.0'} |
| 94 | $temporary=Join-Path $receiptDir ('ccs-tools.'+[Guid]::NewGuid().ToString('N')+'.json') |
| 95 | [IO.File]::WriteAllText($temporary,($receipt | ConvertTo-Json),(New-Object Text.UTF8Encoding($false))) |
| 96 | Protect-Path $temporary $false |
| 97 | Move-Item -LiteralPath $temporary -Destination (Join-Path $receiptDir 'ccs-tools.json') -Force |
| 98 | Write-Output 'Native CCS runtime validated. Management must copy the staged ccs-tools.json into the readonly paired bridge. Existing CCS profiles and credentials were not imported or changed.' |