Skip to content

Fix parsing of received environment variables from SSH clients #1935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions modules/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import (
"code.gitea.io/gitea/modules/setting"
)

// environmentVariable will be used to parse environment variables from the SSH wire format.
type environmentVariable struct {
Name string
Value string
}

func cleanCommand(cmd string) string {
i := strings.Index(cmd, "git")
if i == -1 {
Expand Down Expand Up @@ -48,16 +54,14 @@ 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)
env := environmentVariable{}
if err := ssh.Unmarshal(req.Payload, &env); err != nil {
log.Warn("SSH: Unable to parse environment variable: %v", err)
continue
}
args[0] = strings.TrimLeft(args[0], "\x04")
_, _, err := com.ExecCmdBytes("env", args[0]+"="+args[1])
if err != nil {
log.Error(3, "env: %v", err)
return
if _, _, err = com.ExecCmdBytes("env", env.Name+"="+env.Value); err != nil {
log.Warn("SSH: Unable to set environment variable %v=%v: %v", env.Name, env.Value, err)
continue
}
case "exec":
cmdName := strings.TrimLeft(payload, "'()")
Expand Down