Skip to content

Commit 90e7389

Browse files
committed
fix: harden installers and script wrappers
1 parent b835303 commit 90e7389

File tree

9 files changed

+282
-128
lines changed

9 files changed

+282
-128
lines changed

src/playbook/Executables/AtlasDesktop/2. Drivers/Run Update Drivers.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if not exist "%script%" (
1818
exit /b 1
1919
)
2020

21-
powershell -ExecutionPolicy Bypass -NoProfile -File "%script%"
21+
powershell -ExecutionPolicy RemoteSigned -NoProfile -File "%script%"
2222

2323
echo.
2424
pause > null

src/playbook/Executables/AtlasModules/Scripts/ScriptWrappers/RemoveEdge.ps1

Lines changed: 89 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ param (
3838
[switch]$NonInteractive
3939
)
4040

41+
Set-StrictMode -Version 3.0
42+
4143
$version = '1.9.5'
4244

4345
$ProgressPreference = 'SilentlyContinue'
4446
$sys32 = [Environment]::GetFolderPath('System')
4547
$windir = [Environment]::GetFolderPath('Windows')
4648
$env:path = "$windir;$sys32;$sys32\Wbem;$sys32\WindowsPowerShell\v1.0;" + $env:path
47-
$baseKey = 'HKLM:\SOFTWARE' + $(if ([Environment]::Is64BitOperatingSystem) { '\WOW6432Node' }) + '\Microsoft'
48-
$msedgeExe = "$([Environment]::GetFolderPath('ProgramFilesx86'))\Microsoft\Edge\Application\msedge.exe"
49-
$edgeUWP = "$windir\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe"
49+
$msedgeExePaths = @(
50+
"$([Environment]::GetFolderPath('ProgramFilesx86'))\Microsoft\Edge\Application\msedge.exe",
51+
"$([Environment]::GetFolderPath('ProgramFiles'))\Microsoft\Edge\Application\msedge.exe"
52+
)
5053

