From 404f24d3613c89cfd65fb1b655cc328b73adb5cf Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Fri, 8 Mar 2024 11:23:08 -0500 Subject: [PATCH] Better sanitize user input in inputInt --- src/cli/add.go | 2 +- src/cli/edit.go | 2 +- src/cli/init.go | 6 ++++++ src/cli/utilitiesMisc.go | 13 +++++++++---- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/cli/add.go b/src/cli/add.go index 32d4ffd..e0a8cd5 100644 --- a/src/cli/add.go +++ b/src/cli/add.go @@ -25,7 +25,7 @@ func AddEntry(targetLocation string, hidePassword bool, entryType uint8) { if entryType == 0 { password = inputHidden("Password:") } else { - password = offline.StringGen(inputInt("Password length:"), inputBinary("Generate a complex (special characters) password?"), 0.2) + password = offline.StringGen(inputInt("Password length:", -1), inputBinary("Generate a complex (special characters) password?"), 0.2) } url := input("URL:") diff --git a/src/cli/edit.go b/src/cli/edit.go index d719597..0b770e4 100644 --- a/src/cli/edit.go +++ b/src/cli/edit.go @@ -82,7 +82,7 @@ func GenUpdate(targetLocation string, hidePassword bool) { unencryptedEntry := offline.DecryptGPG(targetLocation) // generate a new password - unencryptedEntry[0] = offline.StringGen(inputInt("Password length:"), inputBinary("Generate a complex (special characters) password?"), 0.2) + unencryptedEntry[0] = offline.StringGen(inputInt("Password length:", -1), inputBinary("Generate a complex (special characters) password?"), 0.2) // write and preview the modified entry writeEntryShortcut(targetLocation, unencryptedEntry, hidePassword) diff --git a/src/cli/init.go b/src/cli/init.go index 9ee9af9..ae9e297 100644 --- a/src/cli/init.go +++ b/src/cli/init.go @@ -1,7 +1,9 @@ package cli import ( + "fmt" "github.com/rwinkhart/MUTN/src/offline" + "os" ) // TempInitCli initializes the MUTN environment based on user input @@ -14,6 +16,10 @@ func TempInitCli() { // select GPG key from menu uidSlice := offline.GpgUIDListGen() gpgIDInt := inputMenuGen("Select GPG key:", uidSlice) + if gpgIDInt == 0 { + fmt.Println(offline.AnsiError + "No GPG keys found - please generate one" + offline.AnsiReset) + os.Exit(1) + } gpgID = uidSlice[gpgIDInt-1] } diff --git a/src/cli/utilitiesMisc.go b/src/cli/utilitiesMisc.go index 8764c12..59874dd 100644 --- a/src/cli/utilitiesMisc.go +++ b/src/cli/utilitiesMisc.go @@ -34,14 +34,19 @@ func inputHidden(prompt string) string { return password } -// inputInt prompts the user for input and returns the input as an integer (0 is not a valid input) -func inputInt(prompt string) int { +// inputInt prompts the user for input and returns the input as an integer +// a maxValue of 0 will cause the function to return 0, an error - a negative maxValue will disable the maxValue check +func inputInt(prompt string, maxValue int) int { + if maxValue == 0 { + return 0 + } + // loop until a valid integer is entered for { fmt.Print("\n" + prompt + " ") var userInput int _, err := fmt.Scanln(&userInput) - if err == nil && userInput > 0 { + if err == nil && userInput > 0 && (userInput <= maxValue || maxValue < 0) { return userInput } } @@ -63,7 +68,7 @@ func inputMenuGen(prompt string, options []string) int { for i, option := range options { fmt.Printf("%d. %s\n", i+1, option) } - return inputInt(prompt) + return inputInt(prompt, len(options)) } // writeEntryShortcut writes an entry to targetLocation (trimming trailing blank lines) and previews it, or errors if no data is supplied