From d19287d5b7826f0c1122a6ce29480a7ea5c40c27 Mon Sep 17 00:00:00 2001 From: wameidemao Date: Sun, 12 Apr 2020 12:18:43 +0800 Subject: ssh: improve env command processing (#6095) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * modify ssh env command processing mode * Update internal/ssh/ssh.go Co-Authored-By: ᴜɴᴋɴᴡᴏɴ * Update internal/ssh/ssh.go Co-Authored-By: ᴜɴᴋɴᴡᴏɴ * Update internal/ssh/ssh.go Co-Authored-By: ᴜɴᴋɴᴡᴏɴ * Update ssh.go Co-authored-by: ᴜɴᴋɴᴡᴏɴ --- internal/ssh/ssh.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'internal/ssh') diff --git a/internal/ssh/ssh.go b/internal/ssh/ssh.go index cb9d5ed7..ff9fb8cc 100644 --- a/internal/ssh/ssh.go +++ b/internal/ssh/ssh.go @@ -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 -- cgit v1.2.3