Skip to content

Commit 0ac8cef

Browse files
committed
fix: generate random admin password on quick setup
This should help mitigate issues like filebrowser#3646
1 parent 35d1c09 commit 0ac8cef

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

cmd/root.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,12 @@ func quickSetup(flags *pflag.FlagSet, d pythonData) {
378378
password := getParam(flags, "password")
379379

380380
if password == "" {
381-
password, err = users.HashPwd("admin")
381+
pwd, err := users.RandomPwd()
382+
checkErr(err)
383+
384+
log.Println("Generated random admin password for quick setup:", pwd)
385+
386+
password, err = users.HashPwd(pwd)
382387
checkErr(err)
383388
}
384389

users/password.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package users
22

33
import (
4+
"crypto/rand"
5+
"encoding/base64"
46
"golang.org/x/crypto/bcrypt"
57
)
68

9+
// randomPasswordBytesCount is chosen to fit in a base64 string without padding
10+
const randomPasswordBytesCount = 9
11+
712
// HashPwd hashes a password.
813
func HashPwd(password string) (string, error) {
914
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
@@ -15,3 +20,15 @@ func CheckPwd(password, hash string) bool {
1520
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
1621
return err == nil
1722
}
23+
24+
func RandomPwd() (string, error) {
25+
randomPasswordBytes := make([]byte, randomPasswordBytesCount)
26+
var _, err = rand.Read(randomPasswordBytes)
27+
if err != nil {
28+
return "", err
29+
}
30+
31+
// This is done purely to make the password human-readable
32+
var randomPasswordString = base64.URLEncoding.EncodeToString(randomPasswordBytes)
33+
return randomPasswordString, nil
34+
}

0 commit comments

Comments
 (0)