File tree 2 files changed +18
-6
lines changed
2 files changed +18
-6
lines changed Original file line number Diff line number Diff line change 1
1
package bastion // import "moul.io/sshportal/pkg/bastion"
2
2
3
3
import (
4
+ "crypto/rand"
4
5
"fmt"
5
6
"io/ioutil"
6
7
"log"
7
- "math/rand "
8
+ "math/big "
8
9
"os"
9
10
"os/user"
10
11
"strings"
@@ -617,7 +618,10 @@ func DBInit(db *gorm.DB) error {
617
618
}
618
619
if count == 0 {
619
620
// 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
+ }
621
625
if os .Getenv ("SSHPORTAL_DEFAULT_ADMIN_INVITE_TOKEN" ) != "" {
622
626
inviteToken = os .Getenv ("SSHPORTAL_DEFAULT_ADMIN_INVITE_TOKEN" )
623
627
}
@@ -673,12 +677,16 @@ func DBInit(db *gorm.DB) error {
673
677
}).Error
674
678
}
675
679
676
- func randStringBytes (n int ) string {
680
+ func randStringBytes (n int ) ( string , error ) {
677
681
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
678
682
679
683
b := make ([]byte , n )
680
684
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 ()]
682
690
}
683
- return string (b )
691
+ return string (b ), nil
684
692
}
Original file line number Diff line number Diff line change @@ -1640,11 +1640,15 @@ GLOBAL OPTIONS:
1640
1640
name = c .String ("name" )
1641
1641
}
1642
1642
1643
+ r , err := randStringBytes (16 )
1644
+ if err != nil {
1645
+ return err
1646
+ }
1643
1647
user := dbmodels.User {
1644
1648
Name : name ,
1645
1649
Email : email ,
1646
1650
Comment : c .String ("comment" ),
1647
- InviteToken : randStringBytes ( 16 ) ,
1651
+ InviteToken : r ,
1648
1652
}
1649
1653
1650
1654
if _ , err := govalidator .ValidateStruct (user ); err != nil {
You can’t perform that action at this time.
0 commit comments