Repository navigation
Expand file tree
/
Copy pathUpdate-ReverseThis.ps1
More file actions
207 lines (202 loc) · 13.3 KB
/
Copy pathUpdate-ReverseThis.ps1
File metadata and controls
207 lines (202 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#Requires -Version 5.1
<# Install or update a released workbench's public source from its official digest-verified archive.
Private sandbox files/settings stay in place; retained backups and errors live under sandbox/backups and sandbox/log.
Git checkouts use their normal reviewed Git workflow instead. Plan/Offline/NoUpdate write and download nothing. #>
[CmdletBinding()]
param([switch]$Plan, [switch]$Offline, [switch]$NoUpdate, [switch]$DebugLog, [string]$Archive, [string]$Sha256,
[Alias('KitRoot')][string]$Destination)
$ErrorActionPreference = 'Stop'
# Windows PowerShell 5.1 leaves $PSScriptRoot empty in an advanced script's param() defaults.
if (-not $Destination) { $Destination = $PSScriptRoot }
# Failures and version transitions remain local; detailed paths are opt-in.
function Write-SourceUpdateLog {
param([string]$Message, [switch]$Detail)
if (-not $sourceLog -or ($Detail -and -not $DebugLog)) { return }
Add-Content -LiteralPath $sourceLog -Value ((Get-Date -Format 's') + ' ' + $Message) -Encoding UTF8
}
# Only repository source paths are eligible; release manifests never authorize private or Git writes.
function Test-PublicSourcePath {
param([string]$Path)
return $Path -match '^(?:[A-Za-z][A-Za-z0-9.-]*\.(?:ps1|cmd|md|txt|json)|VERSION|LICENSE|\.(?:gitattributes|gitignore)|(?:catalog|docs|tests|kit|scripts|encyclopedia|skills|\.github)/[^:]+)$' -and
$Path -notmatch '(^|/)\.\.(/|$)|\\|(^|/)\.git(/|$)'
}
# Existing links anywhere above a write could redirect an otherwise valid source path.
function Assert-SourceWritePath {
param([string]$Path)
$probe = $Path
while ($probe) {
if ((Test-Path -LiteralPath $probe) -and ((Get-Item -LiteralPath $probe -Force).Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw "Linked source destination refused: $Path" }
$parent = Split-Path -Parent $probe
if ($parent -eq $probe) { break }
$probe = $parent
}
}
# Check paths, symlinks, file hashes and version before any installed source file is touched.
function Read-SourceRelease {
param([string]$Directory)
$manifest = Get-Content -LiteralPath (Join-Path $Directory 'RELEASE.json') -Raw -Encoding UTF8 | ConvertFrom-Json
if ($manifest.product -ne 'Reverse THIS!' -or $manifest.version -notmatch '^\d+\.\d+\.\d+$' -or
$manifest.commit -notmatch '^[a-f0-9]{40}$' -or -not $manifest.files) { throw 'Invalid source release receipt.' }
$seen = @{}
foreach ($file in $manifest.files) {
# Old release receipts include three harmless sandbox documentation placeholders; never deploy them.
if ($file.path -in @('sandbox/README.md','sandbox/manual/README.md','sandbox/work/.gitkeep')) { continue }
if (-not (Test-PublicSourcePath $file.path) -or $seen.ContainsKey($file.path) -or $file.sha256 -notmatch '^[a-f0-9]{64}$') { throw "Unsafe/duplicate source receipt path: $($file.path)" }
$seen[$file.path] = $true
$path = Join-Path $Directory $file.path
Assert-SourceWritePath $path
if (-not (Test-Path -LiteralPath $path -PathType Leaf) -or (Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash -ne $file.sha256) { throw "Source receipt mismatch: $($file.path)" }
}
foreach ($required in @('Install-Modkit.ps1','VERSION','catalog/tools.json')) {
if (-not $seen.ContainsKey($required)) { throw "Source receipt omitted $required" }
}
if ((Get-Content -LiteralPath (Join-Path $Directory 'VERSION') -Raw).Trim() -ne $manifest.version) { throw 'Source VERSION disagrees with its receipt.' }
return $manifest
}
if ($Plan -or $Offline -or $NoUpdate) {
Write-Host "Source update disabled/read-only. Owner: $Destination; official release: https://github.com/SenjuWoo/reverse-this/releases/latest"
exit 0
}
$updateRoot = [IO.Path]::GetFullPath($Destination).TrimEnd('\')
if (-not [IO.Path]::IsPathRooted($Destination) -or $updateRoot -eq [IO.Path]::GetPathRoot($updateRoot).TrimEnd('\')) { throw 'Destination must be an absolute non-root directory.' }
$fresh = -not (Test-Path -LiteralPath (Join-Path $updateRoot 'VERSION') -PathType Leaf)
if ($fresh -and (Test-Path -LiteralPath $updateRoot) -and @(Get-ChildItem -LiteralPath $updateRoot -Force).Count) { throw 'Nonempty unowned destination preserved; select an empty destination or an existing released workbench.' }
$currentVersion = if ($fresh) { '0.0.0' } else { (Get-Content -LiteralPath (Join-Path $updateRoot 'VERSION') -Raw).Trim() }
$ancestor = $updateRoot
while ($ancestor) {
if (Test-Path -LiteralPath (Join-Path $ancestor '.git')) { throw 'Git development checkout preserved. Fetch/review and fast-forward through Git instead of the release updater.' }
$parent = Split-Path -Parent $ancestor
if ($parent -eq $ancestor) { break }
$ancestor = $parent
}
Assert-SourceWritePath $updateRoot
$logDirectory = Join-Path $updateRoot 'sandbox\log'
Assert-SourceWritePath $logDirectory
$sourceLog = $null
if (-not $fresh) {
New-Item -ItemType Directory -Force -Path $logDirectory | Out-Null
$sourceLog = Join-Path $logDirectory 'source-update.log'
Write-SourceUpdateLog ('source update from ' + $currentVersion)
}
$temp = Join-Path ([IO.Path]::GetTempPath()) ('reverse-this-update-' + [guid]::NewGuid().ToString('N'))
$lock = $null
$changed = @()
$backup = $null
try {
New-Item -ItemType Directory -Path $temp | Out-Null
if (-not $Archive) {
$release = Invoke-RestMethod -Uri 'https://api.github.com/repos/SenjuWoo/reverse-this/releases/latest' -Headers @{ 'User-Agent'='ReverseTHIS-Updater' } -UseBasicParsing
if ($release.tag_name -notmatch '^v\d+\.\d+\.\d+$') { throw 'Invalid official release version.' }
if ([version]$release.tag_name.TrimStart('v') -le [version]$currentVersion) { Write-Host 'Workbench source is current or newer.'; exit 0 }
$assets = @($release.assets | Where-Object { $_.name -eq ('Reverse-This-' + $release.tag_name.TrimStart('v') + '.zip') })
if ($assets.Count -ne 1 -or $assets[0].digest -notmatch '^sha256:([a-f0-9]{64})$') { throw 'Official source archive lacks a unique SHA-256 asset digest.' }
$Sha256 = $Matches[1]
$url = [string]$assets[0].browser_download_url
if ($url -notmatch '^https://github\.com/SenjuWoo/reverse-this/releases/download/v\d+\.\d+\.\d+/Reverse-This-\d+\.\d+\.\d+\.zip$') { throw 'Unexpected official archive URL.' }
$Archive = Join-Path $temp 'release.zip'
Invoke-WebRequest -Uri $url -OutFile $Archive -UseBasicParsing
}
if ($Sha256 -notmatch '^[a-fA-F0-9]{64}$' -or (Get-FileHash -LiteralPath $Archive -Algorithm SHA256).Hash -ne $Sha256) { throw 'Source archive SHA-256 mismatch or missing expected digest.' }
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip = [IO.Compression.ZipFile]::OpenRead($Archive)
$unpack = Join-Path $temp 'source'
try {
foreach ($entry in $zip.Entries) {
$path = $entry.FullName.Replace('\','/')
if ($path -match '(^|/)\.\.(/|$)|^/|:' -or (($entry.ExternalAttributes -shr 16) -band 0xF000) -eq 0xA000) { throw 'Unsafe source archive entry.' }
}
[IO.Compression.ZipFileExtensions]::ExtractToDirectory($zip, $unpack)
} finally { $zip.Dispose() }
if (-not (Test-Path -LiteralPath (Join-Path $unpack 'RELEASE.json'))) {
$children = @(Get-ChildItem -LiteralPath $unpack -Directory)
if ($children.Count -ne 1) { throw 'Expected one source release root.' }
$unpack = $children[0].FullName
}
# The verified release applies itself: its own updater knows paths this older copy may reject
# (1.16 refused the skills/ folder added in 1.17). One hop only, guarded against recursion.
$incomingUpdater = Join-Path $unpack 'Update-ReverseThis.ps1'
if (-not $env:REVERSE_THIS_UPDATER_HANDOFF -and $PSCommandPath -and (Test-Path -LiteralPath $incomingUpdater -PathType Leaf) -and
(Get-FileHash -LiteralPath $incomingUpdater -Algorithm SHA256).Hash -ne (Get-FileHash -LiteralPath $PSCommandPath -Algorithm SHA256).Hash) {
Write-SourceUpdateLog 'handing off to the incoming release updater'
$env:REVERSE_THIS_UPDATER_HANDOFF = '1'
try {
$handoff = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $incomingUpdater, '-Destination', $updateRoot, '-Archive', $Archive, '-Sha256', $Sha256)
if ($DebugLog) { $handoff += '-DebugLog' }
& (Get-Process -Id $PID).Path @handoff
$handoffExit = $LASTEXITCODE
} finally { Remove-Item Env:\REVERSE_THIS_UPDATER_HANDOFF -ErrorAction SilentlyContinue }
exit $handoffExit
}
$manifest = Read-SourceRelease $unpack
if ([version]$manifest.version -le [version]$currentVersion) { Write-Host 'Workbench source is current or newer.'; exit 0 }
New-Item -ItemType Directory -Force -Path $logDirectory | Out-Null
$sourceLog = Join-Path $logDirectory 'source-update.log'
Write-SourceUpdateLog ('source update from ' + $currentVersion)
Write-SourceUpdateLog -Detail "destination=$updateRoot release=$($manifest.version) commit=$($manifest.commit) files=$(@($manifest.files).Count)"
$sandbox = Join-Path $updateRoot 'sandbox'
New-Item -ItemType Directory -Force -Path $sandbox | Out-Null
$lock = [IO.File]::Open((Join-Path $sandbox '.install.lock'), 'OpenOrCreate', 'ReadWrite', 'None')
# Only unchanged release-owned files may be replaced; new release paths must not collide with personal files.
$previousPaths = @()
if (-not $fresh) {
try { $previous = Read-SourceRelease $updateRoot }
catch { throw "Installed source was changed or its release receipt is invalid; preserved: $($_.Exception.Message)" }
if ([version]$manifest.version -le [version]$previous.version) { Write-Host 'Workbench source advanced during download; current or newer source preserved.'; exit 0 }
$previousPaths = @($previous.files | ForEach-Object path)
}
foreach ($file in $manifest.files) {
if ((Test-PublicSourcePath $file.path) -and $file.path -notin $previousPaths -and
(Test-Path -LiteralPath (Join-Path $updateRoot $file.path))) { throw "New release path collides with a personal file; preserved: $($file.path)" }
}
$backup = Join-Path $sandbox ('backups/source-' + $manifest.version + '-' + [guid]::NewGuid().ToString('N'))
Assert-SourceWritePath $backup
New-Item -ItemType Directory -Path $backup -Force | Out-Null
$incomingPaths = @($manifest.files | ForEach-Object path)
foreach ($relative in $previousPaths) {
if ($relative -in $incomingPaths -or -not (Test-PublicSourcePath $relative)) { continue }
$target = Join-Path $updateRoot $relative
Assert-SourceWritePath $target
$old = Join-Path $backup $relative
New-Item -ItemType Directory -Force -Path (Split-Path -Parent $old) | Out-Null
Copy-Item -LiteralPath $target -Destination $old
$changed += [pscustomobject]@{ path=$target; backup=$old; existed=$true }
Remove-Item -LiteralPath $target -Force
}
$paths = @($manifest.files | Where-Object { Test-PublicSourcePath $_.path } | ForEach-Object path) + @('RELEASE.json')
foreach ($relative in $paths) {
$target = Join-Path $updateRoot $relative
$directory = Split-Path -Parent $target
Assert-SourceWritePath $target
$old = Join-Path $backup $relative
$existed = Test-Path -LiteralPath $target
if ($existed) { New-Item -ItemType Directory -Force -Path (Split-Path -Parent $old) | Out-Null; Copy-Item -LiteralPath $target -Destination $old }
$changed += [pscustomobject]@{ path=$target; backup=$old; existed=$existed }
New-Item -ItemType Directory -Force -Path $directory | Out-Null
$pending = Join-Path $directory ('.reverse-update-' + [guid]::NewGuid().ToString('N'))
try { Copy-Item -LiteralPath (Join-Path $unpack $relative) -Destination $pending; Move-Item -LiteralPath $pending -Destination $target -Force }
finally { if (Test-Path -LiteralPath $pending) { Remove-Item -LiteralPath $pending -Force } }
}
Write-Host "Updated source to $($manifest.version). Previous files retained: $backup. Run Install-Modkit.ps1 to update selected tools."
Write-SourceUpdateLog "source updated to $($manifest.version); rollback=$backup"
} catch {
$failure = $_
$rollbackErrors = @()
foreach ($entry in @($changed | Sort-Object { $_.path.Length } -Descending)) {
try {
if ($entry.existed) { Copy-Item -LiteralPath $entry.backup -Destination $entry.path -Force }
elseif (Test-Path -LiteralPath $entry.path) { Remove-Item -LiteralPath $entry.path -Force }
} catch { $rollbackErrors += $_.Exception.Message }
}
Write-SourceUpdateLog "FAIL $($failure.Exception.Message)"
if ($rollbackErrors.Count) { throw "Source update failed: $($failure.Exception.Message). Rollback incomplete: $($rollbackErrors -join '; ')" }
throw $failure
} finally {
if ($lock) { $lock.Dispose() }
if (Test-Path -LiteralPath $temp) {
$resolved = [IO.Path]::GetFullPath($temp)
if ((Split-Path -Parent $resolved).TrimEnd('\') -ne ([IO.Path]::GetTempPath()).TrimEnd('\') -or
(Split-Path -Leaf $resolved) -notmatch '^reverse-this-update-[a-f0-9]{32}$' -or
((Get-Item -LiteralPath $resolved -Force).Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw 'Unsafe source-update temporary cleanup refused.' }
Remove-Item -LiteralPath $resolved -Recurse -Force
}
}