Skip to content

Commit f9c8f60

Browse files
authored
Merge pull request #465 from Doozers/fix/isma/rand-lib
fix: change rand lib(math/rand => crypto/rand)
2 parents db5cfec + a309523 commit f9c8f60

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

pkg/bastion/dbinit.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package bastion // import "moul.io/sshportal/pkg/bastion"
22

33
import (
4+
"crypto/rand"
45
"fmt"
56
"io/ioutil"
67
"log"
7-
"math/rand"
8+
"math/big"
89
"os"
910
"os/user"
1011
"strings"
@@ -617,7 +618,10 @@ func DBInit(db *gorm.DB) error {
617618
}
618619
if count == 0 {
619620
// if no admin, create an account for the first connection
620-
inviteToken := randStringBytes(16)
621+
inviteToken, err := randStringBytes(16)
622+
if err != nil {
623+
return err
624+
}
621625
if os.Getenv("SSHPORTAL_DEFAULT_ADMIN_INVITE_TOKEN") != "" {
622626
inviteToken = os.Getenv("SSHPORTAL_DEFAULT_ADMIN_INVITE_TOKEN")
623627
}
@@ -673,12 +677,16 @@ func DBInit(db *gorm.DB) error {
673677
}).Error
674678
}
675679

676-
func randStringBytes(n int) string {
680+
func randStringBytes(n int) (string, error) {
677681
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
678682

679683
b := make([]byte, n)
680684
for i := range b {
681-
b[i] = letterBytes[rand.Intn(len(letterBytes))]
685+
r, err := rand.Int(rand.Reader, big.NewInt(int64(len(letterBytes))))
686+
if err != nil {
687+
return "", fmt.Errorf("failed to generate random string: %s", err)
688+
}
689+
b[i] = letterBytes[r.Int64()]
682690
}
683-
return string(b)
691+
return string(b), nil
684692
}

pkg/bastion/shell.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1640,11 +1640,15 @@ GLOBAL OPTIONS:
16401640
name = c.String("name")
16411641
}
16421642

1643+
r, err := randStringBytes(16)
1644+
if err != nil {
1645+
return err
1646+
}
16431647
user := dbmodels.User{
16441648
Name: name,
16451649
Email: email,
16461650
Comment: c.String("comment"),
1647-
InviteToken: randStringBytes(16),
1651+
InviteToken: r,
16481652
}
16491653

16501654
if _, err := govalidator.ValidateStruct(user); err != nil {

0 commit comments

Comments
 (0)