Skip to content

Commit

Permalink
Add 'X-Forwarded-For' to (s)FTP requests (minio#20709)
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost authored Nov 29, 2024
1 parent f0d4ef6 commit abd6bf0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
10 changes: 8 additions & 2 deletions cmd/ftp-server-driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"path"
"strings"
Expand Down Expand Up @@ -286,6 +288,10 @@ func (driver *ftpDriver) CheckPasswd(c *ftp.Context, username, password string)
}

func (driver *ftpDriver) getMinIOClient(ctx *ftp.Context) (*minio.Client, error) {
tr := http.RoundTripper(globalRemoteFTPClientTransport)
if host, _, err := net.SplitHostPort(ctx.Sess.RemoteAddr().String()); err == nil {
tr = forwardForTransport{tr: tr, fwd: host}
}
ui, ok := globalIAMSys.GetUser(context.Background(), ctx.Sess.LoginUser())
if !ok && !globalIAMSys.LDAPConfig.Enabled() {
return nil, errNoSuchUser
Expand Down Expand Up @@ -363,7 +369,7 @@ func (driver *ftpDriver) getMinIOClient(ctx *ftp.Context) (*minio.Client, error)
return minio.New(driver.endpoint, &minio.Options{
Creds: mcreds,
Secure: globalIsTLS,
Transport: globalRemoteFTPClientTransport,
Transport: tr,
})
}

Expand All @@ -377,7 +383,7 @@ func (driver *ftpDriver) getMinIOClient(ctx *ftp.Context) (*minio.Client, error)
return minio.New(driver.endpoint, &minio.Options{
Creds: credentials.NewStaticV4(ui.Credentials.AccessKey, ui.Credentials.SecretKey, ""),
Secure: globalIsTLS,
Transport: globalRemoteFTPClientTransport,
Transport: tr,
})
}

Expand Down
27 changes: 24 additions & 3 deletions cmd/sftp-server-driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"io"
"net/http"
"os"
"path"
"strings"
Expand All @@ -45,6 +46,7 @@ const ftpMaxWriteOffset = 100 << 20
type sftpDriver struct {
permissions *ssh.Permissions
endpoint string
remoteIP string
}

//msgp:ignore sftpMetrics
Expand Down Expand Up @@ -89,8 +91,12 @@ func (m *sftpMetrics) log(s *sftp.Request, user string) func(sz int64, err error
// - sftp.Filewrite
// - sftp.Filelist
// - sftp.Filecmd
func NewSFTPDriver(perms *ssh.Permissions) sftp.Handlers {
handler := &sftpDriver{endpoint: fmt.Sprintf("127.0.0.1:%s", globalMinioPort), permissions: perms}
func NewSFTPDriver(perms *ssh.Permissions, remoteIP string) sftp.Handlers {
handler := &sftpDriver{
endpoint: fmt.Sprintf("127.0.0.1:%s", globalMinioPort),
permissions: perms,
remoteIP: remoteIP,
}
return sftp.Handlers{
FileGet: handler,
FilePut: handler,
Expand All @@ -99,16 +105,31 @@ func NewSFTPDriver(perms *ssh.Permissions) sftp.Handlers {
}
}

type forwardForTransport struct {
tr http.RoundTripper
fwd string
}

func (f forwardForTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("X-Forwarded-For", f.fwd)
return f.tr.RoundTrip(r)
}

func (f *sftpDriver) getMinIOClient() (*minio.Client, error) {
mcreds := credentials.NewStaticV4(
f.permissions.CriticalOptions["AccessKey"],
f.permissions.CriticalOptions["SecretKey"],
f.permissions.CriticalOptions["SessionToken"],
)
// Set X-Forwarded-For on all requests.
tr := http.RoundTripper(globalRemoteFTPClientTransport)
if f.remoteIP != "" {
tr = forwardForTransport{tr: tr, fwd: f.remoteIP}
}
return minio.New(f.endpoint, &minio.Options{
Creds: mcreds,
Secure: globalIsTLS,
Transport: globalRemoteFTPClientTransport,
Transport: tr,
})
}

Expand Down
7 changes: 6 additions & 1 deletion cmd/sftp-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,12 @@ func startSFTPServer(args []string) {
sshConfig.AddHostKey(private)

handleSFTPSession := func(channel ssh.Channel, sconn *ssh.ServerConn) {
server := sftp.NewRequestServer(channel, NewSFTPDriver(sconn.Permissions), sftp.WithRSAllocator())
var remoteIP string

if host, _, err := net.SplitHostPort(sconn.RemoteAddr().String()); err == nil {
remoteIP = host
}
server := sftp.NewRequestServer(channel, NewSFTPDriver(sconn.Permissions, remoteIP), sftp.WithRSAllocator())
defer server.Close()
server.Serve()
}
Expand Down

0 comments on commit abd6bf0

Please sign in to comment.