Repository navigation
fix(upgrade): verify the release archive before replacing the binary - #335
Conversation
The UI asset download already verifies its tarball against the release checksums file, but the fetch, parse, and hashing helpers were private to `internal/uidist`, so a second caller had nothing to reuse: `internal/version` builds the checksums URL and `internal/utils` already hashes files. Move the parser and the fetcher next to the URL they consume, keep the same messages, and drop uidist's private SHA-256 duplicate in favour of `utils.FileHash`. No behavior change.
`skillshare upgrade --cli` streamed the release archive straight into the extractor and renamed the result over the live executable, so an archive that did not match the release was installed anyway. The UI asset download in the same product has always refused on a mismatch, and the release workflow checks every asset against checksums.txt before publishing, so the published hashes were the one integrity signal this path did not consult. Stage the archive to a temp file, compare its SHA-256 with the entry for the downloaded asset name, and extract only when they agree. A mismatch, an entry for a different file, or an unreachable checksums.txt stops the upgrade and leaves the running binary untouched. This guards against damaged or mismatched downloads rather than a release whose assets and checksums were replaced together. Tests use a local server and build the archive each platform extracts, so all four outcomes are covered without network access. The sudo child re-enters this same function, so elevated upgrades are covered too.
| if err != nil { | ||
| os.Remove(tmp.Name()) | ||
| return "", err | ||
| } | ||
| if n > maxArchiveSize { | ||
| os.Remove(tmp.Name()) | ||
| return "", fmt.Errorf("archive exceeds the maximum size (%d MB)", maxArchiveSize/(1024*1024)) |
There was a problem hiding this comment.
Minor (Windows only): in both error branches os.Remove(tmp.Name()) runs while tmp is still open (defer tmp.Close() hasn't fired yet). On Windows, removing an open file fails, so an interrupted or oversized download can leave up to ~100 MB in %TEMP%. Checksum verification is unaffected.
Closing before removing fixes it:
if err != nil {
tmp.Close()
os.Remove(tmp.Name())
return "", err
}
if n > maxArchiveSize {
tmp.Close()
os.Remove(tmp.Name())
return "", fmt.Errorf(...)
}The same pattern exists in downloadToTemp in internal/uidist/uidist.go, so it could be fixed there too (here or in a follow-up).
There was a problem hiding this comment.
Fixed in follow-up #340 — both downloadArchive and uidist.downloadToTemp now close the temp file before removing it on error.
runkids
left a comment
There was a problem hiding this comment.
Thanks — checksum verification looks right and CI is green. The Windows temp-file cleanup note will be handled in a follow-up PR.
Both release downloaders removed the temp file while it was still open, relying on the deferred Close. Windows refuses to delete an open file, so an interrupted or oversized download could leave up to ~100 MB behind in %TEMP%. Closing first makes the cleanup work on every platform. Raised in review of #335; downloadToTemp in uidist had the same pattern.
What
skillshare upgrade --clinow checks the downloaded release archive against therelease's
checksums.txtbefore it replaces the binary, which is what the UI assetdownload in the same command has always done. Details and evidence are in #333.
Two commits:
refactor(version): own the release checksum helpers— movesfetchChecksum/parseChecksumfrominternal/uidistintointernal/version(next to theBuildChecksumsURLthey consume), keeps their messages, and drops uidist's privateSHA-256 helper for the existing
utils.FileHash. No behavior change.fix(upgrade): verify the release archive before replacing the binary— the actual fix.Behavior
downloadAndReplace(downloadURL, checksumsURL, destPath, onProgress)stages the archiveto a temp file, hashes it, and extracts only on a match:
checksum mismatch: expected …, got …, binary untouchedchecksums.txtchecksum for … not found in checksums.txt, untouchedchecksums.txtunreachablechecksums.txt returned HTTP …, untouchedThe asset name is
path.Base(downloadURL), which is the exact name GoReleaser writes inchecksums.txt(checked againstv0.23.4's file, and the asset lists ofv0.1.0,v0.10.0,v0.21.0,v0.23.0). Nothing moves before the check, so the Windowsexe → exe.old → exerename dance never runs for a rejected archive. The sudo pathre-enters the same function via
--replace-binary=<ver>, so elevated upgrades arecovered too.
Tests
cmd/skillshare/upgrade_checksum_test.gobuilds the archive each platform actuallyextracts (zip with
skillshare.exeon Windows, tar.gz withskillshareelsewhere) andserves both endpoints from an
httptestserver, so the four rows above are coveredwithout network access. Each failure case asserts the previous binary is still there,
not just that an error came back.
A throwaway version of the mismatch case run against stock
mainwas the repro in #333:err = <nil>, and the destination content became the tampered payload. That file is notin the branch.
The unit suite was run in a fresh Linux clone (ext4). Two caveats about my environment
rather than the change:
TestEnsureProjectConfig_NoProjectStillInitializesblocks onBubble Tea input without a TTY, so it was skipped (
-skip) — it hangs identically onstock
main; and in agit worktreecheckout,internal/git,internal/hooks,internal/install, andinternal/serverfail on an unresolvable Windowsgitdir:path,identically before and after the change. In the plain clone both problems are absent
except the TTY one.
Deliberately not included
checksums.txtships from the samerelease over the same TLS connection, so this cannot stop a release whose assets and
checksums were replaced together. It costs ~20 lines to catch the mundane cases
(damaged transfer, wrong asset from a cache or mirror, wrong platform build) and to
make the two download paths in one command agree. If signed releases are wanted, that
is a separate decision.
uidist.downloadToTempstays separate from the newdownloadArchive. Same shape,but different size cap, timeout, and temp prefix; folding them would change UI-dist
behavior that this fix has no reason to touch.
brew, unchanged.Docs
website/docs/reference/commands/upgrade.mdgains the verification under CLI Binary,mirrored into the
ja,ko,zh-Hans, andzh-Hantcopies that track this page.No flag changed, so completions and
--helpare unaffected.Closes #333