Skip to content

fix(upgrade): verify the release archive before replacing the binary - #335

Merged
runkids merged 5 commits into
runkids:mainfrom
star-nebula:fix/upgrade-checksum-verify
Oct 2, 2026
Merged

runkids merged 5 commits into
runkids:mainfrom
star-nebula:fix/upgrade-checksum-verify

Conversation

@star-nebula

Copy link
Copy Markdown
Contributor

What

skillshare upgrade --cli now checks the downloaded release archive against the
release's checksums.txt before it replaces the binary, which is what the UI asset
download in the same command has always done. Details and evidence are in #333.

Two commits:

  1. refactor(version): own the release checksum helpers — moves fetchChecksum /
    parseChecksum from internal/uidist into internal/version (next to the
    BuildChecksumsURL they consume), keeps their messages, and drops uidist's private
    SHA-256 helper for the existing utils.FileHash. No behavior change.
  2. fix(upgrade): verify the release archive before replacing the binary — the actual fix.

Behavior

downloadAndReplace(downloadURL, checksumsURL, destPath, onProgress) stages the archive
to a temp file, hashes it, and extracts only on a match:

situation before after
hash matches installs installs
hash differs installs checksum mismatch: expected …, got …, binary untouched
asset missing from checksums.txt installs checksum for … not found in checksums.txt, untouched
checksums.txt unreachable installs checksums.txt returned HTTP …, untouched

The asset name is path.Base(downloadURL), which is the exact name GoReleaser writes in
checksums.txt (checked against v0.23.4's file, and the asset lists of v0.1.0,
v0.10.0, v0.21.0, v0.23.0). Nothing moves before the check, so the Windows
exe → exe.old → exe rename dance never runs for a rejected archive. The sudo path
re-enters the same function via --replace-binary=<ver>, so elevated upgrades are
covered too.

Tests

cmd/skillshare/upgrade_checksum_test.go builds the archive each platform actually
extracts (zip with skillshare.exe on Windows, tar.gz with skillshare elsewhere) and
serves both endpoints from an httptest server, so the four rows above are covered
without 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 main was the repro in #333:
err = <nil>, and the destination content became the tampered payload. That file is not
in the branch.

go build ./cmd/... ./internal/...                                    ok
go vet ./cmd/skillshare ./internal/version ./internal/uidist         ok
gofmt -l cmd internal                                                 (no output)
go test ./cmd/... ./internal/... -count=1   28 packages ok, exit 0
python3 scripts/ai-context.py check                                  ok

The unit suite was run in a fresh Linux clone (ext4). Two caveats about my environment
rather than the change: TestEnsureProjectConfig_NoProjectStillInitializes blocks on
Bubble Tea input without a TTY, so it was skipped (-skip) — it hangs identically on
stock main; and in a git worktree checkout, internal/git, internal/hooks,
internal/install, and internal/server fail on an unresolvable Windows gitdir: path,
identically before and after the change. In the plain clone both problems are absent
except the TTY one.

Deliberately not included

  • No signature or attestation verification. checksums.txt ships from the same
    release 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.downloadToTemp stays separate from the new downloadArchive. 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.
  • Homebrew installs still delegate to brew, unchanged.

Docs

website/docs/reference/commands/upgrade.md gains the verification under CLI Binary,
mirrored into the ja, ko, zh-Hans, and zh-Hant copies that track this page.
No flag changed, so completions and --help are unaffected.

Closes #333

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.
Comment thread cmd/skillshare/upgrade.go
Comment on lines +464 to +470
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))

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in follow-up #340 — both downloadArchive and uidist.downloadToTemp now close the temp file before removing it on error.

@runkids runkids left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — checksum verification looks right and CI is green. The Windows temp-file cleanup note will be handled in a follow-up PR.

@runkids
runkids merged commit 2bf7e66 into runkids:main Oct 2, 2026
7 checks passed
runkids added a commit that referenced this pull request Oct 2, 2026
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.
@star-nebula
star-nebula deleted the fix/upgrade-checksum-verify branch October 3, 2026 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

skillshare upgrade --cli replaces the running binary without verifying the release checksum

2 participants