Skip to content

Commit e4cbc44

Browse files
Check the downloaded file's hash
1 parent 5f3bc99 commit e4cbc44

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

internal/updater/http_client.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package updater
1717

1818
import (
19+
"bytes"
1920
"context"
2021
"crypto/sha256"
2122
"encoding/hex"
@@ -137,8 +138,8 @@ func (c *Client) FetchZip(ctx context.Context, zipURL string) (io.ReadCloser, in
137138
// DownloadFile downloads a file from a URL into the specified path. An optional config and options may be passed (or nil to use the defaults).
138139
// A DownloadProgressCB callback function must be passed to monitor download progress.
139140
// If a not empty queryParameter is passed, it is appended to the URL for analysis purposes.
140-
func DownloadFile(ctx context.Context, path *paths.Path, url string, label string, downloadCB flasher.DownloadProgressCB, config downloader.Config, options ...downloader.DownloadOptions) (returnedError error) {
141-
downloadCB.Start(url, label)
141+
func DownloadFile(ctx context.Context, path *paths.Path, rel *Release, downloadCB flasher.DownloadProgressCB, config downloader.Config, options ...downloader.DownloadOptions) (returnedError error) {
142+
downloadCB.Start(rel.Url, rel.Version)
142143
defer func() {
143144
if returnedError == nil {
144145
downloadCB.End(true, "")
@@ -147,7 +148,7 @@ func DownloadFile(ctx context.Context, path *paths.Path, url string, label strin
147148
}
148149
}()
149150

150-
d, err := downloader.DownloadWithConfigAndContext(ctx, path.String(), url, config, options...)
151+
d, err := downloader.DownloadWithConfigAndContext(ctx, path.String(), rel.Url, config, options...)
151152
if err != nil {
152153
return err
153154
}
@@ -165,5 +166,23 @@ func DownloadFile(ctx context.Context, path *paths.Path, url string, label strin
165166
return fmt.Errorf("%s", msg)
166167
}
167168

169+
// Check the hash
170+
checksum := sha256.New()
171+
tmpZipFile, err := path.Open()
172+
if err != nil {
173+
return fmt.Errorf("could not open archive: %w", err)
174+
}
175+
defer tmpZipFile.Close()
176+
177+
_, err = io.Copy(checksum, tmpZipFile)
178+
if err != nil {
179+
return err
180+
}
181+
if sha256Byte, err := hex.DecodeString(rel.Sha256); err != nil {
182+
return fmt.Errorf("could not convert sha256 from hex to bytes: %w", err)
183+
} else if s := checksum.Sum(nil); !bytes.Equal(s, sha256Byte) {
184+
return fmt.Errorf("bad hash: %x (expected %x)", s, sha256Byte)
185+
}
186+
168187
return nil
169188
}

service/service_download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (s *flasherServerImpl) Download(req *flasher.DownloadRequest, stream flashe
5858

5959
tmpZip := paths.New(req.GetDownloadPath(), "arduino-unoq-debian-image-"+rel.Version+".tar.zst")
6060

61-
if err := updater.DownloadFile(ctx, tmpZip, rel.Url, rel.Version, downloadCB, downloader.Config{}); err != nil {
61+
if err := updater.DownloadFile(ctx, tmpZip, rel, downloadCB, downloader.Config{}); err != nil {
6262
return err
6363
}
6464

service/service_flash.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (s *flasherServerImpl) Flash(req *flasher.FlashRequest, stream flasher.Flas
103103
tmpZip := paths.New(req.GetTempPath(), "arduino-unoq-debian-image-"+rel.Version+".tar.zst")
104104
defer func() { _ = tmpZip.RemoveAll() }()
105105

106-
if err := updater.DownloadFile(ctx, tmpZip, rel.Url, rel.Version, downloadCB, downloader.Config{}); err != nil {
106+
if err := updater.DownloadFile(ctx, tmpZip, rel, downloadCB, downloader.Config{}); err != nil {
107107
return err
108108
}
109109

0 commit comments

Comments
 (0)