@@ -3,7 +3,6 @@ package cli
3
3
import (
4
4
"context"
5
5
"io"
6
- "math/rand"
7
6
"net"
8
7
"os"
9
8
"strings"
@@ -20,6 +19,7 @@ import (
20
19
"github.com/coder/coder/cli/cliui"
21
20
"github.com/coder/coder/coderd/database"
22
21
"github.com/coder/coder/codersdk"
22
+ "github.com/coder/coder/cryptorand"
23
23
)
24
24
25
25
func ssh () * cobra.Command {
@@ -52,7 +52,11 @@ func ssh() *cobra.Command {
52
52
return xerrors .New ("no workspaces to shuffle" )
53
53
}
54
54
55
- workspace = workspaces [rand .Intn (len (workspaces ))]
55
+ idx , err := cryptorand .Intn (len (workspaces ))
56
+ if err != nil {
57
+ return err
58
+ }
59
+ workspace = workspaces [idx ]
56
60
} else {
57
61
err := cobra .MinimumNArgs (1 )(cmd , args )
58
62
if err != nil {
@@ -108,11 +112,14 @@ func ssh() *cobra.Command {
108
112
}
109
113
if agent .ID == uuid .Nil {
110
114
if len (agents ) > 1 {
111
- if shuffle {
112
- agent = agents [rand .Intn (len (agents ))]
113
- } else {
115
+ if ! shuffle {
114
116
return xerrors .New ("you must specify the name of an agent" )
115
117
}
118
+ idx , err := cryptorand .Intn (len (agents ))
119
+ if err != nil {
120
+ return err
121
+ }
122
+ agent = agents [idx ]
116
123
} else {
117
124
agent = agents [0 ]
118
125
}
@@ -191,7 +198,7 @@ func ssh() *cobra.Command {
191
198
}
192
199
cliflag .BoolVarP (cmd .Flags (), & stdio , "stdio" , "" , "CODER_SSH_STDIO" , false , "Specifies whether to emit SSH output over stdin/stdout." )
193
200
cliflag .BoolVarP (cmd .Flags (), & shuffle , "shuffle" , "" , "CODER_SSH_SHUFFLE" , false , "Specifies whether to choose a random workspace" )
194
- cmd .Flags ().MarkHidden ("shuffle" )
201
+ _ = cmd .Flags ().MarkHidden ("shuffle" )
195
202
196
203
return cmd
197
204
}
0 commit comments