Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix: harden Windows binary upgrades

Salman Qureshida30062194ce
diff --git a/docs/install.ps1 b/docs/install.ps1
index 1ea3265c..4d8e99f7 100644
--- a/docs/install.ps1
+++ b/docs/install.ps1
@@ -51,6 +51,36 @@ function Get-RequiredChecksum ([string]$Checksums, [string]$Asset) {
return $matches[0].ToLower()
}
+function Save-VerifiedAsset ([string]$Uri, [string]$Path, [string]$ExpectedHash, [string]$Asset) {
+ $actualHash = ''
+ for ($attempt = 1; $attempt -le 2; $attempt++) {
+ $downloadUri = $Uri
+ if ($attempt -gt 1) {
+ $separator = if ($Uri.Contains('?')) { '&' } else { '?' }
+ $downloadUri = "$Uri${separator}cacheBust=$([guid]::NewGuid().ToString('N'))"
+ }
+
+ try {
+ Invoke-WebRequest -Uri $downloadUri -OutFile $Path -UseBasicParsing `
+ -Headers @{ 'Cache-Control' = 'no-cache' }
+ } catch {
+ if ($attempt -eq 2) { Die "Failed to download $Asset from $Uri" }
+ Write-Warn2 "Download failed for $Asset; retrying without cache..."
+ continue
+ }
+
+ $actualHash = (Get-FileHash -Algorithm SHA256 -Path $Path).Hash.ToLower()
+ if ($actualHash -eq $ExpectedHash) { return }
+
+ Remove-Item $Path -Force -ErrorAction SilentlyContinue
+ if ($attempt -eq 1) {
+ Write-Warn2 "Checksum mismatch for $Asset; retrying without cache..."
+ }
+ }
+
+ Die "Checksum mismatch for $Asset`n expected: $ExpectedHash`n actual: $actualHash"
+}
+
function Update-UserPath ([string]$BinDir) {
if ($env:MERCURY_NO_PATH -eq '1') { return $false }
@@ -122,28 +152,11 @@ try {
$expectedWeb = Get-RequiredChecksum -Checksums $checksums -Asset 'web.tar.gz'
Write-Info "Downloading $asset ..."
- try {
- Invoke-WebRequest -Uri $url -OutFile $binaryTmp -UseBasicParsing
- } catch {
- Die @"
-Failed to download $url
- The binary for v$version on win-x64 may not have been published yet.
- Browse releases: https://github.com/$Repo/releases
-"@
- }
+ Save-VerifiedAsset -Uri $url -Path $binaryTmp -ExpectedHash $expectedBinary -Asset $asset
$webTarUrl = "$GhDl/v$version/web.tar.gz"
Write-Info 'Downloading web.tar.gz ...'
- Invoke-WebRequest -Uri $webTarUrl -OutFile $webTmp -UseBasicParsing
-
- $actualBinary = (Get-FileHash -Algorithm SHA256 -Path $binaryTmp).Hash.ToLower()
- if ($actualBinary -ne $expectedBinary) {
- Die "Checksum mismatch for $asset`n expected: $expectedBinary`n actual: $actualBinary"
- }
- $actualWeb = (Get-FileHash -Algorithm SHA256 -Path $webTmp).Hash.ToLower()
- if ($actualWeb -ne $expectedWeb) {
- Die "Checksum mismatch for web.tar.gz`n expected: $expectedWeb`n actual: $actualWeb"
- }
+ Save-VerifiedAsset -Uri $webTarUrl -Path $webTmp -ExpectedHash $expectedWeb -Asset 'web.tar.gz'
Write-Info 'Checksums verified (sha256)'
$archiveEntries = @(tar -tzf $webTmp)
diff --git a/scripts/install.ps1 b/scripts/install.ps1
index 1ea3265c..4d8e99f7 100644
--- a/scripts/install.ps1
+++ b/scripts/install.ps1
@@ -51,6 +51,36 @@ function Get-RequiredChecksum ([string]$Checksums, [string]$Asset) {
return $matches[0].ToLower()
}
+function Save-VerifiedAsset ([string]$Uri, [string]$Path, [string]$ExpectedHash, [string]$Asset) {
+ $actualHash = ''
+ for ($attempt = 1; $attempt -le 2; $attempt++) {
+ $downloadUri = $Uri
+ if ($attempt -gt 1) {
+ $separator = if ($Uri.Contains('?')) { '&' } else { '?' }
+ $downloadUri = "$Uri${separator}cacheBust=$([guid]::NewGuid().ToString('N'))"
+ }
+
+ try {
+ Invoke-WebRequest -Uri $downloadUri -OutFile $Path -UseBasicParsing `
+ -Headers @{ 'Cache-Control' = 'no-cache' }
+ } catch {
+ if ($attempt -eq 2) { Die "Failed to download $Asset from $Uri" }
+ Write-Warn2 "Download failed for $Asset; retrying without cache..."
+ continue
+ }
+
+ $actualHash = (Get-FileHash -Algorithm SHA256 -Path $Path).Hash.ToLower()
+ if ($actualHash -eq $ExpectedHash) { return }
+
+ Remove-Item $Path -Force -ErrorAction SilentlyContinue
+ if ($attempt -eq 1) {
+ Write-Warn2 "Checksum mismatch for $Asset; retrying without cache..."
+ }
+ }
+
+ Die "Checksum mismatch for $Asset`n expected: $ExpectedHash`n actual: $actualHash"
+}
+
function Update-UserPath ([string]$BinDir) {
if ($env:MERCURY_NO_PATH -eq '1') { return $false }
@@ -122,28 +152,11 @@ try {
$expectedWeb = Get-RequiredChecksum -Checksums $checksums -Asset 'web.tar.gz'
Write-Info "Downloading $asset ..."
- try {
- Invoke-WebRequest -Uri $url -OutFile $binaryTmp -UseBasicParsing
- } catch {
- Die @"
-Failed to download $url
- The binary for v$version on win-x64 may not have been published yet.
- Browse releases: https://github.com/$Repo/releases
-"@
- }
+ Save-VerifiedAsset -Uri $url -Path $binaryTmp -ExpectedHash $expectedBinary -Asset $asset
$webTarUrl = "$GhDl/v$version/web.tar.gz"
Write-Info 'Downloading web.tar.gz ...'
- Invoke-WebRequest -Uri $webTarUrl -OutFile $webTmp -UseBasicParsing
-
- $actualBinary = (Get-FileHash -Algorithm SHA256 -Path $binaryTmp).Hash.ToLower()
- if ($actualBinary -ne $expectedBinary) {
- Die "Checksum mismatch for $asset`n expected: $expectedBinary`n actual: $actualBinary"
- }
- $actualWeb = (Get-FileHash -Algorithm SHA256 -Path $webTmp).Hash.ToLower()
- if ($actualWeb -ne $expectedWeb) {
- Die "Checksum mismatch for web.tar.gz`n expected: $expectedWeb`n actual: $actualWeb"
- }
+ Save-VerifiedAsset -Uri $webTarUrl -Path $webTmp -ExpectedHash $expectedWeb -Asset 'web.tar.gz'
Write-Info 'Checksums verified (sha256)'
$archiveEntries = @(tar -tzf $webTmp)
diff --git a/src/index.ts b/src/index.ts
index cf8deeab..bcef43a8 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -4311,15 +4311,20 @@ serviceCmd
console.log(chalk.dim(' Standalone binary detected — re-running installer...'));
console.log('');
- const { execSync } = await import('node:child_process');
+ const { execSync, spawn } = await import('node:child_process');
const platform = process.platform;
- const binPath = process.execPath;
if (platform === 'win32') {
- // Windows: use PowerShell installer
- const psCmd = `irm https://mercuryagent.sh/install.ps1 | iex`;
+ // Run after this process exits so Windows releases the current executable.
+ const psCmd = 'Start-Sleep -Seconds 1; irm https://mercuryagent.sh/install.ps1 | iex';
try {
- execSync(psCmd, { stdio: 'inherit', shell: 'powershell.exe' });
+ const installer = spawn(
+ 'powershell.exe',
+ ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', psCmd],
+ { detached: true, stdio: 'inherit' },
+ );
+ installer.unref();
+ console.log(chalk.green(' ✓ Upgrade installer started. Mercury will exit before installation begins.'));
} catch {
console.log(chalk.red(' ✗ Upgrade failed. Try manually:'));
console.log(chalk.dim(' irm https://mercuryagent.sh/install.ps1 | iex'));