@@ -188,16 +188,28 @@ func (t *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
188
188
}
189
189
190
190
func (t * Tools ) install (ctx context.Context , path , url , checksum string ) (* tools.Operation , error ) {
191
- // Download
191
+ // Download the archive
192
192
res , err := http .Get (url )
193
193
if err != nil {
194
194
return nil , err
195
195
}
196
196
defer res .Body .Close ()
197
197
198
- // Use a teereader to only read once
199
198
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
+ }
201
213
202
214
safePath , err := utilities .SafeJoin (t .folder , path )
203
215
if err != nil {
@@ -210,20 +222,12 @@ func (t *Tools) install(ctx context.Context, path, url, checksum string) (*tools
210
222
return nil , err
211
223
}
212
224
213
- err = extract .Archive (ctx , reader , t .folder , rename (path ))
225
+ err = extract .Archive (ctx , & buffer , t .folder , rename (path ))
214
226
if err != nil {
215
227
os .RemoveAll (safePath )
216
228
return nil , err
217
229
}
218
230
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
-
227
231
// Write installed.json for retrocompatibility with v1
228
232
err = writeInstalled (t .folder , path )
229
233
if err != nil {
0 commit comments