From e1969d1e3360fd268184b810b5f08ad327725c66 Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Wed, 19 Feb 2020 15:53:20 +0100 Subject: [PATCH] Put host last in SSH command line This is how the SSH manpage says the command line should look, and the "--" prevents mistakes in hostnames from being interpreted as options. --- internal/backend/sftp/sftp.go | 9 ++++----- internal/backend/sftp/sshcmd_test.go | 12 ++++++------ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/internal/backend/sftp/sftp.go b/internal/backend/sftp/sftp.go index fc2de79d8..b0758dcf3 100644 --- a/internal/backend/sftp/sftp.go +++ b/internal/backend/sftp/sftp.go @@ -189,11 +189,8 @@ func buildSSHCommand(cfg Config) (cmd string, args []string, err error) { cmd = "ssh" - host, port := cfg.Host, cfg.Port - - args = []string{host} - if port != "" { - args = append(args, "-p", port) + if cfg.Port != "" { + args = append(args, "-p", cfg.Port) } if cfg.User != "" { args = append(args, "-l") @@ -201,6 +198,8 @@ func buildSSHCommand(cfg Config) (cmd string, args []string, err error) { } args = append(args, "-s") args = append(args, "sftp") + + args = append(args, "--", cfg.Host) return cmd, args, nil } diff --git a/internal/backend/sftp/sshcmd_test.go b/internal/backend/sftp/sshcmd_test.go index 822f28b5d..63d926a37 100644 --- a/internal/backend/sftp/sshcmd_test.go +++ b/internal/backend/sftp/sshcmd_test.go @@ -13,34 +13,34 @@ var sshcmdTests = []struct { { Config{User: "user", Host: "host", Path: "dir/subdir"}, "ssh", - []string{"host", "-l", "user", "-s", "sftp"}, + []string{"-l", "user", "-s", "sftp", "--", "host"}, }, { Config{Host: "host", Path: "dir/subdir"}, "ssh", - []string{"host", "-s", "sftp"}, + []string{"-s", "sftp", "--", "host"}, }, { Config{Host: "host", Port: "10022", Path: "/dir/subdir"}, "ssh", - []string{"host", "-p", "10022", "-s", "sftp"}, + []string{"-p", "10022", "-s", "sftp", "--", "host"}, }, { Config{User: "user", Host: "host", Port: "10022", Path: "/dir/subdir"}, "ssh", - []string{"host", "-p", "10022", "-l", "user", "-s", "sftp"}, + []string{"-p", "10022", "-l", "user", "-s", "sftp", "--", "host"}, }, { // IPv6 address. Config{User: "user", Host: "::1", Path: "dir"}, "ssh", - []string{"::1", "-l", "user", "-s", "sftp"}, + []string{"-l", "user", "-s", "sftp", "--", "::1"}, }, { // IPv6 address with zone and port. Config{User: "user", Host: "::1%lo0", Port: "22", Path: "dir"}, "ssh", - []string{"::1%lo0", "-p", "22", "-l", "user", "-s", "sftp"}, + []string{"-p", "22", "-l", "user", "-s", "sftp", "--", "::1%lo0"}, }, }