diff --git a/README.md b/README.md index 8a78cad..0381fcd 100644 --- a/README.md +++ b/README.md @@ -62,18 +62,20 @@ The zero value works for a goreleaser project with default archive names. | `AssetTemplate` | `{name}_{version}_{os}_{arch}.tar.gz` | archive asset name. `{version}` has no leading `v` | | `ChecksumAsset` | `checksums.txt` | goreleaser checksum file | | `SkipChecksum` | `false` | set when the repo publishes no checksum file | +| `VerifyChecksums` | `nil` | runs on the downloaded checksum file before it is trusted, e.g. to check a signature | | `TargetPath` | running executable, symlinks resolved | file to replace | | `Managed` | Homebrew Cellar, `/usr/{s,}bin`, `/usr/lib{,64,exec}`, Nix store | locations owned by a package manager | | `Client` | default transport with a 30s response header timeout | HTTP client | | `Out` | `os.Stdout` | progress lines | -`Prepare` runs before any hook. It resolves the target, refuses a binary that -lives under a package manager (the error names the manager and its upgrade -command), refuses an unwritable directory, then downloads and verifies the -archive into a temp directory. Nothing is downloaded when a check fails. -`Commit` renames the extracted binary over the target and, on macOS, re-signs -it ad hoc. On a terminal the download line shows a progress bar. Elsewhere it -is a single line. +`Prepare` runs before `PreInstall`. When `VerifyChecksums` is set, it runs +inside `Prepare`, before the archive is downloaded. `Prepare` resolves the +target, refuses a binary that lives under a package manager (the error names +the manager and its upgrade command), refuses an unwritable directory, then +downloads and verifies the archive into a temp directory. Nothing is +downloaded when a check fails. `Commit` renames the extracted binary over the +target and, on macOS, re-signs it ad hoc. On a terminal the download line +shows a progress bar. Elsewhere it is a single line. Replacement binaries are written through an exclusively created temporary file beside the target, then atomically renamed into place. Existing @@ -84,8 +86,22 @@ leftover cannot safely be distinguished from another update's active file. The installation directory must be trusted against modification by other users. Linux and macOS with `.tar.gz` assets are supported. Windows is not. -Verification proves the download matches what the release page lists. It -does not prove who published it. Signature checking is out of scope. +Verification proves the download matches what the release page lists. The +library checks no signature itself. Set `VerifyChecksums` to check one, as +below. + +## Verifying checksums + +`VerifyChecksums` runs before the archive is downloaded, and its error stops +the update. A checksum file the release lists no size for is capped at 1 MiB. + +```go +installer.VerifyChecksums = func(ctx context.Context, rel selfupdate.Release, path string) error { + return exec.CommandContext(ctx, "gh", "attestation", "verify", path, + "--repo", "acme/mytool", + "--signer-workflow", "acme/mytool/.github/workflows/release.yml").Run() +} +``` ## Hooks diff --git a/archive.go b/archive.go index 98e709d..f5ec993 100644 --- a/archive.go +++ b/archive.go @@ -3,6 +3,7 @@ package selfupdate import ( "context" "crypto/sha256" + "errors" "fmt" "io" "net/http" @@ -20,6 +21,14 @@ const DefaultAssetTemplate = "{name}_{version}_{os}_{arch}.tar.gz" // DefaultChecksumAsset is goreleaser's default checksum file name. const DefaultChecksumAsset = "checksums.txt" +// maxChecksumBytes caps a checksum file the release lists no size for, +// on the VerifyChecksums path. +const maxChecksumBytes = 1 << 20 + +// verifyDirMode is the permission set on the subdirectory of Prepare's +// temp dir that holds the checksum file passed to VerifyChecksums. +const verifyDirMode os.FileMode = 0o700 + // responseHeaderTimeout bounds how long the default client waits for // response headers. Bodies stream until ctx is cancelled. const responseHeaderTimeout = 30 * time.Second @@ -32,14 +41,20 @@ const binaryMode os.FileMode = 0o755 // replaces the running executable in place. It needs no shell, curl or // install script. type ArchiveInstaller struct { - Name string // binary name inside the archive; default: base name of the resolved target - AssetTemplate string // default DefaultAssetTemplate; {version} has no leading v - ChecksumAsset string // default DefaultChecksumAsset - SkipChecksum bool // disable verification for repos that publish no checksum file - TargetPath string // default: os.Executable(); symlinks are always resolved - Managed []ManagedInstall // nil means DefaultManagedInstalls; empty slice disables the check - Client *http.Client // default: a shared DefaultTransport clone with a 30s ResponseHeaderTimeout - Out io.Writer // progress lines; default os.Stdout + Name string // binary name inside the archive; default: base name of the resolved target + AssetTemplate string // default DefaultAssetTemplate; {version} has no leading v + ChecksumAsset string // default DefaultChecksumAsset + SkipChecksum bool // disable verification for repos that publish no checksum file + // VerifyChecksums, when set, runs on the downloaded checksum file + // before it is trusted, for example to check a signature or an + // attestation. path is the file inside Prepare's temp dir. An error + // stops the update before the archive is downloaded. A checksum file + // the release lists no size for is capped at 1 MiB. + VerifyChecksums func(ctx context.Context, rel Release, path string) error + TargetPath string // default: os.Executable(); symlinks are always resolved + Managed []ManagedInstall // nil means DefaultManagedInstalls; empty slice disables the check + Client *http.Client // default: a shared DefaultTransport clone with a 30s ResponseHeaderTimeout + Out io.Writer // progress lines; default os.Stdout } // defaultClient is built on first use and shared by every ArchiveInstaller @@ -59,8 +74,12 @@ type archiveStaged struct { // Prepare resolves the target, refuses managed or unwritable locations, // then downloads, verifies and extracts the platform asset into a temp -// directory. The running binary is untouched. +// directory. The running binary is untouched. VerifyChecksums together +// with SkipChecksum is refused before anything else. func (a *ArchiveInstaller) Prepare(ctx context.Context, rel Release) (Staged, error) { + if a.VerifyChecksums != nil && a.SkipChecksum { + return nil, errors.New("selfupdate: VerifyChecksums needs the checksum file, but SkipChecksum is set") + } target, err := a.target() if err != nil { return nil, err @@ -98,12 +117,29 @@ func (a *ArchiveInstaller) Sweep() error { } // stage downloads asset into staged.dir, verifies it unless SkipChecksum, -// and extracts name beside it. +// and extracts name beside it. With VerifyChecksums set, the checksum file +// is downloaded and passed to the hook before the archive is requested. func (a *ArchiveInstaller) stage( ctx context.Context, rel Release, asset Asset, name string, staged *archiveStaged, ) error { archivePath := filepath.Join(staged.dir, asset.Name) - sum, err := a.download(ctx, asset, archivePath) + var err error + if a.VerifyChecksums != nil { + err = a.downloadTrusted(ctx, rel, asset, archivePath, staged.dir) + } else { + err = a.downloadChecked(ctx, rel, asset, archivePath) + } + if err != nil { + return err + } + staged.binary = filepath.Join(staged.dir, name) + return extractTo(archivePath, name, staged.binary) +} + +// downloadChecked downloads asset to path, then streams the checksum file +// to verify it unless SkipChecksum. It is the path without VerifyChecksums. +func (a *ArchiveInstaller) downloadChecked(ctx context.Context, rel Release, asset Asset, path string) error { + sum, err := a.download(ctx, asset, path) if err != nil { return err } @@ -113,23 +149,67 @@ func (a *ArchiveInstaller) stage( } a.printf("Verified %s\n", asset.Name) } - staged.binary = filepath.Join(staged.dir, name) - return extractTo(archivePath, name, staged.binary) + return nil } -// download streams asset.URL to path, drawing a progress bar on a -// terminal, and returns the SHA-256 of the bytes written. When the release -// lists a size the body must match it exactly. +// downloadTrusted saves the checksum file into a verify subdirectory of +// dir and passes it to VerifyChecksums. Only after the hook accepts it is +// asset downloaded to path and checked against the saved file, so the +// bytes the hook checked are the bytes the archive is verified against. +// The saved file keeps only the base of the asset name, so it can neither +// escape dir nor collide with the archive. +func (a *ArchiveInstaller) downloadTrusted( + ctx context.Context, rel Release, asset Asset, path, dir string, +) error { + checksums, err := selectAsset(rel, a.checksumAsset()) + if err != nil { + return err + } + verifyDir := filepath.Join(dir, "verify") + if err := os.Mkdir(verifyDir, verifyDirMode); err != nil { + return fmt.Errorf("selfupdate: create verify dir: %w", err) + } + checksumsPath := filepath.Join(verifyDir, filepath.Base(checksums.Name)) + if _, err := a.downloadTo(ctx, checksums, checksumsPath, io.Discard, maxChecksumBytes); err != nil { + return err + } + if err := a.VerifyChecksums(ctx, rel, checksumsPath); err != nil { + return fmt.Errorf("selfupdate: verify %s: %w", checksums.Name, err) + } + sum, err := a.download(ctx, asset, path) + if err != nil { + return err + } + if err := verifySaved(checksumsPath, asset.Name, sum); err != nil { + return err + } + a.printf("Verified %s\n", asset.Name) + return nil +} + +// download prints the download line and streams asset to path with a +// progress bar on a terminal. It returns the SHA-256 of the bytes written. func (a *ArchiveInstaller) download(ctx context.Context, asset Asset, path string) ([]byte, error) { a.printf("Downloading %s (%s)...\n", asset.Name, formatMB(asset.Size)) + return a.downloadTo(ctx, asset, path, a.out(), 0) +} + +// downloadTo streams asset.URL to path, drawing progress to progressOut +// when it is a terminal, and returns the SHA-256 of the bytes written. +// When the release lists a size the body must match it exactly. When it +// lists none, a positive maxUnlisted caps the body and zero leaves it +// unbounded. +func (a *ArchiveInstaller) downloadTo( + ctx context.Context, asset Asset, path string, progressOut io.Writer, maxUnlisted int64, +) ([]byte, error) { hash := sha256.New() err := a.fetch(ctx, asset.URL, func(resp *http.Response) error { f, err := os.Create(path) // path is inside the temp dir created by Prepare if err != nil { return fmt.Errorf("selfupdate: create download: %w", err) } - progress := newProgressWriter(a.out(), resp.ContentLength) - copyErr := copyBounded(io.MultiWriter(f, hash, progress), resp.Body, asset.Size) + progress := newProgressWriter(progressOut, resp.ContentLength) + copyErr := copyBody(io.MultiWriter(f, hash, progress), resp.Body, asset.Size, maxUnlisted) progress.Finish() if closeErr := f.Close(); copyErr == nil { copyErr = closeErr @@ -145,6 +225,28 @@ func (a *ArchiveInstaller) download(ctx context.Context, asset Asset, path strin return hash.Sum(nil), nil } +// copyBody streams body into dst. It applies copyBounded when size is +// listed, and copyCapped when size is not and maxUnlisted is positive. +func copyBody(dst io.Writer, body io.Reader, size, maxUnlisted int64) error { + if size <= 0 && maxUnlisted > 0 { + return copyCapped(dst, body, maxUnlisted) + } + return copyBounded(dst, body, size) +} + +// copyCapped streams body into dst and reads at most limit+1 bytes. A +// body longer than limit is an error. +func copyCapped(dst io.Writer, body io.Reader, limit int64) error { + n, err := io.Copy(dst, io.LimitReader(body, limit+1)) + if err != nil { + return err + } + if n > limit { + return fmt.Errorf("body exceeds %d bytes", limit) + } + return nil +} + // copyBounded streams body into dst. A positive size caps the read one // byte past it, so a hostile payload cannot fill the temp dir before the // checksum rejects it, and a body of any other length is an error. A zero @@ -179,6 +281,21 @@ func (a *ArchiveInstaller) verify(ctx context.Context, rel Release, assetName st }) } +// verifySaved checks sum against assetName's entry in the checksum file +// saved at path. +func verifySaved(path, assetName string, sum []byte) error { + f, err := os.Open(path) // path is inside the temp dir created by Prepare + if err != nil { + return fmt.Errorf("selfupdate: open checksums: %w", err) + } + defer func() { _ = f.Close() }() + sums, err := parseChecksums(f) + if err != nil { + return err + } + return verifyChecksum(sums, assetName, sum) +} + // fetch GETs url and hands the 200 response to fn. The body is closed // when fn returns. Any other status is an error. func (a *ArchiveInstaller) fetch(ctx context.Context, url string, fn func(*http.Response) error) error { diff --git a/archive_test.go b/archive_test.go index 9acabd4..c1b282b 100644 --- a/archive_test.go +++ b/archive_test.go @@ -39,6 +39,8 @@ type archiveFixture struct { srv *httptest.Server rel Release assetName string + archive []byte // body served at the archive path + checksums string // body served at checksumsPath mu sync.Mutex requests []string } @@ -59,11 +61,13 @@ func newArchiveFixture(t *testing.T, body string, tamper bool) *archiveFixture { t.Helper() fx := &archiveFixture{assetName: fmt.Sprintf("tool_1.2.3_%s_%s.tar.gz", runtime.GOOS, runtime.GOARCH)} archive := tarGz(t, tarEntry{name: "LICENSE.txt", body: "license text"}, tarEntry{name: testRepo, body: body}) + fx.archive = archive sum := sha256.Sum256(archive) if tamper { sum[0] ^= 0xff } checksums := hex.EncodeToString(sum[:]) + " " + fx.assetName + "\n" + fx.checksums = checksums mux := http.NewServeMux() mux.HandleFunc("/"+fx.assetName, func(w http.ResponseWriter, r *http.Request) { @@ -91,6 +95,19 @@ func newArchiveFixture(t *testing.T, body string, tamper bool) *archiveFixture { return fx } +// assertEmptyDir fails the test when dir holds any entry, such as a +// selfupdate-* temp dir or a download that escaped it. +func assertEmptyDir(t *testing.T, dir string) { + t.Helper() + entries, err := os.ReadDir(dir) + if err != nil { + t.Fatal(err) + } + if len(entries) != 0 { + t.Fatalf("%s is not empty: %v", dir, entries) + } +} + // newTestInstaller targets an explicit file and disables managed-path // detection so an unusual TMPDIR cannot trip the defaults. func newTestInstaller(target string, out io.Writer) *ArchiveInstaller { @@ -409,3 +426,299 @@ func TestArchiveInstaller_UnknownSizeIsUnbounded(t *testing.T) { } _ = staged.Close() } + +func TestArchiveInstaller_VerifyChecksumsSeesTrustedBytes(t *testing.T) { + fx := newArchiveFixture(t, newBinary, false) + target := writeTarget(t, contentOld) + var out bytes.Buffer + inst := newTestInstaller(target, &out) + var gotPath string + var gotBytes []byte + var seenBefore []string + inst.VerifyChecksums = func(_ context.Context, _ Release, path string) error { + gotPath = path + seenBefore = fx.requested() + var err error + gotBytes, err = os.ReadFile(path) + return err + } + + staged, err := inst.Prepare(context.Background(), fx.rel) + if err != nil { + t.Fatal(err) + } + defer func() { _ = staged.Close() }() + if string(gotBytes) != fx.checksums { + t.Fatalf("hook read %q, server sent %q", gotBytes, fx.checksums) + } + if dir := filepath.Join(staged.(*archiveStaged).dir, "verify"); filepath.Dir(gotPath) != dir { + t.Fatalf("hook path %s must be inside %s", gotPath, dir) + } + if len(seenBefore) != 1 || seenBefore[0] != checksumsPath { + t.Fatalf("the hook must run before the archive is requested, saw %v", seenBefore) + } + if got := fx.requested(); len(got) != 2 || got[0] != checksumsPath || got[1] != "/"+fx.assetName { + t.Fatalf("the checksum file must be fetched once, before the archive: %v", got) + } + if strings.Count(out.String(), "Downloading") != 1 || !strings.Contains(out.String(), "Verified "+fx.assetName) { + t.Fatalf("output must keep one download line and the Verified line:\n%s", out.String()) + } + + if err := staged.Commit(context.Background()); err != nil { + t.Fatal(err) + } + if got := readTarget(t, target); got != newBinary { + t.Fatalf("target after Commit: %q", got) + } +} + +func TestArchiveInstaller_VerifyChecksumsStillChecksArchive(t *testing.T) { + fx := newArchiveFixture(t, newBinary, true) + target := writeTarget(t, contentOld) + inst := newTestInstaller(target, io.Discard) + inst.VerifyChecksums = func(context.Context, Release, string) error { return nil } + staged, err := inst.Prepare(context.Background(), fx.rel) + if !errors.Is(err, ErrChecksumMismatch) { + t.Fatalf("an accepted checksum file must still be checked against the archive, got %v", err) + } + if staged != nil { + t.Fatal("no staged update may be returned on failure") + } + if got := readTarget(t, target); got != contentOld { + t.Fatalf("target changed: %q", got) + } +} + +func TestArchiveInstaller_VerifyChecksumsErrorStopsUpdate(t *testing.T) { + fx := newArchiveFixture(t, newBinary, false) + target := writeTarget(t, contentOld) + tmp := t.TempDir() + t.Setenv("TMPDIR", tmp) + sentinel := errors.New("bad signature") + inst := newTestInstaller(target, io.Discard) + inst.VerifyChecksums = func(context.Context, Release, string) error { return sentinel } + + staged, err := inst.Prepare(context.Background(), fx.rel) + if !errors.Is(err, sentinel) || !strings.Contains(err.Error(), DefaultChecksumAsset) { + t.Fatalf("got %v", err) + } + if staged != nil { + t.Fatal("no staged update may be returned on failure") + } + for _, p := range fx.requested() { + if p == "/"+fx.assetName { + t.Fatal("the archive must not be requested after the hook fails") + } + } + if got := readTarget(t, target); got != contentOld { + t.Fatalf("target changed: %q", got) + } + assertEmptyDir(t, tmp) +} + +func TestArchiveInstaller_VerifyChecksumsWithSkipChecksumIsError(t *testing.T) { + fx := newArchiveFixture(t, newBinary, false) + target := writeTarget(t, contentOld) + tmp := t.TempDir() + t.Setenv("TMPDIR", tmp) + inst := newTestInstaller(target, io.Discard) + inst.SkipChecksum = true + called := false + inst.VerifyChecksums = func(context.Context, Release, string) error { + called = true + return nil + } + staged, err := inst.Prepare(context.Background(), fx.rel) + if err == nil || !strings.Contains(err.Error(), "SkipChecksum") { + t.Fatalf("got %v", err) + } + if staged != nil || called { + t.Fatal("nothing may be staged or verified when both are set") + } + if got := fx.requested(); len(got) != 0 { + t.Fatalf("the conflict must be refused before any request: %v", got) + } + assertEmptyDir(t, tmp) +} + +func TestArchiveInstaller_VerifyChecksumsOversizeChecksumFile(t *testing.T) { + fx := newArchiveFixture(t, newBinary, false) + assets := append([]Asset(nil), fx.rel.Assets...) + assets[3].Size = int64(len(fx.checksums)) - 1 + rel := Release{Tag: fx.rel.Tag, Assets: assets} + target := writeTarget(t, contentOld) + inst := newTestInstaller(target, io.Discard) + called := false + inst.VerifyChecksums = func(context.Context, Release, string) error { + called = true + return nil + } + staged, err := inst.Prepare(context.Background(), rel) + if err == nil || !strings.Contains(err.Error(), "release lists") { + t.Fatalf("got %v", err) + } + if staged != nil || called { + t.Fatal("an oversize checksum file must be rejected before the hook runs") + } + for _, p := range fx.requested() { + if p == "/"+fx.assetName { + t.Fatal("the archive must not be requested after the checksum file is rejected") + } + } +} + +func TestArchiveInstaller_VerifyChecksumsGetsRelease(t *testing.T) { + fx := newArchiveFixture(t, newBinary, false) + target := writeTarget(t, contentOld) + inst := newTestInstaller(target, io.Discard) + var gotTag string + inst.VerifyChecksums = func(_ context.Context, rel Release, _ string) error { + gotTag = rel.Tag + return nil + } + staged, err := inst.Prepare(context.Background(), fx.rel) + if err != nil { + t.Fatal(err) + } + _ = staged.Close() + if gotTag != fx.rel.Tag { + t.Fatalf("hook got tag %q, want %q", gotTag, fx.rel.Tag) + } +} + +func TestArchiveInstaller_VerifyChecksumsPathStaysInTempDir(t *testing.T) { + const escape = "../escape-sums.txt" + fx := newArchiveFixture(t, newBinary, false) + assets := append([]Asset(nil), fx.rel.Assets...) + assets[3].Name = escape + rel := Release{Tag: fx.rel.Tag, Assets: assets} + target := writeTarget(t, contentOld) + tmp := t.TempDir() + t.Setenv("TMPDIR", tmp) + inst := newTestInstaller(target, io.Discard) + inst.ChecksumAsset = escape + var gotPath string + inst.VerifyChecksums = func(_ context.Context, _ Release, path string) error { + gotPath = path + return nil + } + + staged, err := inst.Prepare(context.Background(), rel) + if err != nil { + t.Fatal(err) + } + relPath, err := filepath.Rel(tmp, gotPath) + if err != nil { + t.Fatal(err) + } + first := strings.Split(relPath, string(filepath.Separator))[0] + if !strings.HasPrefix(first, "selfupdate-") || relPath == first { + t.Fatalf("hook path %s must be inside a selfupdate-* dir under %s", gotPath, tmp) + } + if err := staged.Commit(context.Background()); err != nil { + t.Fatal(err) + } + if err := staged.Close(); err != nil { + t.Fatal(err) + } + assertEmptyDir(t, tmp) +} + +func TestArchiveInstaller_VerifyChecksumsNamedLikeArchive(t *testing.T) { + fx := newArchiveFixture(t, newBinary, false) + // One URL serves the checksum file first and the archive second, so + // both assets share the archive's name. + var served sync.Mutex + calls := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + served.Lock() + calls++ + n := calls + served.Unlock() + if n == 1 { + _, _ = io.WriteString(w, fx.checksums) + return + } + _, _ = w.Write(fx.archive) + })) + t.Cleanup(srv.Close) + rel := Release{Tag: fx.rel.Tag, Assets: []Asset{{Name: fx.assetName, URL: srv.URL + "/" + fx.assetName}}} + target := writeTarget(t, contentOld) + inst := newTestInstaller(target, io.Discard) + inst.ChecksumAsset = fx.assetName + var hookPath string + var hookBytes []byte + inst.VerifyChecksums = func(_ context.Context, _ Release, path string) error { + hookPath = path + var err error + hookBytes, err = os.ReadFile(path) + return err + } + + staged, err := inst.Prepare(context.Background(), rel) + if string(hookBytes) != fx.checksums { + t.Fatalf("the hook must see the checksum file, got %q", hookBytes) + } + if err != nil { + return // refusing the collision is acceptable + } + defer func() { _ = staged.Close() }() + if after, err := os.ReadFile(hookPath); err != nil || string(after) != fx.checksums { + t.Fatalf("the archive download replaced the checked file: %q, %v", after, err) + } + if err := staged.Commit(context.Background()); err != nil { + t.Fatal(err) + } + if got := readTarget(t, target); got != newBinary { + t.Fatalf("target after Commit: %q", got) + } +} + +// prepareUnlistedChecksums runs Prepare with VerifyChecksums against a +// release that lists no size for the checksum file, which serves body. It +// reports whether the hook ran. +func prepareUnlistedChecksums(t *testing.T, fx *archiveFixture, body string) (Staged, bool, error) { + t.Helper() + mux := http.NewServeMux() + mux.HandleFunc("/"+fx.assetName, func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write(fx.archive) + }) + mux.HandleFunc(checksumsPath, func(w http.ResponseWriter, _ *http.Request) { + _, _ = io.WriteString(w, body) + }) + srv := httptest.NewServer(mux) + t.Cleanup(srv.Close) + rel := Release{Tag: fx.rel.Tag, Assets: []Asset{ + {Name: fx.assetName, URL: srv.URL + "/" + fx.assetName, Size: int64(len(fx.archive))}, + {Name: DefaultChecksumAsset, URL: srv.URL + checksumsPath, Size: 0}, + }} + inst := newTestInstaller(writeTarget(t, contentOld), io.Discard) + called := false + inst.VerifyChecksums = func(context.Context, Release, string) error { + called = true + return nil + } + staged, err := inst.Prepare(context.Background(), rel) + return staged, called, err +} + +func TestArchiveInstaller_VerifyChecksumsUnlistedSizePasses(t *testing.T) { + fx := newArchiveFixture(t, newBinary, false) + staged, called, err := prepareUnlistedChecksums(t, fx, fx.checksums) + if err != nil || !called { + t.Fatalf("a small unlisted checksum file must pass: err %v, hook called %v", err, called) + } + _ = staged.Close() +} + +func TestArchiveInstaller_VerifyChecksumsUnlistedSizeIsCapped(t *testing.T) { + fx := newArchiveFixture(t, newBinary, false) + body := strings.Repeat("x", maxChecksumBytes+1) + staged, called, err := prepareUnlistedChecksums(t, fx, body) + if err == nil || !strings.Contains(err.Error(), DefaultChecksumAsset) { + t.Fatalf("got %v", err) + } + if staged != nil || called { + t.Fatal("an oversize unlisted checksum file must be rejected before the hook runs") + } +}