|
28 | 28 | $tempDir = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath "Tukui_$($Addon.Slug)_Update_$([guid]::NewGuid().ToString('N'))" |
29 | 29 | try { |
30 | 30 | if (Test-Path $tempDir) { |
31 | | - Remove-Item $tempDir -Recurse -Force |
| 31 | + Remove-Item $tempDir -Recurse -Force -ErrorAction Stop |
32 | 32 | } |
33 | | - $null = New-Item -ItemType Directory -Path $tempDir |
| 33 | + $null = New-Item -ItemType Directory -Path $tempDir -ErrorAction Stop |
34 | 34 |
|
35 | 35 | # Download |
36 | 36 | $zipPath = Join-Path -Path $tempDir -ChildPath "$($Addon.Slug)-$($Addon.Version).zip" |
37 | 37 | Write-Verbose "Downloading $($Addon.Name) $($Addon.Version) ..." |
38 | | - Invoke-WebRequest -Uri $Addon.DownloadUrl -OutFile $zipPath |
| 38 | + Invoke-WebRequest -Uri $Addon.DownloadUrl -OutFile $zipPath -ErrorAction Stop |
39 | 39 |
|
40 | 40 | # Extract |
41 | 41 | $extractPath = Join-Path -Path $tempDir -ChildPath 'extracted' |
42 | 42 | Write-Verbose 'Extracting...' |
43 | | - Expand-Archive -Path $zipPath -DestinationPath $extractPath -Force |
| 43 | + Expand-Archive -Path $zipPath -DestinationPath $extractPath -Force -ErrorAction Stop |
44 | 44 |
|
45 | 45 | # Remove old addon folders matching the known directory names |
| 46 | + $normalizedAddOnsPath = [System.IO.Path]::GetFullPath($AddOnsPath) |
| 47 | + $normalizedAddOnsPath = $normalizedAddOnsPath.TrimEnd([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar) + [System.IO.Path]::DirectorySeparatorChar |
46 | 48 | foreach ($dir in $Addon.Directories) { |
| 49 | + if ([string]::IsNullOrWhiteSpace($dir) -or $dir.Contains('\') -or $dir.Contains('/') -or $dir.Contains('..')) { |
| 50 | + throw "Invalid addon directory entry '$dir' returned by API." |
| 51 | + } |
| 52 | + |
47 | 53 | $oldPath = Join-Path -Path $AddOnsPath -ChildPath $dir |
48 | | - if (Test-Path $oldPath) { |
| 54 | + $resolvedOldPath = [System.IO.Path]::GetFullPath($oldPath) |
| 55 | + if (-not $resolvedOldPath.StartsWith($normalizedAddOnsPath, [System.StringComparison]::OrdinalIgnoreCase)) { |
| 56 | + throw "Resolved addon directory path '$resolvedOldPath' is outside the AddOns directory." |
| 57 | + } |
| 58 | + |
| 59 | + if (Test-Path -LiteralPath $resolvedOldPath) { |
49 | 60 | Write-Verbose " Removing $dir" |
50 | | - Remove-Item $oldPath -Recurse -Force |
| 61 | + Remove-Item -LiteralPath $resolvedOldPath -Recurse -Force -ErrorAction Stop |
51 | 62 | } |
52 | 63 | } |
53 | 64 |
|
54 | 65 | # Copy new folders |
55 | | - $extractedFolders = Get-ChildItem -Path $extractPath -Directory |
| 66 | + $extractedFolders = Get-ChildItem -Path $extractPath -Directory -ErrorAction Stop |
56 | 67 | foreach ($folder in $extractedFolders) { |
57 | 68 | $destination = Join-Path -Path $AddOnsPath -ChildPath $folder.Name |
58 | 69 | Write-Verbose " Installing $($folder.Name)" |
59 | | - Copy-Item -Path $folder.FullName -Destination $destination -Recurse -Force |
| 70 | + Copy-Item -Path $folder.FullName -Destination $destination -Recurse -Force -ErrorAction Stop |
60 | 71 | } |
61 | 72 |
|
62 | 73 | Write-Verbose "$($Addon.Name) $($Addon.Version) installed successfully!" |
|
0 commit comments