ssh: improve env command processing (#6095)

* modify ssh env command processing mode

* Update internal/ssh/ssh.go

Co-Authored-By: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>

* Update internal/ssh/ssh.go

Co-Authored-By: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>

* Update internal/ssh/ssh.go

Co-Authored-By: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>

* Update ssh.go

Co-authored-by: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>
This commit is contained in:
wameidemao 2020-04-12 12:18:43 +08:00 committed by GitHub
parent 26a2d0b2a1
commit d19287d5b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -51,20 +51,22 @@ func handleServerConn(keyID string, chans <-chan ssh.NewChannel) {
payload := cleanCommand(string(req.Payload))
switch req.Type {
case "env":
args := strings.Split(strings.Replace(payload, "\x00", "", -1), "\v")
if len(args) != 2 {
log.Warn("SSH: Invalid env arguments: '%#v'", args)
var env struct {
Name string
Value string
}
if err := ssh.Unmarshal(req.Payload, &env); err != nil {
log.Warn("SSH: Invalid env payload %q: %v", req.Payload, err)
continue
}
args[0] = strings.TrimLeft(args[0], "\x04")
// Sometimes the client could send malformed command (i.e. missing "="),
// see https://discuss.gogs.io/t/ssh/3106.
if args[0] == "" {
if env.Name == "" || env.Value == "" {
log.Warn("SSH: Invalid env arguments: %+v", env)
continue
}
_, stderr, err := com.ExecCmd("env", args[0]+"="+args[1])
_, stderr, err := com.ExecCmd("env", fmt.Sprintf("%s=%s", env.Name, env.Value))
if err != nil {
log.Error("env: %v - %s", err, stderr)
return