From 65ee9808279724472cfbca19f1244b0dbe48d436 Mon Sep 17 00:00:00 2001 From: jabuxas Date: Fri, 20 Sep 2024 15:19:25 -0300 Subject: [PATCH] feat: sanitize input before passing it around --- helpers.go | 13 ++++++++++++- spire.go | 10 +++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/helpers.go b/helpers.go index d3b1835..8a681ed 100644 --- a/helpers.go +++ b/helpers.go @@ -7,6 +7,7 @@ import ( "net/http" "os" "path/filepath" + "strings" ) func DownloadCache() error { @@ -51,7 +52,7 @@ func DownloadBepinex() error { return fmt.Errorf("could not save file: %w", err) } - err = unzip(tmpPath, GAME_LOCATION) + err = unzip(tmpPath, GAME_PATH) if err != nil { return fmt.Errorf("could not extract BepInEx: %w", err) } @@ -100,3 +101,13 @@ func unzip(src, dest string) error { } return nil } + +func SanitizeInput(input *string) error { + if *input == "" { + return fmt.Errorf("%s is not set", *input) + } + if strings.HasSuffix(*input, "/") { + *input = strings.TrimSuffix(*input, "/") + } + return nil +} diff --git a/spire.go b/spire.go index 19fe3bd..c24bb68 100644 --- a/spire.go +++ b/spire.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "log" "os" "time" ) @@ -11,10 +12,13 @@ const ( PAYLOAD = TMPDIR + "/payload.json" ) -var GAME_LOCATION = os.Getenv("GAME_PATH") +var GAME_PATH = os.Getenv("GAME_PATH") func main() { getCache() + if err := SanitizeInput(&GAME_PATH); err != nil { + log.Fatal("GAME_PATH is not set") + } installBepinex() } @@ -35,12 +39,12 @@ func getCache() error { } func installBepinex() { - _, err := os.Stat(GAME_LOCATION + "/BepInEx") + _, err := os.Stat(GAME_PATH + "/BepInEx") if err != nil { // install bepinex DownloadBepinex() } else { - fmt.Print("bepinex already installed") + fmt.Println("bepinex already installed") } }