Skip to content

Commit ec24497

Browse files
authored
fix(internal/fetch): validate SHA256 in download (#3189)
DownloadTarball now returns an error early if expectedSha256 is empty, avoiding unnecessary download attempts.
1 parent 117d767 commit ec24497

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

internal/fetch/fetch.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const branch = "master"
3434

3535
var (
3636
errChecksumMismatch = errors.New("checksum mismatch")
37+
errMissingSHA256 = errors.New("expectedSha256 is required")
3738
defaultBackoff = 10 * time.Second
3839
)
3940

@@ -146,6 +147,9 @@ func DownloadTarball(ctx context.Context, target, url, expectedSha256 string) er
146147
if fileExists(target) {
147148
return nil
148149
}
150+
if expectedSha256 == "" {
151+
return errMissingSHA256
152+
}
149153
if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
150154
return err
151155
}

internal/fetch/fetch_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,14 @@ func TestDownloadTarballErrors(t *testing.T) {
530530
}
531531
}
532532

533+
func TestDownloadTarballEmptySha(t *testing.T) {
534+
target := path.Join(t.TempDir(), "target")
535+
err := DownloadTarball(t.Context(), target, "https://any-url", "")
536+
if !errors.Is(err, errMissingSHA256) {
537+
t.Errorf("expected errMissingSHA256, got: %v", err)
538+
}
539+
}
540+
533541
func TestLatestCommitAndChecksum(t *testing.T) {
534542
const (
535543
expectedCommit = "testcommit123"

0 commit comments

Comments
 (0)