sftpfs: improve endpoint validation

Validation will fail if the endpoint is not specified as host:port
This commit is contained in:
Nicola Murino 2021-02-03 11:29:04 +01:00
parent 1cde50f050
commit 70bb3c34ce
No known key found for this signature in database
GPG key ID: 2F1FB59433D5A8CB
2 changed files with 11 additions and 2 deletions

View file

@ -1463,11 +1463,16 @@ func TestUserSFTPFs(t *testing.T) {
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated) user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
assert.NoError(t, err) assert.NoError(t, err)
user.FsConfig.Provider = dataprovider.SFTPFilesystemProvider user.FsConfig.Provider = dataprovider.SFTPFilesystemProvider
user.FsConfig.SFTPConfig.Endpoint = "127.0.0.1:2022" user.FsConfig.SFTPConfig.Endpoint = "127.0.0.1" // missing port
user.FsConfig.SFTPConfig.Username = "sftp_user" user.FsConfig.SFTPConfig.Username = "sftp_user"
user.FsConfig.SFTPConfig.Password = kms.NewPlainSecret("sftp_pwd") user.FsConfig.SFTPConfig.Password = kms.NewPlainSecret("sftp_pwd")
user.FsConfig.SFTPConfig.PrivateKey = kms.NewPlainSecret(sftpPrivateKey) user.FsConfig.SFTPConfig.PrivateKey = kms.NewPlainSecret(sftpPrivateKey)
user.FsConfig.SFTPConfig.Fingerprints = []string{sftpPkeyFingerprint} user.FsConfig.SFTPConfig.Fingerprints = []string{sftpPkeyFingerprint}
_, resp, err := httpdtest.UpdateUser(user, http.StatusBadRequest, "")
assert.NoError(t, err)
assert.Contains(t, string(resp), "invalid endpoint")
user.FsConfig.SFTPConfig.Endpoint = "127.0.0.1:2022"
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "") user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "/", user.FsConfig.SFTPConfig.Prefix) assert.Equal(t, "/", user.FsConfig.SFTPConfig.Prefix)
@ -5512,7 +5517,7 @@ func TestWebUserSFTPFsMock(t *testing.T) {
err = render.DecodeJSON(rr.Body, &user) err = render.DecodeJSON(rr.Body, &user)
assert.NoError(t, err) assert.NoError(t, err)
user.FsConfig.Provider = dataprovider.SFTPFilesystemProvider user.FsConfig.Provider = dataprovider.SFTPFilesystemProvider
user.FsConfig.SFTPConfig.Endpoint = "127.0.0.1" user.FsConfig.SFTPConfig.Endpoint = "127.0.0.1:22"
user.FsConfig.SFTPConfig.Username = "sftpuser" user.FsConfig.SFTPConfig.Username = "sftpuser"
user.FsConfig.SFTPConfig.Password = kms.NewPlainSecret("pwd") user.FsConfig.SFTPConfig.Password = kms.NewPlainSecret("pwd")
user.FsConfig.SFTPConfig.PrivateKey = kms.NewPlainSecret(sftpPrivateKey) user.FsConfig.SFTPConfig.PrivateKey = kms.NewPlainSecret(sftpPrivateKey)

View file

@ -55,6 +55,10 @@ func (c *SFTPFsConfig) Validate() error {
if c.Endpoint == "" { if c.Endpoint == "" {
return errors.New("endpoint cannot be empty") return errors.New("endpoint cannot be empty")
} }
_, _, err := net.SplitHostPort(c.Endpoint)
if err != nil {
return fmt.Errorf("invalid endpoint: %v", err)
}
if c.Username == "" { if c.Username == "" {
return errors.New("username cannot be empty") return errors.New("username cannot be empty")
} }