@@ -45,7 +45,7 @@ type Server struct {
45
45
config * ssh.ServerConfig
46
46
shell string
47
47
shellCreds * syscall.Credential
48
- passEnv bool
48
+ envWhitelist map [ string ] bool
49
49
errorHandler ErrorHandler
50
50
verbosity int
51
51
}
@@ -57,13 +57,13 @@ type ErrorHandler func(error, map[string]string)
57
57
// and an ssh.ServerConfig. If no ServerConfig is provided, then
58
58
// ServerConfig.NoClientAuth is set to true. ed25519, rsa, ecdsa and dsa
59
59
// keys are loaded, and generated if they do not exist. Returns a new Server.
60
- func New (keyDir , shell string , passEnv bool , shellCreds * syscall.Credential , verbosity int , sshConfig * ssh.ServerConfig , errorHandler ErrorHandler ) (* Server , error ) {
60
+ func New (keyDir , shell string , envWhitelist [] string , shellCreds * syscall.Credential , verbosity int , sshConfig * ssh.ServerConfig , errorHandler ErrorHandler ) (* Server , error ) {
61
61
s := & Server {
62
62
keyDir : keyDir ,
63
63
config : sshConfig ,
64
64
shell : shell ,
65
65
shellCreds : shellCreds ,
66
- passEnv : passEnv ,
66
+ envWhitelist : make ( map [ string ] bool ) ,
67
67
errorHandler : errorHandler ,
68
68
verbosity : verbosity ,
69
69
}
@@ -72,6 +72,9 @@ func New(keyDir, shell string, passEnv bool, shellCreds *syscall.Credential, ver
72
72
NoClientAuth : true ,
73
73
}
74
74
}
75
+ for _ , envKey := range envWhitelist {
76
+ s .envWhitelist [envKey ] = true
77
+ }
75
78
for _ , keyType := range []string {"ed25519" , "rsa" , "ecdsa" , "dsa" } {
76
79
if err := s .addHostKey (keyType ); err != nil {
77
80
return nil , err
@@ -211,15 +214,16 @@ func (s *Server) handleRequests(reqs <-chan *ssh.Request, channel ssh.Channel, c
211
214
for req := range reqs {
212
215
switch req .Type {
213
216
case "env" :
214
- if s .passEnv {
215
- // append client env to the command environment
216
- keyLen := binary .BigEndian .Uint32 (req .Payload [:4 ])
217
- valLen := binary .BigEndian .Uint32 (req .Payload [keyLen + 4 : keyLen + 8 ])
218
- key := string (req .Payload [4 : keyLen + 4 ])
217
+ // append client env to the command environment
218
+ keyLen := binary .BigEndian .Uint32 (req .Payload [:4 ])
219
+ valLen := binary .BigEndian .Uint32 (req .Payload [keyLen + 4 : keyLen + 8 ])
220
+ key := string (req .Payload [4 : keyLen + 4 ])
221
+ _ , ok := s .envWhitelist [key ]
222
+ if ok {
219
223
val := string (req .Payload [keyLen + 8 : keyLen + valLen + 8 ])
220
224
env = append (env , fmt .Sprintf ("%s=%s" , key , val ))
221
225
}
222
- if err := req .Reply (s . passEnv , nil ); err != nil {
226
+ if err := req .Reply (ok , nil ); err != nil {
223
227
return err
224
228
}
225
229
case "pty-req" :
0 commit comments