From 0fda2ececc674f289d0e77e43d622d26d11642f0 Mon Sep 17 00:00:00 2001 From: thielepaul Date: Fri, 23 Oct 2020 08:54:54 +0200 Subject: [PATCH] fix (sftp): use password type for sftp password and passphrase field (#324) --- server/model/backend/sftp.go | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/server/model/backend/sftp.go b/server/model/backend/sftp.go index a71f4f4e..95ec644b 100644 --- a/server/model/backend/sftp.go +++ b/server/model/backend/sftp.go @@ -55,11 +55,12 @@ func (s Sftp) Init(params map[string]string, app *App) (IBackend, error) { addr := p.hostname + ":" + p.port var auth []ssh.AuthMethod + keyStartMatcher := regexp.MustCompile(`^-----BEGIN [A-Z\ ]+-----`) + keyEndMatcher := regexp.MustCompile(`-----END [A-Z\ ]+-----$`) + keyContentMatcher := regexp.MustCompile(`^[a-zA-Z0-9\+\/\=\n]+$`) + isPrivateKey := func(pass string) bool { p := strings.TrimSpace(pass) - keyStartMatcher := regexp.MustCompile(`^-----BEGIN [A-Z\ ]+-----`) - keyEndMatcher := regexp.MustCompile(`-----END [A-Z\ ]+-----$`) - keyContentMatcher := regexp.MustCompile(`^[a-zA-Z0-9\+\/\=\n]+$`) // match private key beginning if keyStartMatcher.FindStringIndex(p) == nil { @@ -71,6 +72,7 @@ func (s Sftp) Init(params map[string]string, app *App) (IBackend, error) { return false } p = keyEndMatcher.ReplaceAllString(p, "") + p = strings.Replace(p, " ", "", -1) // match private key content if keyContentMatcher.FindStringIndex(p) == nil { return false @@ -78,12 +80,26 @@ func (s Sftp) Init(params map[string]string, app *App) (IBackend, error) { return true } + restorePrivateKeyLineBreaks := func(pass string) string { + p := strings.TrimSpace(pass) + + keyStartString := keyStartMatcher.FindString(p) + p = keyStartMatcher.ReplaceAllString(p, "") + keyEndString := keyEndMatcher.FindString(p) + p = keyEndMatcher.ReplaceAllString(p, "") + p = strings.Replace(p, " ", "", -1) + keyContentString := keyContentMatcher.FindString(p) + + return keyStartString + "\n" + keyContentString + "\n" + keyEndString + } + if isPrivateKey(p.password) { + privateKey := restorePrivateKeyLineBreaks(p.password) signer, err := func() (ssh.Signer, error) { if p.passphrase == "" { - return ssh.ParsePrivateKey([]byte(p.password)) + return ssh.ParsePrivateKey([]byte(privateKey)) } - return ssh.ParsePrivateKeyWithPassphrase([]byte(p.password), []byte(p.passphrase)) + return ssh.ParsePrivateKeyWithPassphrase([]byte(privateKey), []byte(p.passphrase)) }() if err != nil { return nil, err @@ -143,7 +159,7 @@ func (b Sftp) LoginForm() Form { }, FormElement{ Name: "password", - Type: "long_password", + Type: "password", Placeholder: "Password", }, FormElement{ @@ -168,7 +184,7 @@ func (b Sftp) LoginForm() Form { FormElement{ Id: "sftp_passphrase", Name: "passphrase", - Type: "text", + Type: "password", Placeholder: "Passphrase", }, FormElement{