Skip to content

Commit 83f2c86

Browse files
authored
ssh: make env command a passthrough (gogs#7868)
## Describe the pull request Fixes GHSA-vm62-9jw3-c8w3
1 parent 96cf4e9 commit 83f2c86

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

internal/database/ssh_key.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,9 @@ func GetPublicKeyByID(keyID int64) (*PublicKey, error) {
447447
return key, nil
448448
}
449449

450-
// SearchPublicKeyByContent searches content as prefix (leak e-mail part)
451-
// and returns public key found.
450+
// SearchPublicKeyByContent searches a public key using the content as prefix
451+
// (i.e. ignore the email part). It returns ErrKeyNotExist if no such key
452+
// exists.
452453
func SearchPublicKeyByContent(content string) (*PublicKey, error) {
453454
key := new(PublicKey)
454455
has, err := x.Where("content like ?", content+"%").Get(key)

internal/ssh/ssh.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package ssh
66

77
import (
88
"context"
9-
"fmt"
109
"io"
1110
"net"
1211
"os"
@@ -55,26 +54,8 @@ func handleServerConn(keyID string, chans <-chan ssh.NewChannel) {
5554
payload := cleanCommand(string(req.Payload))
5655
switch req.Type {
5756
case "env":
58-
var env struct {
59-
Name string
60-
Value string
61-
}
62-
if err := ssh.Unmarshal(req.Payload, &env); err != nil {
63-
log.Warn("SSH: Invalid env payload %q: %v", req.Payload, err)
64-
continue
65-
}
66-
// Sometimes the client could send malformed command (i.e. missing "="),
67-
// see https://discuss.gogs.io/t/ssh/3106.
68-
if env.Name == "" || env.Value == "" {
69-
log.Warn("SSH: Invalid env arguments: %+v", env)
70-
continue
71-
}
72-
73-
_, stderr, err := com.ExecCmd("env", fmt.Sprintf("%s=%s", env.Name, env.Value))
74-
if err != nil {
75-
log.Error("env: %v - %s", err, stderr)
76-
return
77-
}
57+
// We only need to accept the request and do nothing since whatever environment
58+
// variables being set here won't be used in subsequent commands anyway.
7859

7960
case "exec":
8061
cmdName := strings.TrimLeft(payload, "'()")
@@ -175,7 +156,9 @@ func Listen(opts conf.SSHOpts, appDataPath string) {
175156
PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
176157
pkey, err := database.SearchPublicKeyByContent(strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key))))
177158
if err != nil {
178-
log.Error("SearchPublicKeyByContent: %v", err)
159+
if !database.IsErrKeyNotExist(err) {
160+
log.Error("SearchPublicKeyByContent: %v", err)
161+
}
179162
return nil, err
180163
}
181164
return &ssh.Permissions{Extensions: map[string]string{"key-id": com.ToStr(pkey.ID)}}, nil

0 commit comments

Comments
 (0)