fix(autobrrctl): prevent empty password (#1468)

* fix(autobrrctl): prevent empty password

* fix(autobrrctl): prevent empty password for create-user aswell
fix(autobrrctl): stringify password in checks

* feat(autobrrctl): validate password length

---------

Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
martylukyy 2024-03-24 10:39:51 +01:00 committed by GitHub
parent 65b42f517d
commit 2337ee4d75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 13 deletions

View file

@ -286,32 +286,32 @@ func main() {
} }
} }
func readPassword() ([]byte, error) { func readPassword() (password []byte, err error) {
var password []byte
var err error
fd := int(os.Stdin.Fd()) fd := int(os.Stdin.Fd())
if term.IsTerminal(fd) { if term.IsTerminal(fd) {
fmt.Printf("Password: ") fmt.Printf("Password: ")
password, err = term.ReadPassword(int(os.Stdin.Fd())) password, err = term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return nil, err
}
fmt.Printf("\n") fmt.Printf("\n")
if err != nil {
return nil, errors.Wrap(err, "failed to read password from terminal")
}
} else { } else {
//fmt.Fprintf(os.Stderr, "warning: Reading password from stdin.\n")
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
if !scanner.Scan() { if !scanner.Scan() {
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
log.Fatalf("failed to read password from stdin: %v", err) return nil, errors.Wrap(err, "failed to read password from stdin")
} }
log.Fatalf("failed to read password from stdin: stdin is empty %v", err)
}
password = scanner.Bytes()
if len(password) == 0 { return nil, errors.New("password input is empty")
return nil, errors.New("zero length password")
} }
password = scanner.Bytes()
}
// make sure the password is not empty
if len(password) == 0 {
return nil, errors.New("zero length password")
} }
return password, nil return password, nil

View file

@ -154,5 +154,9 @@ func (s *service) ComparePasswordAndHash(password string, hash string) (match bo
} }
func (s *service) CreateHash(password string) (hash string, err error) { func (s *service) CreateHash(password string) (hash string, err error) {
if password == "" {
return "", errors.New("must supply non empty password to CreateHash")
}
return argon2id.CreateHash(password, argon2id.DefaultParams) return argon2id.CreateHash(password, argon2id.DefaultParams)
} }