Skip to content

Commit baaf233

Browse files
bk2204gitster
authored andcommitted
connect: improve check for plink to reduce false positives
The git_connect function has code to handle plink and tortoiseplink specially, as they require different command line arguments from OpenSSH (-P instead of -p for ports; tortoiseplink additionally requires -batch). However, the match was done by checking for "plink" anywhere in the string, which led to a GIT_SSH value containing "uplink" being treated as an invocation of putty's plink. Improve the check by looking for "plink" or "tortoiseplink" (or those names suffixed with ".exe") only in the final component of the path. This has the downside that a program such as "plink-0.63" would no longer be recognized, but the increased robustness is likely worth it. Add tests to cover these cases to avoid regressions. Signed-off-by: brian m. carlson <[email protected]> Acked-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d1018c2 commit baaf233

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

connect.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ struct child_process *git_connect(int fd[2], const char *url,
722722
conn->in = conn->out = -1;
723723
if (protocol == PROTO_SSH) {
724724
const char *ssh;
725-
int putty;
725+
int putty, tortoiseplink = 0;
726726
char *ssh_host = hostandport;
727727
const char *port = NULL;
728728
get_host_and_port(&ssh_host, &port);
@@ -747,14 +747,26 @@ struct child_process *git_connect(int fd[2], const char *url,
747747
conn->use_shell = 1;
748748
putty = 0;
749749
} else {
750+
const char *base;
751+
char *ssh_dup;
752+
750753
ssh = getenv("GIT_SSH");
751754
if (!ssh)
752755
ssh = "ssh";
753-
putty = !!strcasestr(ssh, "plink");
756+
757+
ssh_dup = xstrdup(ssh);
758+
base = basename(ssh_dup);
759+
760+
tortoiseplink = !strcasecmp(base, "tortoiseplink") ||
761+
!strcasecmp(base, "tortoiseplink.exe");
762+
putty = !strcasecmp(base, "plink") ||
763+
!strcasecmp(base, "plink.exe") || tortoiseplink;
764+
765+
free(ssh_dup);
754766
}
755767

756768
argv_array_push(&conn->args, ssh);
757-
if (putty && !strcasestr(ssh, "tortoiseplink"))
769+
if (tortoiseplink)
758770
argv_array_push(&conn->args, "-batch");
759771
if (port) {
760772
/* P is for PuTTY, p is for OpenSSH */

t/t5601-clone.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ setup_ssh_wrapper () {
296296
'
297297
}
298298

299+
copy_ssh_wrapper_as () {
300+
cp "$TRASH_DIRECTORY/ssh-wrapper" "$1" &&
301+
GIT_SSH="$1" &&
302+
export GIT_SSH
303+
}
304+
299305
expect_ssh () {
300306
test_when_finished '
301307
(cd "$TRASH_DIRECTORY" && rm -f ssh-expect && >ssh-output)
@@ -335,6 +341,33 @@ test_expect_success 'bracketed hostnames are still ssh' '
335341
expect_ssh "-p 123" myhost src
336342
'
337343

344+
test_expect_success 'uplink is not treated as putty' '
345+
copy_ssh_wrapper_as "$TRASH_DIRECTORY/uplink" &&
346+
git clone "[myhost:123]:src" ssh-bracket-clone-uplink &&
347+
expect_ssh "-p 123" myhost src
348+
'
349+
350+
test_expect_success 'plink is treated specially (as putty)' '
351+
copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
352+
git clone "[myhost:123]:src" ssh-bracket-clone-plink-0 &&
353+
expect_ssh "-P 123" myhost src
354+
'
355+
356+
test_expect_success 'plink.exe is treated specially (as putty)' '
357+
copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink.exe" &&
358+
git clone "[myhost:123]:src" ssh-bracket-clone-plink-1 &&
359+
expect_ssh "-P 123" myhost src
360+
'
361+
362+
test_expect_success 'tortoiseplink is like putty, with extra arguments' '
363+
copy_ssh_wrapper_as "$TRASH_DIRECTORY/tortoiseplink" &&
364+
git clone "[myhost:123]:src" ssh-bracket-clone-plink-2 &&
365+
expect_ssh "-batch -P 123" myhost src
366+
'
367+
368+
# Reset the GIT_SSH environment variable for clone tests.
369+
setup_ssh_wrapper
370+
338371
counter=0
339372
# $1 url
340373
# $2 none|host

0 commit comments

Comments
 (0)