Better sanitize user input in inputInt

This commit is contained in:
2024-03-08 11:23:08 -05:00
parent 8e1987f250
commit f8bf12677f
4 changed files with 17 additions and 6 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ func AddEntry(targetLocation string, hidePassword bool, entryType uint8) {
if entryType == 0 { if entryType == 0 {
password = inputHidden("Password:") password = inputHidden("Password:")
} else { } 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:") url := input("URL:")
+1 -1
View File
@@ -82,7 +82,7 @@ func GenUpdate(targetLocation string, hidePassword bool) {
unencryptedEntry := offline.DecryptGPG(targetLocation) unencryptedEntry := offline.DecryptGPG(targetLocation)
// generate a new password // 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 // write and preview the modified entry
writeEntryShortcut(targetLocation, unencryptedEntry, hidePassword) writeEntryShortcut(targetLocation, unencryptedEntry, hidePassword)
+6
View File
@@ -1,7 +1,9 @@
package cli package cli
import ( import (
"fmt"
"github.com/rwinkhart/MUTN/src/offline" "github.com/rwinkhart/MUTN/src/offline"
"os"
) )
// TempInitCli initializes the MUTN environment based on user input // TempInitCli initializes the MUTN environment based on user input
@@ -14,6 +16,10 @@ func TempInitCli() {
// select GPG key from menu // select GPG key from menu
uidSlice := offline.GpgUIDListGen() uidSlice := offline.GpgUIDListGen()
gpgIDInt := inputMenuGen("Select GPG key:", uidSlice) 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] gpgID = uidSlice[gpgIDInt-1]
} }
+9 -4
View File
@@ -34,14 +34,19 @@ func inputHidden(prompt string) string {
return password return password
} }
// inputInt prompts the user for input and returns the input as an integer (0 is not a valid input) // inputInt prompts the user for input and returns the input as an integer
func inputInt(prompt string) int { // 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 // loop until a valid integer is entered
for { for {
fmt.Print("\n" + prompt + " ") fmt.Print("\n" + prompt + " ")
var userInput int var userInput int
_, err := fmt.Scanln(&userInput) _, err := fmt.Scanln(&userInput)
if err == nil && userInput > 0 { if err == nil && userInput > 0 && (userInput <= maxValue || maxValue < 0) {
return userInput return userInput
} }
} }
@@ -63,7 +68,7 @@ func inputMenuGen(prompt string, options []string) int {
for i, option := range options { for i, option := range options {
fmt.Printf("%d. %s\n", i+1, option) 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 // writeEntryShortcut writes an entry to targetLocation (trimming trailing blank lines) and previews it, or errors if no data is supplied