File tree Expand file tree Collapse file tree 2 files changed +18
-6
lines changed Expand file tree Collapse file tree 2 files changed +18
-6
lines changed Original file line number Diff line number Diff line change 11package bastion // import "moul.io/sshportal/pkg/bastion"
22
33import (
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}
Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments