Skip to content

Commit d787bea

Browse files
committed
fix the bug:
The buffer was not containing the full archive because when red by `extract.Archive(...)` some bytes were skipped because of padding. This was causing the checksum to be different from the expected one
1 parent 7bf49d9 commit d787bea

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

v2/pkgs/tools.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,28 @@ func (t *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
188188
}
189189

190190
func (t *Tools) install(ctx context.Context, path, url, checksum string) (*tools.Operation, error) {
191-
// Download
191+
// Download the archive
192192
res, err := http.Get(url)
193193
if err != nil {
194194
return nil, err
195195
}
196196
defer res.Body.Close()
197197

198-
// Use a teereader to only read once
199198
var buffer bytes.Buffer
200-
reader := io.TeeReader(res.Body, &buffer)
199+
200+
// We copy the body of the response to a buffer to calculate the checksum
201+
_, err = io.Copy(&buffer, res.Body)
202+
if err != nil {
203+
return nil, err
204+
}
205+
206+
// Check the checksum
207+
sum := sha256.Sum256(buffer.Bytes())
208+
sumString := "SHA-256:" + hex.EncodeToString(sum[:sha256.Size])
209+
210+
if sumString != checksum {
211+
return nil, errors.New("checksum of downloaded file doesn't match, expected: " + checksum + " got: " + sumString)
212+
}
201213

202214
safePath, err := utilities.SafeJoin(t.folder, path)
203215
if err != nil {
@@ -210,20 +222,12 @@ func (t *Tools) install(ctx context.Context, path, url, checksum string) (*tools
210222
return nil, err
211223
}
212224

213-
err = extract.Archive(ctx, reader, t.folder, rename(path))
225+
err = extract.Archive(ctx, &buffer, t.folder, rename(path))
214226
if err != nil {
215227
os.RemoveAll(safePath)
216228
return nil, err
217229
}
218230

219-
sum := sha256.Sum256(buffer.Bytes())
220-
sumString := "SHA-256:" + hex.EncodeToString(sum[:sha256.Size])
221-
222-
if sumString != checksum {
223-
os.RemoveAll(safePath)
224-
return nil, errors.New("checksum doesn't match")
225-
}
226-
227231
// Write installed.json for retrocompatibility with v1
228232
err = writeInstalled(t.folder, path)
229233
if err != nil {

0 commit comments

Comments
 (0)