Skip to content

Commit d22e374

Browse files
fix(install): address checksum-verification review
- install.ps1: decode a Byte[] checksums.txt body so verification works on Windows PowerShell 5.1. GitHub serves release assets as octet-stream, so on PS 5.1 Invoke-WebRequest returns .Content as Byte[]; it coerced to a decimal string and every check silently soft-skipped (sahrizvi, P1). - install + install.ps1: pin the archive and checksums.txt to the resolved release tag instead of the mutable latest/ URL, so a release published mid-install can't hand back mismatched assets and trigger a spurious hard-fail (cubic, P2). Falls back to latest/ only when the version can't be resolved. - install: in verify_checksum, clean up via $(dirname "$file") rather than the caller's dynamically-scoped $tmp_dir local — self-contained (cubic, P2). - tests: Pester coverage for Test-Checksum (String + Byte[] + mismatch paths, verified to fail without the decode) and TS guards for the decode and same-release pinning. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_019M7GkS3bYZaFhEbBhVTecG
1 parent bb09151 commit d22e374

4 files changed

Lines changed: 121 additions & 4 deletions

File tree

‎install‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,17 @@ else
204204
fi
205205

206206
if [ -z "$requested_version" ]; then
207-
url="https://github.com/AltimateAI/altimate-code/releases/latest/download/$filename"
208207
specific_version=$(curl -s https://api.github.com/repos/AltimateAI/altimate-code/releases/latest | sed -n 's/.*"tag_name": *"v\([^"]*\)".*/\1/p')
209208

210209
if [[ $? -ne 0 || -z "$specific_version" ]]; then
211210
echo -e "${RED}Failed to fetch version information${NC}"
212211
exit 1
213212
fi
213+
# Pin to the resolved tag so the archive and checksums.txt come from the
214+
# same release. The mutable latest/ URL would fetch them in separate
215+
# requests, so a release published mid-install could mix the two and
216+
# trigger a spurious checksum hard-fail.
217+
url="https://github.com/AltimateAI/altimate-code/releases/download/v${specific_version}/$filename"
214218
else
215219
# Strip leading 'v' if present
216220
requested_version="${requested_version#v}"
@@ -395,7 +399,10 @@ verify_checksum() {
395399
print_message error "Checksum mismatch for $name"
396400
print_message error " expected: $expected"
397401
print_message error " actual: $actual"
398-
rm -rf "$tmp_dir"
402+
# Clean up via the file's own directory rather than the caller's $tmp_dir,
403+
# so this stays self-contained and doesn't depend on a dynamically-scoped
404+
# local from download_and_install.
405+
rm -rf "$(dirname "$file")"
399406
exit 1
400407
fi
401408
print_message info "${MUTED}Verified ${NC}$name${MUTED} (sha256)${NC}"

‎install.ps1‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,17 @@ function Test-Checksum {
8686

8787
$sums = $null
8888
try {
89-
$sums = (Invoke-WebRequest -Uri $ChecksumsUrl -UseBasicParsing).Content
89+
$resp = Invoke-WebRequest -Uri $ChecksumsUrl -UseBasicParsing
90+
# On Windows PowerShell 5.1, .Content is a Byte[] (not a String) whenever the
91+
# response isn't a text-recognized content-type — and GitHub serves release
92+
# assets as application/octet-stream. A raw Byte[] coerces to a "49 50 51 …"
93+
# decimal string when split, so verification would silently soft-skip on the
94+
# default Windows shell. Decode the bytes explicitly to recover real text.
95+
if ($resp.Content -is [byte[]]) {
96+
$sums = [System.Text.Encoding]::UTF8.GetString($resp.Content)
97+
} else {
98+
$sums = $resp.Content
99+
}
90100
} catch {
91101
Write-Muted "Skipping integrity check — checksums.txt not published for this release"
92102
return
@@ -200,7 +210,13 @@ function Install-Target {
200210
if ($Baseline) { $target = "$target-baseline" }
201211
$filename = "$App-$target.zip"
202212

203-
if ($useLatest) {
213+
# Pin BOTH the archive and checksums.txt to the same resolved release. The
214+
# mutable releases/latest/download URL would fetch the two assets in separate
215+
# requests, so a release published mid-install could hand back an archive from
216+
# one release and checksums from another → a spurious hard-fail. We resolve
217+
# the concrete tag up front ($specificVersion), so pin to it. Only fall back
218+
# to the mutable latest/ URL when the version genuinely couldn't be resolved.
219+
if ($useLatest -and -not $specificVersion) {
204220
$base = "https://github.com/AltimateAI/altimate-code/releases/latest/download"
205221
} else {
206222
$base = "https://github.com/AltimateAI/altimate-code/releases/download/v$specificVersion"

‎packages/opencode/test/install/checksum-verification.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,29 @@ describe("PowerShell installer verifies checksums", () => {
4646
// Expand-Archive mention in the top-of-file ProgressPreference comment).
4747
expect(PS1.indexOf("Test-Checksum -Path")).toBeLessThan(PS1.indexOf("Expand-Archive -Path"))
4848
})
49+
50+
test("decodes a Byte[] checksums.txt body (Windows PowerShell 5.1)", () => {
51+
// GitHub serves release assets as octet-stream, so PS 5.1 returns .Content
52+
// as Byte[]; without an explicit decode it coerces to decimal text and the
53+
// check silently soft-skips. See test/windows/install.Tests.ps1 for the
54+
// behavioral guard.
55+
expect(PS1).toContain("-is [byte[]]")
56+
expect(PS1).toContain("[System.Text.Encoding]::UTF8.GetString")
57+
})
58+
})
59+
60+
describe("archive and checksums come from the same release (no latest/ race)", () => {
61+
test("bash pins the download to the resolved tag", () => {
62+
// The archive URL is built from the resolved $specific_version, and the
63+
// checksums URL is derived from it (${url%/*}) — so both always resolve to
64+
// the same release even if a new one publishes mid-install.
65+
expect(BASH).toContain('releases/download/v${specific_version}/$filename')
66+
expect(BASH).toContain('checksums_url="${url%/*}/checksums.txt"')
67+
})
68+
69+
test("PowerShell derives both URLs from one pinned base", () => {
70+
expect(PS1).toContain('$url = "$base/$filename"')
71+
expect(PS1).toContain('$checksumsUrl = "$base/checksums.txt"')
72+
expect(PS1).toContain('$base = "https://github.com/AltimateAI/altimate-code/releases/download/v$specificVersion"')
73+
})
4974
})

‎test/windows/install.Tests.ps1‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,72 @@ Describe "install.ps1 version handling" {
9696
$r.Output | Should -Match "Available releases"
9797
}
9898
}
99+
100+
Describe "install.ps1 Test-Checksum" {
101+
# Exercise the real Test-Checksum function in isolation. install.ps1 runs
102+
# top-to-bottom (arch detection, version resolution, exit) so it can't just be
103+
# dot-sourced; instead extract the function via the AST and define it here,
104+
# alongside a recording Write-Muted stub and a fake Invoke-WebRequest that
105+
# returns canned content.
106+
BeforeAll {
107+
$src = Get-Content -Raw $script:InstallScript
108+
$tokens = $null; $errors = $null
109+
$ast = [System.Management.Automation.Language.Parser]::ParseInput($src, [ref]$tokens, [ref]$errors)
110+
$def = $ast.Find({
111+
param($n)
112+
$n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq "Test-Checksum"
113+
}, $true)
114+
if (-not $def) { throw "Test-Checksum not found in install.ps1" }
115+
. ([ScriptBlock]::Create($def.Extent.Text))
116+
117+
# Records what Test-Checksum reports, so we can tell a real "Verified" from a
118+
# silent "Skipping integrity check" soft-skip.
119+
$script:Muted = [System.Collections.Generic.List[string]]::new()
120+
function Write-Muted { param([string]$Message) $script:Muted.Add($Message) }
121+
122+
# Fake Invoke-WebRequest: a function shadows the cmdlet, returning whatever
123+
# $script:FakeContent is set to (string or Byte[]) as .Content.
124+
function Invoke-WebRequest { param($Uri, [switch]$UseBasicParsing) [pscustomobject]@{ Content = $script:FakeContent } }
125+
126+
function New-FixtureArchive {
127+
$tmp = New-TemporaryFile
128+
"altimate-archive-fixture" | Set-Content -NoNewline -Path $tmp
129+
return $tmp
130+
}
131+
}
132+
133+
BeforeEach { $script:Muted.Clear() }
134+
135+
It "verifies a matching archive when checksums.txt is served as a String (PS 7)" {
136+
$tmp = New-FixtureArchive
137+
$name = Split-Path $tmp -Leaf
138+
$hash = (Get-FileHash -Path $tmp -Algorithm SHA256).Hash.ToLower()
139+
$script:FakeContent = "$hash $name`n"
140+
{ Test-Checksum -Path $tmp -Name $name -ChecksumsUrl "https://x/checksums.txt" } | Should -Not -Throw
141+
($script:Muted -join "`n") | Should -Match "Verified"
142+
($script:Muted -join "`n") | Should -Not -Match "Skipping"
143+
Remove-Item $tmp -Force
144+
}
145+
146+
It "verifies a matching archive when checksums.txt is served as Byte[] (Windows PowerShell 5.1)" {
147+
# The regression guard: GitHub serves release assets as octet-stream, so on
148+
# PS 5.1 .Content is a Byte[]. Without the explicit UTF8 decode it coerces to
149+
# a "49 50 51 …" decimal string, no entry matches, and the check soft-skips.
150+
$tmp = New-FixtureArchive
151+
$name = Split-Path $tmp -Leaf
152+
$hash = (Get-FileHash -Path $tmp -Algorithm SHA256).Hash.ToLower()
153+
$script:FakeContent = [System.Text.Encoding]::UTF8.GetBytes("$hash $name`n")
154+
{ Test-Checksum -Path $tmp -Name $name -ChecksumsUrl "https://x/checksums.txt" } | Should -Not -Throw
155+
($script:Muted -join "`n") | Should -Match "Verified"
156+
($script:Muted -join "`n") | Should -Not -Match "Skipping"
157+
Remove-Item $tmp -Force
158+
}
159+
160+
It "hard-fails on a real checksum mismatch (Byte[] content)" {
161+
$tmp = New-FixtureArchive
162+
$name = Split-Path $tmp -Leaf
163+
$script:FakeContent = [System.Text.Encoding]::UTF8.GetBytes((("0" * 64) + " $name`n"))
164+
{ Test-Checksum -Path $tmp -Name $name -ChecksumsUrl "https://x/checksums.txt" } | Should -Throw
165+
Remove-Item $tmp -Force
166+
}
167+
}

0 commit comments

Comments
 (0)