diff --git a/packages/util/check-for-update.go b/packages/util/check-for-update.go index 33289140..6c49a68c 100644 --- a/packages/util/check-for-update.go +++ b/packages/util/check-for-update.go @@ -9,6 +9,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "runtime" "strings" "sync" @@ -296,6 +297,32 @@ func GetUpdateInstructions() string { return getUpdateInstructions(runtime.GOOS, execPath) } +var homebrewTapName = regexp.MustCompile(`^[a-z0-9][a-z0-9_.-]*/[a-z0-9][a-z0-9_.-]*$`) + +// homebrewFormula names the formula the way `brew upgrade` will act on it. Homebrew skips a formula +// from a tap the user hasn't trusted when it's named by its short name, but trusts it when it's named +// in full, so an install from a tap gets its full name. The tap comes from the receipt Homebrew writes +// next to the binary (Cellar/infisical//INSTALL_RECEIPT.json). +func homebrewFormula(execPath string) string { + receipt, err := os.ReadFile(filepath.Join(filepath.Dir(filepath.Dir(execPath)), "INSTALL_RECEIPT.json")) + if err != nil { + return "infisical" + } + var parsed struct { + Source struct { + Tap string `json:"tap"` + } `json:"source"` + } + if json.Unmarshal(receipt, &parsed) != nil { + return "infisical" + } + tap := parsed.Source.Tap + if tap == "" || tap == "homebrew/core" || !homebrewTapName.MatchString(tap) { + return "infisical" + } + return tap + "/infisical" +} + func getUpdateInstructions(goos string, execPath string) string { p := strings.ToLower(execPath) isNpm := strings.Contains(p, "node_modules") @@ -306,7 +333,7 @@ func getUpdateInstructions(goos string, execPath string) string { return "To update, run: npm update -g @infisical/cli" } if strings.Contains(p, "/homebrew/") || strings.Contains(p, "/cellar/") { - return "To update, run: brew update && brew upgrade infisical" + return "To update, run: brew update && brew upgrade " + homebrewFormula(execPath) } return "" case "windows": diff --git a/packages/util/check-for-update_test.go b/packages/util/check-for-update_test.go index f460118f..db2d6973 100644 --- a/packages/util/check-for-update_test.go +++ b/packages/util/check-for-update_test.go @@ -397,3 +397,35 @@ func TestWriteUpdateCheckCache_AtomicWrite(t *testing.T) { t.Errorf("got permissions %o, want 0600", perm) } } + +func TestHomebrewUpdateInstructionsNameTheTap(t *testing.T) { + keg := func(t *testing.T, receipt string) string { + dir := filepath.Join(t.TempDir(), "Cellar", "infisical", "0.45.0") + if err := os.MkdirAll(filepath.Join(dir, "bin"), 0o755); err != nil { + t.Fatal(err) + } + if receipt != "" { + if err := os.WriteFile(filepath.Join(dir, "INSTALL_RECEIPT.json"), []byte(receipt), 0o644); err != nil { + t.Fatal(err) + } + } + return filepath.Join(dir, "bin", "infisical") + } + tests := []struct { + name, receipt, want string + }{ + {"infisical tap", `{"source":{"tap":"infisical/get-cli"}}`, "brew update && brew upgrade infisical/get-cli/infisical"}, + {"homebrew core", `{"source":{"tap":"homebrew/core"}}`, "brew update && brew upgrade infisical"}, + {"no receipt", "", "brew update && brew upgrade infisical"}, + {"unreadable receipt", `not json`, "brew update && brew upgrade infisical"}, + {"no tap in receipt", `{"source":{}}`, "brew update && brew upgrade infisical"}, + {"unsafe tap name", `{"source":{"tap":"evil/tap; rm -rf ~"}}`, "brew update && brew upgrade infisical"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := getUpdateInstructions("darwin", filepath.ToSlash(keg(t, tt.receipt))); got != "To update, run: "+tt.want { + t.Errorf("got %q, want %q", got, "To update, run: "+tt.want) + } + }) + } +}