From 94368579721d8892011bf57f22e84a46545c93f4 Mon Sep 17 00:00:00 2001 From: Marwan Hawari <59078997+marwanhawari@users.noreply.github.com> Date: Sun, 19 Jan 2025 15:34:33 -0500 Subject: [PATCH] chore: disallow self-install/upgrade (#61) --- constants/constants.go | 6 ++++++ lib/errors.go | 9 +++++++++ lib/github.go | 4 ++++ 3 files changed, 19 insertions(+) diff --git a/constants/constants.go b/constants/constants.go index 40a4e14..0a031da 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -45,3 +45,9 @@ var RegexGithubSearch = `(?i)^[A-Za-z0-9\_\.\-\/\:]+$` // RegexURL is a regular express for valid URLs var RegexURL = `(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])` + +// StewOwner is the username of the stew github repo owner +var StewOwner = `marwanhawari` + +// StewRepo is the name of the stew github repo +var StewRepo = `stew` diff --git a/lib/errors.go b/lib/errors.go index a5399b5..6437f86 100644 --- a/lib/errors.go +++ b/lib/errors.go @@ -163,6 +163,7 @@ func (e InvalidGithubSearchQueryError) Error() string { return fmt.Sprintf("%v The search query %v contains invalid characters", constants.RedColor("Error:"), constants.RedColor(e.SearchQuery)) } +// BinaryMismatchError occurs if the downloaded binary hash doesn't match the hash in the lockfile type BinaryMismatchError struct { BinaryName string } @@ -170,3 +171,11 @@ type BinaryMismatchError struct { 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)) } + +// SelfInstallError occurs when attempting to install or upgrade stew using stew +type SelfInstallError struct { +} + +func (e SelfInstallError) Error() string { + return fmt.Sprintf("%v Stew cannot self-install/self-upgrade. You should upgrade stew with the original install method, whether it was manually or through a package manager", constants.RedColor("Error:")) +} diff --git a/lib/github.go b/lib/github.go index ff477fb..60568a4 100644 --- a/lib/github.go +++ b/lib/github.go @@ -56,6 +56,10 @@ func getGithubJSON(owner, repo string) (string, error) { // NewGithubProject creates a new instance of the GithubProject struct func NewGithubProject(owner, repo string) (GithubProject, error) { + if owner == constants.StewOwner && repo == constants.StewRepo { + return GithubProject{}, SelfInstallError{} + } + ghJSON, err := getGithubJSON(owner, repo) if err != nil { return GithubProject{}, err