5154
if ($NonInteractive -and (!$UninstallEdge -and !$InstallEdge -and !$InstallWebView)) {
5255
$NonInteractive = $false
@@ -113,7 +116,13 @@ function DeleteIfExist($Path) {
113116

114117
# True if it's installed
115118
function EdgeInstalled {
116-
Test-Path $msedgeExe
119+
foreach ($msedgeExe in $msedgeExePaths) {
120+
if (Test-Path $msedgeExe) {
121+
return $true
122+
}
123+
}
124+
125+
return $false
117126
}
118127

119128
function KillEdgeProcesses {
@@ -136,6 +145,7 @@ function InstallEdgeChromium {
136145
$temp = mkdir (Join-Path $([System.IO.Path]::GetTempPath()) $(New-Guid))
137146
$msi = "$temp\edge.msi"
138147
$msiLog = "$temp\edgeMsi.log"
148+
$link = 'Undefined'
139149

140150
if ([Environment]::Is64BitOperatingSystem) {
141151
$arm = ((Get-CimInstance -Class Win32_ComputerSystem).SystemType -match 'ARM64') -or ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64')
@@ -155,15 +165,15 @@ function InstallEdgeChromium {
155165
Error: $_" -Level Critical -Exit -ExitCode 4
156166
}
157167

158-
$edgeItem = ($edgeUpdateApi | ? { $_.Product -eq 'Stable' }).Releases |
168+
$edgeItem = ($edgeUpdateApi | Where-Object { $_.Product -eq 'Stable' }).Releases |
159169
Where-Object { $_.Platform -eq 'Windows' -and $_.Architecture -eq $archString } |
160170
Where-Object { $_.Artifacts.Count -ne 0 } | Select-Object -First 1
161171

162172
if ($null -eq $edgeItem) {
163173
Write-Status 'Failed to parse EdgeUpdate API! No matching artifacts found.' -Level Critical -Exit
164174
}
165175

166-
$hashAlg = $edgeItem.Artifacts.HashAlgorithm | % { if ([string]::IsNullOrEmpty($_)) { 'SHA256' } else { $_ } }
176+
$hashAlg = $edgeItem.Artifacts.HashAlgorithm | ForEach-Object { if ([string]::IsNullOrEmpty($_)) { 'SHA256' } else { $_ } }
167177
foreach ($var in @{
168178
link = $edgeItem.Artifacts.Location
169179
hash = $edgeItem.Artifacts.Hash
@@ -223,7 +233,7 @@ Error: $_" -Level Critical -Exit -ExitCode 6
223233
Write-Status 'Verified the Microsoft Edge installer!' -Level Success
224234
}
225235
else {
226-
Write-Status 'Edge installer hash does not match. The installer might be corrupted. Continuing anyways...' -Level Error
236+
Write-Status 'Edge installer hash does not match. Refusing to continue with an untrusted installer.' -Level Critical -Exit -ExitCode 10
227237
}
228238
}
229239

@@ -280,7 +290,7 @@ Please relaunch this script under a regular admin account." -Level Critical -Exi
280290
else {
281291
if (!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)) {
282292
if ($PSBoundParameters.Count -le 0 -and !$args) {
283-
Start-Process cmd "/c PowerShell -NoP -EP Bypass -File `"$PSCommandPath`"" -Verb RunAs
293+
Start-Process cmd "/c PowerShell -NoP -EP RemoteSigned -File `"$PSCommandPath`"" -Verb RunAs
284294
exit
285295
}
286296
else {
@@ -293,6 +303,7 @@ $edgeInstalled = EdgeInstalled
293303
if (!$UninstallEdge -and !$InstallEdge -and !$InstallWebView) {
294304
$host.UI.RawUI.WindowTitle = "AtlasOS EdgeRemover"
295305

306+
$continue = $false
296307
$RemoveEdgeData = $false
297308
while (!$continue) {
298309
Clear-Host
@@ -346,31 +357,82 @@ To perform an action, also type its number.
346357
Clear-Host
347358
}
348359

349-
# Project originally made by ShadowWhisperer and is licensed under CC0-1.0 License
350-
# https://github.com/ShadowWhisperer/Remove-MS-Edge
351-
# https://api.github.com/repos/ShadowWhisperer/Remove-MS-Edge/contents/Batch/Edge.bat
352360
if ($UninstallEdge) {
353-
Write-Status "Uninstalling Edge Chromium..."
354-
try {
355-
$tempDirectory = Join-Path ([IO.Path]::GetTempPath()) ([IO.Path]::GetRandomFileName())
356-
New-Item -ItemType Directory -Path $tempDirectory | Out-Null
361+
Write-Status 'Uninstalling Edge Chromium...'
362+
KillEdgeProcesses
357363

358-
& curl.exe -LSs "https://raw.githubusercontent.com/ShadowWhisperer/Remove-MS-Edge/main/Batch/Edge.bat" -o "$tempDirectory\Edge.bat"
359-
if (!$?) {
360-
Write-Error "Downloading script failed."
361-
exit 1
364+
$setupCandidates = @()
365+
foreach ($root in @(
366+
"$([Environment]::GetFolderPath('ProgramFilesx86'))\Microsoft\Edge\Application",
367+
"$([Environment]::GetFolderPath('ProgramFiles'))\Microsoft\Edge\Application"
368+
)) {
369+
if (Test-Path $root) {
370+
$setupCandidates += Get-ChildItem -Path $root -Filter 'setup.exe' -Recurse -ErrorAction SilentlyContinue
362371
}
372+
}
373+
374+
$setupCandidates = @($setupCandidates | Sort-Object -Property FullName -Unique)
375+
if ($setupCandidates.Count -gt 0) {
376+
foreach ($setup in $setupCandidates) {
377+
Write-Status "Running uninstaller at '$($setup.FullName)'..."
378+
$process = Start-Process -FilePath $setup.FullName -ArgumentList '--uninstall --msedge --system-level --verbose-logging --force-uninstall' -WindowStyle Hidden -Wait -PassThru
379+
if (($process.ExitCode -eq 0) -or (-not (EdgeInstalled))) {
380+
break
381+
}
363382

364-
Start-Process -FilePath "$tempDirectory\Edge.bat" -WindowStyle Hidden -Wait -ArgumentList '-auto'
365-
Write-Output "Successfully removed Microsoft Edge..."
366-
Write-Output "Press any key to exit"
367-
Read-Host
368-
exit
383+
Write-Status "Edge uninstaller exited with code $($process.ExitCode); trying fallback methods." -Level Info
384+
}
369385
}
370-
catch {
371-
Write-Warning "An error occurred: $_"
372-
return $false
386+
elseif (EdgeInstalled) {
387+
Write-Status 'Could not locate a local Edge installer to perform uninstallation.' -Level Warning
373388
}
389+
390+
KillEdgeProcesses
391+
if (EdgeInstalled) {
392+
$legacyRemoved = $false
393+
try {
394+
$legacyTempDirectory = Join-Path ([IO.Path]::GetTempPath()) ([IO.Path]::GetRandomFileName())
395+
New-Item -ItemType Directory -Path $legacyTempDirectory -Force | Out-Null
396+
$legacyScript = Join-Path $legacyTempDirectory 'Edge.bat'
397+
398+
# Project originally made by ShadowWhisperer and licensed under CC0-1.0.
399+
# https://github.com/ShadowWhisperer/Remove-MS-Edge
400+
Write-Status 'Trying legacy Edge removal fallback...'
401+
if ($null -ne (Get-Command curl.exe -ErrorAction SilentlyContinue)) {
402+
& curl.exe -LSs "https://raw.githubusercontent.com/ShadowWhisperer/Remove-MS-Edge/main/Batch/Edge.bat" -o "$legacyScript"
403+
}
404+
else {
405+
Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/ShadowWhisperer/Remove-MS-Edge/main/Batch/Edge.bat' -OutFile $legacyScript -UseBasicParsing -ErrorAction Stop
406+
}
407+
408+
if (Test-Path $legacyScript) {
409+
Start-Process -FilePath $legacyScript -WindowStyle Hidden -Wait -ArgumentList '-auto' | Out-Null
410+
KillEdgeProcesses
411+
$legacyRemoved = -not (EdgeInstalled)
412+
}
413+
}
414+
catch {
415+
if (EdgeInstalled) {
416+
Write-Status "Legacy fallback failed: $($_.Exception.Message)" -Level Warning
417+
}
418+
}
419+
420+
if ((-not $legacyRemoved) -and (EdgeInstalled)) {
421+
if ($KeepAppX -or $NonInteractive) {
422+
Write-Status 'Edge binaries were not fully removed. Continuing so playbook cleanup can finish.' -Level Warning
423+
}
424+
else {
425+
Write-Status 'Failed to uninstall Microsoft Edge using all available removal methods.' -Level Critical -Exit -ExitCode 12
426+
}
427+
}
428+
else {
429+
Write-Status 'Successfully removed Microsoft Edge.' -Level Success
430+
}
431+
}
432+
else {
433+
Write-Status 'Edge is already uninstalled.' -Level Success
434+
}
435+
374436
Write-Output ""
375437
}
376438

0 commit comments

Comments
 (0)