add input validation on parsing
* add input validation on parsing
* updates from pr comments
updates error output to be more correct/descriptive
adds warning about unhandled newline conventions
updates split to handle ":" as a valid password char
@@ -66,8 +66,25 @@ func UserHomeDir() string {
}
func parseCredentials(dat string) msgs.BasicAuthCredentials {
+ // splits by new line to ensure that user on has one line in pgouser file/creds
+ // this split does not take into account newline conventions of different systems
+ // ex. windows new lines ("\r\n")
+ lines := strings.Split(strings.TrimSpace(dat), "\n")
+ if (len(lines) != 1){
+ log.Debugf("expected one and only one line in pgouser file - found %d", len(lines))
+ fmt.Println("unable to parse credentials in pgouser file",)
+ os.Exit(2) // TODO: graceful exit
+ }
- fields := strings.Split(strings.TrimSpace(dat), ":")
+ // the delimiting char ":" is a valid password char so SplitN will handle if
+ // ":" is used by always splitting into two substrings including the username
+ // and everything after the first ":"
+ fields := strings.SplitN(lines[0], ":", 2)
+ if len(fields) != 2 {
+ log.Debug("invalid credential format: expecting \"<username>:<password>\"")
+ fmt.Println("unable to parse credentials in pgouser file",)
+ os.Exit(2) // TODO: graceful exit
+ }
log.Debugf("%v", fields)
log.Debugf("username=[%s] password=[%s]", fields[0], fields[1])
