fix: improve private key parsing by handling escaped newlines and whitespace in credentials

This commit is contained in:
2026-06-09 15:22:41 +01:00
parent f27a5830ea
commit 33c26ec310
+11 -2
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"path/filepath"
"strings"
"time"
"github.com/pkg/sftp"
@@ -32,8 +33,16 @@ func (e *SFTPExplorer) Connect() error {
}
var authMethod ssh.AuthMethod
signer, err := ssh.ParsePrivateKey([]byte(e.secret))
if err == nil {
secretStr := strings.TrimSpace(e.secret)
// Handle case where newlines might be escaped by mistake
secretStr = strings.ReplaceAll(secretStr, "\\n", "\n")
if strings.Contains(secretStr, "-----BEGIN") {
signer, err := ssh.ParsePrivateKey([]byte(secretStr))
if err != nil {
return fmt.Errorf("failed to parse private key: %w", err)
}
authMethod = ssh.PublicKeys(signer)
} else {
authMethod = ssh.Password(e.secret)