From ca20d8809302341acf628117544e921965445c35 Mon Sep 17 00:00:00 2001 From: Draylar Date: Mon, 19 Oct 2020 16:57:52 -0500 Subject: [PATCH 1/6] Clarify hash mismatch error in mod.go and also check with uppercase hash --- core/mod.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/mod.go b/core/mod.go index 02c41ba..16b1384 100644 --- a/core/mod.go +++ b/core/mod.go @@ -8,6 +8,8 @@ import ( "os" "path/filepath" "strconv" + "fmt" + "strings" "github.com/BurntSushi/toml" ) @@ -146,8 +148,8 @@ func (m Mod) DownloadFile(dest io.Writer) error { } calculatedHash := hex.EncodeToString(h.Sum(nil)) - if calculatedHash != m.Download.Hash { - return errors.New("hash of saved file is invalid") + if calculatedHash != m.Download.Hash && strings.ToUpper(calculatedHash) != m.Download.Hash { + return errors.New(fmt.Sprintf("hash of saved file is invalid!\n .toml hash: %s\n download hash: %s", calculatedHash, m.Download.Hash)) } return nil } From 463848eaa023206d81ba0330f47785c4448060fc Mon Sep 17 00:00:00 2001 From: Draylar Date: Mon, 19 Oct 2020 17:20:07 -0500 Subject: [PATCH 2/6] Print error when a file hash only matches when uppercase --- core/mod.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/core/mod.go b/core/mod.go index 16b1384..d955bc5 100644 --- a/core/mod.go +++ b/core/mod.go @@ -148,8 +148,14 @@ func (m Mod) DownloadFile(dest io.Writer) error { } calculatedHash := hex.EncodeToString(h.Sum(nil)) - if calculatedHash != m.Download.Hash && strings.ToUpper(calculatedHash) != m.Download.Hash { + + // Check if the hash of the downloaded file matches the expected hash. + // Also check if the uppercase version of the expected hash matches. + if(strings.ToUpper(calculatedHash) != m.Download.Hash) { + return errors.New(fmt.Sprintf("Hash of saved file %s only matches when uppercased. Consider changing this to prevent future issues.", m.GetFilePath())) + } else if calculatedHash != m.Download.Hash { return errors.New(fmt.Sprintf("hash of saved file is invalid!\n .toml hash: %s\n download hash: %s", calculatedHash, m.Download.Hash)) } + return nil } From 0656d794e196cda48679353116010a011cacd4c7 Mon Sep 17 00:00:00 2001 From: Draylar Date: Mon, 19 Oct 2020 17:24:49 -0500 Subject: [PATCH 3/6] Add newlines after prints --- core/mod.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/mod.go b/core/mod.go index d955bc5..d60f260 100644 --- a/core/mod.go +++ b/core/mod.go @@ -152,9 +152,9 @@ func (m Mod) DownloadFile(dest io.Writer) error { // Check if the hash of the downloaded file matches the expected hash. // Also check if the uppercase version of the expected hash matches. if(strings.ToUpper(calculatedHash) != m.Download.Hash) { - return errors.New(fmt.Sprintf("Hash of saved file %s only matches when uppercased. Consider changing this to prevent future issues.", m.GetFilePath())) + return errors.New(fmt.Sprintf("Hash of saved file %s only matches when uppercased. Consider changing this to prevent future issues.\n", m.GetFilePath())) } else if calculatedHash != m.Download.Hash { - return errors.New(fmt.Sprintf("hash of saved file is invalid!\n .toml hash: %s\n download hash: %s", calculatedHash, m.Download.Hash)) + return errors.New(fmt.Sprintf("hash of saved file is invalid!\n .toml hash: %s\n download hash: %s\n", calculatedHash, m.Download.Hash)) } return nil From 1a21eca3a2db517830d78cc1c64be81158d57a24 Mon Sep 17 00:00:00 2001 From: Draylar Date: Mon, 19 Oct 2020 17:29:54 -0500 Subject: [PATCH 4/6] Remove special casing for casing differences, clarify error message --- core/mod.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core/mod.go b/core/mod.go index d60f260..38e9b4f 100644 --- a/core/mod.go +++ b/core/mod.go @@ -9,7 +9,6 @@ import ( "path/filepath" "strconv" "fmt" - "strings" "github.com/BurntSushi/toml" ) @@ -150,11 +149,8 @@ func (m Mod) DownloadFile(dest io.Writer) error { calculatedHash := hex.EncodeToString(h.Sum(nil)) // Check if the hash of the downloaded file matches the expected hash. - // Also check if the uppercase version of the expected hash matches. - if(strings.ToUpper(calculatedHash) != m.Download.Hash) { - return errors.New(fmt.Sprintf("Hash of saved file %s only matches when uppercased. Consider changing this to prevent future issues.\n", m.GetFilePath())) - } else if calculatedHash != m.Download.Hash { - return errors.New(fmt.Sprintf("hash of saved file is invalid!\n .toml hash: %s\n download hash: %s\n", calculatedHash, m.Download.Hash)) + if calculatedHash != m.Download.Hash { + return errors.New(fmt.Sprintf("Hash of downloaded file does no match with expected hash!\n download hash: %s\n expected hash: %s\n", calculatedHash, m.Download.Hash)) } return nil From aaf6ae77ac4fdce7f6830f059579b1ea38518c17 Mon Sep 17 00:00:00 2001 From: Draylar Date: Mon, 19 Oct 2020 17:31:05 -0500 Subject: [PATCH 5/6] no -> not --- core/mod.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/mod.go b/core/mod.go index 38e9b4f..5760cb9 100644 --- a/core/mod.go +++ b/core/mod.go @@ -150,7 +150,7 @@ func (m Mod) DownloadFile(dest io.Writer) error { // Check if the hash of the downloaded file matches the expected hash. if calculatedHash != m.Download.Hash { - return errors.New(fmt.Sprintf("Hash of downloaded file does no match with expected hash!\n download hash: %s\n expected hash: %s\n", calculatedHash, m.Download.Hash)) + return errors.New(fmt.Sprintf("Hash of downloaded file does not match with expected hash!\n download hash: %s\n expected hash: %s\n", calculatedHash, m.Download.Hash)) } return nil From c5328ee78865eb1392ffa97ba2f1912685b07043 Mon Sep 17 00:00:00 2001 From: Draylar Date: Tue, 20 Oct 2020 11:40:11 -0500 Subject: [PATCH 6/6] Update core/mod.go Co-authored-by: comp500 --- core/mod.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/mod.go b/core/mod.go index 5760cb9..4a15943 100644 --- a/core/mod.go +++ b/core/mod.go @@ -150,7 +150,7 @@ func (m Mod) DownloadFile(dest io.Writer) error { // Check if the hash of the downloaded file matches the expected hash. if calculatedHash != m.Download.Hash { - return errors.New(fmt.Sprintf("Hash of downloaded file does not match with expected hash!\n download hash: %s\n expected hash: %s\n", calculatedHash, m.Download.Hash)) + return fmt.Errorf("Hash of downloaded file does not match with expected hash!\n download hash: %s\n expected hash: %s\n", calculatedHash, m.Download.Hash) } return nil