Skip to content

Commit d8c4e0b

Browse files
committed
Make tools.install check the checksum
1 parent e5690c5 commit d8c4e0b

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

v2/pkgs/indexes_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
)
1515

1616
// TestIndexes performs a series of operations about indexes, ensuring it behaves as expected.
17-
// This test depends on the internet so it could fail unexpectedly
1817
func TestIndexes(t *testing.T) {
1918
// Use local file as index
2019
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

v2/pkgs/tools.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package pkgs
22

33
import (
4+
"bytes"
45
"context"
6+
"crypto/sha256"
7+
"encoding/hex"
8+
"errors"
59
"fmt"
10+
"io"
611
"io/ioutil"
712
"net/http"
13+
"os"
814
"os/user"
915
"path/filepath"
1016
"runtime"
@@ -133,25 +139,36 @@ func (c *Tools) install(ctx context.Context, packager string, tool Tool) error {
133139
}
134140
defer res.Body.Close()
135141

136-
err = extract.Archive(ctx, res.Body, c.Folder, rename(packager, tool.Name, tool.Version))
142+
// Use a teereader to only read once
143+
var buffer bytes.Buffer
144+
reader := io.TeeReader(res.Body, &buffer)
145+
146+
basepath := filepath.Join(packager, tool.Name, tool.Version)
147+
err = extract.Archive(ctx, reader, c.Folder, rename(basepath))
137148
if err != nil {
138149
return err
139150
}
140151

152+
checksum := sha256.Sum256(buffer.Bytes())
153+
checkSumString := "SHA-256:" + hex.EncodeToString(checksum[:sha256.Size])
154+
155+
if checkSumString != tool.Systems[i].Checksum {
156+
os.RemoveAll(basepath)
157+
return errors.New("checksum doesn't match")
158+
}
159+
141160
return nil
142161
}
143162

144163
func (c *Tools) Remove(ctx context.Context, payload *tools.ToolPayload) error {
145164
return nil
146165
}
147166

148-
func rename(packager, name, version string) extract.Renamer {
149-
base := filepath.Join(packager, name, version)
167+
func rename(base string) extract.Renamer {
150168
return func(path string) string {
151169
parts := strings.Split(path, string(filepath.Separator))
152170
path = strings.Join(parts[1:], string(filepath.Separator))
153171
path = filepath.Join(base, path)
154-
fmt.Println("path", path)
155172
return path
156173
}
157174
}

0 commit comments

Comments
 (0)