Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(installers): hash downloads with .NET instead of Get-FileHash (#4418)

dduponta22426adac19
diff --git a/libs/cua-driver/scripts/install.ps1 b/libs/cua-driver/scripts/install.ps1
index 77fa23d391..2f2282d417 100644
--- a/libs/cua-driver/scripts/install.ps1
+++ b/libs/cua-driver/scripts/install.ps1
@@ -117,6 +117,20 @@ $ErrorActionPreference = "Stop"
# over PowerShell ISE and Windows PowerShell 5 — silence it. Restored
# nowhere on purpose: this script is a one-shot, the user can re-set it.
$ProgressPreference = "SilentlyContinue"
+# Windows PowerShell 5.1 started from PowerShell 7 (a pwsh terminal, or
+# `cua-driver update --apply` launched from one) inherits pwsh's
+# PSModulePath. Autoload then finds PowerShell 7's Core-only copies of
+# Microsoft.PowerShell.Utility / .Security / .Archive first, fails to load
+# them, and Get-FileHash, Get-AuthenticodeSignature and Expand-Archive are
+# "not recognized". Put this edition's own modules first and drop the
+# PowerShell 7 module paths before any of them is used.
+if ($PSVersionTable.PSEdition -eq 'Desktop') {
+ $desktopModules = Join-Path $PSHOME 'Modules'
+ $modulePaths = @($desktopModules) + @(($env:PSModulePath -split ';') | Where-Object {
+ $_ -and ($_ -notmatch '\\PowerShell\\') -and ($_.TrimEnd('\') -ne $desktopModules.TrimEnd('\'))
+ })
+ $env:PSModulePath = $modulePaths -join ';'
+}
$Repo = "trycua/cua"
$TagPrefix = "cua-driver-rs-v"
@@ -210,6 +224,15 @@ $ChannelWasExplicit = $PSBoundParameters.ContainsKey('Channel')
# in Resolve-KeepVersions below; 0 means "never GC".
$Script:KeepVersionsDefault = 5
+function Get-Sha256Hex([string]$Path) {
+ # .NET directly: Get-FileHash is missing when the Utility module is not
+ # loaded (e.g. under `cua-driver update --apply` on older drivers).
+ $sha = [System.Security.Cryptography.SHA256]::Create()
+ $stream = [System.IO.File]::OpenRead($Path)
+ try { return ([System.BitConverter]::ToString($sha.ComputeHash($stream)) -replace '-', '').ToLowerInvariant() }
+ finally { $stream.Dispose(); $sha.Dispose() }
+}
+
function Resolve-KeepVersions {
$raw = $env:CUA_DRIVER_RS_KEEP_VERSIONS
if (-not $raw) { return $Script:KeepVersionsDefault }
@@ -1261,7 +1284,7 @@ function Assert-ReleaseZipIntegrity([string]$zipPath, [string]$version) {
exit 1
}
else {
- $actual = (Get-FileHash -LiteralPath $zipPath -Algorithm SHA256).Hash.ToLowerInvariant()
+ $actual = Get-Sha256Hex $zipPath
if ($actual -ne $expected) {
Write-ErrorStep "$zipName does not match $tag's $sumsName (expected $expected, got $actual); refusing to install it."
exit 1
diff --git a/scripts/install/install.ps1 b/scripts/install/install.ps1
index 2e346ebdee..56f09ef92f 100644
--- a/scripts/install/install.ps1
+++ b/scripts/install/install.ps1
@@ -65,6 +65,21 @@ param(
[switch]$Yes
)
+# Windows PowerShell 5.1 started from PowerShell 7 (a pwsh terminal, or
+# `cua-driver update --apply` launched from one) inherits pwsh's
+# PSModulePath. Autoload then finds PowerShell 7's Core-only copies of
+# Microsoft.PowerShell.Utility / .Security / .Archive first, fails to load
+# them, and Get-FileHash, Get-AuthenticodeSignature and Expand-Archive are
+# "not recognized". Put this edition's own modules first and drop the
+# PowerShell 7 module paths before any of them is used.
+if ($PSVersionTable.PSEdition -eq 'Desktop') {
+ $desktopModules = Join-Path $PSHOME 'Modules'
+ $modulePaths = @($desktopModules) + @(($env:PSModulePath -split ';') | Where-Object {
+ $_ -and ($_ -notmatch '\\PowerShell\\') -and ($_.TrimEnd('\') -ne $desktopModules.TrimEnd('\'))
+ })
+ $env:PSModulePath = $modulePaths -join ';'
+}
+
$Repo = if ($env:CUA_INSTALL_REPO) { $env:CUA_INSTALL_REPO } else { 'trycua/cua' }
$BaseUrl = if ($env:CUA_INSTALL_BASE_URL) { $env:CUA_INSTALL_BASE_URL } else { "https://github.com/$Repo/releases/download" }
$LatestTag = 'cua-install-latest'
@@ -76,6 +91,15 @@ $script:Picked = $false
# the Windows app back (Install-App is kept).
$script:SpacesSupported = $false
+function Get-Sha256Hex([string]$Path) {
+ # .NET directly: Get-FileHash is missing when the Utility module is not
+ # loaded (e.g. under `cua-driver update --apply` on older drivers).
+ $sha = [System.Security.Cryptography.SHA256]::Create()
+ $stream = [System.IO.File]::OpenRead($Path)
+ try { return ([System.BitConverter]::ToString($sha.ComputeHash($stream)) -replace '-', '').ToLowerInvariant() }
+ finally { $stream.Dispose(); $sha.Dispose() }
+}
+
function Write-Info([string]$Message) { Write-Host "cua-install: $Message" }
function Stop-Install([string]$Message) { throw "cua-install: error: $Message" }
@@ -278,7 +302,7 @@ function Get-Artifact($Manifest, [string]$ManifestUrl, [string]$Component, [stri
}
Write-Info "downloading $($entry.name)"
Get-Remote $url $dest $ManifestUrl
- $actual = (Get-FileHash -Algorithm SHA256 -LiteralPath $dest).Hash.ToLowerInvariant()
+ $actual = Get-Sha256Hex $dest
if ($actual -ne $entry.sha256.ToLowerInvariant()) {
Stop-Install "checksum mismatch for $($entry.name) (expected $($entry.sha256), got $actual); aborting"
}