|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Verifies native release payload dependencies needed on clean Windows hosts. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | + The Windows node uses NSec.Cryptography, which loads libsodium.dll. The |
| 7 | + NuGet-provided Windows libsodium binary imports the Visual C++ runtime, so |
| 8 | + app-local/installer payloads must make that runtime available before the |
| 9 | + tray can generate or load device keys. |
| 10 | +#> |
| 11 | +[CmdletBinding()] |
| 12 | +param( |
| 13 | + [Parameter(Mandatory = $true)] |
| 14 | + [string]$PayloadPath, |
| 15 | + |
| 16 | + [switch]$RequireAppLocalVCRuntime, |
| 17 | + |
| 18 | + [switch]$RequireInstallerVCRedist, |
| 19 | + |
| 20 | + [string]$InstallerVCRedistPath, |
| 21 | + |
| 22 | + [switch]$SkipNativeLoadProbe |
| 23 | +) |
| 24 | + |
| 25 | +Set-StrictMode -Version Latest |
| 26 | +$ErrorActionPreference = "Stop" |
| 27 | + |
| 28 | +$payloadRoot = (Resolve-Path -LiteralPath $PayloadPath).Path |
| 29 | +$errors = New-Object System.Collections.Generic.List[string] |
| 30 | +$runningOnWindows = $env:OS -eq "Windows_NT" |
| 31 | +$shouldProbeNativeLoad = $runningOnWindows -and -not $SkipNativeLoadProbe |
| 32 | + |
| 33 | +if ($shouldProbeNativeLoad -and |
| 34 | + -not ([System.Management.Automation.PSTypeName]"OpenClawNativeDependencyProbe").Type) { |
| 35 | + Add-Type @" |
| 36 | +using System; |
| 37 | +using System.Runtime.InteropServices; |
| 38 | +
|
| 39 | +public static class OpenClawNativeDependencyProbe { |
| 40 | + [DllImport("kernel32", SetLastError=true, CharSet=CharSet.Unicode)] |
| 41 | + public static extern IntPtr LoadLibrary(string lpFileName); |
| 42 | +
|
| 43 | + [DllImport("kernel32", SetLastError=true, CharSet=CharSet.Unicode)] |
| 44 | + public static extern bool SetDllDirectory(string lpPathName); |
| 45 | +} |
| 46 | +"@ |
| 47 | +} |
| 48 | + |
| 49 | +function Get-RelativePath { |
| 50 | + param( |
| 51 | + [Parameter(Mandatory = $true)][string]$Root, |
| 52 | + [Parameter(Mandatory = $true)][string]$Path |
| 53 | + ) |
| 54 | + |
| 55 | + [System.IO.Path]::GetRelativePath($Root, $Path).Replace('/', '\') |
| 56 | +} |
| 57 | + |
| 58 | +function Add-MicrosoftSignatureErrors { |
| 59 | + param( |
| 60 | + [Parameter(Mandatory = $true)] |
| 61 | + [System.IO.FileInfo]$File |
| 62 | + ) |
| 63 | + |
| 64 | + if (-not $runningOnWindows) { |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + $signature = Get-AuthenticodeSignature -LiteralPath $File.FullName |
| 69 | + if ($signature.Status -ne "Valid") { |
| 70 | + $errors.Add("Microsoft runtime file $(Get-RelativePath -Root $payloadRoot -Path $File.FullName) has Authenticode status $($signature.Status).") |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + $subject = if ($signature.SignerCertificate) { $signature.SignerCertificate.Subject } else { "" } |
| 75 | + if ($subject -notmatch "O=Microsoft Corporation") { |
| 76 | + $errors.Add("Microsoft runtime file $(Get-RelativePath -Root $payloadRoot -Path $File.FullName) was not signed by Microsoft Corporation: $subject") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function Get-VCRuntimeFiles { |
| 81 | + param( |
| 82 | + [Parameter(Mandatory = $true)] |
| 83 | + [string]$Directory |
| 84 | + ) |
| 85 | + |
| 86 | + @( |
| 87 | + Get-ChildItem -LiteralPath $Directory -File -Filter "vcruntime140*.dll" |
| 88 | + Get-ChildItem -LiteralPath $Directory -File -Filter "msvcp140*.dll" |
| 89 | + Get-ChildItem -LiteralPath $Directory -File -Filter "concrt140.dll" |
| 90 | + ) | Sort-Object FullName -Unique |
| 91 | +} |
| 92 | + |
| 93 | +$libsodiumFiles = @( |
| 94 | + Get-ChildItem -LiteralPath $payloadRoot -Recurse -File -Filter libsodium.dll | |
| 95 | + Sort-Object FullName |
| 96 | +) |
| 97 | + |
| 98 | +if ($libsodiumFiles.Count -eq 0) { |
| 99 | + $errors.Add("Missing libsodium.dll under $payloadRoot.") |
| 100 | +} |
| 101 | + |
| 102 | +foreach ($libsodium in $libsodiumFiles) { |
| 103 | + $runtimePath = Join-Path $libsodium.DirectoryName "vcruntime140.dll" |
| 104 | + if ($RequireAppLocalVCRuntime -and -not (Test-Path -LiteralPath $runtimePath)) { |
| 105 | + $errors.Add("Missing app-local vcruntime140.dll next to $(Get-RelativePath -Root $payloadRoot -Path $libsodium.FullName).") |
| 106 | + } |
| 107 | + |
| 108 | + if ($RequireAppLocalVCRuntime) { |
| 109 | + foreach ($runtimeFile in Get-VCRuntimeFiles -Directory $libsodium.DirectoryName) { |
| 110 | + Add-MicrosoftSignatureErrors -File $runtimeFile |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if ($shouldProbeNativeLoad) { |
| 115 | + [OpenClawNativeDependencyProbe]::SetDllDirectory($libsodium.DirectoryName) | Out-Null |
| 116 | + Push-Location $libsodium.DirectoryName |
| 117 | + try { |
| 118 | + $handle = [OpenClawNativeDependencyProbe]::LoadLibrary("libsodium.dll") |
| 119 | + $lastError = [Runtime.InteropServices.Marshal]::GetLastWin32Error() |
| 120 | + } finally { |
| 121 | + Pop-Location |
| 122 | + } |
| 123 | + |
| 124 | + if ($handle -eq [IntPtr]::Zero) { |
| 125 | + $errors.Add("libsodium.dll failed to load from $(Get-RelativePath -Root $payloadRoot -Path $libsodium.FullName) (Win32 error $lastError).") |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +if ($RequireInstallerVCRedist) { |
| 131 | + $redist = if ([string]::IsNullOrWhiteSpace($InstallerVCRedistPath)) { |
| 132 | + Join-Path $payloadRoot "vc_redist.x64.exe" |
| 133 | + } else { |
| 134 | + $InstallerVCRedistPath |
| 135 | + } |
| 136 | + |
| 137 | + if (-not (Test-Path -LiteralPath $redist)) { |
| 138 | + $errors.Add("Missing bundled Visual C++ Runtime redistributable at $redist.") |
| 139 | + } elseif ($runningOnWindows) { |
| 140 | + Add-MicrosoftSignatureErrors -File (Get-Item -LiteralPath $redist) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +if ($errors.Count -gt 0) { |
| 145 | + $errors | ForEach-Object { Write-Error $_ } |
| 146 | + exit 1 |
| 147 | +} |
| 148 | + |
| 149 | +Write-Host "Release native dependency policy passed." -ForegroundColor Green |
0 commit comments