Skip to content

Commit

Permalink
throw error on binary hash mismatch (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
marwanhawari authored Apr 7, 2024
1 parent 484b5f6 commit ed3f20f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func upgradeOne(binaryName, userOS, userArch string, lockFile stew.LockFile, sys
}
fmt.Printf("✅ Downloaded %v to %v\n", constants.GreenColor(asset), constants.GreenColor(stewPkgPath))

_, binaryHash, err := stew.InstallBinary(downloadPath, repo, systemInfo, &lockFile, true, pkg.Binary, pkg.BinaryHash)
_, binaryHash, err := stew.InstallBinary(downloadPath, repo, systemInfo, &lockFile, true, pkg.Binary, "")
if err != nil {
if err := os.RemoveAll(downloadPath); err != nil {
return err
Expand Down
8 changes: 8 additions & 0 deletions lib/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,11 @@ type InvalidGithubSearchQueryError struct {
func (e InvalidGithubSearchQueryError) Error() string {
return fmt.Sprintf("%v The search query %v contains invalid characters", constants.RedColor("Error:"), constants.RedColor(e.SearchQuery))
}

type BinaryMismatchError struct {
BinaryName string
}

func (e BinaryMismatchError) Error() string {
return fmt.Sprintf("%v The hash for the downloaded binary %v does not match the hash in the lockfile", constants.RedColor("Error:"), constants.RedColor(e.BinaryName))
}
29 changes: 29 additions & 0 deletions lib/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,32 @@ func TestInvalidGithubSearchQueryError_Error(t *testing.T) {
})
}
}

func TestBinaryMismatchError_Error(t *testing.T) {
type fields struct {
BinaryName string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "test1",
fields: fields{
BinaryName: "testBinaryName",
},
want: fmt.Sprintf("%v The hash for the downloaded binary %v does not match the hash in the lockfile", constants.RedColor("Error:"), constants.RedColor("testBinaryName")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := BinaryMismatchError{
BinaryName: tt.fields.BinaryName,
}
if got := e.Error(); got != tt.want {
t.Errorf("BinaryMismatchError.Error() = %v, want %v", got, tt.want)
}
})
}
}
9 changes: 8 additions & 1 deletion lib/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ func copyFile(srcFile, destFile string) error {
func walkDir(rootDir string) ([]string, error) {
allFilePaths := []string{}
err := filepath.Walk(rootDir, func(filePath string, fileInfo os.FileInfo, err error) error {
if !fileInfo.IsDir() {
if err != nil {
return err
}
fileMode := fileInfo.Mode()
if fileMode.IsRegular() {
allFilePaths = append(allFilePaths, filePath)
}
return nil
Expand Down Expand Up @@ -180,6 +184,9 @@ func getBinary(filePaths []string, desiredBinaryRename, expectedBinaryHash strin
}

if desiredBinaryRename != "" {
if expectedBinaryHash != "" && expectedBinaryHash != executableFiles[0].fileHash {
return "", "", "", BinaryMismatchError{BinaryName: desiredBinaryRename}
}
return executableFiles[0].filePath, desiredBinaryRename, executableFiles[0].fileHash, nil
}
return executableFiles[0].filePath, executableFiles[0].fileName, executableFiles[0].fileHash, nil
Expand Down

0 comments on commit ed3f20f

Please sign in to comment.