|
| 1 | +# SPDX-License-Identifier: AGPL-3.0-only |
| 2 | +# Interactive installer dependency preflight, never a realtime audio callback. |
| 3 | +[CmdletBinding()]param([switch]$PolicySelfTest) |
| 4 | +Set-StrictMode -Version Latest |
| 5 | +$ErrorActionPreference='Stop' |
| 6 | +$official='https://go.microsoft.com/fwlink/p/?LinkId=2124703' |
| 7 | +$guid='{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}' |
| 8 | +function Valid-Version([string]$value) { |
| 9 | + if($value -notmatch '^\d+\.\d+\.\d+\.\d+$'){return $false} |
| 10 | + try {return ([version]$value) -ge [version]'109.0.0.0'}catch{return $false} |
| 11 | +} |
| 12 | +function Microsoft-Uri([uri]$uri) { |
| 13 | + if($null -eq $uri -or $uri.Scheme -cne 'https'){return $false} |
| 14 | + $hostName=$uri.DnsSafeHost.ToLowerInvariant() |
| 15 | + return $hostName -eq 'microsoft.com' -or $hostName.EndsWith('.microsoft.com') |
| 16 | +} |
| 17 | +function Microsoft-Signer($signature) { |
| 18 | + if($null -eq $signature -or [string]$signature.Status -cne 'Valid' -or |
| 19 | + $null -eq $signature.SignerCertificate){return $false} |
| 20 | + return [string]$signature.SignerCertificate.Subject -match '(?i)(^|,\s*)(CN|O)=Microsoft Corporation(,|$)' |
| 21 | +} |
| 22 | +if($PolicySelfTest) { |
| 23 | + $good=[pscustomobject]@{Status='Valid';SignerCertificate=[pscustomobject]@{Subject='CN=Microsoft Corporation, O=Microsoft Corporation'}} |
| 24 | + $bad=[pscustomobject]@{Status='NotTrusted';SignerCertificate=[pscustomobject]@{Subject='CN=Microsoft Corporation'}} |
| 25 | + if(-not (Valid-Version '120.0.0.1') -or (Valid-Version '0.0.0.0') -or |
| 26 | + (Valid-Version 'bogus') -or (Valid-Version '108.0.0.1') -or |
| 27 | + -not (Microsoft-Uri ([uri]$official)) -or |
| 28 | + (Microsoft-Uri ([uri]'http://go.microsoft.com/setup.exe')) -or |
| 29 | + (Microsoft-Uri ([uri]'https://malicious-microsoft.com/setup.exe')) -or |
| 30 | + -not (Microsoft-Signer $good) -or (Microsoft-Signer $bad)){ |
| 31 | + throw 'ARSONMIX_WEBVIEW2_PREREQUISITE_POLICY_FAIL' |
| 32 | + } |
| 33 | + Write-Output 'ARSONMIX_WEBVIEW2_PREREQUISITE_POLICY_PASS' |
| 34 | + exit 0 |
| 35 | +} |
| 36 | +$folder=Join-Path $env:LOCALAPPDATA 'ArSonMix\diagnostics' |
| 37 | +$report=Join-Path $folder 'webview2-prerequisite.json' |
| 38 | +function Record([string]$state,[string]$detail) { |
| 39 | + try { |
| 40 | + [IO.Directory]::CreateDirectory($folder)|Out-Null |
| 41 | + $temp=$report+'.'+[guid]::NewGuid().ToString('N')+'.tmp' |
| 42 | + [ordered]@{schema='arsonmix.webview2-prerequisite.v1';state=$state;detail=$detail;utc=[DateTime]::UtcNow.ToString('o')} | |
| 43 | + ConvertTo-Json -Compress | Set-Content -LiteralPath $temp -Encoding UTF8 |
| 44 | + Move-Item -LiteralPath $temp -Destination $report -Force |
| 45 | + }catch{Write-Warning ('Unable to save WebView2 diagnostic: '+$_.Exception.Message)} |
| 46 | +} |
| 47 | +function Find-EvergreenRuntime { |
| 48 | + $keys=@( |
| 49 | + "HKLM:\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\$guid", |
| 50 | + "HKLM:\SOFTWARE\Microsoft\EdgeUpdate\Clients\$guid", |
| 51 | + "HKCU:\SOFTWARE\Microsoft\EdgeUpdate\Clients\$guid", |
| 52 | + "HKCU:\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\$guid" |
| 53 | + ) |
| 54 | + $roots=@( |
| 55 | + (Join-Path $env:LOCALAPPDATA 'Microsoft\EdgeWebView\Application'), |
| 56 | + (Join-Path ([Environment]::GetFolderPath('ProgramFilesX86')) 'Microsoft\EdgeWebView\Application'), |
| 57 | + (Join-Path $env:ProgramFiles 'Microsoft\EdgeWebView\Application') |
| 58 | + ) | Select-Object -Unique |
| 59 | + foreach($key in $keys){ |
| 60 | + try { |
| 61 | + $pv=[string](Get-ItemProperty -LiteralPath $key -Name pv -ErrorAction Stop).pv |
| 62 | + if(-not (Valid-Version $pv)){continue} |
| 63 | + foreach($root in $roots){ |
| 64 | + if(Test-Path -LiteralPath (Join-Path (Join-Path $root $pv) 'msedgewebview2.exe') -PathType Leaf){return $pv} |
| 65 | + } |
| 66 | + }catch{Write-Verbose ('WebView2 registration query: '+$_.Exception.Message)} |
| 67 | + } |
| 68 | + return '' |
| 69 | +} |
| 70 | +$work=$null |
| 71 | +try { |
| 72 | + $os=Get-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' -ErrorAction Stop |
| 73 | + if(-not [Environment]::Is64BitOperatingSystem -or [int]$os.CurrentBuildNumber -lt 17763){ |
| 74 | + Record 'UNSUPPORTED_OS' 'ArSonMix requires Windows 10 build 17763+ x64 or Windows 11 x64' |
| 75 | + exit 3 |
| 76 | + } |
| 77 | + $installed=Find-EvergreenRuntime |
| 78 | + if($installed){ |
| 79 | + Record 'ALREADY_PRESENT' ('WebView2 '+$installed+' is installed; no changes needed') |
| 80 | + Write-Output 'ARSONMIX_WEBVIEW2_ALREADY_PRESENT' |
| 81 | + exit 0 |
| 82 | + } |
| 83 | + # Download only when missing. Final HTTPS host and Microsoft Authenticode |
| 84 | + # must both validate. No package from third-party mirrors is ever executed. |
| 85 | + $work=Join-Path $env:TEMP ('arsonmix-webview2-'+[guid]::NewGuid().ToString('N')) |
| 86 | + [IO.Directory]::CreateDirectory($work)|Out-Null |
| 87 | + $file=Join-Path $work 'MicrosoftEdgeWebView2Setup.exe' |
| 88 | + [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12 |
| 89 | + $request=[Net.HttpWebRequest]::Create($official) |
| 90 | + $request.AllowAutoRedirect=$true |
| 91 | + $request.MaximumAutomaticRedirections=8 |
| 92 | + $request.Timeout=20000 |
| 93 | + $request.ReadWriteTimeout=20000 |
| 94 | + $response=$request.GetResponse() |
| 95 | + try { |
| 96 | + if(-not (Microsoft-Uri $response.ResponseUri)){throw 'Unapproved Microsoft bootstrapper redirect host'} |
| 97 | + $stream=$response.GetResponseStream() |
| 98 | + $output=[IO.File]::Open($file,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write) |
| 99 | + try { |
| 100 | + $bytes=New-Object byte[] 65536 |
| 101 | + $total=0 |
| 102 | + while(($n=$stream.Read($bytes,0,$bytes.Length)) -gt 0){ |
| 103 | + $total+=$n |
| 104 | + if($total -gt 16777216){throw 'Oversized WebView2 bootstrapper'} |
| 105 | + $output.Write($bytes,0,$n) |
| 106 | + } |
| 107 | + if($total -lt 32768){throw 'Incomplete WebView2 download'} |
| 108 | + }finally{$output.Dispose();$stream.Dispose()} |
| 109 | + }finally{$response.Dispose()} |
| 110 | + $signature=Get-AuthenticodeSignature -LiteralPath $file -ErrorAction Stop |
| 111 | + if(-not (Microsoft-Signer $signature)){throw 'Untrusted WebView2 bootstrapper (not executed)'} |
| 112 | + # Official supported Evergreen installer switches; respects Windows policy. |
| 113 | + $process=Start-Process -FilePath $file -ArgumentList @('/silent','/install') -Wait -PassThru -ErrorAction Stop |
| 114 | + if($process.ExitCode -ne 0){throw ('Microsoft WebView2 setup failed: '+$process.ExitCode)} |
| 115 | + $installed='' |
| 116 | + for($i=0;$i -lt 5;$i++){ |
| 117 | + $installed=Find-EvergreenRuntime |
| 118 | + if($installed){break} |
| 119 | + Start-Sleep -Seconds 2 |
| 120 | + } |
| 121 | + if(-not $installed){throw 'WebView2 setup finished but runtime was not detected'} |
| 122 | + Record 'INSTALLED' ('WebView2 '+$installed+' verified') |
| 123 | + Write-Output 'ARSONMIX_WEBVIEW2_INSTALLED' |
| 124 | + exit 0 |
| 125 | +}catch{ |
| 126 | + # Offline, group-policy, restricted permissions and failed downloads are |
| 127 | + # recoverable. Do not loop, claim success, or delete user session data. |
| 128 | + Record 'NEEDS_ATTENTION' $_.Exception.Message |
| 129 | + Write-Warning ('WebView2 installation incomplete: '+$_.Exception.Message) |
| 130 | + exit 2 |
| 131 | +}finally{ |
| 132 | + if($work -and (Test-Path -LiteralPath $work)){ |
| 133 | + Remove-Item -LiteralPath $work -Recurse -Force -ErrorAction SilentlyContinue |
| 134 | + } |
| 135 | +} |
0 commit comments